diff --git a/engine/attributes_test.go b/engine/attributes_test.go index a4ec20395..94c70e6da 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -27,34 +27,59 @@ import ( ) var ( - atrPs AttributeProfiles - sev, sev2, sev3 *utils.CGREvent - srv AttributeService - dmAtr *DataManager -) + cloneExpTime time.Time + expTime = time.Now().Add(time.Duration(20 * time.Minute)) + srv AttributeService + dmAtr *DataManager -func TestAttributePopulateAttrService(t *testing.T) { - var filters1 []*RequestFilter - var filters2 []*RequestFilter - var preffilter []*RequestFilter - var defaultf []*RequestFilter - second := 1 * time.Second - data, _ := NewMapStorage() - dmAtr = NewDataManager(data) - context := utils.MetaRating - attrMap := make(map[string]map[string]*Attribute) - attrMap["FL1"] = make(map[string]*Attribute) - attrMap["FL1"]["In1"] = &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", - Append: true, + context = utils.MetaRating + + attrMap = map[string]map[string]*Attribute{ + "FL1": map[string]*Attribute{ + "In1": &Attribute{ + FieldName: "FL1", + Initial: "In1", + Substitute: "Al1", + Append: true, + }, + }, } - //Need clone because time.Now add extra information and DeepEqual don't like - var cloneExpTime time.Time - expTime := time.Now().Add(time.Duration(20 * time.Minute)) - if err := utils.Clone(expTime, &cloneExpTime); err != nil { - t.Error(err) + + sev = &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "attribute_event", + Context: &context, + Event: map[string]interface{}{ + "Attribute": "AttributeProfile1", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + "Weight": "20.0", + }, + } + sev2 = &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "attribute_event", + Context: &context, + Event: map[string]interface{}{ + "Attribute": "AttributeProfile2", + }, + } + sev3 = &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "attribute_event", + Context: &context, + Event: map[string]interface{}{ + "Attribute": "AttributeProfilePrefix", + }, + } + sev4 = &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "attribute_event", + Context: &context, + Event: map[string]interface{}{ + "Weight": "200.0", + }, } atrPs = AttributeProfiles{ &AttributeProfile{ @@ -106,7 +131,52 @@ func TestAttributePopulateAttrService(t *testing.T) { Weight: 20, }, } - x, err := NewRequestFilter(MetaString, "attributeprofile1", []string{"Attribute"}) +) + +func TestAttributeCache(t *testing.T) { + //Need clone because time.Now adds extra information that DeepEqual doesn't like + if err := utils.Clone(expTime, &cloneExpTime); err != nil { + t.Error(err) + } + data, _ := NewMapStorage() + dmAtr = NewDataManager(data) + for _, atr := range atrPs { + if err = dmAtr.SetAttributeProfile(atr, false); err != nil { + t.Errorf("Error: %+v", err) + } + } + //Test each attribute from cache + for _, atr := range atrPs { + if tempAttr, err := dmAtr.GetAttributeProfile(atr.Tenant, atr.ID, false, utils.NonTransactional); err != nil { + t.Errorf("Error: %+v", err) + } else if !reflect.DeepEqual(atr, tempAttr) { + t.Errorf("Expecting: %+v, received: %+v", atr, tempAttr) + } + } +} + +func TestAttributePopulateAttrService(t *testing.T) { + var filters1 []*RequestFilter + var filters2 []*RequestFilter + var preffilter []*RequestFilter + var defaultf []*RequestFilter + second := 1 * time.Second + //refresh the DM + data, _ := NewMapStorage() + dmAtr = NewDataManager(data) + srv = AttributeService{ + dm: dmAtr, + filterS: &FilterS{dm: dmAtr}, + } + ref := NewReqFilterIndexer(dmAtr, utils.AttributeProfilePrefix, + utils.ConcatenatedKey(config.CgrConfig().DefaultTenant, utils.MetaRating)) + for _, atr := range atrPs { + if err = dmAtr.SetAttributeProfile(atr, false); err != nil { + t.Errorf("Error: %+v", err) + } + } + //filter1 + x, err := NewRequestFilter(MetaString, "Attribute", []string{"AttributeProfile1"}) if err != nil { t.Errorf("Error: %+v", err) } @@ -121,78 +191,38 @@ func TestAttributePopulateAttrService(t *testing.T) { t.Errorf("Error: %+v", err) } filters1 = append(filters1, x) + filter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter1", RequestFilters: filters1} + dmAtr.SetFilter(filter1) + ref.IndexTPFilter(FilterToTPFilter(filter1), "attributeprofile1") - x, err = NewRequestFilter(MetaString, "attributeprofile2", []string{"Attribute"}) + //filter2 + x, err = NewRequestFilter(MetaString, "Attribute", []string{"AttributeProfile2"}) if err != nil { t.Errorf("Error: %+v", err) } filters2 = append(filters2, x) + filter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter2", RequestFilters: filters2} + dmAtr.SetFilter(filter2) + ref.IndexTPFilter(FilterToTPFilter(filter2), "attributeprofile2") - x, err = NewRequestFilter(MetaPrefix, "attributeprofile1Prefix", []string{"Attribute"}) + //prefix filter + x, err = NewRequestFilter(MetaPrefix, "Attribute", []string{"AttributeProfilePrefix"}) if err != nil { t.Errorf("Error: %+v", err) } preffilter = append(preffilter, x) + preffilter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter1", RequestFilters: preffilter} + dmAtr.SetFilter(preffilter1) + ref.IndexTPFilter(FilterToTPFilter(preffilter1), "attributeprofile3") + + //default filter x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"200.00"}) if err != nil { t.Errorf("Error: %+v", err) } defaultf = append(defaultf, x) - filter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter1", RequestFilters: filters1} - filter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter2", RequestFilters: filters2} - preffilter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter1", RequestFilters: preffilter} defaultf1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf1", RequestFilters: defaultf} - dmAtr.SetFilter(filter1) - dmAtr.SetFilter(filter2) - dmAtr.SetFilter(preffilter1) dmAtr.SetFilter(defaultf1) - srv = AttributeService{ - dm: dmAtr, - filterS: &FilterS{dm: dmAtr}, - stringIndexedFields: &[]string{"attributeprofile1", "attributeprofile2"}, - prefixIndexedFields: &[]string{"attributeprofile1Prefix"}, - } - sev = &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "attribute_event", - Context: &context, - Event: map[string]interface{}{ - "attributeprofile1": "Attribute", - "attributeprofile2": "Attribute", - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "UsageInterval": "1s", - "PddInterval": "1s", - "Weight": "20.0", - }, - } - for _, atr := range atrPs { - if err = dmAtr.SetAttributeProfile(atr, false); err != nil { - t.Errorf("Error: %+v", err) - } - } - sev2 = &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "attribute_event", - Context: &context, - Event: map[string]interface{}{ - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "attributeprofile1Prefix": "Attribute", - }, - } - sev3 = &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "attribute_event", - Context: &context, - Event: map[string]interface{}{ - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "Weight": "200.0", - }, - } - prefix := utils.ConcatenatedKey(sev.Tenant, *sev.Context) - ref := NewReqFilterIndexer(dmAtr, utils.AttributeProfilePrefix, prefix) - ref.IndexTPFilter(FilterToTPFilter(filter1), "attributeprofile1") - ref.IndexTPFilter(FilterToTPFilter(filter2), "attributeprofile2") - ref.IndexTPFilter(FilterToTPFilter(preffilter1), "attributeprofile3") ref.IndexTPFilter(FilterToTPFilter(defaultf1), "attributeprofile4") err = ref.StoreIndexes() if err != nil { @@ -201,91 +231,129 @@ func TestAttributePopulateAttrService(t *testing.T) { } func TestAttributeMatchingAttributeProfilesForEvent(t *testing.T) { - atrpl, err := srv.matchingAttributeProfilesForEvent(sev) + atrp, err := srv.matchingAttributeProfilesForEvent(sev) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[0], atrpl[0]) && !reflect.DeepEqual(atrPs[0], atrpl[1]) { - t.Errorf("Expecting: %+v, received: %+v ", atrPs[0], atrpl[0]) - } else if !reflect.DeepEqual(atrPs[1], atrpl[1]) && !reflect.DeepEqual(atrPs[1], atrpl[0]) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs), utils.ToJSON(atrpl)) + if !reflect.DeepEqual(atrPs[0], atrp[0]) { + t.Errorf("Expecting: %+v, received: %+v ", atrPs[0], atrp[0]) } - atrpl2, err := srv.matchingAttributeProfilesForEvent(sev2) + atrp, err = srv.matchingAttributeProfilesForEvent(sev2) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[2], atrpl2[0]) { - t.Errorf("Expecting: %+v, received: %+v ", atrPs[2], atrpl2[0]) + if !reflect.DeepEqual(atrPs[1], atrp[0]) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs), utils.ToJSON(atrp)) } - atrpl3, err := srv.matchingAttributeProfilesForEvent(sev3) + atrp, err = srv.matchingAttributeProfilesForEvent(sev3) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[3], atrpl3[0]) { - t.Errorf("Expecting: %+v, received: %+v ", atrPs[3], atrpl3[0]) + if !reflect.DeepEqual(atrPs[2], atrp[0]) { + t.Errorf("Expecting: %+v, received: %+v ", atrPs[2], atrp[0]) + } + atrp, err = srv.matchingAttributeProfilesForEvent(sev4) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(atrPs[3], atrp[0]) { + t.Errorf("Expecting: %+v, received: %+v ", atrPs[3], atrp[0]) } } func TestAttributeProfileForEvent(t *testing.T) { - context := utils.MetaRating - sev = &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "attribute_event", - Context: &context, - Event: map[string]interface{}{ - "attributeprofile1": "Attribute", - "UsageInterval": "1s", - "Weight": "9.0", - }, - } - atrpl, err := srv.attributeProfileForEvent(sev) + atrp, err := srv.attributeProfileForEvent(sev) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[0], atrpl) && !reflect.DeepEqual(atrPs[1], atrpl) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[0]), utils.ToJSON(atrpl)) + if !reflect.DeepEqual(atrPs[0], atrp) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[0]), utils.ToJSON(atrp)) } - atrpl, err = srv.attributeProfileForEvent(sev2) + + atrp, err = srv.attributeProfileForEvent(sev2) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[2], atrpl) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[2]), utils.ToJSON(atrpl)) + if !reflect.DeepEqual(atrPs[1], atrp) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[1]), utils.ToJSON(atrp)) + } - atrpl, err = srv.attributeProfileForEvent(sev3) + atrp, err = srv.attributeProfileForEvent(sev3) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(atrPs[3], atrpl) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[3]), utils.ToJSON(atrpl)) + if !reflect.DeepEqual(atrPs[2], atrp) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[2]), utils.ToJSON(atrp)) + } + atrp, err = srv.attributeProfileForEvent(sev4) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(atrPs[3], atrp) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[3]), utils.ToJSON(atrp)) } } func TestAttributeProcessEvent(t *testing.T) { - context := utils.MetaRating - sev = &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "attribute_event", - Context: &context, - Event: map[string]interface{}{ - "attributeprofile1": "Attribute", - "UsageInterval": "1s", - "Weight": "9.0", - }, - } + eRply := &AttrSProcessEventReply{ MatchedProfile: "attributeprofile1", CGREvent: sev, } - atrpl, err := srv.processEvent(sev) + atrp, err := srv.processEvent(sev) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(eRply.MatchedProfile, atrpl.MatchedProfile) { - t.Errorf("Expecting: %+v, received: %+v", eRply.MatchedProfile, atrpl.MatchedProfile) - } else if !reflect.DeepEqual(eRply.AlteredFields, atrpl.AlteredFields) { - t.Errorf("Expecting: %+v, received: %+v", eRply.AlteredFields, atrpl.AlteredFields) - } else if !reflect.DeepEqual(eRply.CGREvent, atrpl.CGREvent) { - t.Errorf("Expecting: %+v, received: %+v", eRply.CGREvent, atrpl.CGREvent) + if !reflect.DeepEqual(eRply.MatchedProfile, atrp.MatchedProfile) { + t.Errorf("Expecting: %+v, received: %+v", eRply.MatchedProfile, atrp.MatchedProfile) + } else if !reflect.DeepEqual(eRply.AlteredFields, atrp.AlteredFields) { + t.Errorf("Expecting: %+v, received: %+v", eRply.AlteredFields, atrp.AlteredFields) + } else if !reflect.DeepEqual(eRply.CGREvent, atrp.CGREvent) { + t.Errorf("Expecting: %+v, received: %+v", eRply.CGREvent, atrp.CGREvent) + } + eRply = &AttrSProcessEventReply{ + MatchedProfile: "attributeprofile2", + CGREvent: sev2, + } + atrp, err = srv.processEvent(sev2) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eRply.MatchedProfile, atrp.MatchedProfile) { + t.Errorf("Expecting: %+v, received: %+v", eRply.MatchedProfile, atrp.MatchedProfile) + } else if !reflect.DeepEqual(eRply.AlteredFields, atrp.AlteredFields) { + t.Errorf("Expecting: %+v, received: %+v", eRply.AlteredFields, atrp.AlteredFields) + } else if !reflect.DeepEqual(eRply.CGREvent, atrp.CGREvent) { + t.Errorf("Expecting: %+v, received: %+v", eRply.CGREvent, atrp.CGREvent) + } + eRply = &AttrSProcessEventReply{ + MatchedProfile: "attributeprofile3", + CGREvent: sev3, + } + atrp, err = srv.processEvent(sev3) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eRply.MatchedProfile, atrp.MatchedProfile) { + t.Errorf("Expecting: %+v, received: %+v", eRply.MatchedProfile, atrp.MatchedProfile) + } else if !reflect.DeepEqual(eRply.AlteredFields, atrp.AlteredFields) { + t.Errorf("Expecting: %+v, received: %+v", eRply.AlteredFields, atrp.AlteredFields) + } else if !reflect.DeepEqual(eRply.CGREvent, atrp.CGREvent) { + t.Errorf("Expecting: %+v, received: %+v", eRply.CGREvent, atrp.CGREvent) + } + eRply = &AttrSProcessEventReply{ + MatchedProfile: "attributeprofile4", + CGREvent: sev4, + } + atrp, err = srv.processEvent(sev4) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eRply.MatchedProfile, atrp.MatchedProfile) { + t.Errorf("Expecting: %+v, received: %+v", eRply.MatchedProfile, atrp.MatchedProfile) + } else if !reflect.DeepEqual(eRply.AlteredFields, atrp.AlteredFields) { + t.Errorf("Expecting: %+v, received: %+v", eRply.AlteredFields, atrp.AlteredFields) + } else if !reflect.DeepEqual(eRply.CGREvent, atrp.CGREvent) { + t.Errorf("Expecting: %+v, received: %+v", eRply.CGREvent, atrp.CGREvent) } } diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index ec062a819..08e6289db 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -27,207 +27,9 @@ import ( ) var ( - splserv SupplierService - argPagEv *ArgsGetSuppliers - dmspl *DataManager - sprsmatch SupplierProfiles -) - -func TestSuppliersSort(t *testing.T) { - sprs := SupplierProfiles{ - &SupplierProfile{ - Tenant: "cgrates.org", - ID: "supplierprofile1", - FilterIDs: []string{}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - Sorting: "", - SortingParams: []string{}, - Suppliers: []*Supplier{ - &Supplier{ - ID: "supplier1", - FilterIDs: []string{}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 10.0, - SupplierParameters: "param1", - }, - }, - Blocker: false, - Weight: 10, - }, - &SupplierProfile{ - Tenant: "cgrates.org", - ID: "supplierprofile2", - FilterIDs: []string{}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - Sorting: "", - SortingParams: []string{}, - Suppliers: []*Supplier{ - &Supplier{ - ID: "supplier1", - FilterIDs: []string{}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 20.0, - SupplierParameters: "param1", - }, - }, - Blocker: false, - Weight: 20.0, - }, - } - eSupplierProfile := SupplierProfiles{ - &SupplierProfile{ - Tenant: "cgrates.org", - ID: "supplierprofile2", - FilterIDs: []string{}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - Sorting: "", - SortingParams: []string{}, - Suppliers: []*Supplier{ - &Supplier{ - ID: "supplier1", - FilterIDs: []string{}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 20.0, - SupplierParameters: "param1", - }, - }, - Blocker: false, - Weight: 20.0, - }, - &SupplierProfile{ - Tenant: "cgrates.org", - ID: "supplierprofile1", - FilterIDs: []string{}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - Sorting: "", - SortingParams: []string{}, - Suppliers: []*Supplier{ - &Supplier{ - ID: "supplier1", - FilterIDs: []string{}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 10.0, - SupplierParameters: "param1", - }, - }, - Blocker: false, - Weight: 10.0, - }, - } - sprs.Sort() - if !reflect.DeepEqual(eSupplierProfile, sprs) { - t.Errorf("Expecting: %+v, received: %+v", eSupplierProfile, sprs) - } -} - -func TestSuppliersPopulateSupplierService(t *testing.T) { - data, _ := NewMapStorage() - dmspl = NewDataManager(data) - var filters1 []*RequestFilter - var filters2 []*RequestFilter - var preffilter []*RequestFilter - var defaultf []*RequestFilter - second := 1 * time.Second - x, err := NewRequestFilter(MetaString, "supplierprofile1", []string{"Supplier"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewRequestFilter(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"9.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewRequestFilter(MetaString, "supplierprofile2", []string{"Supplier"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewRequestFilter(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"15.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewRequestFilter(MetaPrefix, "Supplier", []string{"supplierprofilePrefix"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"200.00"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - filter3 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter3", RequestFilters: filters1} - filter4 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter4", RequestFilters: filters2} - preffilter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter2", RequestFilters: preffilter} - defaultf2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf2", RequestFilters: defaultf} - dmspl.SetFilter(filter3) - dmspl.SetFilter(filter4) - dmspl.SetFilter(preffilter2) - dmspl.SetFilter(defaultf2) - ssd := make(map[string]SuppliersSorter) - ssd[utils.MetaWeight] = NewWeightSorter() - splserv = SupplierService{ - dm: dmspl, - filterS: &FilterS{dm: dmspl}, - sorter: ssd, - } - ssd[utils.MetaLeastCost] = NewLeastCostSorter(&splserv) - argPagEv = &ArgsGetSuppliers{ - CGREvent: utils.CGREvent{ - Tenant: "cgrates.org", - ID: "utils.CGREvent1", - Event: map[string]interface{}{ - "supplierprofile1": "Supplier", - "supplierprofile2": "Supplier", - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "UsageInterval": "1s", - "PddInterval": "1s", - "Weight": "20.0", - }, - }, - } - var cloneExpTime time.Time - expTime := time.Now().Add(time.Duration(20 * time.Minute)) - if err := utils.Clone(expTime, &cloneExpTime); err != nil { - t.Error(err) - } - sprsmatch = SupplierProfiles{ + splserv SupplierService + dmSPP *DataManager + sppTest = SupplierProfiles{ &SupplierProfile{ Tenant: "cgrates.org", ID: "supplierprofile1", @@ -369,15 +171,266 @@ func TestSuppliersPopulateSupplierService(t *testing.T) { Weight: 20.0, }, } - - for _, spr := range sprsmatch { - dmspl.SetSupplierProfile(spr, true) + argPagEv = &ArgsGetSuppliers{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Supplier": "SupplierProfile1", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + "Weight": "20.0", + }, + }, } - ref := NewReqFilterIndexer(dmspl, utils.SupplierProfilePrefix, "cgrates.org") + argPagEv2 = &ArgsGetSuppliers{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Supplier": "SupplierProfile2", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + "Weight": "20.0", + }, + }, + } + argPagEv3 = &ArgsGetSuppliers{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Supplier": "supplierprofilePrefix", + }, + }, + } + argPagEv4 = &ArgsGetSuppliers{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Weight": "200.00", + }, + }, + } +) + +func TestSuppliersSort(t *testing.T) { + sprs := SupplierProfiles{ + &SupplierProfile{ + Tenant: "cgrates.org", + ID: "supplierprofile1", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Sorting: "", + SortingParams: []string{}, + Suppliers: []*Supplier{ + &Supplier{ + ID: "supplier1", + FilterIDs: []string{}, + AccountIDs: []string{}, + RatingPlanIDs: []string{}, + ResourceIDs: []string{}, + StatIDs: []string{}, + Weight: 10.0, + SupplierParameters: "param1", + }, + }, + Blocker: false, + Weight: 10, + }, + &SupplierProfile{ + Tenant: "cgrates.org", + ID: "supplierprofile2", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Sorting: "", + SortingParams: []string{}, + Suppliers: []*Supplier{ + &Supplier{ + ID: "supplier1", + FilterIDs: []string{}, + AccountIDs: []string{}, + RatingPlanIDs: []string{}, + ResourceIDs: []string{}, + StatIDs: []string{}, + Weight: 20.0, + SupplierParameters: "param1", + }, + }, + Blocker: false, + Weight: 20.0, + }, + } + eSupplierProfile := SupplierProfiles{ + &SupplierProfile{ + Tenant: "cgrates.org", + ID: "supplierprofile2", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Sorting: "", + SortingParams: []string{}, + Suppliers: []*Supplier{ + &Supplier{ + ID: "supplier1", + FilterIDs: []string{}, + AccountIDs: []string{}, + RatingPlanIDs: []string{}, + ResourceIDs: []string{}, + StatIDs: []string{}, + Weight: 20.0, + SupplierParameters: "param1", + }, + }, + Blocker: false, + Weight: 20.0, + }, + &SupplierProfile{ + Tenant: "cgrates.org", + ID: "supplierprofile1", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Sorting: "", + SortingParams: []string{}, + Suppliers: []*Supplier{ + &Supplier{ + ID: "supplier1", + FilterIDs: []string{}, + AccountIDs: []string{}, + RatingPlanIDs: []string{}, + ResourceIDs: []string{}, + StatIDs: []string{}, + Weight: 10.0, + SupplierParameters: "param1", + }, + }, + Blocker: false, + Weight: 10.0, + }, + } + sprs.Sort() + if !reflect.DeepEqual(eSupplierProfile, sprs) { + t.Errorf("Expecting: %+v, received: %+v", eSupplierProfile, sprs) + } +} + +func TestSuppliersCache(t *testing.T) { + //Need clone because time.Now adds extra information that DeepEqual doesn't like + if err := utils.Clone(expTime, &cloneExpTime); err != nil { + t.Error(err) + } + data, _ := NewMapStorage() + dmSPP = NewDataManager(data) + for _, spp := range sppTest { + if err = dmSPP.SetSupplierProfile(spp, false); err != nil { + t.Errorf("Error: %+v", err) + } + } + //Test each supplier profile from cache + for _, spp := range sppTest { + if tempSpp, err := dmSPP.GetSupplierProfile(spp.Tenant, spp.ID, false, utils.NonTransactional); err != nil { + t.Errorf("Error: %+v", err) + } else if !reflect.DeepEqual(spp, tempSpp) { + t.Errorf("Expecting: %+v, received: %+v", spp, tempSpp) + } + } +} + +func TestSuppliersPopulateSupplierService(t *testing.T) { + data, _ := NewMapStorage() + dmSPP = NewDataManager(data) + var filters1 []*RequestFilter + var filters2 []*RequestFilter + var preffilter []*RequestFilter + var defaultf []*RequestFilter + second := 1 * time.Second + //refresh the DM + ref := NewReqFilterIndexer(dmSPP, utils.SupplierProfilePrefix, "cgrates.org") + + //filter1 + x, err := NewRequestFilter(MetaString, "Supplier", []string{"SupplierProfile1"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + x, err = NewRequestFilter(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"9.0"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + filter3 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter3", RequestFilters: filters1} + dmSPP.SetFilter(filter3) ref.IndexTPFilter(FilterToTPFilter(filter3), "supplierprofile1") + + //filter2 + x, err = NewRequestFilter(MetaString, "Supplier", []string{"SupplierProfile2"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + x, err = NewRequestFilter(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"15.0"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + filter4 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter4", RequestFilters: filters2} + dmSPP.SetFilter(filter4) ref.IndexTPFilter(FilterToTPFilter(filter4), "supplierprofile2") + + //prefix filter + x, err = NewRequestFilter(MetaPrefix, "Supplier", []string{"supplierprofilePrefix"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + preffilter = append(preffilter, x) + preffilter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter2", RequestFilters: preffilter} + dmSPP.SetFilter(preffilter2) ref.IndexTPFilter(FilterToTPFilter(preffilter2), "supplierprofile3") + + //default filter + x, err = NewRequestFilter(MetaGreaterOrEqual, "Weight", []string{"200.00"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + defaultf = append(defaultf, x) + defaultf2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf2", RequestFilters: defaultf} + dmSPP.SetFilter(defaultf2) ref.IndexTPFilter(FilterToTPFilter(defaultf2), "supplierprofile4") + splserv = SupplierService{ + dm: dmSPP, + filterS: &FilterS{dm: dmSPP}, + sorter: map[string]SuppliersSorter{ + utils.MetaWeight: NewWeightSorter(), + utils.MetaLeastCost: NewLeastCostSorter(&splserv), + }, + } + for _, spr := range sppTest { + dmSPP.SetSupplierProfile(spr, false) + } err = ref.StoreIndexes() if err != nil { t.Errorf("Error: %+v", err) @@ -389,44 +442,55 @@ func TestSuppliersmatchingSupplierProfilesForEvent(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sprsmatch[1], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sprsmatch[1], sprf[0]) - } else if !reflect.DeepEqual(sprsmatch[0], sprf[1]) { - t.Errorf("Expecting: %+v, received: %+v", sprsmatch[0], sprf[1]) + if !reflect.DeepEqual(sppTest[0], sprf[0]) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) + } + sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv2.CGREvent) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(sppTest[1], sprf[0]) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) } - stringPref := &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "utils.CGREvent1", - Event: map[string]interface{}{ - "Supplier": "supplierprofilePrefix", - }, - } - sprf, err = splserv.matchingSupplierProfilesForEvent(stringPref) + sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv3.CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sprsmatch[2], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sprsmatch[2], sprf[0]) + if !reflect.DeepEqual(sppTest[2], sprf[0]) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0]) } - evDefault := &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "utils.CGREvent1", - Event: map[string]interface{}{ - "Weight": "200.00", - }, - } - sprf, err = splserv.matchingSupplierProfilesForEvent(evDefault) + sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv4.CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sprsmatch[3], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sprsmatch[3], sprf[0]) + if !reflect.DeepEqual(sppTest[3], sprf[0]) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[3], sprf[0]) } } func TestSuppliersSortedForEvent(t *testing.T) { eFirstSupplierProfile := &SortedSuppliers{ + ProfileID: "supplierprofile1", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + &SortedSupplier{ + SupplierID: "supplier1", + SortingData: map[string]interface{}{ + "Weight": 10.0, + }, + SupplierParameters: "param1", + }, + }, + } + sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) + } + eFirstSupplierProfile = &SortedSuppliers{ ProfileID: "supplierprofile2", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ @@ -453,7 +517,61 @@ func TestSuppliersSortedForEvent(t *testing.T) { }, }, } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + sprf, err = splserv.sortedSuppliersForEvent(argPagEv2) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) + } + eFirstSupplierProfile = &SortedSuppliers{ + ProfileID: "supplierprofile3", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + &SortedSupplier{ + SupplierID: "supplier1", + SortingData: map[string]interface{}{ + "Weight": 10.0, + }, + SupplierParameters: "param1", + }, + }, + } + sprf, err = splserv.sortedSuppliersForEvent(argPagEv3) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) + } + eFirstSupplierProfile = &SortedSuppliers{ + ProfileID: "supplierprofile4", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + &SortedSupplier{ + SupplierID: "supplier1", + SortingData: map[string]interface{}{ + "Weight": 30.0, + }, + SupplierParameters: "param1", + }, + &SortedSupplier{ + SupplierID: "supplier2", + SortingData: map[string]interface{}{ + "Weight": 20.0, + }, + SupplierParameters: "param2", + }, + &SortedSupplier{ + SupplierID: "supplier3", + SortingData: map[string]interface{}{ + "Weight": 10.0, + }, + SupplierParameters: "param3", + }, + }, + } + sprf, err = splserv.sortedSuppliersForEvent(argPagEv4) if err != nil { t.Errorf("Error: %+v", err) } @@ -483,10 +601,10 @@ func TestSuppliersSortedForEventWithLimit(t *testing.T) { }, }, } - argPagEv.Paginator = utils.Paginator{ + argPagEv2.Paginator = utils.Paginator{ Limit: utils.IntPointer(2), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) if err != nil { t.Errorf("Error: %+v", err) } @@ -509,10 +627,10 @@ func TestSuppliersSortedForEventWithOffset(t *testing.T) { }, }, } - argPagEv.Paginator = utils.Paginator{ + argPagEv2.Paginator = utils.Paginator{ Offset: utils.IntPointer(2), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) if err != nil { t.Errorf("Error: %+v", err) } @@ -535,11 +653,11 @@ func TestSuppliersSortedForEventWithLimitAndOffset(t *testing.T) { }, }, } - argPagEv.Paginator = utils.Paginator{ + argPagEv2.Paginator = utils.Paginator{ Limit: utils.IntPointer(1), Offset: utils.IntPointer(1), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) if err != nil { t.Errorf("Error: %+v", err) }