From dda327c13d39147bfde9fd3d0e3ec1428bb91c3f Mon Sep 17 00:00:00 2001 From: andronache Date: Wed, 19 May 2021 16:21:45 +0300 Subject: [PATCH] Cover tests in apis for rates --- apis/rates_test.go | 279 ++++++++++++++++++++++++++++++++++++++++++- engine/datadbmock.go | 12 +- rates/rates.go | 1 - 3 files changed, 287 insertions(+), 5 deletions(-) diff --git a/apis/rates_test.go b/apis/rates_test.go index fc675ac4b..b934ad733 100644 --- a/apis/rates_test.go +++ b/apis/rates_test.go @@ -575,7 +575,6 @@ func TestApisRateNewRateSv1(t *testing.T) { } } -/* func TestApisRateCostForEvent(t *testing.T) { cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone @@ -600,6 +599,7 @@ func TestApisRateCostForEvent(t *testing.T) { APIOpts: nil, }, } + rpCost := &utils.RateProfileCost{} err := rateSv1.CostForEvent(context.Background(), args, rpCost) if err == nil || err != utils.ErrNotFound { @@ -610,4 +610,279 @@ func TestApisRateCostForEvent(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected2, rpCost) } } -*/ + +func TestApisRateRemoveRateProfile(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, nil, connMgr) + 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 + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + + arg := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "tenant", + ID: "2", + }, + } + + reply := utils.StringPointer("") + err = admS.RemoveRateProfile(context.Background(), arg, reply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + if !reflect.DeepEqual(utils.ToJSON(reply), utils.ToJSON("OK")) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON("OK"), utils.ToJSON(reply)) + } + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "tenant", + ID: "2", + }, + } + var result utils.RateProfile + err = admS.GetRateProfile(context.Background(), args, &result) + if err == nil || err != utils.ErrNotFound { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ErrNotFound, err) + } +} + +func TestApisRateRemoveRateProfileMissing(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, nil, connMgr) + 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 + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + + arg := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "tenant", + }, + } + + reply := utils.StringPointer("") + err = admS.RemoveRateProfile(context.Background(), arg, reply) + expectedErr := "MANDATORY_IE_MISSING: [ID]" + if err == nil || err.Error() != expectedErr { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedErr, err) + } + +} + +func TestApisRateRemoveRateProfileEmptyTenant(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, nil, connMgr) + admS := NewAdminSv1(cfg, dm, connMgr) + ext := &utils.APIRateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.APIRate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, + }, + } + var rtRply string + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + + arg := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + ID: "2", + }, + } + + reply := utils.StringPointer("") + err = admS.RemoveRateProfile(context.Background(), arg, reply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + if !reflect.DeepEqual(utils.ToJSON(reply), utils.ToJSON("OK")) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON("OK"), utils.ToJSON(reply)) + } + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + ID: "2", + }, + } + var result utils.RateProfile + err = admS.GetRateProfile(context.Background(), args, &result) + if err == nil || err != utils.ErrNotFound { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ErrNotFound, err) + } +} + +func TestApisRateSetGetRateProfileError(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := &engine.DataDBMock{} + 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: "* * * * *", + }, + }, + } + expected := "SERVER_ERROR: NOT_IMPLEMENTED" + var rtRply string + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "tenant", + ID: "2", + }, + } + var result utils.RateProfile + err = admS.GetRateProfile(context.Background(), args, &result) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + engine.Cache = cacheInit +} + +func TestApisRateSetRemoveRateProfileError(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := &engine.DataDBMock{} + 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: "* * * * *", + }, + }, + } + expected := "SERVER_ERROR: NOT_IMPLEMENTED" + var rtRply string + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + arg := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + ID: "2", + }, + } + + reply := utils.StringPointer("") + err = admS.RemoveRateProfile(context.Background(), arg, reply) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + if !reflect.DeepEqual(utils.ToJSON(reply), utils.ToJSON("")) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(""), utils.ToJSON(reply)) + } + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "tenant", + ID: "2", + }, + } + var result utils.RateProfile + err = admS.GetRateProfile(context.Background(), args, &result) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + engine.Cache = cacheInit +} + +func TestApisRateSetRemoveRateProfileError2(t *testing.T) { + cacheInit := engine.Cache + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + 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: "* * * * *", + IntervalRates: []*utils.APIIntervalRate{ + { + IntervalStart: "error", + FixedFee: nil, + RecurrentFee: nil, + Unit: nil, + Increment: nil, + }, + }, + }, + }, + } + expected := "strconv.ParseInt: parsing \"error\": invalid syntax" + var rtRply string + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } + engine.Cache = cacheInit +} diff --git a/engine/datadbmock.go b/engine/datadbmock.go index dddefb931..97d074209 100644 --- a/engine/datadbmock.go +++ b/engine/datadbmock.go @@ -24,6 +24,8 @@ 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) @@ -266,11 +268,17 @@ func (dbM *DataDBMock) RemoveDispatcherHostDrv(string, string) error { return utils.ErrNotImplemented } -func (dbM *DataDBMock) GetRateProfileDrv(*context.Context, string, string) (*utils.RateProfile, error) { +func (dbM *DataDBMock) GetRateProfileDrv(ctx *context.Context, tnt string, id string) (*utils.RateProfile, error) { + if dbM.GetRateProfileDrvF != nil { + return dbM.GetRateProfileDrvF(ctx, tnt, id) + } return nil, utils.ErrNotImplemented } -func (dbM *DataDBMock) SetRateProfileDrv(*context.Context, *utils.RateProfile) error { +func (dbM *DataDBMock) SetRateProfileDrv(ctx *context.Context, rt *utils.RateProfile) error { + if dbM.SetRateProfileDrvF != nil { + return dbM.SetRateProfileDrvF(ctx, rt) + } return utils.ErrNotImplemented } diff --git a/rates/rates.go b/rates/rates.go index 0b27e96b9..1ee5eab8c 100644 --- a/rates/rates.go +++ b/rates/rates.go @@ -95,7 +95,6 @@ func (rS *RateS) matchingRateProfileForEvent(ctx *context.Context, tnt string, r if rPf, err = rS.dm.GetRateProfile(ctx, tnt, rPfID, true, true, utils.NonTransactional); err != nil { if err == utils.ErrNotFound { - fmt.Println(err) err = nil continue }