diff --git a/engine/balances_test.go b/engine/balances_test.go index 68cba929f..d399dd791 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -378,3 +378,63 @@ func TestBalanceAsInterface(t *testing.T) { } } + +func TestValueFactorFieldAsInterface(t *testing.T) { + v := &ValueFactor{ + "FACT_VAL": 20.22, + } + if _, err := v.FieldAsInterface([]string{}); err == nil || err != utils.ErrNotFound { + t.Error(err) + } else if _, err = v.FieldAsInterface([]string{"TEST"}); err == nil || err != utils.ErrNotFound { + t.Error(err) + } else if _, err = v.FieldAsInterface([]string{"FACT_VAL"}); err != nil { + t.Error(err) + } +} +func TestValueFactorFieldAsString(t *testing.T) { + v := &ValueFactor{ + "FACT_VAL": 20.22, + } + if _, err = v.FieldAsString([]string{"TEST"}); err == nil { + t.Error(err) + } else if _, err = v.FieldAsString([]string{"FACT_VAL"}); err != nil { + t.Error(err) + } +} + +func TestBalancesHasBalance(t *testing.T) { + bc := Balances{ + { + Uuid: "uuid", + ID: "id", + Value: 12.22, + ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: true, + precision: 2, + }, + { + Uuid: "uuid2", + ID: "id2", + Value: 133.22, + ExpirationDate: time.Date(2023, 3, 21, 5, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: true, + precision: 2, + }, + } + balance := &Balance{ + Uuid: "uuid", + ID: "id", + Value: 12.22, + ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: true, + precision: 2, + } + + if !bc.HasBalance(balance) { + t.Error("should be true") + } + +} diff --git a/engine/filters.go b/engine/filters.go index 6c075fd1d..253b18d62 100644 --- a/engine/filters.go +++ b/engine/filters.go @@ -81,7 +81,7 @@ func (fS *FilterS) Pass(tenant string, filterIDs []string, return } -//checkPrefix verify if the value has as prefix one of the prefixes +// checkPrefix verify if the value has as prefix one of the prefixes func checkPrefix(value string, prefixes []string) (hasPrefix bool) { for _, prefix := range prefixes { if strings.HasPrefix(value, prefix) { @@ -95,7 +95,7 @@ func checkPrefix(value string, prefixes []string) (hasPrefix bool) { return } -//verifyPrefixes verify the Element and the Values from FilterRule if has as prefix one of the prefixes +// verifyPrefixes verify the Element and the Values from FilterRule if has as prefix one of the prefixes func verifyPrefixes(rule *FilterRule, prefixes []string) (hasPrefix bool) { if strings.HasPrefix(rule.Element, utils.DynamicDataPrefix) { if hasPrefix = checkPrefix(rule.Element, prefixes); !hasPrefix { @@ -113,8 +113,8 @@ func verifyPrefixes(rule *FilterRule, prefixes []string) (hasPrefix bool) { return true } -//LazyPass is almost the same as Pass except that it verify if the -//Element of the Values from FilterRules has as prefix one of the pathPrfxs +// LazyPass is almost the same as Pass except that it verify if the +// Element of the Values from FilterRules has as prefix one of the pathPrfxs func (fS *FilterS) LazyPass(tenant string, filterIDs []string, ev utils.DataProvider, pathPrfxs []string) (pass bool, lazyCheckRules []*FilterRule, err error) { if len(filterIDs) == 0 { diff --git a/engine/filters_test.go b/engine/filters_test.go index 5553b205f..fd1f292f0 100644 --- a/engine/filters_test.go +++ b/engine/filters_test.go @@ -840,6 +840,13 @@ func TestFilterNewRequestFilter(t *testing.T) { if !reflect.DeepEqual(erf, rf) { t.Errorf("Expecting: %+v, received: %+v", erf, rf) } + if _, err = NewFilterRule("", "~MetaRegex", []string{"Regex"}); err == nil { + t.Error(err) + } else if _, err = NewFilterRule(utils.MetaRegex, "", []string{"Regex"}); err == nil { + t.Error(err) + } else if _, err = NewFilterRule(utils.MetaRegex, "~MetaRegex", []string{}); err == nil { + t.Error(err) + } } func TestInlineFilterPassFiltersForEvent(t *testing.T) { diff --git a/engine/libroutes_test.go b/engine/libroutes_test.go index 2975c565c..74fb3f4f8 100644 --- a/engine/libroutes_test.go +++ b/engine/libroutes_test.go @@ -1473,3 +1473,201 @@ func TestSortedRoutesListRoutesWithParams(t *testing.T) { } } + +func TestSortedRoutesListAsNavigableMap(t *testing.T) { + sRs := SortedRoutesList{ + &SortedRoutes{ + ProfileID: "TEST_ID1", + Sorting: utils.MetaWeight, + Routes: []*SortedRoute{ + { + RouteID: "ROUTE1", + RouteParameters: "SORTING_PARAMETER", + sortingDataF64: map[string]float64{ + utils.Ratio: 6.0, + utils.Load: 10.0, + utils.Weight: 15.5, + }, + SortingData: map[string]interface{}{ + utils.Ratio: 6.0, + utils.Load: 10.0, + utils.Weight: 15.5, + }, + }, + { + RouteID: "ROUTE2", + RouteParameters: "SORTING_PARAMETER_SECOND", + sortingDataF64: map[string]float64{ + utils.Ratio: 7.0, + utils.Load: 10.0, + }, + SortingData: map[string]interface{}{ + utils.Ratio: 7.0, + utils.Load: 10.0, + }, + }, + }, + }, + &SortedRoutes{ + ProfileID: "TEST_ID2", + Sorting: utils.MetaWeight, + Routes: []*SortedRoute{ + { + RouteID: "ROUTE1", + RouteParameters: "SORTING_PARAMETER", + sortingDataF64: map[string]float64{ + utils.Ratio: 6.0, + utils.Load: 10.0, + utils.Weight: 15.5, + }, + SortingData: map[string]interface{}{ + utils.Ratio: 6.0, + utils.Load: 10.0, + utils.Weight: 15.5, + }, + }, + { + RouteID: "ROUTE2", + RouteParameters: "SORTING_PARAMETER_SECOND", + sortingDataF64: map[string]float64{ + utils.Ratio: 7.0, + utils.Load: 10.0, + }, + SortingData: map[string]interface{}{ + utils.Ratio: 7.0, + utils.Load: 10.0, + }, + }, + }, + }, + } + expNavMap := &utils.DataNode{ + Type: utils.NMSliceType, + Slice: []*utils.DataNode{ + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.ProfileID: utils.NewLeafNode("TEST_ID1"), + utils.Sorting: utils.NewLeafNode(utils.MetaWeight), + utils.CapRoutes: { + Type: utils.NMSliceType, + Slice: []*utils.DataNode{ + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.RouteID: utils.NewLeafNode("ROUTE1"), + utils.RouteParameters: utils.NewLeafNode("SORTING_PARAMETER"), + utils.SortingData: { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.Ratio: utils.NewLeafNode(6.0), + utils.Load: utils.NewLeafNode(10.0), + utils.Weight: utils.NewLeafNode(15.5), + }, + }, + }, + }, + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.RouteID: utils.NewLeafNode("ROUTE2"), + utils.RouteParameters: utils.NewLeafNode("SORTING_PARAMETER_SECOND"), + utils.SortingData: { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.Ratio: utils.NewLeafNode(7.0), + utils.Load: utils.NewLeafNode(10.0), + }, + }, + }, + }, + }, + }, + }, + }, + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.ProfileID: utils.NewLeafNode("TEST_ID2"), + utils.Sorting: utils.NewLeafNode(utils.MetaWeight), + utils.CapRoutes: { + Type: utils.NMSliceType, + Slice: []*utils.DataNode{ + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.RouteID: utils.NewLeafNode("ROUTE1"), + utils.RouteParameters: utils.NewLeafNode("SORTING_PARAMETER"), + utils.SortingData: { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.Ratio: utils.NewLeafNode(6.0), + utils.Load: utils.NewLeafNode(10.0), + utils.Weight: utils.NewLeafNode(15.5), + }, + }, + }, + }, + { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.RouteID: utils.NewLeafNode("ROUTE2"), + utils.RouteParameters: utils.NewLeafNode("SORTING_PARAMETER_SECOND"), + utils.SortingData: { + Type: utils.NMMapType, + Map: map[string]*utils.DataNode{ + utils.Ratio: utils.NewLeafNode(7.0), + utils.Load: utils.NewLeafNode(10.0), + }, + }, + }, + }, + }, + }, + }, + }, + }, + } + if val := sRs.AsNavigableMap(); !reflect.DeepEqual(val, expNavMap) { + t.Errorf("expected %v ,received %v", utils.ToJSON(expNavMap), utils.ToJSON(val)) + } + +} + +func TestSortedRoutesListDigest(t *testing.T) { + + sRs := &SortedRoutesList{ + { + ProfileID: "TEST_ID1", + Routes: []*SortedRoute{ + { + RouteID: "ROUTE_ID1", + RouteParameters: "PARAM_1", + }, + { + RouteID: "ROUTE_ID2", + RouteParameters: "PARAM_2", + }, + }, + }, + { + ProfileID: "TEST_ID2", + Routes: []*SortedRoute{ + { + RouteID: "ROUTE_ID1", + RouteParameters: "PARAM_1", + }, + { + RouteID: "ROUTE_ID2", + RouteParameters: "PARAM_2", + }, + }, + }, + } + + exp := "ROUTE_ID1:PARAM_1,ROUTE_ID2:PARAM_2,ROUTE_ID1:PARAM_1,ROUTE_ID2:PARAM_2" + + if val := sRs.Digest(); val != exp { + t.Errorf("received %v", val) + } +}