From 0f6ef42c2d44fa23a4411bb28c72c48a09546d2f Mon Sep 17 00:00:00 2001 From: gezimbll Date: Wed, 8 Mar 2023 10:58:40 -0500 Subject: [PATCH] improving coverage test at engine --- engine/datamanager_test.go | 171 +++++++++++++++++++++++++++++++++ engine/filters_test.go | 183 ++++++++++++++++++++++++++++++++++++ engine/libsuppliers_test.go | 68 +++++++++++++- engine/suppliers_test.go | 46 +++++++++ 4 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 engine/datamanager_test.go diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go new file mode 100644 index 000000000..ec818cf50 --- /dev/null +++ b/engine/datamanager_test.go @@ -0,0 +1,171 @@ +/* +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 engine + +import ( + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" +) + +func TestDmSetSupplierProfileRpl(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + defer func() { + cfg2, _ := config.NewDefaultCGRConfig() + config.SetCgrConfig(cfg2) + }() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + cfg.DataDbCfg().Items[utils.MetaSupplierProfiles].Replicate = true + cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1SetSupplierProfile: func(args, reply interface{}) error { + *reply.(*string) = utils.OK + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + fltrSupp1 := &Filter{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "FLTR_SUPP_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Supplier", + Values: []string{"SupplierProfile2"}, + }, + { + Type: utils.MetaGreaterOrEqual, + Element: "~*req.PddInterval", + Values: []string{(1 * time.Second).String()}, + }, + { + Type: utils.MetaGreaterOrEqual, + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight, + Values: []string{"15.0"}, + }, + }, + } + if err := dm.SetFilter(fltrSupp1); err != nil { + t.Error(err) + } + fltrSupp2 := &Filter{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "FLTR_SUPP_2", + Rules: []*FilterRule{ + { + Type: utils.MetaPrefix, + Element: "~*req.Supplier", + Values: []string{"SupplierProfilePrefix"}, + }, + }, + } + if err := dm.SetFilter(fltrSupp2); err != nil { + t.Error(err) + } + supp := &SupplierProfile{ + Tenant: "cgrates.org", + ID: "SUP1", + FilterIDs: []string{"FLTR_SUPP_1"}, + Weight: 10, + Sorting: utils.MetaQOS, + SortingParameters: []string{}, + Suppliers: []*Supplier{ + { + ID: "Sup", + FilterIDs: []string{}, + AccountIDs: []string{"1001"}, + RatingPlanIDs: []string{"RT_PLAN1"}, + ResourceIDs: []string{"RES1"}, + Weight: 10, + }, + }, + } + config.SetCgrConfig(cfg) + if err := dm.SetSupplierProfile(supp, true); err != nil { + t.Error(err) + } + supp1 := &SupplierProfile{ + Tenant: "cgrates.org", + ID: "SUP1", + FilterIDs: []string{"FLTR_SUPP_2"}, + Weight: 10, + Sorting: utils.MetaQOS, + SortingParameters: []string{}, + Suppliers: []*Supplier{ + { + ID: "Sup", + FilterIDs: []string{}, + AccountIDs: []string{"1001"}, + RatingPlanIDs: []string{"RT_PLAN1"}, + ResourceIDs: []string{"RES1"}, + Weight: 10, + }, + }, + } + if err := dm.SetSupplierProfile(supp1, true); err != nil { + t.Error(err) + } + if err := dm.RemoveSupplierProfile("cgrates.org", supp1.ID, utils.NonTransactional, true); err != nil { + t.Error(err) + } +} + +func TestDmMatchFilterIndexFromKey(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + fltr := &Filter{ + Tenant: "cgrates.org", + ID: "RES_FLT_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Account, + Values: []string{"1002"}, + }, + }, + } + if err := dm.SetFilter(fltr); err != nil { + t.Error(err) + } + rp := &ResourceProfile{ + Tenant: "cgrates.org", + ID: "RES1", + FilterIDs: []string{"RES_FLT_1"}, + UsageTTL: time.Second, + Limit: 1, + Weight: 10, + ThresholdIDs: []string{"TH1"}, + } + if err := dm.SetResourceProfile(rp, true); err != nil { + t.Error(err) + } + if err := dm.MatchFilterIndexFromKey(utils.CacheResourceFilterIndexes, "cgrates.org:*string:Account:1002"); err == nil { + t.Error(err) + } + //unifinished +} diff --git a/engine/filters_test.go b/engine/filters_test.go index ba5308f7f..25ac7b7e0 100644 --- a/engine/filters_test.go +++ b/engine/filters_test.go @@ -1504,6 +1504,9 @@ func TestRemoveItemFromIndexRP(t *testing.T) { if err := fltInd.RemoveItemFromIndex("cgrates.org", rs.ID, []string{}); err != nil { t.Error(err) } + if err := dm.RemoveResourceProfile("cgrates.org", rs.ID, utils.NonTransactional, true); err != nil { + t.Error(err) + } } func TestRemoveItemFromIndexCHP(t *testing.T) { @@ -1876,3 +1879,183 @@ func TestRemoveItemFromIndexDP(t *testing.T) { t.Error(err) } } + +func TestUpdateFilterIndexes(t *testing.T) { + + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + oldFlt := &Filter{ + Tenant: "cgrates.org", + ID: "FLTR_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Account, + Values: []string{"1001", "1002"}, + }, + { + Type: "*prefix", + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Destination, + Values: []string{"10", "20"}, + }, + { + Type: "*rsr", + Element: "", + Values: []string{"~*req.Subject(~^1.*1$)", "~*req.Destination(1002)"}, + }, + }, + } + if err := dm.SetFilter(oldFlt); err != nil { + t.Error(err) + } + chg := &ChargerProfile{ + Tenant: "cgrates.org", + ID: "CPP_3", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + RunID: "*rated", + AttributeIDs: []string{"ATTR_1"}, + Weight: 20, + } + + if err := dm.SetChargerProfile(chg, true); err != nil { + t.Error(err) + } + thP := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "THD_ACNT_1001", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 29, 15, 0, 0, 0, time.UTC), + }, + MaxHits: 1, + MinHits: 1, + MinSleep: time.Duration(1 * time.Second), + Weight: 10.0, + ActionIDs: []string{"ACT_LOG_WARNING"}, + Async: true, + } + if err := dm.SetThresholdProfile(thP, true); err != nil { + t.Error(err) + } + rcf := &ResourceProfile{ + Tenant: "cgrates.org", + ID: "RCFG1", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(10) * time.Microsecond, + Limit: 10, + AllocationMessage: "MessageAllocation", + Blocker: true, + Stored: true, + Weight: 20, + } + if err := dm.SetResourceProfile(rcf, true); err != nil { + t.Error(err) + } + supp := &SupplierProfile{ + Tenant: "cgrates.org", + ID: "SPL_DESTINATION", + FilterIDs: []string{"FLTR_1"}, + Sorting: utils.MetaLC, + Suppliers: []*Supplier{ + { + ID: "local", + RatingPlanIDs: []string{"RP_LOCAL"}, + Weight: 10, + }, + { + ID: "mobile", + RatingPlanIDs: []string{"RP_MOBILE"}, + FilterIDs: []string{"*destinations:~*req.Destination:DST_MOBILE"}, + Weight: 10, + }, + }, + Weight: 100, + } + if err := dm.SetSupplierProfile(supp, true); err != nil { + t.Error(err) + } + stat := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "TEST_PROFILE1", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + QueueLength: 10, + TTL: time.Duration(10) * time.Second, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaACD, + }, + }, + ThresholdIDs: []string{"THD_ACNT_1001"}, + Blocker: true, + Stored: true, + Weight: 20, + MinItems: 1, + } + if err := dm.SetStatQueueProfile(stat, true); err != nil { + t.Error(err) + } + attr := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ApierTest", + Contexts: []string{utils.META_ANY}, + FilterIDs: []string{"FLTR_1"}, + Attributes: []*Attribute{ + { + Path: utils.MetaReq + utils.NestingSep + utils.Subject, + Value: config.NewRSRParsersMustCompile("1011", true, utils.INFIELD_SEP), + }, + }, + Weight: 20, + } + if err := dm.SetAttributeProfile(attr, true); err != nil { + t.Error(err) + } + dpp := &DispatcherProfile{ + Tenant: "cgrates.org", + ID: "DSP_Test1", + FilterIDs: []string{"FLTR_1"}, + Strategy: utils.MetaFirst, + Subsystems: []string{utils.MetaAttributes, utils.MetaSessionS}, + Weight: 20, + } + if err := dm.SetDispatcherProfile(dpp, true); err != nil { + t.Error(err) + } + newFlt := &Filter{ + Tenant: "cgrates.org", + ID: "FLTR_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Account, + Values: []string{"1001"}, + }, + { + Type: utils.MetaString, + Element: "~*req.Subject", + Values: []string{"1001"}, + }, + { + Type: utils.MetaRSR, + Element: utils.EmptyString, + Values: []string{"~*req.Tenant(~^cgr.*\\.org$)"}, + }, + }, + } + if err := UpdateFilterIndexes(dm, "cgrates.org", oldFlt, newFlt); err != nil { + t.Error(err) + } + +} diff --git a/engine/libsuppliers_test.go b/engine/libsuppliers_test.go index 3852d3388..9e3096223 100644 --- a/engine/libsuppliers_test.go +++ b/engine/libsuppliers_test.go @@ -779,5 +779,71 @@ func TestSortSuppliersSortResourceAscendent(t *testing.T) { if _, err := hCostSort.SortSuppliers("HCS_SUPP", suppls, suplEv, spl, nil); err != nil { t.Error(err) } - +} + +func TestPopulateSortingDataStatMetrics(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + cfg.SupplierSCfg().StatSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStatS)} + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.StatSv1GetQueueFloatMetrics: func(args, reply interface{}) error { + rpl := map[string]float64{ + "metric1": 22.1, + } + *reply.(*map[string]float64) = rpl + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStatS): clientConn, + }) + spS, err := NewSupplierService(dm, nil, cfg, connMgr) + if err != nil { + t.Error(err) + } + + suppls := &Supplier{ + ID: "supplier2", + ResourceIDs: []string{"ResourceSupplier2"}, + StatIDs: []string{"Stat1"}, + Weight: 20, + Blocker: false, + cacheSupplier: map[string]interface{}{ + utils.MetaRatio: 3.3, + }, + } + + suplEv := &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "utils.CGREvent1", + Event: map[string]interface{}{ + "Account": "1001", + "Destination": "1002", + "Supplier": "SupplierProfile2", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + utils.SetupTime: time.Date(2018, time.January, 7, 16, 60, 0, 0, time.UTC), + "Weight": "20.0", + }, + } + spl := &optsGetSuppliers{ + maxCost: 25.0, + sortingParameters: []string{ + "ResourceUsage", + }, + sortingStragety: utils.MetaReload, + } + + if _, pass, err := spS.populateSortingData(suplEv, suppls, spl, nil); err != nil || !pass { + t.Error(err) + } + spl.sortingStragety = utils.MetaLoad + if _, pass, err := spS.populateSortingData(suplEv, suppls, spl, nil); err != nil || !pass { + t.Error(err) + } } diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index 530587c7b..b6e887e86 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -865,3 +865,49 @@ func TestSupplierServiceGetSPForEvent(t *testing.T) { t.Errorf("expected %v,received %v", utils.ToJSON([]*SupplierProfile{supplier}), utils.ToJSON(reply)) } } + +func TestV1GetSuppliersWithAttributeS(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + cfg.SupplierSCfg().AttributeSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes)} + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.AttributeSv1ProcessEvent: func(args, reply interface{}) error { + + return utils.ErrNotFound + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes): clientConn, + }) + spS, err := NewSupplierService(dm, nil, cfg, connMgr) + if err != nil { + t.Error(err) + } + args := &ArgsGetSuppliers{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + utils.Account: "1002", + utils.Subject: "1002", + utils.Destination: "1001", + utils.SetupTime: time.Date(2017, 12, 1, 14, 25, 0, 0, time.UTC), + utils.Usage: "1m20s", + }, + }, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("sup12345"), + }, + } + var reply SortedSuppliers + + if err := spS.V1GetSuppliers(args, &reply); err == nil { + t.Error(err) + } + +}