From ee78db2617f03be1d6debe1b64cee4838dfbb3fd Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 28 Aug 2020 17:04:29 +0300 Subject: [PATCH] Added unit tests for composeArgsReload --- apier/v1/apier.go | 47 +++------------------------- apier/v1/libapier.go | 43 ++++++++++++++++++++++++- apier/v1/libapier_test.go | 66 +++++++++++++++++++++++++++++++++++++++ utils/consts.go | 20 ++++++------ 4 files changed, 122 insertions(+), 54 deletions(-) create mode 100644 apier/v1/libapier_test.go diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 2dc754291..17d180bb3 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -1338,51 +1338,12 @@ func (apierSv1 *APIerSv1) ReplayFailedPosts(args *ArgsReplyFailedPosts, reply *s return nil } -// CallCache caching the item based on cacheopt -// visible in APIerSv2 -func (apierSv1 *APIerSv1) CallCache(cacheopt *string, tnt, cacheID, itemID string, filters *[]string, contexts []string, opts map[string]interface{}) (err error) { - var reply, method string - var args interface{} - cacheOpt := apierSv1.Config.GeneralCfg().DefaultCaching - if cacheopt != nil && *cacheopt != utils.EmptyString { - cacheOpt = *cacheopt - } - switch cacheOpt { - case utils.META_NONE: - return - case utils.MetaReload: - method = utils.CacheSv1ReloadCache - if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { - return - } - case utils.MetaLoad: - method = utils.CacheSv1LoadCache - if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { - return - } - case utils.MetaRemove: - method = utils.CacheSv1RemoveItems - if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { - return - } - case utils.MetaClear: - method = utils.CacheSv1Clear - args = &utils.AttrCacheIDsWithOpts{ - TenantArg: utils.TenantArg{Tenant: tnt}, - CacheIDs: []string{cacheID, utils.CacheInstanceToCacheIndex[cacheID]}, - Opts: opts, - } - } - return apierSv1.ConnMgr.Call(apierSv1.Config.ApierCfg().CachesConns, nil, - method, args, &reply) -} - func (apierSv1 *APIerSv1) GetLoadIDs(args *string, reply *map[string]int64) (err error) { - if loadIDs, err := apierSv1.DataManager.GetItemLoadIDs(*args, false); err != nil { - return err - } else { - *reply = loadIDs + var loadIDs map[string]int64 + if loadIDs, err = apierSv1.DataManager.GetItemLoadIDs(*args, false); err != nil { + return } + *reply = loadIDs return } diff --git a/apier/v1/libapier.go b/apier/v1/libapier.go index 1bb5120ce..41d7681e1 100644 --- a/apier/v1/libapier.go +++ b/apier/v1/libapier.go @@ -25,6 +25,46 @@ import ( "github.com/cgrates/cgrates/utils" ) +// CallCache caching the item based on cacheopt +// visible in APIerSv2 +func (apierSv1 *APIerSv1) CallCache(cacheopt *string, tnt, cacheID, itemID string, + filters *[]string, contexts []string, opts map[string]interface{}) (err error) { + var reply, method string + var args interface{} + cacheOpt := apierSv1.Config.GeneralCfg().DefaultCaching + if cacheopt != nil && *cacheopt != utils.EmptyString { + cacheOpt = *cacheopt + } + switch cacheOpt { + case utils.META_NONE: + return + case utils.MetaReload: + method = utils.CacheSv1ReloadCache + if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { + return + } + case utils.MetaLoad: + method = utils.CacheSv1LoadCache + if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { + return + } + case utils.MetaRemove: + method = utils.CacheSv1RemoveItems + if args, err = apierSv1.composeArgsReload(tnt, cacheID, itemID, filters, contexts, opts); err != nil { + return + } + case utils.MetaClear: + method = utils.CacheSv1Clear + args = &utils.AttrCacheIDsWithOpts{ + TenantArg: utils.TenantArg{Tenant: tnt}, + CacheIDs: []string{cacheID, utils.CacheInstanceToCacheIndex[cacheID]}, + Opts: opts, + } + } + return apierSv1.ConnMgr.Call(apierSv1.Config.ApierCfg().CachesConns, nil, + method, args, &reply) +} + // composeArgsReload add the ItemID to AttrReloadCache // for a specific CacheID func (apierSv1 *APIerSv1) composeArgsReload(tnt, cacheID, itemID string, filterIDs *[]string, contexts []string, opts map[string]interface{}) (rpl utils.AttrReloadCacheWithOpts, err error) { @@ -39,7 +79,7 @@ func (apierSv1 *APIerSv1) composeArgsReload(tnt, cacheID, itemID string, filterI return } // popultate the indexes - idxCacheID := utils.CacheInstanceToCacheIndex[cacheID] + idxCacheID := utils.CacheInstanceToArg[utils.CacheInstanceToCacheIndex[cacheID]] if len(*filterIDs) == 0 { // in case we do not have any filters reload the *none filter indexes indxID := utils.ConcatenatedKey(utils.META_NONE, utils.META_ANY, utils.META_ANY) if cacheID != utils.CacheAttributeProfiles && @@ -51,6 +91,7 @@ func (apierSv1 *APIerSv1) composeArgsReload(tnt, cacheID, itemID string, filterI for i, ctx := range contexts { rpl.ArgsCache[idxCacheID][i] = utils.ConcatenatedKey(tnt, ctx, indxID) } + return } indxIDs := make([]string, 0, len(*filterIDs)) for _, id := range *filterIDs { diff --git a/apier/v1/libapier_test.go b/apier/v1/libapier_test.go new file mode 100644 index 000000000..e0144bc4d --- /dev/null +++ b/apier/v1/libapier_test.go @@ -0,0 +1,66 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package v1 + +import ( + "reflect" + "testing" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func TestComposeArgsReload(t *testing.T) { + apv1 := &APIerSv1{DataManager: &engine.DataManager{}} + expArgs := utils.AttrReloadCacheWithOpts{ + Opts: make(map[string]interface{}), + TenantArg: utils.TenantArg{Tenant: "cgrates.org"}, + ArgsCache: map[string][]string{ + utils.AttributeProfileIDs: {"cgrates.org:ATTR1"}, + }, + } + + if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheAttributeProfiles, + "cgrates.org:ATTR1", nil, nil, make(map[string]interface{})); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(expArgs, rply) { + t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply)) + } + + expArgs.ArgsCache[utils.AttributeFilterIndexIDs] = []string{"cgrates.org:*cdrs:*none:*any:*any"} + + if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheAttributeProfiles, + "cgrates.org:ATTR1", &[]string{}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(expArgs, rply) { + t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply)) + } + + expArgs.ArgsCache[utils.AttributeFilterIndexIDs] = []string{ + "cgrates.org:*cdrs:*string:*req.Account:1001", + "cgrates.org:*cdrs:*prefix:*req.Destination:1001", + } + + if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheAttributeProfiles, + "cgrates.org:ATTR1", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(expArgs, rply) { + t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply)) + } +} diff --git a/utils/consts.go b/utils/consts.go index b005a98b0..cce5571b1 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -112,16 +112,16 @@ var ( } CacheInstanceToCacheIndex = map[string]string{ - CacheThresholdFilterIndexes: CacheThresholdProfiles, - CacheResourceFilterIndexes: CacheResourceProfiles, - CacheStatFilterIndexes: CacheStatQueueProfiles, - CacheRouteFilterIndexes: CacheRouteProfiles, - CacheAttributeFilterIndexes: CacheAttributeProfiles, - CacheChargerFilterIndexes: CacheChargerProfiles, - CacheDispatcherFilterIndexes: CacheDispatcherProfiles, - CacheRateProfilesFilterIndexes: CacheRateProfiles, - // CacheRateFilterIndexes: CacheRates, - CacheReverseFilterIndexes: CacheFilters, + CacheThresholdProfiles: CacheThresholdFilterIndexes, + CacheResourceProfiles: CacheResourceFilterIndexes, + CacheStatQueueProfiles: CacheStatFilterIndexes, + CacheRouteProfiles: CacheRouteFilterIndexes, + CacheAttributeProfiles: CacheAttributeFilterIndexes, + CacheChargerProfiles: CacheChargerFilterIndexes, + CacheDispatcherProfiles: CacheDispatcherFilterIndexes, + CacheRateProfiles: CacheRateProfilesFilterIndexes, + CacheFilters: CacheReverseFilterIndexes, + // CacheRates: CacheRateFilterIndexes, } // NonMonetaryBalances are types of balances which are not handled as monetary