diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index e472a07cd..ec062a819 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -376,8 +376,8 @@ func TestSuppliersPopulateSupplierService(t *testing.T) { ref := NewReqFilterIndexer(dmspl, utils.SupplierProfilePrefix, "cgrates.org") ref.IndexTPFilter(FilterToTPFilter(filter3), "supplierprofile1") ref.IndexTPFilter(FilterToTPFilter(filter4), "supplierprofile2") - ref.IndexTPFilter(FilterToTPFilter(filter3), "supplierprofile3") - ref.IndexTPFilter(FilterToTPFilter(filter4), "supplierprofile4") + ref.IndexTPFilter(FilterToTPFilter(preffilter2), "supplierprofile3") + ref.IndexTPFilter(FilterToTPFilter(defaultf2), "supplierprofile4") err = ref.StoreIndexes() if err != nil { t.Errorf("Error: %+v", err) diff --git a/engine/thresholds_test.go b/engine/thresholds_test.go index 7414b019e..dfe2772d3 100644 --- a/engine/thresholds_test.go +++ b/engine/thresholds_test.go @@ -20,6 +20,15 @@ package engine import ( "reflect" "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +var ( + thServ ThresholdService + dmTH *DataManager ) func TestThresholdsSort(t *testing.T) { @@ -40,3 +49,168 @@ func TestThresholdsSort(t *testing.T) { t.Errorf("expecting: %+v, received: %+v", eInst, ts) } } + +func TestThresholdsPopulateThresholdService(t *testing.T) { + data, _ := NewMapStorage() + dmTH = NewDataManager(data) + var filters1 []*RequestFilter + var filters2 []*RequestFilter + var preffilter []*RequestFilter + var defaultf []*RequestFilter + second := 1 * time.Second + x, err := NewRequestFilter(MetaString, "Threshold", []string{"ThresholdProfile1"}) + 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, "Threshold", []string{"ThresholdProfile1"}) + 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, "Threshold", []string{"ThresholdProfilePrefix"}) + 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) + filter5 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter5", RequestFilters: filters1} + filter6 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter6", RequestFilters: filters2} + preffilter3 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter3", RequestFilters: preffilter} + defaultf3 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf3", RequestFilters: defaultf} + dmTH.SetFilter(filter5) + dmTH.SetFilter(filter6) + dmTH.SetFilter(preffilter3) + dmTH.SetFilter(defaultf3) + thServ = ThresholdService{ + dm: dmTH, + filterS: &FilterS{dm: dmspl}, + stringIndexedFields: nil, + prefixIndexedFields: nil, + } + tPrfl := []*ThresholdProfile{ + &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TEST_PROFILE1", + FilterIDs: []string{"filter5"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: false, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"ACT_1", "ACT_2"}, + Async: true, + }, + &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TEST_PROFILE2", + FilterIDs: []string{"filter6"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: false, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"ACT_1", "ACT_2"}, + Async: true, + }, + &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TEST_PROFILE3", + FilterIDs: []string{"preffilter3"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: false, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"ACT_1", "ACT_2"}, + Async: true, + }, + &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TEST_PROFILE4", + FilterIDs: []string{"defaultf3"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: false, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"ACT_1", "ACT_2"}, + Async: true, + }, + } + for _, profile := range tPrfl { + if err = dmTH.SetThresholdProfile(profile, true); err != nil { + t.Errorf("Error: %+v", err) + } + if err = dmTH.SetThreshold(&Threshold{Tenant: profile.Tenant, ID: profile.ID}); err != nil { + t.Errorf("Error: %+v", err) + } + } + ref := NewReqFilterIndexer(dmTH, utils.ThresholdProfilePrefix, "cgrates.org") + ref.IndexTPFilter(FilterToTPFilter(filter5), "TEST_PROFILE1") + ref.IndexTPFilter(FilterToTPFilter(filter6), "TEST_PROFILE2") + ref.IndexTPFilter(FilterToTPFilter(preffilter3), "TEST_PROFILE3") + ref.IndexTPFilter(FilterToTPFilter(defaultf3), "TEST_PROFILE4") + err = ref.StoreIndexes() + if err != nil { + t.Errorf("Error: %+v", err) + } +} + +// func TestThresholdsmatchingThresholdsForEvent(t *testing.T) { +// stringEv := utils.CGREvent{ +// Tenant: "cgrates.org", +// ID: "utils.CGREvent1", +// Event: map[string]interface{}{ +// "Threshold": "ThresholdProfile1", +// utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), +// "UsageInterval": "1s", +// "PddInterval": "1s", +// "Weight": "20.0", +// }, +// } +// argEv := &ArgsProcessEvent{CGREvent: stringEv} +// sprf, err := thServ.matchingThresholdsForEvent(argEv) +// if err != nil { +// t.Errorf("Error: %+v", err) +// } +// if !reflect.DeepEqual(sprsmatch, sprf) { +// t.Errorf("Expecting: %+v, received: %+v", sprsmatch, sprf) +// }//should not pass atm but still return something other than empty string +// } + +// func TestThresholdsprocessEvent(t *testing.T) { + +// }