diff --git a/data/tariffplans/tutorial/Filters.csv b/data/tariffplans/tutorial/Filters.csv index ae3fffbe9..fd2b2ce35 100644 --- a/data/tariffplans/tutorial/Filters.csv +++ b/data/tariffplans/tutorial/Filters.csv @@ -1,6 +1,7 @@ #Tenant[0],ID[1],FilterType[2],FilterFieldName[3],FilterFieldValues[4],ActivationInterval[5] cgrates.org,FLTR_1,*string,Account,1001;1002,2014-07-29T15:00:00Z -cgrates.org,FLTR_DST_FS,*destinations,Destination,DST_FS,2014-07-29T15:00:00Z +cgrates.org,FLTR_DST_FS,string,Account,1001,2014-07-29T15:00:00Z +cgrates.org,FLTR_DST_FS,*destinations,Destination,DST_FS, cgrates.org,FLTR_RES_GR3,*string,Account,1003,2014-07-29T15:00:00Z cgrates.org,FLTR_ACNT_1001_1002,*string,Account,1001;1002,2014-07-29T15:00:00Z cgrates.org,FLTR_ACNT_1001_1002,*string,RunID,*default, diff --git a/data/tariffplans/tutorial/Resources.csv b/data/tariffplans/tutorial/Resources.csv index 3eab7d183..645680b4e 100644 --- a/data/tariffplans/tutorial/Resources.csv +++ b/data/tariffplans/tutorial/Resources.csv @@ -1,3 +1,2 @@ #Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],TTL[4],Limit[5],AllocationMessage[6],Blocker[7],Stored[8],Weight[9],ThresholdIDs[10] -cgrates.org,ResGroup1,FLTR_1,2014-07-29T15:00:00Z,3600s,7,,false,true,10, -cgrates.org,ResGroup2,FLTR_DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1003,false,true,10, \ No newline at end of file +cgrates.org,ResGroup1,FLTR_1,2014-07-29T15:00:00Z,3600s,7,,false,true,10, \ No newline at end of file diff --git a/dispatcher/resources_it_test.go b/dispatcher/resources_it_test.go index 89ca99752..1dd57f038 100755 --- a/dispatcher/resources_it_test.go +++ b/dispatcher/resources_it_test.go @@ -254,11 +254,6 @@ func testDspResTestAuthKey2(t *testing.T) { ID: "ResGroup1", Usages: map[string]*engine.ResourceUsage{}, }, - &engine.Resource{ - Tenant: "cgrates.org", - ID: "ResGroup2", - Usages: map[string]*engine.ResourceUsage{}, - }, } if err := dspResRPC.Call(utils.ResourceSv1GetResourcesForEvent, diff --git a/engine/attributes_test.go b/engine/attributes_test.go index aa06ea894..69d49e0b3 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -29,70 +29,63 @@ import ( var ( cloneExpTimeAttributes time.Time expTimeAttributes = time.Now().Add(time.Duration(20 * time.Minute)) - srv AttributeService + attrService *AttributeService dmAtr *DataManager - context = utils.MetaSessionS mapSubstitutes = map[string]map[interface{}]*Attribute{ - "FL1": map[interface{}]*Attribute{ - "In1": &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", + utils.Account: map[interface{}]*Attribute{ + utils.META_ANY: &Attribute{ + FieldName: utils.Account, + Initial: utils.META_ANY, + Substitute: "1010", Append: true, }, }, } - 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", + attrEvs = []*utils.CGREvent{ + &utils.CGREvent{ //matching AttributeProfile1 + Tenant: config.CgrConfig().DefaultTenant, + ID: utils.GenUUID(), + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Attribute": "AttributeProfile1", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + utils.Weight: "20.0", + }, + }, + &utils.CGREvent{ //matching AttributeProfile2 + Tenant: config.CgrConfig().DefaultTenant, + ID: utils.GenUUID(), + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Attribute": "AttributeProfile2", + }, + }, + &utils.CGREvent{ //matching AttributeProfilePrefix + Tenant: config.CgrConfig().DefaultTenant, + ID: utils.GenUUID(), + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Attribute": "AttributeProfilePrefix", + }, }, } + atrPs = AttributeProfiles{ &AttributeProfile{ - Tenant: "cgrates.org", - ID: "attributeprofile1", - Contexts: []string{context}, - FilterIDs: []string{"filter1"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "AttributeProfile1", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"FLTR_ATTR_1"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeAttributes, }, Attributes: []*Attribute{ &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", + FieldName: utils.Account, + Initial: utils.META_ANY, + Substitute: "1010", Append: true, }, }, @@ -100,19 +93,19 @@ var ( attributes: mapSubstitutes, }, &AttributeProfile{ - Tenant: "cgrates.org", - ID: "attributeprofile2", - Contexts: []string{context}, - FilterIDs: []string{"filter2"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "AttributeProfile2", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"FLTR_ATTR_2"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeAttributes, }, Attributes: []*Attribute{ &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", + FieldName: utils.Account, + Initial: utils.META_ANY, + Substitute: "1010", Append: true, }, }, @@ -120,39 +113,19 @@ var ( attributes: mapSubstitutes, }, &AttributeProfile{ - Tenant: "cgrates.org", - ID: "attributeprofile3", - Contexts: []string{context}, - FilterIDs: []string{"preffilter1"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "AttributeProfilePrefix", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"FLTR_ATTR_3"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeAttributes, }, Attributes: []*Attribute{ &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", - Append: true, - }, - }, - attributes: mapSubstitutes, - Weight: 20, - }, - &AttributeProfile{ - Tenant: "cgrates.org", - ID: "attributeprofile4", - Contexts: []string{context}, - FilterIDs: []string{"defaultf1"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: cloneExpTimeAttributes, - }, - Attributes: []*Attribute{ - &Attribute{ - FieldName: "FL1", - Initial: "In1", - Substitute: "Al1", + FieldName: utils.Account, + Initial: utils.META_ANY, + Substitute: "1010", Append: true, }, }, @@ -162,19 +135,91 @@ var ( } ) -func TestAttributeCache(t *testing.T) { +func TestAttributePopulateAttrService(t *testing.T) { //Need clone because time.Now adds extra information that DeepEqual doesn't like if err := utils.Clone(expTimeAttributes, &cloneExpTimeAttributes); err != nil { t.Error(err) } data, _ := NewMapStorage() dmAtr = NewDataManager(data) + defaultCfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Errorf("Error: %+v", err) + } + attrService, err = NewAttributeService(dmAtr, &FilterS{dm: dmAtr, cfg: defaultCfg}, nil, nil) + if err != nil { + t.Errorf("Error: %+v", err) + } +} + +func TestAttributeAddFilters(t *testing.T) { + fltrAttr1 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_ATTR_1", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Attribute", + Values: []string{"AttributeProfile1"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "UsageInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"9.0"}, + }, + }, + } + dmAtr.SetFilter(fltrAttr1) + fltrAttr2 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_ATTR_2", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Attribute", + Values: []string{"AttributeProfile2"}, + }, + }, + } + dmAtr.SetFilter(fltrAttr2) + fltrAttrPrefix := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_ATTR_3", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaPrefix, + FieldName: "Attribute", + Values: []string{"AttributeProfilePrefix"}, + }, + }, + } + dmAtr.SetFilter(fltrAttrPrefix) + fltrAttr4 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_ATTR_4", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"200.00"}, + }, + }, + } + dmAtr.SetFilter(fltrAttr4) +} + +func TestAttributeCache(t *testing.T) { for _, atr := range atrPs { - if err = dmAtr.SetAttributeProfile(atr, false); err != nil { + if err = dmAtr.SetAttributeProfile(atr, true); err != nil { t.Errorf("Error: %+v", err) } } - //Test each attribute from cache + //verify each attribute from cache for _, atr := range atrPs { if tempAttr, err := dmAtr.GetAttributeProfile(atr.Tenant, atr.ID, false, utils.NonTransactional); err != nil { @@ -185,212 +230,73 @@ func TestAttributeCache(t *testing.T) { } } -func TestAttributePopulateAttrService(t *testing.T) { - var filters1 []*FilterRule - var filters2 []*FilterRule - var preffilter []*FilterRule - var defaultf []*FilterRule - second := 1 * time.Second - //refresh the DM - data, _ := NewMapStorage() - dmAtr = NewDataManager(data) - defaultCfg, err := config.NewDefaultCGRConfig() - if err != nil { - t.Errorf("Error: %+v", err) - } - srv = AttributeService{ - dm: dmAtr, - filterS: &FilterS{dm: dmAtr, cfg: defaultCfg}, - } - ref := NewFilterIndexer(dmAtr, utils.AttributeProfilePrefix, - utils.ConcatenatedKey(config.CgrConfig().DefaultTenant, utils.MetaSessionS)) - for _, atr := range atrPs { - if err = dmAtr.SetAttributeProfile(atr, false); err != nil { - t.Errorf("Error: %+v", err) - } - } - //filter1 - x, err := NewFilterRule(MetaString, "Attribute", []string{"AttributeProfile1"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"9.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - filter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter1", Rules: filters1} - dmAtr.SetFilter(filter1) - ref.IndexTPFilter(FilterToTPFilter(filter1), "attributeprofile1") - //filter2 - x, err = NewFilterRule(MetaString, "Attribute", []string{"AttributeProfile2"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - filter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter2", Rules: filters2} - dmAtr.SetFilter(filter2) - ref.IndexTPFilter(FilterToTPFilter(filter2), "attributeprofile2") - //prefix filter - x, err = NewFilterRule(MetaPrefix, "Attribute", []string{"AttributeProfilePrefix"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - preffilter1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter1", Rules: preffilter} - dmAtr.SetFilter(preffilter1) - ref.IndexTPFilter(FilterToTPFilter(preffilter1), "attributeprofile3") - //default filter - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"200.00"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - defaultf1 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf1", Rules: defaultf} - dmAtr.SetFilter(defaultf1) - ref.IndexTPFilter(FilterToTPFilter(defaultf1), "attributeprofile4") - err = ref.StoreIndexes(true, utils.NonTransactional) - if err != nil { - t.Errorf("Error: %+v", err) - } -} - func TestAttributeMatchingAttributeProfilesForEvent(t *testing.T) { - atrp, err := srv.matchingAttributeProfilesForEvent(sev) + atrp, err := attrService.matchingAttributeProfilesForEvent(attrEvs[0]) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(atrPs[0], atrp[0]) { t.Errorf("Expecting: %+v, received: %+v ", atrPs[0], atrp[0]) } - atrp, err = srv.matchingAttributeProfilesForEvent(sev2) + atrp, err = attrService.matchingAttributeProfilesForEvent(attrEvs[1]) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(atrPs[1], atrp[0]) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs), utils.ToJSON(atrp)) } - atrp, err = srv.matchingAttributeProfilesForEvent(sev3) + atrp, err = attrService.matchingAttributeProfilesForEvent(attrEvs[2]) if err != nil { t.Errorf("Error: %+v", err) } 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) { - atrp, err := srv.attributeProfileForEvent(sev) + atrp, err := attrService.attributeProfileForEvent(attrEvs[0]) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(atrPs[0], atrp) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[0]), utils.ToJSON(atrp)) } - atrp, err = srv.attributeProfileForEvent(sev2) + + atrp, err = attrService.attributeProfileForEvent(attrEvs[1]) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(atrPs[1], atrp) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs[1]), utils.ToJSON(atrp)) } - atrp, err = srv.attributeProfileForEvent(sev3) + + atrp, err = attrService.attributeProfileForEvent(attrEvs[2]) if err != nil { t.Errorf("Error: %+v", err) } 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) { + attrEvs[0].Event["Account"] = "1010" //Field added in event after process eRply := &AttrSProcessEventReply{ - MatchedProfile: "attributeprofile1", - AlteredFields: []string{"FL1"}, - CGREvent: sev, + MatchedProfile: "AttributeProfile1", + AlteredFields: []string{"Account"}, + CGREvent: attrEvs[0], } - atrp, err := srv.processEvent(sev) + atrp, err := attrService.processEvent(attrEvs[0]) 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) + if !reflect.DeepEqual(eRply, atrp) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eRply), utils.ToJSON(atrp)) } - eRply = &AttrSProcessEventReply{ - MatchedProfile: "attributeprofile2", - AlteredFields: []string{"FL1"}, - 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", - AlteredFields: []string{"FL1"}, - 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", - AlteredFields: []string{"FL1"}, - 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) - // } } -func TestAttrSProcessEventReplyDigest(t *testing.T) { +func TestAttributeEventReplyDigest(t *testing.T) { eRpl := &AttrSProcessEventReply{ MatchedProfile: "ATTR_1", AlteredFields: []string{utils.Account, utils.Subject}, @@ -412,7 +318,7 @@ func TestAttrSProcessEventReplyDigest(t *testing.T) { } } -func TestAttrSProcessEventReplyDigest2(t *testing.T) { +func TestAttributeEventReplyDigest2(t *testing.T) { eRpl := &AttrSProcessEventReply{ MatchedProfile: "ATTR_1", AlteredFields: []string{}, @@ -434,7 +340,7 @@ func TestAttrSProcessEventReplyDigest2(t *testing.T) { } } -func TestAttrSProcessEventReplyDigest3(t *testing.T) { +func TestAttributeEventReplyDigest3(t *testing.T) { eRpl := &AttrSProcessEventReply{ MatchedProfile: "ATTR_1", AlteredFields: []string{"Subject"}, @@ -456,7 +362,7 @@ func TestAttrSProcessEventReplyDigest3(t *testing.T) { } } -func TestAttrSProcessEventReplyDigest4(t *testing.T) { +func TestAttributeEventReplyDigest4(t *testing.T) { eRpl := &AttrSProcessEventReply{ MatchedProfile: "ATTR_1", AlteredFields: []string{"Subject"}, diff --git a/engine/filterhelpers_test.go b/engine/filterhelpers_test.go index df0a0ca6b..fd019e751 100644 --- a/engine/filterhelpers_test.go +++ b/engine/filterhelpers_test.go @@ -36,7 +36,6 @@ func TestFilterMatchingItemIDsForEvent(t *testing.T) { var stringFilter, prefixFilter, defaultFilter []*FilterRule stringFilterID := "stringFilterID" prefixFilterID := "prefixFilterID" - // defaultFilterID := "defaultFilterID" data, _ := NewMapStorage() dmMatch = NewDataManager(data) context := utils.MetaRating @@ -68,7 +67,6 @@ func TestFilterMatchingItemIDsForEvent(t *testing.T) { atrRFI := NewFilterIndexer(dmMatch, utils.AttributeProfilePrefix, prefix) atrRFI.IndexTPFilter(FilterToTPFilter(attribStringF), stringFilterID) atrRFI.IndexTPFilter(FilterToTPFilter(attribPrefF), prefixFilterID) - // atrRFI.IndexTPFilter(FilterToTPFilter(attribDefaultF), defaultFilterID) err = atrRFI.StoreIndexes(true, utils.NonTransactional) if err != nil { t.Errorf("Error: %+v", err) @@ -98,16 +96,4 @@ func TestFilterMatchingItemIDsForEvent(t *testing.T) { if !has { t.Errorf("Expecting: %+v, received: %+v", prefixFilterID, aPrflIDs) } - matchEV = map[string]interface{}{ - "Weight": "200", - } - // aPrflIDs, err = matchingItemIDsForEvent(matchEV, nil, nil, - // dmMatch, utils.CacheAttributeFilterIndexes, prefix) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } - // _, has = aPrflIDs[defaultFilterID] - // if !has { - // t.Errorf("Expecting: %+v, received: %+v", defaultFilterID, aPrflIDs) - // } } diff --git a/engine/resources_test.go b/engine/resources_test.go index 3c8f9465f..3974b39ea 100644 --- a/engine/resources_test.go +++ b/engine/resources_test.go @@ -33,142 +33,110 @@ var ( cloneExpTimeResource time.Time expTimeResource = time.Now().Add(time.Duration(20 * time.Minute)) timeDurationExample = time.Duration(10) * time.Second - resserv ResourceService + resService *ResourceService dmRES *DataManager resprf = []*ResourceProfile{ &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile1", // identifier of this resource - FilterIDs: []string{"filter9"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile1", + FilterIDs: []string{"FLTR_RES_1"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, - UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + UsageTTL: time.Duration(10) * time.Second, + Limit: 10.00, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{""}, }, &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile2", // identifier of this resource - FilterIDs: []string{"filter10"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile2", // identifier of this resource + FilterIDs: []string{"FLTR_RES_2"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, - UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + UsageTTL: time.Duration(10) * time.Second, + Limit: 10.00, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{""}, }, &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile3", // identifier of this resource - FilterIDs: []string{"preffilter5"}, + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile3", + FilterIDs: []string{"FLTR_RES_3"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, - UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit - }, - &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile4", // identifier of this resource - FilterIDs: []string{"defaultf5"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + UsageTTL: time.Duration(10) * time.Second, + Limit: 10.00, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{""}, }, } resourceTest = []*Resource{ &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile1", + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile1", Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled - rPrf: resprf[0], // for ordering purposes + TTLIdx: []string{}, + rPrf: resprf[0], }, &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile2", + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile2", Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled - rPrf: resprf[1], // for ordering purposes + TTLIdx: []string{}, + rPrf: resprf[1], }, &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile3", + Tenant: config.CgrConfig().DefaultTenant, + ID: "ResourceProfile3", Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled - rPrf: resprf[2], // for ordering purposes - }, - &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile4", - Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled - rPrf: resprf[3], // for ordering purposes + TTLIdx: []string{}, + rPrf: resprf[2], }, } resEvs = []*utils.CGREvent{ &utils.CGREvent{ - Tenant: "cgrates.org", + Tenant: config.CgrConfig().DefaultTenant, ID: "event1", Event: map[string]interface{}{ - "Resources": "ResourcesProfile1", + "Resources": "ResourceProfile1", utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), "UsageInterval": "1s", "PddInterval": "1s", - "Weight": "20.0", + utils.Weight: "20.0", utils.Usage: time.Duration(135 * time.Second), utils.COST: 123.0, - }}, + }, + }, &utils.CGREvent{ - Tenant: "cgrates.org", + Tenant: config.CgrConfig().DefaultTenant, ID: "event2", Event: map[string]interface{}{ - "Resources": "ResourcesProfile2", + "Resources": "ResourceProfile2", utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), "UsageInterval": "1s", "PddInterval": "1s", - "Weight": "21.0", + utils.Weight: "15.0", utils.Usage: time.Duration(45 * time.Second), - }}, + }, + }, &utils.CGREvent{ - Tenant: "cgrates.org", + Tenant: config.CgrConfig().DefaultTenant, ID: "event3", Event: map[string]interface{}{ - "Resources": "ResourcesProfilePrefix", + "Resources": "ResourceProfilePrefix", utils.Usage: time.Duration(30 * time.Second), - }}, - &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "event3", - Event: map[string]interface{}{ - "Weight": "200.0", - utils.Usage: time.Duration(65 * time.Second), - }}, + }, + }, } ) -func TestRSRecordUsage1(t *testing.T) { +func TestResourceRecordUsage1(t *testing.T) { ru1 = &ResourceUsage{ Tenant: "cgrates.org", ID: "RU1", @@ -223,7 +191,7 @@ func TestRSRecordUsage1(t *testing.T) { } } -func TestRSRemoveExpiredUnits(t *testing.T) { +func TestResourceRemoveExpiredUnits(t *testing.T) { r1.Usages = map[string]*ResourceUsage{ ru1.ID: ru1, } @@ -242,7 +210,7 @@ func TestRSRemoveExpiredUnits(t *testing.T) { } } -func TestRSUsedUnits(t *testing.T) { +func TestResourceUsedUnits(t *testing.T) { r1.Usages = map[string]*ResourceUsage{ ru1.ID: ru1, } @@ -252,7 +220,7 @@ func TestRSUsedUnits(t *testing.T) { } } -func TestRSRsort(t *testing.T) { +func TestResourceSort(t *testing.T) { r2 = &Resource{ Tenant: "cgrates.org", ID: "RL2", @@ -284,7 +252,7 @@ func TestRSRsort(t *testing.T) { } } -func TestRSClearUsage(t *testing.T) { +func TestResourceClearUsage(t *testing.T) { r1.Usages = map[string]*ResourceUsage{ ru1.ID: ru1, } @@ -305,7 +273,7 @@ func TestRSClearUsage(t *testing.T) { } } -func TestRSRecordUsages(t *testing.T) { +func TestResourceRecordUsages(t *testing.T) { r1.Usages = map[string]*ResourceUsage{ ru1.ID: ru1, } @@ -315,7 +283,7 @@ func TestRSRecordUsages(t *testing.T) { } } -func TestRSAllocateResource(t *testing.T) { +func TestResourceAllocateResource(t *testing.T) { rs.clearUsage(ru1.ID) rs.clearUsage(ru2.ID) ru1.ExpiryTime = time.Now().Add(time.Duration(1 * time.Second)) @@ -394,18 +362,21 @@ func TestRSCacheSetGet(t *testing.T) { } } -func TestV1AuthorizeResourceMissingStruct(t *testing.T) { +func TestResourcePopulateResourceService(t *testing.T) { data, _ := NewMapStorage() - dmresmiss := NewDataManager(data) + dmRES = NewDataManager(data) defaultCfg, err := config.NewDefaultCGRConfig() if err != nil { t.Errorf("Error: %+v", err) } - rserv := &ResourceService{ - dm: dmresmiss, - filterS: &FilterS{dm: dmresmiss, cfg: defaultCfg}, - stringIndexedFields: &[]string{}, // speed up query on indexes + resService, err = NewResourceService(dmRES, time.Duration(1), nil, + &FilterS{dm: dmRES, cfg: defaultCfg}, nil, nil) + if err != nil { + t.Errorf("Error: %+v", err) } +} + +func TestResourceV1AuthorizeResourceMissingStruct(t *testing.T) { var reply *string argsMissingTenant := utils.ArgRSv1ResourceUsage{ CGREvent: utils.CGREvent{ @@ -423,126 +394,84 @@ func TestV1AuthorizeResourceMissingStruct(t *testing.T) { }, Units: 20, } - if err := rserv.V1AuthorizeResources(argsMissingTenant, reply); err.Error() != "MANDATORY_IE_MISSING: [Tenant]" { + if err := resService.V1AuthorizeResources(argsMissingTenant, reply); err.Error() != "MANDATORY_IE_MISSING: [Tenant]" { t.Error(err.Error()) } - if err := rserv.V1AuthorizeResources(argsMissingUsageID, reply); err.Error() != "MANDATORY_IE_MISSING: [UsageID]" { + if err := resService.V1AuthorizeResources(argsMissingUsageID, reply); err.Error() != "MANDATORY_IE_MISSING: [UsageID]" { t.Error(err.Error()) } } -func TestRSPopulateResourceService(t *testing.T) { - data, _ := NewMapStorage() - dmRES = NewDataManager(data) - defaultCfg, err := config.NewDefaultCGRConfig() - if err != nil { - t.Errorf("Error: %+v", err) +func TestResourceAddFilters(t *testing.T) { + fltrRes1 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_RES_1", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Resources", + Values: []string{"ResourceProfile1"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "UsageInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Usage, + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"9.0"}, + }, + }, } - var filters1 []*FilterRule - var filters2 []*FilterRule - var preffilter []*FilterRule - var defaultf []*FilterRule - second := 1 * time.Second - resserv = ResourceService{ - dm: dmRES, - filterS: &FilterS{dm: dmRES, cfg: defaultCfg}, + dmRES.SetFilter(fltrRes1) + fltrRes2 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_RES_2", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Resources", + Values: []string{"ResourceProfile2"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "PddInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Usage, + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"15.0"}, + }, + }, } - ref := NewFilterIndexer(dmRES, utils.ResourceProfilesPrefix, "cgrates.org") - //filter1 - x, err := NewFilterRule(MetaString, "Resources", []string{"ResourcesProfile1"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"9.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - filter9 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter9", Rules: filters1} - dmRES.SetFilter(filter9) - ref.IndexTPFilter(FilterToTPFilter(filter9), "resourcesprofile1") - //filter2 - x, err = NewFilterRule(MetaString, "Resources", []string{"ResourcesProfile2"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"15.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - filter10 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter10", Rules: filters2} - dmRES.SetFilter(filter10) - ref.IndexTPFilter(FilterToTPFilter(filter10), "resourcesprofile2") - //prefix filter - x, err = NewFilterRule(MetaPrefix, "Resources", []string{"ResourcesProfilePrefix"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - preffilter5 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter5", Rules: preffilter} - dmRES.SetFilter(preffilter5) - ref.IndexTPFilter(FilterToTPFilter(preffilter5), "resourcesprofile3") - //default filter - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"200.00"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - defaultf5 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf5", Rules: defaultf} - dmRES.SetFilter(defaultf5) - ref.IndexTPFilter(FilterToTPFilter(defaultf5), "resourcesprofile4") - for _, res := range resourceTest { - dmRES.SetResource(res) - } - for _, resp := range resprf { - dmRES.SetResourceProfile(resp, false) - } - err = ref.StoreIndexes(true, utils.NonTransactional) - if err != nil { - t.Errorf("Error: %+v", err) + dmRES.SetFilter(fltrRes2) + fltrRes3 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_RES_3", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaPrefix, + FieldName: "Resources", + Values: []string{"ResourceProfilePrefix"}, + }, + }, } + dmRES.SetFilter(fltrRes3) } -func TestCachedResourcesForEvent(t *testing.T) { - data, _ := NewMapStorage() - dmRES = NewDataManager(data) - resS := ResourceService{ - dm: dmRES, - filterS: &FilterS{dm: dmRES}, - } +func TestResourceCachedResourcesForEvent(t *testing.T) { args := &utils.ArgRSv1ResourceUsage{ CGREvent: *resEvs[0], UsageID: "IDF", @@ -588,15 +517,33 @@ func TestCachedResourcesForEvent(t *testing.T) { resources[0], nil, true, "") Cache.Set(utils.CacheEventResources, args.TenantID(), val, nil, true, "") - rcv := resS.cachedResourcesForEvent(args.TenantID()) + rcv := resService.cachedResourcesForEvent(args.TenantID()) if !reflect.DeepEqual(resources[0], rcv[0]) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(resources[0]), utils.ToJSON(rcv[0])) } } -func TestRSmatchingResourcesForEvent(t *testing.T) { - mres, err := resserv.matchingResourcesForEvent(resEvs[0], &timeDurationExample) +func TestResourceAddResourceProfile(t *testing.T) { + for _, resProfile := range resprf { + dmRES.SetResourceProfile(resProfile, true) + } + for _, res := range resourceTest { + dmRES.SetResource(res) + } + //Test each resourceProfile from cache + for _, resPrf := range resprf { + if tempRes, err := dmRES.GetResourceProfile(resPrf.Tenant, + resPrf.ID, false, utils.NonTransactional); err != nil { + t.Errorf("Error: %+v", err) + } else if !reflect.DeepEqual(resPrf, tempRes) { + t.Errorf("Expecting: %+v, received: %+v", resPrf, tempRes) + } + } +} + +func TestResourceMatchingResourcesForEvent(t *testing.T) { + mres, err := resService.matchingResourcesForEvent(resEvs[0], &timeDurationExample) if err != nil { t.Errorf("Error: %+v", err) } @@ -607,7 +554,8 @@ func TestRSmatchingResourcesForEvent(t *testing.T) { } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) } - mres, err = resserv.matchingResourcesForEvent(resEvs[1], &timeDurationExample) + + mres, err = resService.matchingResourcesForEvent(resEvs[1], &timeDurationExample) if err != nil { t.Errorf("Error: %+v", err) } @@ -618,7 +566,8 @@ func TestRSmatchingResourcesForEvent(t *testing.T) { } else if !reflect.DeepEqual(resourceTest[1].rPrf, mres[0].rPrf) { t.Errorf("Expecting: %+v, received: %+v", resourceTest[1].rPrf, mres[0].rPrf) } - mres, err = resserv.matchingResourcesForEvent(resEvs[2], &timeDurationExample) + + mres, err = resService.matchingResourcesForEvent(resEvs[2], &timeDurationExample) if err != nil { t.Errorf("Error: %+v", err) } @@ -629,237 +578,108 @@ func TestRSmatchingResourcesForEvent(t *testing.T) { } else if !reflect.DeepEqual(resourceTest[2].rPrf, mres[0].rPrf) { t.Errorf("Expecting: %+v, received: %+v", resourceTest[2].rPrf, mres[0].rPrf) } - // mres, err = resserv.matchingResourcesForEvent(resEvs[3], &timeDurationExample) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } - // if !reflect.DeepEqual(resourceTest[3].Tenant, mres[0].Tenant) { - // t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].Tenant, mres[0].Tenant) - // } else if !reflect.DeepEqual(resourceTest[3].ID, mres[0].ID) { - // t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].ID, mres[0].ID) - // } else if !reflect.DeepEqual(resourceTest[3].rPrf, mres[0].rPrf) { - // t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].rPrf, mres[0].rPrf) - // } } //UsageTTL 0 in ResourceProfile and give 10s duration -func TestRSUsageTTLCase1(t *testing.T) { - resPrf := &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile1", - FilterIDs: []string{"filter9"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - UsageTTL: time.Duration(0), - Limit: 10.00, - AllocationMessage: "AllocationMessage", - Blocker: false, - Stored: false, - Weight: 20.00, - ThresholdIDs: []string{""}, - } - res := &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile1", - Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, - rPrf: resPrf, - ttl: &timeDurationExample, - } - ev := &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "event1", - Event: map[string]interface{}{ - "Resources": "ResourcesProfile1", - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "UsageInterval": "1s", - "PddInterval": "1s", - "Weight": "20.0", - utils.Usage: time.Duration(135 * time.Second), - utils.COST: 123.0, - }} - if err := dmRES.SetResourceProfile(resPrf, false); err != nil { +func TestResourceUsageTTLCase1(t *testing.T) { + resprf[0].UsageTTL = time.Duration(0) + resourceTest[0].rPrf = resprf[0] + resourceTest[0].ttl = &timeDurationExample + if err := dmRES.SetResourceProfile(resprf[0], true); err != nil { t.Error(err) } - if err := dmRES.SetResource(res); err != nil { + if err := dmRES.SetResource(resourceTest[0]); err != nil { t.Error(err) } - mres, err := resserv.matchingResourcesForEvent(ev, &timeDurationExample) + mres, err := resService.matchingResourcesForEvent(resEvs[0], &timeDurationExample) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(res.Tenant, mres[0].Tenant) { - t.Errorf("Expecting: %+v, received: %+v", res.Tenant, mres[0].Tenant) - } else if !reflect.DeepEqual(res.ID, mres[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", res.ID, mres[0].ID) - } else if !reflect.DeepEqual(res.rPrf, mres[0].rPrf) { - t.Errorf("Expecting: %+v, received: %+v", res.rPrf, mres[0].rPrf) - } else if !reflect.DeepEqual(res.ttl, mres[0].ttl) { - t.Errorf("Expecting: %+v, received: %+v", res.ttl, mres[0].ttl) + if !reflect.DeepEqual(resourceTest[0].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[0].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) + } else if !reflect.DeepEqual(resourceTest[0].ttl, mres[0].ttl) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ttl, mres[0].ttl) } } //UsageTTL 5s in ResourceProfile and give nil duration -func TestRSUsageTTLCase2(t *testing.T) { - resPrf := &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile2", - FilterIDs: []string{"filter10"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - UsageTTL: time.Duration(5) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit - } - res := &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile2", - Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, - rPrf: resPrf, - ttl: &resPrf.UsageTTL, - } - ev := &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "event1", - Event: map[string]interface{}{ - "Resources": "ResourcesProfile2", - utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), - "UsageInterval": "1s", - "PddInterval": "1s", - "Weight": "20.0", - utils.Usage: time.Duration(135 * time.Second), - utils.COST: 123.0, - }} - if err := dmRES.SetResource(res); err != nil { +func TestResourceUsageTTLCase2(t *testing.T) { + resprf[0].UsageTTL = time.Duration(0) + resourceTest[0].rPrf = resprf[0] + resourceTest[0].ttl = &resprf[0].UsageTTL + if err := dmRES.SetResourceProfile(resprf[0], true); err != nil { t.Error(err) } - if err := dmRES.SetResourceProfile(resPrf, false); err != nil { + if err := dmRES.SetResource(resourceTest[0]); err != nil { t.Error(err) } - mres, err := resserv.matchingResourcesForEvent(ev, nil) + mres, err := resService.matchingResourcesForEvent(resEvs[0], nil) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(res.Tenant, mres[0].Tenant) { - t.Errorf("Expecting: %+v, received: %+v", res.Tenant, mres[0].Tenant) - } else if !reflect.DeepEqual(res.ID, mres[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", res.ID, mres[0].ID) - } else if !reflect.DeepEqual(res.rPrf, mres[0].rPrf) { - t.Errorf("Expecting: %+v, received: %+v", res.rPrf, mres[0].rPrf) - } else if !reflect.DeepEqual(res.ttl, mres[0].ttl) { - t.Errorf("Expecting: %+v, received: %+v", res.ttl, mres[0].ttl) + if !reflect.DeepEqual(resourceTest[0].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[0].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) + } else if !reflect.DeepEqual(resourceTest[0].ttl, mres[0].ttl) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ttl, mres[0].ttl) } } //UsageTTL 5s in ResourceProfile and give 0 duration -func TestRSUsageTTLCase3(t *testing.T) { - resPrf := &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile3", // identifier of this resource - FilterIDs: []string{"preffilter5"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - UsageTTL: time.Duration(5) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit - } - res := &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile3", - Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, - rPrf: resPrf, - ttl: nil, - } - ev := &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "event3", - Event: map[string]interface{}{ - "Resources": "ResourcesProfilePrefix", - utils.Usage: time.Duration(30 * time.Second), - }} - if err := dmRES.SetResource(res); err != nil { +func TestResourceUsageTTLCase3(t *testing.T) { + resprf[0].UsageTTL = time.Duration(0) + resourceTest[0].rPrf = resprf[0] + resourceTest[0].ttl = nil + if err := dmRES.SetResourceProfile(resprf[0], true); err != nil { t.Error(err) } - if err := dmRES.SetResourceProfile(resPrf, false); err != nil { + if err := dmRES.SetResource(resourceTest[0]); err != nil { t.Error(err) } - mres, err := resserv.matchingResourcesForEvent(ev, utils.DurationPointer(time.Duration(0))) + mres, err := resService.matchingResourcesForEvent(resEvs[0], utils.DurationPointer(time.Duration(0))) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(res.Tenant, mres[0].Tenant) { - t.Errorf("Expecting: %+v, received: %+v", res.Tenant, mres[0].Tenant) - } else if !reflect.DeepEqual(res.ID, mres[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", res.ID, mres[0].ID) - } else if !reflect.DeepEqual(res.rPrf, mres[0].rPrf) { - t.Errorf("Expecting: %+v, received: %+v", res.rPrf, mres[0].rPrf) - } else if !reflect.DeepEqual(res.ttl, mres[0].ttl) { - t.Errorf("Expecting: %+v, received: %+v", res.ttl, mres[0].ttl) + if !reflect.DeepEqual(resourceTest[0].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[0].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) + } else if !reflect.DeepEqual(resourceTest[0].ttl, mres[0].ttl) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ttl, mres[0].ttl) } } //UsageTTL 5s in ResourceProfile and give 10s duration -func TestRSUsageTTLCase4(t *testing.T) { - resPrf := &ResourceProfile{ - Tenant: "cgrates.org", - ID: "resourcesprofile4", // identifier of this resource - FilterIDs: []string{"defaultf5"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - UsageTTL: time.Duration(5) * time.Second, // auto-expire the usage after this duration - Limit: 10.00, // limit value - AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation - Blocker: false, // blocker flag to stop processing on filters matched - Stored: false, - Weight: 20.00, // Weight to sort the resources - ThresholdIDs: []string{""}, // Thresholds to check after changing Limit - } - res := &Resource{ - Tenant: "cgrates.org", - ID: "resourcesprofile4", - Usages: map[string]*ResourceUsage{}, - TTLIdx: []string{}, - rPrf: resPrf, - ttl: &timeDurationExample, - } - // ev := &utils.CGREvent{ - // Tenant: "cgrates.org", - // ID: "event3", - // Event: map[string]interface{}{ - // "Weight": "200.0", - // utils.Usage: time.Duration(65 * time.Second), - // }} - if err := dmRES.SetResource(res); err != nil { +func TestResourceUsageTTLCase4(t *testing.T) { + resprf[0].UsageTTL = time.Duration(5) + resourceTest[0].rPrf = resprf[0] + resourceTest[0].ttl = &timeDurationExample + if err := dmRES.SetResourceProfile(resprf[0], true); err != nil { t.Error(err) } - if err := dmRES.SetResourceProfile(resPrf, false); err != nil { + if err := dmRES.SetResource(resourceTest[0]); err != nil { t.Error(err) } - // mres, err := resserv.matchingResourcesForEvent(ev, &timeDurationExample) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } - // if !reflect.DeepEqual(res.Tenant, mres[0].Tenant) { - // t.Errorf("Expecting: %+v, received: %+v", res.Tenant, mres[0].Tenant) - // } else if !reflect.DeepEqual(res.ID, mres[0].ID) { - // t.Errorf("Expecting: %+v, received: %+v", res.ID, mres[0].ID) - // } else if !reflect.DeepEqual(res.rPrf, mres[0].rPrf) { - // t.Errorf("Expecting: %+v, received: %+v", res.rPrf, mres[0].rPrf) - // } else if !reflect.DeepEqual(res.ttl, mres[0].ttl) { - // t.Errorf("Expecting: %+v, received: %+v", res.ttl, mres[0].ttl) - // } + mres, err := resService.matchingResourcesForEvent(resEvs[0], &timeDurationExample) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(resourceTest[0].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[0].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) + } else if !reflect.DeepEqual(resourceTest[0].ttl, mres[0].ttl) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ttl, mres[0].ttl) + } } diff --git a/engine/stats_test.go b/engine/stats_test.go index 1f6a858af..d06a800a2 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -29,13 +29,13 @@ import ( var ( cloneExpTimeStats time.Time expTimeStats = time.Now().Add(time.Duration(20 * time.Minute)) - stsserv StatService + statService *StatService dmSTS *DataManager sqps = []*StatQueueProfile{ &StatQueueProfile{ Tenant: "cgrates.org", - ID: "statsprofile1", - FilterIDs: []string{"filter7"}, + ID: "StatQueueProfile1", + FilterIDs: []string{"FLTR_STATS_1"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, @@ -55,8 +55,8 @@ var ( }, &StatQueueProfile{ Tenant: "cgrates.org", - ID: "statsprofile2", - FilterIDs: []string{"filter8"}, + ID: "StatQueueProfile2", + FilterIDs: []string{"FLTR_STATS_2"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, @@ -76,29 +76,8 @@ var ( }, &StatQueueProfile{ Tenant: "cgrates.org", - ID: "statsprofile3", - FilterIDs: []string{"preffilter4"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - }, - QueueLength: 10, - TTL: time.Duration(10) * time.Second, - Metrics: []*utils.MetricWithParams{ - &utils.MetricWithParams{ - MetricID: utils.MetaSum, - Parameters: utils.Usage, - }, - }, - ThresholdIDs: []string{}, - Blocker: true, - Stored: true, - Weight: 20, - MinItems: 1, - }, - &StatQueueProfile{ - Tenant: "cgrates.org", - ID: "statsprofile4", - FilterIDs: []string{"defaultf4"}, + ID: "StatQueueProfilePrefix", + FilterIDs: []string{"FLTR_STATS_3"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), }, @@ -118,159 +97,151 @@ var ( }, } stqs = []*StatQueue{ - &StatQueue{Tenant: "cgrates.org", ID: "statsprofile1", sqPrfl: sqps[0]}, - &StatQueue{Tenant: "cgrates.org", ID: "statsprofile2", sqPrfl: sqps[1]}, - &StatQueue{Tenant: "cgrates.org", ID: "statsprofile3", sqPrfl: sqps[2]}, - &StatQueue{Tenant: "cgrates.org", ID: "statsprofile4", sqPrfl: sqps[3]}, + &StatQueue{Tenant: "cgrates.org", ID: "StatQueueProfile1", sqPrfl: sqps[0]}, + &StatQueue{Tenant: "cgrates.org", ID: "StatQueueProfile2", sqPrfl: sqps[1]}, + &StatQueue{Tenant: "cgrates.org", ID: "StatQueueProfilePrefix", sqPrfl: sqps[2]}, } statsEvs = []*utils.CGREvent{ &utils.CGREvent{ Tenant: "cgrates.org", ID: "event1", Event: map[string]interface{}{ - "Stats": "StatsProfile1", + "Stats": "StatQueueProfile1", utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), "UsageInterval": "1s", "PddInterval": "1s", - "Weight": "20.0", + "Weight": "9.0", utils.Usage: time.Duration(135 * time.Second), utils.COST: 123.0, - }}, + }, + }, &utils.CGREvent{ Tenant: "cgrates.org", ID: "event2", Event: map[string]interface{}{ - "Stats": "StatsProfile2", + "Stats": "StatQueueProfile2", utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), "UsageInterval": "1s", "PddInterval": "1s", - "Weight": "21.0", + "Weight": "15.0", utils.Usage: time.Duration(45 * time.Second), - }}, + }, + }, &utils.CGREvent{ Tenant: "cgrates.org", ID: "event3", Event: map[string]interface{}{ - "Stats": "StatsProfilePrefix", + "Stats": "StatQueueProfilePrefix", utils.Usage: time.Duration(30 * time.Second), - }}, - &utils.CGREvent{ - Tenant: "cgrates.org", - ID: "event3", - Event: map[string]interface{}{ - "Weight": "200.0", - utils.Usage: time.Duration(65 * time.Second), - }}, + }, + }, } ) -func TestStatsPopulateStatsService(t *testing.T) { +func TestStatQueuesPopulateService(t *testing.T) { data, _ := NewMapStorage() dmSTS = NewDataManager(data) defaultCfg, err := config.NewDefaultCGRConfig() if err != nil { t.Errorf("Error: %+v", err) } - var filters1 []*FilterRule - var filters2 []*FilterRule - var preffilter []*FilterRule - var defaultf []*FilterRule - second := 1 * time.Second - stsserv = StatService{ - dm: dmSTS, - filterS: &FilterS{dm: dmSTS, cfg: defaultCfg}, - } - ref := NewFilterIndexer(dmSTS, utils.StatQueueProfilePrefix, "cgrates.org") - //filter1 - x, err := NewFilterRule(MetaString, "Stats", []string{"StatsProfile1"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"9.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - filter7 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter7", Rules: filters1} - dmSTS.SetFilter(filter7) - ref.IndexTPFilter(FilterToTPFilter(filter7), "statsprofile1") - //filter2 - x, err = NewFilterRule(MetaString, "Stats", []string{"StatsProfile2"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"15.0"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - filter8 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter8", Rules: filters2} - dmSTS.SetFilter(filter8) - ref.IndexTPFilter(FilterToTPFilter(filter8), "statsprofile2") - //prefix filter - x, err = NewFilterRule(MetaPrefix, "Stats", []string{"StatsProfilePrefix"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - preffilter4 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter4", Rules: preffilter} - dmSTS.SetFilter(preffilter4) - ref.IndexTPFilter(FilterToTPFilter(preffilter4), "statsprofile3") - //default filter - x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"200.00"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - defaultf = append(defaultf, x) - defaultf4 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf4", Rules: defaultf} - dmSTS.SetFilter(defaultf4) - ref.IndexTPFilter(FilterToTPFilter(defaultf4), "statsprofile4") - for _, stq := range stqs { - dmSTS.SetStatQueue(stq) - } - for _, sqp := range sqps { - dmSTS.SetStatQueueProfile(sqp, false) - } - err = ref.StoreIndexes(true, utils.NonTransactional) + + statService, err = NewStatService(dmSTS, time.Duration(1), + nil, &FilterS{dm: dmSTS, cfg: defaultCfg}, nil, nil) if err != nil { t.Errorf("Error: %+v", err) } } -func TestStatsmatchingStatQueuesForEvent(t *testing.T) { - msq, err := stsserv.matchingStatQueuesForEvent(statsEvs[0]) +func TestStatQueuesAddFilters(t *testing.T) { + fltrSts1 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_STATS_1", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Stats", + Values: []string{"StatQueueProfile1"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "UsageInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Usage, + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"9.0"}, + }, + }, + } + dmSTS.SetFilter(fltrSts1) + fltrSts2 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_STATS_2", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Stats", + Values: []string{"StatQueueProfile2"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "PddInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Usage, + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"15.0"}, + }, + }, + } + dmSTS.SetFilter(fltrSts2) + fltrSts3 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_STATS_3", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaPrefix, + FieldName: "Stats", + Values: []string{"StatQueueProfilePrefix"}, + }, + }, + } + dmSTS.SetFilter(fltrSts3) +} + +func TestStatQueuesPopulateStatsService(t *testing.T) { + for _, statQueueProfile := range sqps { + dmSTS.SetStatQueueProfile(statQueueProfile, true) + } + for _, statQueue := range stqs { + dmSTS.SetStatQueue(statQueue) + } + //Test each statQueueProfile from cache + for _, sqp := range sqps { + if tempStat, err := dmSTS.GetStatQueueProfile(sqp.Tenant, + sqp.ID, false, utils.NonTransactional); err != nil { + t.Errorf("Error: %+v", err) + } else if !reflect.DeepEqual(sqp, tempStat) { + t.Errorf("Expecting: %+v, received: %+v", sqp, tempStat) + } + } +} + +func TestStatQueuesMatchingStatQueuesForEvent(t *testing.T) { + msq, err := statService.matchingStatQueuesForEvent(statsEvs[0]) if err != nil { t.Errorf("Error: %+v", err) } @@ -281,7 +252,8 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[0].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[0].sqPrfl, msq[0].sqPrfl) } - msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[1]) + + msq, err = statService.matchingStatQueuesForEvent(statsEvs[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -292,7 +264,8 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[1].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[1].sqPrfl, msq[0].sqPrfl) } - msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[2]) + + msq, err = statService.matchingStatQueuesForEvent(statsEvs[2]) if err != nil { t.Errorf("Error: %+v", err) } @@ -303,64 +276,44 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[2].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[2].sqPrfl, msq[0].sqPrfl) } - // msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[3]) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } - // if !reflect.DeepEqual(stqs[3].Tenant, msq[0].Tenant) { - // t.Errorf("Expecting: %+v, received: %+v", stqs[3].Tenant, msq[0].Tenant) - // } else if !reflect.DeepEqual(stqs[3].ID, msq[0].ID) { - // t.Errorf("Expecting: %+v, received: %+v", stqs[3].ID, msq[0].ID) - // } else if !reflect.DeepEqual(stqs[3].sqPrfl, msq[0].sqPrfl) { - // t.Errorf("Expecting: %+v, received: %+v", stqs[3].sqPrfl, msq[0].sqPrfl) - // } } -func TestStatSprocessEvent(t *testing.T) { +func TestStatQueuesProcessEvent(t *testing.T) { stq := map[string]string{} reply := []string{} - expected := []string{"statsprofile1"} - err := stsserv.V1ProcessEvent(statsEvs[0], &reply) + expected := []string{"StatQueueProfile1"} + err := statService.V1ProcessEvent(statsEvs[0], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(reply, expected) { t.Errorf("Expecting: %+v, received: %+v", expected, reply) } - err = stsserv.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[0].Tenant, ID: stqs[0].ID}, &stq) + err = statService.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[0].Tenant, ID: stqs[0].ID}, &stq) if err != nil { t.Errorf("Error: %+v", err) } - expected = []string{"statsprofile2"} - err = stsserv.V1ProcessEvent(statsEvs[1], &reply) + + expected = []string{"StatQueueProfile2"} + err = statService.V1ProcessEvent(statsEvs[1], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(reply, expected) { t.Errorf("Expecting: %+v, received: %+v", expected, reply) } - err = stsserv.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[1].Tenant, ID: stqs[1].ID}, &stq) + err = statService.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[1].Tenant, ID: stqs[1].ID}, &stq) if err != nil { t.Errorf("Error: %+v", err) } - expected = []string{"statsprofile3"} - err = stsserv.V1ProcessEvent(statsEvs[2], &reply) + + expected = []string{"StatQueueProfilePrefix"} + err = statService.V1ProcessEvent(statsEvs[2], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(reply, expected) { t.Errorf("Expecting: %+v, received: %+v", expected, reply) } - err = stsserv.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[2].Tenant, ID: stqs[2].ID}, &stq) + err = statService.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[2].Tenant, ID: stqs[2].ID}, &stq) if err != nil { t.Errorf("Error: %+v", err) } - // expected = []string{"statsprofile4"} - // err = stsserv.V1ProcessEvent(statsEvs[3], &reply) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } else if !reflect.DeepEqual(reply, expected) { - // t.Errorf("Expecting: %+v, received: %+v", expected, reply) - // } - // err = stsserv.V1GetQueueStringMetrics(&utils.TenantID{Tenant: stqs[3].Tenant, ID: stqs[3].ID}, &stq) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } } diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index 6f2cc6cb2..859e017fd 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -29,13 +29,13 @@ import ( var ( cloneExpTimeSuppliers time.Time expTimeSuppliers = time.Now().Add(time.Duration(20 * time.Minute)) - splserv SupplierService + splService *SupplierService dmSPP *DataManager sppTest = SupplierProfiles{ &SupplierProfile{ Tenant: "cgrates.org", - ID: "supplierprofile1", - FilterIDs: []string{"filter3"}, + ID: "SupplierProfile1", + FilterIDs: []string{"FLTR_SUPP_1"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeSuppliers, @@ -45,7 +45,7 @@ var ( Suppliers: []*Supplier{ &Supplier{ ID: "supplier1", - FilterIDs: []string{"filter3"}, + FilterIDs: []string{"FLTR_SUPP_1"}, AccountIDs: []string{}, RatingPlanIDs: []string{}, ResourceIDs: []string{}, @@ -59,8 +59,8 @@ var ( }, &SupplierProfile{ Tenant: "cgrates.org", - ID: "supplierprofile2", - FilterIDs: []string{"filter4"}, + ID: "SupplierProfile2", + FilterIDs: []string{"FLTR_SUPP_2"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeSuppliers, @@ -70,7 +70,7 @@ var ( Suppliers: []*Supplier{ &Supplier{ ID: "supplier2", - FilterIDs: []string{"filter4"}, + FilterIDs: []string{"FLTR_SUPP_2"}, AccountIDs: []string{}, RatingPlanIDs: []string{}, ResourceIDs: []string{}, @@ -80,7 +80,7 @@ var ( }, &Supplier{ ID: "supplier3", - FilterIDs: []string{"filter4"}, + FilterIDs: []string{"FLTR_SUPP_2"}, AccountIDs: []string{}, RatingPlanIDs: []string{}, ResourceIDs: []string{}, @@ -90,7 +90,7 @@ var ( }, &Supplier{ ID: "supplier1", - FilterIDs: []string{"filter4"}, + FilterIDs: []string{"FLTR_SUPP_2"}, AccountIDs: []string{}, RatingPlanIDs: []string{}, ResourceIDs: []string{}, @@ -104,8 +104,8 @@ var ( }, &SupplierProfile{ Tenant: "cgrates.org", - ID: "supplierprofile3", - FilterIDs: []string{"preffilter2"}, + ID: "SupplierProfilePrefix", + FilterIDs: []string{"FLTR_SUPP_3"}, ActivationInterval: &utils.ActivationInterval{ ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), ExpiryTime: cloneExpTimeSuppliers, @@ -115,7 +115,7 @@ var ( Suppliers: []*Supplier{ &Supplier{ ID: "supplier1", - FilterIDs: []string{"preffilter2"}, + FilterIDs: []string{"FLTR_SUPP_3"}, AccountIDs: []string{}, RatingPlanIDs: []string{}, ResourceIDs: []string{}, @@ -127,93 +127,41 @@ var ( }, Weight: 10, }, - &SupplierProfile{ - Tenant: "cgrates.org", - ID: "supplierprofile4", - FilterIDs: []string{"defaultf2"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), - ExpiryTime: cloneExpTimeSuppliers, - }, - Sorting: utils.MetaWeight, - SortingParameters: []string{}, - Suppliers: []*Supplier{ - &Supplier{ - ID: "supplier2", - FilterIDs: []string{"defaultf2"}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 20.0, - SupplierParameters: "param2", - }, - &Supplier{ - ID: "supplier3", - FilterIDs: []string{"defaultf2"}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 10.0, - SupplierParameters: "param3", - }, - &Supplier{ - ID: "supplier1", - FilterIDs: []string{"defaultf2"}, - AccountIDs: []string{}, - RatingPlanIDs: []string{}, - ResourceIDs: []string{}, - StatIDs: []string{}, - Weight: 30.0, - Blocker: false, - SupplierParameters: "param1", + } + argsGetSuppliers = []*ArgsGetSuppliers{ + &ArgsGetSuppliers{ //matching SupplierProfile1 + 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", }, }, - Weight: 20.0, }, - } - 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", + &ArgsGetSuppliers{ //matching SupplierProfile2 + 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", + }, }, }, - } - 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", + &ArgsGetSuppliers{ //matching SupplierProfilePrefix + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Supplier": "SupplierProfilePrefix", + }, }, }, } @@ -330,21 +278,94 @@ func TestSuppliersSort(t *testing.T) { } } -func TestSuppliersCache(t *testing.T) { +func TestSuppliersPopulateSupplierService(t *testing.T) { //Need clone because time.Now adds extra information that DeepEqual doesn't like if err := utils.Clone(expTimeSuppliers, &cloneExpTimeSuppliers); err != nil { t.Error(err) } data, _ := NewMapStorage() dmSPP = NewDataManager(data) + defaultCfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Errorf("Error: %+v", err) + } + + splService, err = NewSupplierService(dmSPP, config.CgrConfig().DefaultTimezone, + &FilterS{dm: dmSPP, cfg: defaultCfg}, nil, nil, nil, nil) + if err != nil { + t.Errorf("Error: %+v", err) + } +} + +func TestSuppliersAddFilters(t *testing.T) { + fltrSupp1 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_SUPP_1", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Supplier", + Values: []string{"SupplierProfile1"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "UsageInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"9.0"}, + }, + }, + } + dmSPP.SetFilter(fltrSupp1) + fltrSupp2 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_SUPP_2", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaString, + FieldName: "Supplier", + Values: []string{"SupplierProfile2"}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: "PddInterval", + Values: []string{(1 * time.Second).String()}, + }, + &FilterRule{ + Type: MetaGreaterOrEqual, + FieldName: utils.Weight, + Values: []string{"15.0"}, + }, + }, + } + dmSPP.SetFilter(fltrSupp2) + fltrSupp3 := &Filter{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "FLTR_SUPP_3", + Rules: []*FilterRule{ + &FilterRule{ + Type: MetaPrefix, + FieldName: "Supplier", + Values: []string{"SupplierProfilePrefix"}, + }, + }, + } + dmSPP.SetFilter(fltrSupp3) +} + +func TestSuppliersCache(t *testing.T) { for _, spp := range sppTest { - if err = dmSPP.SetSupplierProfile(spp, false); err != nil { + if err = dmSPP.SetSupplierProfile(spp, true); 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 { + 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) @@ -352,106 +373,16 @@ func TestSuppliersCache(t *testing.T) { } } -func TestSuppliersPopulateSupplierService(t *testing.T) { - data, _ := NewMapStorage() - dmSPP = NewDataManager(data) - defaultCfg, err := config.NewDefaultCGRConfig() - if err != nil { - t.Errorf("Error: %+v", err) - } - var filters1 []*FilterRule - var filters2 []*FilterRule - var preffilter []*FilterRule - var defaultf []*FilterRule - second := 1 * time.Second - //refresh the DM - ref := NewFilterIndexer(dmSPP, utils.SupplierProfilePrefix, "cgrates.org") - - //filter1 - x, err := NewFilterRule(MetaString, "Supplier", []string{"SupplierProfile1"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters1 = append(filters1, x) - x, err = NewFilterRule(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", Rules: filters1} - dmSPP.SetFilter(filter3) - ref.IndexTPFilter(FilterToTPFilter(filter3), "supplierprofile1") - - //filter2 - x, err = NewFilterRule(MetaString, "Supplier", []string{"SupplierProfile2"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) - if err != nil { - t.Errorf("Error: %+v", err) - } - filters2 = append(filters2, x) - x, err = NewFilterRule(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", Rules: filters2} - dmSPP.SetFilter(filter4) - ref.IndexTPFilter(FilterToTPFilter(filter4), "supplierprofile2") - - //prefix filter - x, err = NewFilterRule(MetaPrefix, "Supplier", []string{"supplierprofilePrefix"}) - if err != nil { - t.Errorf("Error: %+v", err) - } - preffilter = append(preffilter, x) - preffilter2 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter2", Rules: preffilter} - dmSPP.SetFilter(preffilter2) - ref.IndexTPFilter(FilterToTPFilter(preffilter2), "supplierprofile3") - - //default filter - x, err = NewFilterRule(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", Rules: defaultf} - dmSPP.SetFilter(defaultf2) - ref.IndexTPFilter(FilterToTPFilter(defaultf2), "supplierprofile4") - splserv = SupplierService{ - dm: dmSPP, - filterS: &FilterS{dm: dmSPP, cfg: defaultCfg}, - sorter: map[string]SuppliersSorter{ - utils.MetaWeight: NewWeightSorter(), - utils.MetaLeastCost: NewLeastCostSorter(&splserv), - }, - } - for _, spr := range sppTest { - dmSPP.SetSupplierProfile(spr, false) - } - err = ref.StoreIndexes(true, utils.NonTransactional) - if err != nil { - t.Errorf("Error: %+v", err) - } -} - func TestSuppliersmatchingSupplierProfilesForEvent(t *testing.T) { - sprf, err := splserv.matchingSupplierProfilesForEvent(&argPagEv.CGREvent) + sprf, err := splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[0].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(sppTest[0], sprf[0]) { t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) } - sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv2.CGREvent) + + sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -459,25 +390,18 @@ func TestSuppliersmatchingSupplierProfilesForEvent(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) } - sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv3.CGREvent) + sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } if !reflect.DeepEqual(sppTest[2], sprf[0]) { t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0]) } - // sprf, err = splserv.matchingSupplierProfilesForEvent(&argPagEv4.CGREvent) - // if err != nil { - // t.Errorf("Error: %+v", err) - // } - // 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", + ProfileID: "SupplierProfile1", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -489,7 +413,7 @@ func TestSuppliersSortedForEvent(t *testing.T) { }, }, } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv) + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[0]) if err != nil { t.Errorf("Error: %+v", err) } @@ -497,7 +421,7 @@ func TestSuppliersSortedForEvent(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) } eFirstSupplierProfile = &SortedSuppliers{ - ProfileID: "supplierprofile2", + ProfileID: "SupplierProfile2", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -523,15 +447,17 @@ func TestSuppliersSortedForEvent(t *testing.T) { }, }, } - sprf, err = splserv.sortedSuppliersForEvent(argPagEv2) + + sprf, err = splService.sortedSuppliersForEvent(argsGetSuppliers[1]) 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", + ProfileID: "SupplierProfilePrefix", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -543,52 +469,19 @@ func TestSuppliersSortedForEvent(t *testing.T) { }, }, } - sprf, err = splserv.sortedSuppliersForEvent(argPagEv3) + + sprf, err = splService.sortedSuppliersForEvent(argsGetSuppliers[2]) 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) - // } - // if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { - // t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) - // } } func TestSuppliersSortedForEventWithLimit(t *testing.T) { eFirstSupplierProfile := &SortedSuppliers{ - ProfileID: "supplierprofile2", + ProfileID: "SupplierProfile2", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -607,10 +500,10 @@ func TestSuppliersSortedForEventWithLimit(t *testing.T) { }, }, } - argPagEv2.Paginator = utils.Paginator{ + argsGetSuppliers[1].Paginator = utils.Paginator{ Limit: utils.IntPointer(2), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -621,7 +514,7 @@ func TestSuppliersSortedForEventWithLimit(t *testing.T) { func TestSuppliersSortedForEventWithOffset(t *testing.T) { eFirstSupplierProfile := &SortedSuppliers{ - ProfileID: "supplierprofile2", + ProfileID: "SupplierProfile2", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -633,10 +526,10 @@ func TestSuppliersSortedForEventWithOffset(t *testing.T) { }, }, } - argPagEv2.Paginator = utils.Paginator{ + argsGetSuppliers[1].Paginator = utils.Paginator{ Offset: utils.IntPointer(2), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -647,7 +540,7 @@ func TestSuppliersSortedForEventWithOffset(t *testing.T) { func TestSuppliersSortedForEventWithLimitAndOffset(t *testing.T) { eFirstSupplierProfile := &SortedSuppliers{ - ProfileID: "supplierprofile2", + ProfileID: "SupplierProfile2", Sorting: utils.MetaWeight, SortedSuppliers: []*SortedSupplier{ &SortedSupplier{ @@ -659,11 +552,11 @@ func TestSuppliersSortedForEventWithLimitAndOffset(t *testing.T) { }, }, } - argPagEv2.Paginator = utils.Paginator{ + argsGetSuppliers[1].Paginator = utils.Paginator{ Limit: utils.IntPointer(1), Offset: utils.IntPointer(1), } - sprf, err := splserv.sortedSuppliersForEvent(argPagEv2) + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) if err != nil { t.Errorf("Error: %+v", err) } diff --git a/general_tests/tutorial_calls_test.go b/general_tests/tutorial_calls_test.go index 644d6d828..9f6735f19 100755 --- a/general_tests/tutorial_calls_test.go +++ b/general_tests/tutorial_calls_test.go @@ -303,7 +303,7 @@ func testCallCheckResourceBeforeAllocation(t *testing.T) { }} if err := tutorialCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) - } else if len(*rs) != 2 { + } else if len(*rs) != 1 { t.Errorf("Resources: %+v", utils.ToJSON(rs)) } for _, r := range *rs { @@ -423,7 +423,7 @@ func testCallCheckResourceAllocation(t *testing.T) { }} if err := tutorialCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) - } else if len(*rs) != 2 { + } else if len(*rs) != 1 { t.Errorf("Resources: %+v", utils.ToJSON(rs)) } for _, r := range *rs { @@ -532,7 +532,7 @@ func testCallCheckResourceRelease(t *testing.T) { }} if err := tutorialCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) - } else if len(*rs) != 2 { + } else if len(*rs) != 1 { t.Errorf("Resources: %+v", rs) } for _, r := range *rs { @@ -609,7 +609,7 @@ func testCallSyncSessions(t *testing.T) { } if err := tutorialCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) - } else if len(*rs) != 2 { + } else if len(*rs) != 1 { t.Errorf("Resources: %+v", utils.ToJSON(rs)) } for _, r := range *rs { @@ -683,7 +683,7 @@ func testCallSyncSessions(t *testing.T) { var rsAfter *engine.Resources if err := tutorialCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rsAfter); err != nil { t.Error(err) - } else if len(*rsAfter) != 2 { + } else if len(*rsAfter) != 1 { t.Errorf("Resources: %+v", rsAfter) } for _, r := range *rsAfter { diff --git a/migrator/tp_stats_it_test.go b/migrator/tp_stats_it_test.go index 81b07cb8d..fc4af0c7a 100644 --- a/migrator/tp_stats_it_test.go +++ b/migrator/tp_stats_it_test.go @@ -152,6 +152,7 @@ func testTpStatsITCheckData(t *testing.T) { if err != nil { t.Error("Error when getting TpStat ", err.Error()) } + tpStats[0].Metrics[0].MetricID = "*sum:Param1" //add parametrics to metricID to use multiple parameters for same metric if !reflect.DeepEqual(tpStats[0], result[0]) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(tpStats[0]), utils.ToJSON(result[0]))