From c8f2198c78f4807be51b21e23ecbc778c029eb1c Mon Sep 17 00:00:00 2001 From: arberkatellari Date: Mon, 6 Mar 2023 10:58:14 -0500 Subject: [PATCH] Add Coverage tests for engine --- engine/datamanager_test.go | 379 +++++++++++++++++++++++++++++++++++++ 1 file changed, 379 insertions(+) diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index 6a81e84f2..b0d231930 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -6254,3 +6254,382 @@ func TestDMGetRouteProfileCacheWriteErr2(t *testing.T) { } } + +func TestDMSetRouteProfileNoDMErr(t *testing.T) { + var dm *DataManager + err := dm.SetRouteProfile(context.Background(), &RouteProfile{}, false) + if err != utils.ErrNoDatabaseConn { + t.Errorf("\nExpected error <%+v>, \nReceived error <%+v>", utils.ErrNoDatabaseConn, err) + } +} + +func TestDMSetRouteProfileCheckFiltersErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{":::"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + expErr := "broken reference to filter: <:::> for item with ID: cgrates.org:RP_1" + if err := dm.SetRouteProfile(context.Background(), rpp, true); err == nil || err.Error() != expErr { + t.Errorf("Expected error <%v>, received error <%v>", expErr, err) + } +} + +func TestDMSetRouteProfileGetRouteProfileErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + return &RouteProfile{}, utils.ErrNotImplemented + }, + } + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"FilterID1"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + if err := dm.SetRouteProfile(context.Background(), rpp, false); err == nil || err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, received error <%v>", utils.ErrNotImplemented, err) + } +} + +func TestDMSetRouteProfileSetRouteProfileDrvErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + return &RouteProfile{}, nil + }, + SetRouteProfileDrvF: func(ctx *context.Context, rtPrf *RouteProfile) error { return utils.ErrNotImplemented }, + } + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"FilterID1"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + if err := dm.SetRouteProfile(context.Background(), rpp, false); err == nil || err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, received error <%v>", utils.ErrNotImplemented, err) + } +} + +func TestDMSetRouteProfileUpdatedIndexesErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + return &RouteProfile{}, nil + }, + SetRouteProfileDrvF: func(ctx *context.Context, rtPrf *RouteProfile) error { return nil }, + } + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + if err := dm.SetRouteProfile(context.Background(), rpp, true); err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, received error <%v>", utils.ErrNotImplemented, err) + } +} + +func TestDMSetRouteProfileReplicate(t *testing.T) { + + tmp := Cache + cfgtmp := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(cfgtmp) + }() + Cache.Clear(nil) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + cfg := config.NewDefaultCGRConfig() + cfg.DataDbCfg().Items[utils.MetaRouteProfiles].Replicate = true + cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaReplicator)} + config.SetCgrConfig(cfg) + + cc := make(chan birpc.ClientConnector, 1) + cc <- &ccMock{ + + calls: map[string]func(ctx *context.Context, args interface{}, reply interface{}) error{ + utils.ReplicatorSv1SetRouteProfile: func(ctx *context.Context, args, reply interface{}) error { return utils.ErrNotImplemented }, + }, + } + + cM := NewConnManager(cfg) + cM.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaReplicator), utils.ReplicatorSv1, cc) + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { return rpp, nil }, + SetRouteProfileDrvF: func(ctx *context.Context, rtPrf *RouteProfile) error { return nil }, + } + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + // tests replicate + if err := dm.SetRouteProfile(context.Background(), rpp, false); err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, received error <%v>", utils.ErrNotImplemented, err) + } + +} + +func TestDMRemoveRouteProfileNoDMErr(t *testing.T) { + var dm *DataManager + err := dm.RemoveRouteProfile(context.Background(), "cgrates.org", "RP_1", false) + if err != utils.ErrNoDatabaseConn { + t.Errorf("\nExpected error <%+v>, \nReceived error <%+v>", utils.ErrNoDatabaseConn, err) + } +} + +func TestDMRemoveRouteProfileGetRouteProfileErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + return &RouteProfile{}, utils.ErrNotImplemented + }, + } + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"FilterID1"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + err := dm.RemoveRouteProfile(context.Background(), rpp.Tenant, rpp.ID, false) + if err != utils.ErrNotImplemented { + t.Errorf("\nExpected error <%+v>, \nReceived error <%+v>", utils.ErrNotImplemented, err) + } + +} + +func TestDMRemoveRouteProfileRemoveRouteProfileDrvErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := &DataDBMock{ + GetRouteProfileDrvF: func(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + return &RouteProfile{}, nil + }, + } + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + rpp := &RouteProfile{ + Tenant: "cgrates.org", + ID: "RP_1", + FilterIDs: []string{"FilterID1"}, + Weights: utils.DynamicWeights{{}}, + Sorting: utils.MetaQOS, + SortingParameters: []string{"param"}, + Routes: []*Route{{ + ID: "RT1", + FilterIDs: []string{"fltr1"}, + AccountIDs: []string{"acc1"}, + RateProfileIDs: []string{"rp1"}, + ResourceIDs: []string{"res1"}, + StatIDs: []string{"stat1"}, + Weights: utils.DynamicWeights{{}}, + Blockers: utils.DynamicBlockers{ + { + Blocker: true, + }, + }, + RouteParameters: "params", + }}, + } + + err := dm.RemoveRouteProfile(context.Background(), rpp.Tenant, rpp.ID, false) + if err != utils.ErrNotImplemented { + t.Errorf("\nExpected error <%+v>, \nReceived error <%+v>", utils.ErrNotImplemented, err) + } + +} + +func TestDMRemoveRouteProfileNilOldRppErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + var Id string + var tnt string + err := dm.RemoveRouteProfile(context.Background(), tnt, Id, false) + if err != utils.ErrNotFound { + t.Errorf("\nExpected error <%+v>, \nReceived error <%+v>", utils.ErrNotFound, err) + } + +}