From 19f8a2dc80c28f8a54aca8e01f9d80465dd95087 Mon Sep 17 00:00:00 2001 From: adragusin Date: Wed, 30 Oct 2019 09:05:17 +0200 Subject: [PATCH] Updated tests in utils --- apier/v1/apier_it_test.go | 4 +- apier/v1/tpdestinations.go | 2 +- apier/v1/tpratingprofiles_it_test.go | 2 +- engine/model_helpers.go | 22 +-- utils/apitpdata.go | 75 ++++----- utils/apitpdata_test.go | 227 ++++++++++++++++++++++++++- 6 files changed, 266 insertions(+), 66 deletions(-) diff --git a/apier/v1/apier_it_test.go b/apier/v1/apier_it_test.go index ec53b0b93..145379762 100644 --- a/apier/v1/apier_it_test.go +++ b/apier/v1/apier_it_test.go @@ -421,13 +421,13 @@ func TestApierTPRatingProfile(t *testing.T) { } // Test get var rplyRpf *utils.TPRatingProfile - if err := rater.Call("ApierV1.GetTPRatingProfile", AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileId: rpfTst.GetRatingProfilesId()}, &rplyRpf); err != nil { + if err := rater.Call("ApierV1.GetTPRatingProfile", AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileId: rpfTst.GetId()}, &rplyRpf); err != nil { t.Error("Calling ApierV1.GetTPRatingProfiles, got error: ", err.Error()) } else if !reflect.DeepEqual(rpfTst, rplyRpf) { t.Errorf("Calling ApierV1.GetTPRatingProfiles expected: %v, received: %v", rpfTst, rplyRpf) } // Test remove - if err := rater.Call("ApierV1.RemoveTPRatingProfile", AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileId: rpfTst.GetRatingProfilesId()}, &reply); err != nil { + if err := rater.Call("ApierV1.RemoveTPRatingProfile", AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileId: rpfTst.GetId()}, &reply); err != nil { t.Error("Calling ApierV1.RemoveTPRatingProfile, got error: ", err.Error()) } else if reply != "OK" { t.Error("Calling ApierV1.RemoveTPRatingProfile received: ", reply) diff --git a/apier/v1/tpdestinations.go b/apier/v1/tpdestinations.go index 0ab3069ff..6d575c259 100644 --- a/apier/v1/tpdestinations.go +++ b/apier/v1/tpdestinations.go @@ -27,7 +27,7 @@ func (self *ApierV1) SetTPDestination(attrs utils.TPDestination, reply *string) if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ID", "Prefixes"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.SetTPDestinations([]*utils.TPDestination{attrs.AsTPDestination()}); err != nil { + if err := self.StorDb.SetTPDestinations([]*utils.TPDestination{&attrs}); err != nil { return utils.APIErrorHandler(err) } *reply = utils.OK diff --git a/apier/v1/tpratingprofiles_it_test.go b/apier/v1/tpratingprofiles_it_test.go index 62c4f175a..19db40a87 100644 --- a/apier/v1/tpratingprofiles_it_test.go +++ b/apier/v1/tpratingprofiles_it_test.go @@ -279,7 +279,7 @@ func testTPRatingProfilesGetTPRatingProfileIds(t *testing.T) { func testTPRatingProfilesRemoveTPRatingProfile(t *testing.T) { var resp string if err := tpRatingProfileRPC.Call("ApierV1.RemoveTPRatingProfile", - &AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileId: tpRatingProfile.GetRatingProfilesId()}, &resp); err != nil { + &AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileId: tpRatingProfile.GetId()}, &resp); err != nil { t.Error(err) } else if resp != utils.OK { t.Error("Unexpected reply returned", resp) diff --git a/engine/model_helpers.go b/engine/model_helpers.go index 28ae9c0b2..3d3d6926d 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -262,17 +262,7 @@ func (tps TpTimings) AsMapTPTimings() (map[string]*utils.ApierTPTiming, error) { func MapTPTimings(tps []*utils.ApierTPTiming) (map[string]*utils.TPTiming, error) { result := make(map[string]*utils.TPTiming) for _, tp := range tps { - t := &utils.TPTiming{} - t.ID = tp.ID - t.Years.Parse(tp.Years, utils.INFIELD_SEP) - t.Months.Parse(tp.Months, utils.INFIELD_SEP) - t.MonthDays.Parse(tp.MonthDays, utils.INFIELD_SEP) - t.WeekDays.Parse(tp.WeekDays, utils.INFIELD_SEP) - times := strings.Split(tp.Time, utils.INFIELD_SEP) - t.StartTime = times[0] - if len(times) > 1 { - t.EndTime = times[1] - } + t := utils.NewTiming(tp.ID, tp.Years, tp.Months, tp.MonthDays, tp.WeekDays, tp.Time) if _, found := result[tp.ID]; found { return nil, fmt.Errorf("duplicate timing tag: %s", tp.ID) } @@ -610,9 +600,9 @@ func (tps TpRatingProfiles) AsMapTPRatingProfiles() (map[string]*utils.TPRatingP RatingPlanId: tp.RatingPlanTag, FallbackSubjects: tp.FallbackSubjects, } - if existing, exists := result[rp.KeyIdA()]; !exists { + if existing, exists := result[rp.GetId()]; !exists { rp.RatingPlanActivations = []*utils.TPRatingActivation{ra} - result[rp.KeyIdA()] = rp + result[rp.GetId()] = rp } else { existing.RatingPlanActivations = append(existing.RatingPlanActivations, ra) } @@ -634,10 +624,10 @@ func (tps TpRatingProfiles) AsTPRatingProfiles() (result []*utils.TPRatingProfil func MapTPRatingProfiles(s []*utils.TPRatingProfile) (map[string]*utils.TPRatingProfile, error) { result := make(map[string]*utils.TPRatingProfile) for _, e := range s { - if _, found := result[e.KeyIdA()]; !found { - result[e.KeyIdA()] = e + if _, found := result[e.GetId()]; !found { + result[e.GetId()] = e } else { - return nil, fmt.Errorf("Non unique id %+v", e.KeyIdA()) + return nil, fmt.Errorf("Non unique id %+v", e.GetId()) } } return result, nil diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 01d6b6142..1fc2cad98 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -29,7 +29,7 @@ import ( type TPDistinctIds []string func (tpdi TPDistinctIds) String() string { - return strings.Join(tpdi, ",") + return strings.Join(tpdi, FIELDS_SEP) } type PaginatorWithSearch struct { @@ -61,12 +61,10 @@ func (pgnt *Paginator) PaginateStringSlice(in []string) (out []string) { if offset > len(in) { return } - if offset != 0 { + if offset != 0 && limit != 0 { limit = limit + offset } - if limit == 0 { - limit = len(in[offset:]) - } else if limit > len(in) { + if limit == 0 || limit > len(in) { limit = len(in) } ret := in[offset:limit] @@ -84,10 +82,6 @@ type TPDestination struct { Prefixes []string // Prefixes attached to this destination } -func (v1TPDst *TPDestination) AsTPDestination() *TPDestination { - return &TPDestination{TPid: v1TPDst.TPid, ID: v1TPDst.ID, Prefixes: v1TPDst.Prefixes} -} - // This file deals with tp_* data definition type TPRate struct { @@ -98,8 +92,13 @@ type TPRate struct { // Needed so we make sure we always use SetDurations() on a newly created value func NewRateSlot(connectFee, rate float64, rateUnit, rateIncrement, grpInterval string) (*RateSlot, error) { - rs := &RateSlot{ConnectFee: connectFee, Rate: rate, RateUnit: rateUnit, RateIncrement: rateIncrement, - GroupIntervalStart: grpInterval} + rs := &RateSlot{ + ConnectFee: connectFee, + Rate: rate, + RateUnit: rateUnit, + RateIncrement: rateIncrement, + GroupIntervalStart: grpInterval, + } if err := rs.SetDurations(); err != nil { return nil, err } @@ -119,27 +118,27 @@ type RateSlot struct { } // Used to set the durations we need out of strings -func (self *RateSlot) SetDurations() error { +func (rs *RateSlot) SetDurations() error { var err error - if self.rateUnitDur, err = ParseDurationWithNanosecs(self.RateUnit); err != nil { + if rs.rateUnitDur, err = ParseDurationWithNanosecs(rs.RateUnit); err != nil { return err } - if self.rateIncrementDur, err = ParseDurationWithNanosecs(self.RateIncrement); err != nil { + if rs.rateIncrementDur, err = ParseDurationWithNanosecs(rs.RateIncrement); err != nil { return err } - if self.groupIntervalStartDur, err = ParseDurationWithNanosecs(self.GroupIntervalStart); err != nil { + if rs.groupIntervalStartDur, err = ParseDurationWithNanosecs(rs.GroupIntervalStart); err != nil { return err } return nil } -func (self *RateSlot) RateUnitDuration() time.Duration { - return self.rateUnitDur +func (rs *RateSlot) RateUnitDuration() time.Duration { + return rs.rateUnitDur } -func (self *RateSlot) RateIncrementDuration() time.Duration { - return self.rateIncrementDur +func (rs *RateSlot) RateIncrementDuration() time.Duration { + return rs.rateIncrementDur } -func (self *RateSlot) GroupIntervalStartDuration() time.Duration { - return self.groupIntervalStartDur +func (rs *RateSlot) GroupIntervalStartDuration() time.Duration { + return rs.groupIntervalStartDur } type TPDestinationRate struct { @@ -178,14 +177,14 @@ type TPTiming struct { EndTime string } -func NewTiming(timingInfo ...string) (rt *TPTiming) { +func NewTiming(ID, years, mounths, mounthdays, weekdays, time string) (rt *TPTiming) { rt = &TPTiming{} - rt.ID = timingInfo[0] - rt.Years.Parse(timingInfo[1], INFIELD_SEP) - rt.Months.Parse(timingInfo[2], INFIELD_SEP) - rt.MonthDays.Parse(timingInfo[3], INFIELD_SEP) - rt.WeekDays.Parse(timingInfo[4], INFIELD_SEP) - times := strings.Split(timingInfo[5], INFIELD_SEP) + rt.ID = ID + rt.Years.Parse(years, INFIELD_SEP) + rt.Months.Parse(mounths, INFIELD_SEP) + rt.MonthDays.Parse(mounthdays, INFIELD_SEP) + rt.WeekDays.Parse(weekdays, INFIELD_SEP) + times := strings.Split(time, INFIELD_SEP) rt.StartTime = times[0] if len(times) > 1 { rt.EndTime = times[1] @@ -214,15 +213,6 @@ func (self *TPRatingPlanBinding) Timing() *TPTiming { return self.timing } -// Used to rebuild a TPRatingProfile (empty RatingPlanActivations) out of it's key in nosqldb -func NewTPRatingProfileFromKeyId(tpid, loadId, keyId string) (*TPRatingProfile, error) { - s := strings.Split(keyId, ":") - if len(s) != 4 { - return nil, fmt.Errorf("Cannot parse key %s into RatingProfile", keyId) - } - return &TPRatingProfile{TPid: tpid, LoadId: loadId, Tenant: s[1], Category: s[2], Subject: s[3]}, nil -} - type TPRatingProfile struct { TPid string // Tariff plan id LoadId string // Gives ability to load specific RatingProfile based on load identifier, hence being able to keep history also in stordb @@ -233,17 +223,12 @@ type TPRatingProfile struct { } // Used as key in nosql db (eg: redis) -func (self *TPRatingProfile) KeyId() string { +func (rpf *TPRatingProfile) KeyId() string { return ConcatenatedKey(META_OUT, - self.Tenant, self.Category, self.Subject) + rpf.Tenant, rpf.Category, rpf.Subject) } -func (self *TPRatingProfile) KeyIdA() string { - return ConcatenatedKey(self.LoadId, META_OUT, - self.Tenant, self.Category, self.Subject) -} - -func (rpf *TPRatingProfile) GetRatingProfilesId() string { +func (rpf *TPRatingProfile) GetId() string { return ConcatenatedKey(rpf.LoadId, META_OUT, rpf.Tenant, rpf.Category, rpf.Subject) } diff --git a/utils/apitpdata_test.go b/utils/apitpdata_test.go index 5c96b66a6..abf1cac5a 100644 --- a/utils/apitpdata_test.go +++ b/utils/apitpdata_test.go @@ -19,10 +19,22 @@ package utils import ( "reflect" + "strings" "testing" "time" ) +func TestTPDistinctIdsString(t *testing.T) { + eIn1 := []string{"1", "2", "3", "4"} + eIn2 := TPDistinctIds(eIn1) + expected := strings.Join(eIn1, FIELDS_SEP) + received := eIn2.String() + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + func TestNewDTCSFromRPKey(t *testing.T) { rpKey := "*out:tenant12:call:dan12" if dtcs, err := NewDTCSFromRPKey(rpKey); err != nil { @@ -33,8 +45,32 @@ func TestNewDTCSFromRPKey(t *testing.T) { } func TestPaginatorPaginateStringSlice(t *testing.T) { - eOut := []string{"1", "2", "3", "4"} + //len(in)=0 + eOut := []string{} pgnt := new(Paginator) + rcv := pgnt.PaginateStringSlice(eOut) + if len(rcv) != 0 { + t.Errorf("Expecting an empty slice, received: %+v", rcv) + } + //offset > len(in) + eOut = []string{"1"} + pgnt = &Paginator{Offset: IntPointer(2), Limit: IntPointer(0)} + rcv = pgnt.PaginateStringSlice(eOut) + + if len(rcv) != 0 { + t.Errorf("Expecting an empty slice, received: %+v", rcv) + } + //offset != 0 && limit != 0 + eOut = []string{"3", "4"} + pgnt = &Paginator{Offset: IntPointer(2), Limit: IntPointer(0)} + rcv = pgnt.PaginateStringSlice([]string{"1", "2", "3", "4"}) + + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting an empty slice, received: %+v", rcv) + } + + eOut = []string{"1", "2", "3", "4"} + pgnt = new(Paginator) if rcv := pgnt.PaginateStringSlice([]string{"1", "2", "3", "4"}); !reflect.DeepEqual(eOut, rcv) { t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) } @@ -61,6 +97,195 @@ func TestPaginatorPaginateStringSlice(t *testing.T) { } } +func TestNewRateSlot(t *testing.T) { + var err error + eOut := &RateSlot{ + ConnectFee: 1, + Rate: 1.01, + RateUnit: "1", + RateIncrement: "1", + GroupIntervalStart: "1", + rateUnitDur: 1, + rateIncrementDur: 1, + groupIntervalStartDur: 1, + tag: "", + } + rcv, err := NewRateSlot(eOut.ConnectFee, eOut.Rate, eOut.RateUnit, eOut.RateIncrement, eOut.GroupIntervalStart) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v ,received: %+v ", eOut, rcv) + } + eOut.RateUnit = "a" + rcv, err = NewRateSlot(eOut.ConnectFee, eOut.Rate, eOut.RateUnit, eOut.RateIncrement, eOut.GroupIntervalStart) + //must receive from time an error: "invalid duration a" + if err == nil { + t.Error(err) + } +} + +func TestSetDurations(t *testing.T) { + eOut := &RateSlot{ + RateUnit: "1", + RateIncrement: "1", + GroupIntervalStart: "1", + } + err := eOut.SetDurations() + //must receive "nil" with + if err != nil { + t.Error(err) + } + eOut.RateUnit = "a" + err = eOut.SetDurations() + //at RateUnit if, must receive from time an error: "invalid duration a" + if err == nil { + t.Error(err) + } + eOut.RateUnit = "1" + eOut.RateIncrement = "a" + err = eOut.SetDurations() + //at RateIncrement, must receive from time an error: "invalid duration a" + if err == nil { + t.Error(err) + } + eOut.RateIncrement = "1" + eOut.GroupIntervalStart = "a" + err = eOut.SetDurations() + //at GroupIntervalStart, must receive from time an error: "invalid duration a" + if err == nil { + t.Error(err) + } +} + +func TestRateUnitDuration(t *testing.T) { + eOut := &RateSlot{ + rateUnitDur: 1, + rateIncrementDur: 1, + groupIntervalStartDur: 1, + } + rcv := eOut.RateUnitDuration() + if rcv != eOut.rateUnitDur { + t.Errorf("Expected %+v, received %+v", eOut.rateUnitDur, rcv) + } + rcv = eOut.RateIncrementDuration() + if rcv != eOut.rateIncrementDur { + t.Errorf("Expected %+v, received %+v", eOut.rateIncrementDur, rcv) + } + rcv = eOut.GroupIntervalStartDuration() + if rcv != eOut.groupIntervalStartDur { + t.Errorf("Expected %+v, received %+v", eOut.groupIntervalStartDur, rcv) + } +} + +func TestNewTiming(t *testing.T) { + eOut := &TPTiming{ + ID: "1", + Years: Years{2020}, + Months: []time.Month{time.April}, + MonthDays: MonthDays{18}, + WeekDays: WeekDays{06}, + StartTime: "00:00:00", + EndTime: "11:11:11", + } + rcv := NewTiming("1", "2020", "04", "18", "06", "00:00:00;11:11:11") + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } + //without endtime, check if .Split methong works (only one timestamp) + rcv = NewTiming("1", "2020", "04", "18", "06", "00:00:00") + eOut.EndTime = "" + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } + //check if .Split methong works (ignoring the last timestamp) + rcv = NewTiming("1", "2020", "04", "18", "06", "00:00:00;11:11:11;22:22:22") + eOut.EndTime = "11:11:11" + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } +} + +func TestSetTiming(t *testing.T) { + tpTiming := &TPTiming{ + ID: "1", + Years: Years{2020}, + Months: []time.Month{time.April}, + MonthDays: MonthDays{18}, + WeekDays: WeekDays{06}, + StartTime: "00:00:00", + EndTime: "11:11:11", + } + tpRatingPlanBinding := new(TPRatingPlanBinding) + tpRatingPlanBinding.SetTiming(tpTiming) + if !reflect.DeepEqual(tpTiming, tpRatingPlanBinding.timing) { + t.Errorf("Expected %+v, received %+v", tpTiming, tpRatingPlanBinding.timing) + } + rcv := tpRatingPlanBinding.Timing() + if !reflect.DeepEqual(tpTiming, rcv) { + t.Errorf("Expected %+v, received %+v", tpTiming, rcv) + } +} + +func TestKeys(t *testing.T) { + //empty check -> KeyId + tpRatingProfile := new(TPRatingProfile) + eOut := "*out:::" + rcv := tpRatingProfile.KeyId() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } + //empty check -> GetId + eOut = ":*out:::" + rcv = tpRatingProfile.GetId() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } + // test check -> KeyId + tpRatingProfile.Tenant = "test" + tpRatingProfile.Category = "test" + tpRatingProfile.Subject = "test" + eOut = "*out:test:test:test" + rcv = tpRatingProfile.KeyId() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } + //test check -> GetId + eOut = "test:*out:test:test:test" + tpRatingProfile.TPid = "test" + tpRatingProfile.LoadId = "test" + + rcv = tpRatingProfile.GetId() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %+v, received %+v", eOut, rcv) + } +} + +func TestSetRatingProfilesId(t *testing.T) { + //empty check + tpRatingProfile := new(TPRatingProfile) + tpRatingProfile.SetRatingProfilesId("") + eOut := new(TPRatingProfile) + if !reflect.DeepEqual(eOut, tpRatingProfile) { + t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile) + } + //test check + tpRatingProfile.SetRatingProfilesId("1:2:3:4:5") + eOut.LoadId = "1" + eOut.Tenant = "3" + eOut.Category = "4" + eOut.Subject = "5" + if !reflect.DeepEqual(eOut, tpRatingProfile) { + t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile) + } + //wrong TPRatingProfile sent + err := tpRatingProfile.SetRatingProfilesId("1:2:3:4:5:6") + if err == nil { + t.Error("Wrong TPRatingProfileId sent and no error received") + } + +} + func TestAppendToSMCostFilter(t *testing.T) { var err error smfltr := new(SMCostFilter)