diff --git a/apis/attributes_test.go b/apis/attributes_test.go index e4838bb42..9bbbfb12d 100644 --- a/apis/attributes_test.go +++ b/apis/attributes_test.go @@ -100,30 +100,6 @@ func TestSetGetAttributeProfile(t *testing.T) { dm.DataDB().Flush(utils.EmptyString) } -type dataDBKeys struct { - *engine.DataDBMock -} - -func (dbM *dataDBKeys) GetAttributeProfileDrv(*context.Context, string, string) (*engine.AttributeProfile, error) { - attrPRf := &engine.AttributeProfile{ - Tenant: "cgrates.org", - ID: "TEST", - } - return attrPRf, nil -} - -func (dbM *dataDBKeys) SetAttributeProfileDrv(*context.Context, *engine.AttributeProfile) error { - return nil -} - -func (dbM *dataDBKeys) GetKeysForPrefix(ctx *context.Context, prf string) ([]string, error) { - return nil, utils.ErrNotImplemented -} - -func (dbM *dataDBKeys) RemoveAttributeProfileDrv(*context.Context, string, string) error { - return nil -} - func TestSetAttributeProfileCheckErrors(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone @@ -178,7 +154,25 @@ func TestSetAttributeProfileCheckErrors(t *testing.T) { } cancel() - dbMock := new(dataDBKeys) + dbMock := &engine.DataDBMock{ + GetAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) (*engine.AttributeProfile, error) { + attrPRf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "TEST", + } + return attrPRf, nil + }, + SetAttributeProfileDrvF: func(ctx *context.Context, attr *engine.AttributeProfile) error { + return nil + }, + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return nil, utils.ErrNotImplemented + }, + RemoveAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) error { + return nil + }, + } + admS.dm = engine.NewDataManager(dbMock, cfg.CacheCfg(), nil) expected = "SERVER_ERROR: NOT_IMPLEMENTED" if err := admS.SetAttributeProfile(context.Background(), attrPrf, &reply); err == nil || err.Error() != expected { @@ -298,7 +292,25 @@ func TestRemoveAttributeProfileCheckErrors(t *testing.T) { func TestRemoveAttributeProfileMockErr(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone - dbMock := new(dataDBKeys) + dbMock := &engine.DataDBMock{ + GetAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) (*engine.AttributeProfile, error) { + attrPRf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "TEST", + } + return attrPRf, nil + }, + SetAttributeProfileDrvF: func(ctx *context.Context, attr *engine.AttributeProfile) error { + return nil + }, + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return nil, utils.ErrNotImplemented + }, + RemoveAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) error { + return nil + }, + } + dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil) admS := &AdminSv1{ cfg: cfg, @@ -321,7 +333,24 @@ func TestRemoveAttributeProfileMockErr(t *testing.T) { func TestGetAttributeProfileIDsMockErr(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone - dbMock := new(dataDBKeys) + dbMock := &engine.DataDBMock{ + GetAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) (*engine.AttributeProfile, error) { + attrPRf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "TEST", + } + return attrPRf, nil + }, + SetAttributeProfileDrvF: func(ctx *context.Context, attr *engine.AttributeProfile) error { + return nil + }, + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return nil, utils.ErrNotImplemented + }, + RemoveAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) error { + return nil + }, + } dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil) admS := &AdminSv1{ cfg: cfg, @@ -339,18 +368,14 @@ func TestGetAttributeProfileIDsMockErr(t *testing.T) { dm.DataDB().Flush(utils.EmptyString) } -type dataDBKeysFill struct { - *engine.DataDBMock -} - -func (dbM *dataDBKeysFill) GetKeysForPrefix(_ *context.Context, _ string) ([]string, error) { - return []string{}, nil -} - func TestGetAttributeProfileIDsMockErrKeys(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone - dbMock := new(dataDBKeysFill) + dbMock := &engine.DataDBMock{ + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return []string{}, nil + }, + } dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil) admS := &AdminSv1{ cfg: cfg, @@ -371,7 +396,24 @@ func TestGetAttributeProfileIDsMockErrKeys(t *testing.T) { func TestGetAttributeProfileIDscOUNTMockErr(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone - dbMock := new(dataDBKeys) + dbMock := &engine.DataDBMock{ + GetAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) (*engine.AttributeProfile, error) { + attrPRf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "TEST", + } + return attrPRf, nil + }, + SetAttributeProfileDrvF: func(ctx *context.Context, attr *engine.AttributeProfile) error { + return nil + }, + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return nil, utils.ErrNotImplemented + }, + RemoveAttributeProfileDrvF: func(ctx *context.Context, str1 string, str2 string) error { + return nil + }, + } dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil) admS := &AdminSv1{ cfg: cfg, @@ -387,7 +429,11 @@ func TestGetAttributeProfileIDscOUNTMockErr(t *testing.T) { t.Errorf("Expected %+v, received %+v", expected, err) } - dbMockNew := new(dataDBKeysFill) + dbMockNew := &engine.DataDBMock{ + GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) { + return []string{}, nil + }, + } expected = "NOT_FOUND" admS.dm = engine.NewDataManager(dbMockNew, cfg.CacheCfg(), nil) if err := admS.GetAttributeProfileIDsCount(context.Background(), diff --git a/apis/rates_test.go b/apis/rates_test.go index ac49a1194..a407f6f4c 100644 --- a/apis/rates_test.go +++ b/apis/rates_test.go @@ -1346,5 +1346,132 @@ func TestApisRateRemoveRateProfileRateErrorMissingField(t *testing.T) { if err == nil || err.Error() != expected { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) } + dm.DataDB().Flush(utils.EmptyString) + engine.Cache = cacheInit +} + +func TestApisRateSetRateProfileErrorSetLoadIDs(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := &engine.DataDBMock{ + GetRateProfileDrvF: func(c *context.Context, s string, s2 string) (*utils.RateProfile, error) { + return nil, utils.ErrNotFound + }, + SetRateProfileDrvF: func(c *context.Context, profile *utils.RateProfile) error { + return nil + }, + GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + return nil, nil + }, + SetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) { + return nil + }, + } + dm := engine.NewDataManager(dataDB, nil, connMgr) + newCache := engine.NewCacheS(cfg, dm, nil) + engine.Cache = newCache + admS := NewAdminSv1(cfg, dm, connMgr) + ext := &utils.APIRateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.APIRate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, + }, + } + var rtRply string + expected := "SERVER_ERROR: NOT_IMPLEMENTED" + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + dm.DataDB().Flush(utils.EmptyString) + engine.Cache = cacheInit +} + +func TestApisRateSetRateProfileRatesErrorSetLoadIDs(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := &engine.DataDBMock{ + GetRateProfileDrvF: func(c *context.Context, s string, s2 string) (*utils.RateProfile, error) { + return nil, utils.ErrNotFound + }, + SetRateProfileDrvF: func(c *context.Context, profile *utils.RateProfile) error { + return nil + }, + GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + return nil, nil + }, + SetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) { + return nil + }, + } + dm := engine.NewDataManager(dataDB, nil, connMgr) + newCache := engine.NewCacheS(cfg, dm, nil) + engine.Cache = newCache + admS := NewAdminSv1(cfg, dm, connMgr) + ext := &utils.APIRateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.APIRate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, + }, + } + var rtRply string + expected := utils.ErrNotFound + err := admS.SetRateProfileRates(context.Background(), ext, &rtRply) + if err == nil || err != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + dm.DataDB().Flush(utils.EmptyString) + engine.Cache = cacheInit +} + +func TestApisRateRemoveRateProfileRatesErrorSetLoadIDs(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := &engine.DataDBMock{ + GetRateProfileDrvF: func(c *context.Context, s string, s2 string) (*utils.RateProfile, error) { + return nil, utils.ErrNotFound + }, + SetRateProfileDrvF: func(c *context.Context, profile *utils.RateProfile) error { + return nil + }, + GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + return nil, nil + }, + SetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) { + return nil + }, + } + dm := engine.NewDataManager(dataDB, nil, connMgr) + newCache := engine.NewCacheS(cfg, dm, nil) + engine.Cache = newCache + admS := NewAdminSv1(cfg, dm, connMgr) + ext := &utils.RemoveRPrfRates{ + ID: "2", + Tenant: "tenant", + RateIDs: []string{"RT_WEEK"}, + } + var rtRply string + expected := utils.ErrNotFound + err := admS.RemoveRateProfileRates(context.Background(), ext, &rtRply) + if err == nil || err != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + dm.DataDB().Flush(utils.EmptyString) engine.Cache = cacheInit } diff --git a/engine/datadbmock.go b/engine/datadbmock.go index 97d074209..4e759832a 100644 --- a/engine/datadbmock.go +++ b/engine/datadbmock.go @@ -24,12 +24,14 @@ import ( ) type DataDBMock struct { - SetRateProfileDrvF func(*context.Context, *utils.RateProfile) error - GetRateProfileDrvF func(*context.Context, string, string) (*utils.RateProfile, error) - GetKeysForPrefixF func(*context.Context, string) ([]string, error) - GetChargerProfileDrvF func(string, string) (*ChargerProfile, error) - GetFilterDrvF func(string, string) (*Filter, error) - GetIndexesDrvF func(idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) + SetRateProfileDrvF func(*context.Context, *utils.RateProfile) error + GetRateProfileDrvF func(*context.Context, string, string) (*utils.RateProfile, error) + GetKeysForPrefixF func(*context.Context, string) ([]string, error) + GetIndexesDrvF func(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) + SetIndexesDrvF func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) + GetAttributeProfileDrvF func(ctx *context.Context, str1 string, str2 string) (*AttributeProfile, error) + SetAttributeProfileDrvF func(ctx *context.Context, attr *AttributeProfile) error + RemoveAttributeProfileDrvF func(ctx *context.Context, str1 string, str2 string) error } //Storage methods @@ -124,11 +126,17 @@ func (dbM *DataDBMock) AddLoadHistory(*utils.LoadInstance, int, string) error { } func (dbM *DataDBMock) GetIndexesDrv(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + if dbM.GetIndexesDrvF != nil { + return dbM.GetIndexesDrvF(ctx, idxItmType, tntCtx, idxKey) + } return nil, utils.ErrNotImplemented } func (dbM *DataDBMock) SetIndexesDrv(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) { + if dbM.SetIndexesDrvF != nil { + return dbM.SetIndexesDrvF(ctx, idxItmType, tntCtx, indexes, commit, transactionID) + } return utils.ErrNotImplemented } @@ -208,15 +216,24 @@ func (dbM *DataDBMock) RemoveRouteProfileDrv(string, string) error { return utils.ErrNotImplemented } -func (dbM *DataDBMock) GetAttributeProfileDrv(*context.Context, string, string) (*AttributeProfile, error) { +func (dbM *DataDBMock) GetAttributeProfileDrv(ctx *context.Context, str1 string, str2 string) (*AttributeProfile, error) { + if dbM.GetAttributeProfileDrvF != nil { + return dbM.GetAttributeProfileDrvF(ctx, str1, str2) + } return nil, utils.ErrNotImplemented } -func (dbM *DataDBMock) SetAttributeProfileDrv(*context.Context, *AttributeProfile) error { +func (dbM *DataDBMock) SetAttributeProfileDrv(ctx *context.Context, attr *AttributeProfile) error { + if dbM.SetAttributeProfileDrvF != nil { + return dbM.SetAttributeProfileDrvF(ctx, attr) + } return utils.ErrNotImplemented } -func (dbM *DataDBMock) RemoveAttributeProfileDrv(*context.Context, string, string) error { +func (dbM *DataDBMock) RemoveAttributeProfileDrv(ctx *context.Context, str1 string, str2 string) error { + if dbM.RemoveAttributeProfileDrvF != nil { + return dbM.RemoveAttributeProfileDrvF(ctx, str1, str2) + } return utils.ErrNotImplemented }