Updated GetRoutes/GetRouteProfileEvent methods and tests with default tenant value

This commit is contained in:
porosnicuadrian
2020-10-14 10:20:51 +03:00
committed by Dan Christian Bogos
parent 21120eb9b6
commit b0e9c1aa26
2 changed files with 43 additions and 24 deletions

View File

@@ -194,6 +194,15 @@ func testV1RouteGetWeightRoutes(t *testing.T) {
t.Errorf("Expecting: %s, received: %s",
utils.ToJSON(eSpls), utils.ToJSON(suplsReply))
}
ev.CGREvent.Tenant = utils.EmptyString
if err := splSv1Rpc.Call(utils.RouteSv1GetRoutes,
ev, &suplsReply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eSpls, suplsReply) {
t.Errorf("Expecting: %s, received: %s",
utils.ToJSON(eSpls), utils.ToJSON(suplsReply))
}
}
func testV1RouteGetLeastCostRoutes(t *testing.T) {
@@ -1044,7 +1053,7 @@ func testV1RouteGetRouteForEvent(t *testing.T) {
utils.Account: "1000",
utils.Destination: "1001",
utils.SetupTime: "*now",
"Subject": "TEST",
utils.Subject: "TEST",
},
},
}
@@ -1100,19 +1109,21 @@ func testV1RouteGetRouteForEvent(t *testing.T) {
if !reflect.DeepEqual(expected, *supProf[0]) {
t.Errorf("Expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(supProf))
}
/*
ev.Tenant = utils.EmptyString
ev.ID = "randomID"
expected.ID = "randomID"
if err := splSv1Rpc.Call(utils.RouteSv1GetRouteProfilesForEvent,
ev, &supProf); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, *supProf[0]) {
t.Errorf("Expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(supProf))
}
*/
ev.CGREvent.Tenant = utils.EmptyString
if err := splSv1Rpc.Call(utils.RouteSv1GetRouteProfilesForEvent,
ev, &supProf); err != nil {
t.Fatal(err)
}
sort.Slice(expected.Routes, func(i, j int) bool {
return expected.Routes[i].Weight < expected.Routes[j].Weight
})
sort.Slice(supProf[0].Routes, func(i, j int) bool {
return supProf[0].Routes[i].Weight < supProf[0].Routes[j].Weight
})
if !reflect.DeepEqual(&expected, supProf[0]) {
t.Errorf("Expected: %s \n,received: %s", utils.ToJSON(expected), utils.ToJSON(supProf[0]))
}
}
// Scenario: We create two rating plans RP_MOBILE and RP_LOCAL

View File

@@ -153,7 +153,7 @@ func (rpS *RouteService) Shutdown() error {
}
// matchingRouteProfilesForEvent returns ordered list of matching resources which are active by the time of the call
func (rpS *RouteService) matchingRouteProfilesForEvent(ev *utils.CGREventWithOpts, singleResult bool) (matchingRPrf []*RouteProfile, err error) {
func (rpS *RouteService) matchingRouteProfilesForEvent(tnt string, ev *utils.CGREventWithOpts, singleResult bool) (matchingRPrf []*RouteProfile, err error) {
evNm := utils.MapStorage{
utils.MetaReq: ev.Event,
utils.MetaOpts: ev.Opts,
@@ -162,7 +162,7 @@ func (rpS *RouteService) matchingRouteProfilesForEvent(ev *utils.CGREventWithOpt
rpS.cgrcfg.RouteSCfg().StringIndexedFields,
rpS.cgrcfg.RouteSCfg().PrefixIndexedFields,
rpS.cgrcfg.RouteSCfg().SuffixIndexedFields,
rpS.dm, utils.CacheRouteFilterIndexes, ev.Tenant,
rpS.dm, utils.CacheRouteFilterIndexes, tnt,
rpS.cgrcfg.RouteSCfg().IndexedSelects,
rpS.cgrcfg.RouteSCfg().NestedFields,
)
@@ -175,7 +175,7 @@ func (rpS *RouteService) matchingRouteProfilesForEvent(ev *utils.CGREventWithOpt
matchingRPrf = make([]*RouteProfile, 0, len(rPrfIDs))
}
for lpID := range rPrfIDs {
rPrf, err := rpS.dm.GetRouteProfile(ev.Tenant, lpID, true, true, utils.NonTransactional)
rPrf, err := rpS.dm.GetRouteProfile(tnt, lpID, true, true, utils.NonTransactional)
if err != nil {
if err == utils.ErrNotFound {
continue
@@ -186,7 +186,7 @@ func (rpS *RouteService) matchingRouteProfilesForEvent(ev *utils.CGREventWithOpt
!rPrf.ActivationInterval.IsActiveAtTime(*ev.Time) { // not active
continue
}
if pass, err := rpS.filterS.Pass(ev.Tenant, rPrf.FilterIDs,
if pass, err := rpS.filterS.Pass(tnt, rPrf.FilterIDs,
evNm); err != nil {
return nil, err
} else if !pass {
@@ -519,12 +519,12 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route,
// sortedRoutesForEvent will return the list of valid route IDs
// for event based on filters and sorting algorithms
func (rpS *RouteService) sortedRoutesForEvent(args *ArgsGetRoutes) (sortedRoutes *SortedRoutes, err error) {
func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) (sortedRoutes *SortedRoutes, err error) {
if _, has := args.CGREvent.Event[utils.Usage]; !has {
args.CGREvent.Event[utils.Usage] = time.Duration(time.Minute) // make sure we have default set for Usage
}
var rPrfs []*RouteProfile
if rPrfs, err = rpS.matchingRouteProfilesForEvent(args.CGREventWithOpts, true); err != nil {
if rPrfs, err = rpS.matchingRouteProfilesForEvent(tnt, args.CGREventWithOpts, true); err != nil {
return
}
rPrfl := rPrfs[0]
@@ -541,7 +541,7 @@ func (rpS *RouteService) sortedRoutesForEvent(args *ArgsGetRoutes) (sortedRoutes
// apply filters for event
for _, route := range rPrfl.Routes {
pass, lazyCheckRules, err := rpS.filterS.LazyPass(args.CGREvent.Tenant, route.FilterIDs,
pass, lazyCheckRules, err := rpS.filterS.LazyPass(tnt, route.FilterIDs,
nM, []string{utils.DynamicDataPrefix + utils.MetaReq, utils.DynamicDataPrefix + utils.MetaAccounts,
utils.DynamicDataPrefix + utils.MetaResources, utils.DynamicDataPrefix + utils.MetaStats})
if err != nil {
@@ -642,11 +642,15 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutes) (
if args.CGREvent == nil {
return utils.NewErrMandatoryIeMissing(utils.CGREventString)
}
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID}); len(missing) != 0 {
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.ID}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.CGREvent.Event == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rpS.cgrcfg.GeneralCfg().DefaultTenant
}
if len(rpS.cgrcfg.RouteSCfg().AttributeSConns) != 0 {
if args.Opts == nil {
args.Opts = make(map[string]interface{})
@@ -674,7 +678,7 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutes) (
return utils.NewErrAttributeS(err)
}
}
sSps, err := rpS.sortedRoutesForEvent(args)
sSps, err := rpS.sortedRoutesForEvent(tnt, args)
if err != nil {
if err != utils.ErrNotFound {
err = utils.NewErrServerError(err)
@@ -687,12 +691,16 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutes) (
// V1GetRouteProfilesForEvent returns the list of valid route profiles
func (rpS *RouteService) V1GetRouteProfilesForEvent(args *utils.CGREventWithOpts, reply *[]*RouteProfile) (err error) {
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.Tenant, utils.ID}); len(missing) != 0 {
if missing := utils.MissingStructFields(args.CGREvent, []string{utils.ID}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.CGREvent.Event == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
sPs, err := rpS.matchingRouteProfilesForEvent(args, false)
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rpS.cgrcfg.GeneralCfg().DefaultTenant
}
sPs, err := rpS.matchingRouteProfilesForEvent(tnt, args, false)
if err != nil {
if err != utils.ErrNotFound {
err = utils.NewErrServerError(err)