From bd859b684eb9fd786f7f0767f06f505b24b15810 Mon Sep 17 00:00:00 2001 From: arberkatellari Date: Wed, 4 Jan 2023 10:43:49 -0500 Subject: [PATCH] Coverage tests in engine --- engine/datamanager_test.go | 200 +++++++++++++++++++++++++++++++++ engine/dispatcherprfl_test.go | 206 ++++++++++++++++++++++++++-------- engine/libroutes_test.go | 62 +++++++++- 3 files changed, 423 insertions(+), 45 deletions(-) diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index 96f788c4c..87a0f81a6 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -1648,3 +1648,203 @@ func TestDMSetAccountReplicateTrue(t *testing.T) { // tests replicete dm.SetAccount(context.Background(), ap, false) } + +func TestDMRemoveThresholdProfileNilDM(t *testing.T) { + + var dm *DataManager + + expErr := utils.ErrNoDatabaseConn + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "ThrPrf1", false); err == nil || err != utils.ErrNoDatabaseConn { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileGetErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return nil, utils.ErrNotImplemented + }, + } + + expErr := utils.ErrNotImplemented + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "ThrPrf1", false); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileRmvErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return nil, nil + }, + RemThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (err error) { return utils.ErrNotImplemented }, + } + + expErr := utils.ErrNotImplemented + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "ThrPrf1", false); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileOldThrNil(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return nil, nil + }, + RemThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (err error) { return nil }, + } + + expErr := utils.ErrNotFound + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "ThrPrf1", false); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileIndxTrueErr1(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "THD_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActionProfileIDs: []string{"actPrfID"}, + MaxHits: 7, + MinHits: 0, + Weights: utils.DynamicWeights{ + { + Weight: 20, + }, + }, + Async: true, + }, nil + }, + RemThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (err error) { return nil }, + } + + expErr := utils.ErrNotImplemented + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "THD_2", true); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileIndxTrueErr2(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "THD_2", + FilterIDs: []string{"*string:~*req.Account:1001", "noPrefix"}, + ActionProfileIDs: []string{"actPrfID"}, + MaxHits: 7, + MinHits: 0, + Weights: utils.DynamicWeights{ + { + Weight: 20, + }, + }, + Async: true, + }, nil + }, + RemThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (err error) { return nil }, + GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error) { + return nil, utils.ErrNotImplemented + }, + } + + expErr := utils.ErrNotImplemented + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "THD_2", true); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMRemoveThresholdProfileReplicateTrue(t *testing.T) { + + tmp := Cache + cfgtmp := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(cfgtmp) + }() + + cfg := config.NewDefaultCGRConfig() + cfg.DataDbCfg().Items[utils.MetaThresholdProfiles].Replicate = true + config.SetCgrConfig(cfg) + + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (tp *ThresholdProfile, err error) { + return &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "THD_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActionProfileIDs: []string{"actPrfID"}, + MaxHits: 7, + MinHits: 0, + Weights: utils.DynamicWeights{ + { + Weight: 20, + }, + }, + Async: true, + }, nil + }, + RemThresholdProfileDrvF: func(ctx *context.Context, tenant, id string) (err error) { return nil }, + } + + expErr := utils.ErrNotImplemented + if err := dm.RemoveThresholdProfile(context.Background(), utils.CGRateSorg, "THD_2", false); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} diff --git a/engine/dispatcherprfl_test.go b/engine/dispatcherprfl_test.go index b923e8fd6..0c4e763b6 100644 --- a/engine/dispatcherprfl_test.go +++ b/engine/dispatcherprfl_test.go @@ -374,17 +374,18 @@ func TestDispatcherHostSet(t *testing.T) { exp := DispatcherHost{ Tenant: "cgrates.org", RemoteHost: &config.RemoteHost{ - ID: "ID", - Address: "127.0.0.1", - Transport: utils.MetaJSON, - ConnectAttempts: 1, - Reconnects: 1, - ConnectTimeout: time.Nanosecond, - ReplyTimeout: time.Nanosecond, - TLS: true, - ClientKey: "key", - ClientCertificate: "ce", - CaCertificate: "ca", + ID: "ID", + Address: "127.0.0.1", + Transport: utils.MetaJSON, + ConnectAttempts: 1, + Reconnects: 1, + MaxReconnectInterval: 1, + ConnectTimeout: time.Nanosecond, + ReplyTimeout: time.Nanosecond, + TLS: true, + ClientKey: "key", + ClientCertificate: "ce", + CaCertificate: "ca", }, } if err := dp.Set([]string{}, "", false, utils.EmptyString); err != utils.ErrWrongPath { @@ -415,6 +416,9 @@ func TestDispatcherHostSet(t *testing.T) { if err := dp.Set([]string{utils.Reconnects}, 1, false, utils.EmptyString); err != nil { t.Error(err) } + if err := dp.Set([]string{utils.MaxReconnectInterval}, 1, false, utils.EmptyString); err != nil { + t.Error(err) + } if err := dp.Set([]string{utils.ConnectTimeout}, 1, false, utils.EmptyString); err != nil { t.Error(err) } @@ -675,21 +679,128 @@ func TestDispatcherProfileMerge(t *testing.T) { } } +func TestDispatcherProfileMergeEmptyHostId(t *testing.T) { + dp := &DispatcherProfile{ + StrategyParams: make(map[string]interface{}), + Hosts: DispatcherHostProfiles{ + { + ID: utils.EmptyString, + }, + }, + } + exp := &DispatcherProfile{ + Tenant: "cgrates.org", + ID: "ID", + FilterIDs: []string{"fltr1"}, + Weight: 65, + Strategy: utils.MetaLoad, + StrategyParams: map[string]interface{}{"k": "v"}, + Hosts: DispatcherHostProfiles{ + { + ID: "C3", + FilterIDs: []string{"fltr2"}, + Weight: 20, + Params: map[string]interface{}{ + "param4": "value4", + }, + Blocker: false, + }, + }, + } + if dp.Merge(&DispatcherProfile{ + Tenant: "cgrates.org", + ID: "ID", + FilterIDs: []string{"fltr1"}, + Weight: 65, + Strategy: utils.MetaLoad, + StrategyParams: map[string]interface{}{"k": "v"}, + Hosts: DispatcherHostProfiles{ + { + ID: "C3", + FilterIDs: []string{"fltr2"}, + Weight: 20, + Params: map[string]interface{}{ + "param4": "value4", + }, + Blocker: false, + }, + }, + }); !reflect.DeepEqual(exp, dp) { + t.Errorf("Expected %+v \n but received \n %+v", utils.ToJSON(exp), utils.ToJSON(dp)) + } +} +func TestDispatcherProfileMergeEqualHosts(t *testing.T) { + dp := &DispatcherProfile{ + StrategyParams: make(map[string]interface{}), + Hosts: DispatcherHostProfiles{ + { + ID: "C3", + FilterIDs: []string{"dpFltr1"}, + Weight: 20, + Params: map[string]interface{}{ + "param4": "value4", + }, + Blocker: false, + }, + }, + } + exp := &DispatcherProfile{ + Tenant: "cgrates.org", + ID: "ID", + FilterIDs: []string{"fltr1"}, + Weight: 65, + Strategy: utils.MetaLoad, + StrategyParams: map[string]interface{}{"k": "v"}, + Hosts: DispatcherHostProfiles{ + { + ID: "C3", + FilterIDs: []string{"dpFltr1", "newFltr2"}, + Weight: 20, + Params: map[string]interface{}{ + "param4": "value4", + }, + Blocker: false, + }, + }, + } + if dp.Merge(&DispatcherProfile{ + Tenant: "cgrates.org", + ID: "ID", + FilterIDs: []string{"fltr1"}, + Weight: 65, + Strategy: utils.MetaLoad, + StrategyParams: map[string]interface{}{"k": "v"}, + Hosts: DispatcherHostProfiles{ + { + ID: "C3", + FilterIDs: []string{"newFltr2"}, + Weight: 20, + Params: map[string]interface{}{ + "param4": "value4", + }, + Blocker: false, + }, + }, + }); !reflect.DeepEqual(exp, dp) { + t.Errorf("Expected %+v \n but received \n %+v", utils.ToJSON(exp), utils.ToJSON(dp)) + } +} func TestDispatcherHostAsInterface(t *testing.T) { dh := DispatcherHost{ Tenant: "cgrates.org", RemoteHost: &config.RemoteHost{ - ID: "ID", - Address: "127.0.0.1", - Transport: utils.MetaJSON, - ConnectAttempts: 1, - Reconnects: 1, - ConnectTimeout: time.Nanosecond, - ReplyTimeout: time.Nanosecond, - TLS: true, - ClientKey: "key", - ClientCertificate: "ce", - CaCertificate: "ca", + ID: "ID", + Address: "127.0.0.1", + Transport: utils.MetaJSON, + ConnectAttempts: 1, + Reconnects: 1, + MaxReconnectInterval: 1, + ConnectTimeout: time.Nanosecond, + ReplyTimeout: time.Nanosecond, + TLS: true, + ClientKey: "key", + ClientCertificate: "ce", + CaCertificate: "ca", }, } if _, err := dh.FieldAsInterface(nil); err != utils.ErrNotFound { @@ -732,6 +843,11 @@ func TestDispatcherHostAsInterface(t *testing.T) { } else if exp := dh.Reconnects; exp != val { t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(val)) } + if val, err := dh.FieldAsInterface([]string{utils.MaxReconnectInterval}); err != nil { + t.Fatal(err) + } else if exp := dh.MaxReconnectInterval; exp != val { + t.Errorf("Expected %+v \n but received \n %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } if val, err := dh.FieldAsInterface([]string{utils.ConnectTimeout}); err != nil { t.Fatal(err) } else if exp := dh.ConnectTimeout; exp != val { @@ -782,33 +898,35 @@ func TestDispatcherHostMerge(t *testing.T) { exp := &DispatcherHost{ Tenant: "cgrates.org", RemoteHost: &config.RemoteHost{ - ID: "ID", - Address: "127.0.0.1", - Transport: utils.MetaJSON, - ConnectAttempts: 1, - Reconnects: 1, - ConnectTimeout: time.Nanosecond, - ReplyTimeout: time.Nanosecond, - TLS: true, - ClientKey: "key", - ClientCertificate: "ce", - CaCertificate: "ca", + ID: "ID", + Address: "127.0.0.1", + Transport: utils.MetaJSON, + ConnectAttempts: 1, + Reconnects: 1, + MaxReconnectInterval: 1, + ConnectTimeout: time.Nanosecond, + ReplyTimeout: time.Nanosecond, + TLS: true, + ClientKey: "key", + ClientCertificate: "ce", + CaCertificate: "ca", }, } if dp.Merge(&DispatcherHost{ Tenant: "cgrates.org", RemoteHost: &config.RemoteHost{ - ID: "ID", - Address: "127.0.0.1", - Transport: utils.MetaJSON, - ConnectAttempts: 1, - Reconnects: 1, - ConnectTimeout: time.Nanosecond, - ReplyTimeout: time.Nanosecond, - TLS: true, - ClientKey: "key", - ClientCertificate: "ce", - CaCertificate: "ca", + ID: "ID", + Address: "127.0.0.1", + Transport: utils.MetaJSON, + ConnectAttempts: 1, + Reconnects: 1, + MaxReconnectInterval: 1, + ConnectTimeout: time.Nanosecond, + ReplyTimeout: time.Nanosecond, + TLS: true, + ClientKey: "key", + ClientCertificate: "ce", + CaCertificate: "ca", }, }); !reflect.DeepEqual(exp, dp) { t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(dp)) diff --git a/engine/libroutes_test.go b/engine/libroutes_test.go index 81a754602..9ab9b78c3 100644 --- a/engine/libroutes_test.go +++ b/engine/libroutes_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -1561,7 +1562,7 @@ func TestSortedRoutesListAsNavigableMap(t *testing.T) { } -func TestRouteLazyPass(t *testing.T) { +func TestRouteLazyPassErr(t *testing.T) { filters := []*FilterRule{ { @@ -1589,3 +1590,62 @@ func TestRouteLazyPass(t *testing.T) { } } + +func TestRouteLazyPassTrue(t *testing.T) { + + rsrParse := &config.RSRParser{ + Rules: "~*opts.<~*opts.*originID;~*req.RunID;-Cost>", + } + if err := rsrParse.Compile(); err != nil { + t.Error(err) + } + valParse := config.RSRParsers{ + &config.RSRParser{ + Rules: "~*opts.<~*opts.*originID;~*req.RunID;-Cost>", + }, + } + if err := valParse.Compile(); err != nil { + t.Error(err) + } + + filters := []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Charger", + Values: []string{"ChargerProfile1"}, + rsrElement: rsrParse, + rsrValues: valParse, + }, + } + + ev := &utils.CGREvent{ + ID: "cgrId", + Tenant: "cgrates.org", + Event: utils.MapStorage{ + + utils.RunID: utils.MetaDefault, + }, + APIOpts: utils.MapStorage{ + utils.MetaOriginID: "Uniq", + "Uniq*default-Cost": 10, + }, + } + data := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + + utils.RunID: utils.MetaDefault, + }, + utils.MetaOpts: utils.MapStorage{ + utils.MetaOriginID: "Uniq", + "Uniq*default-Cost": 10, + }, + } + + if ok, err := routeLazyPass(context.Background(), filters, ev, + data, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaResources)}, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStats)}, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts)}); err != nil { + t.Error(err) + } else if !ok { + t.Error("Returned false, expecting true") + } + +}