Updated tests and methods for rateprofiles.go with default tenant value

This commit is contained in:
porosnicuadrian
2020-10-13 14:50:57 +03:00
committed by Dan Christian Bogos
parent a64c492197
commit 0903fdbbf5
3 changed files with 230 additions and 19 deletions

View File

@@ -29,10 +29,14 @@ import (
// GetRateProfile returns an Rate Profile
func (apierSv1 *APIerSv1) GetRateProfile(arg *utils.TenantIDWithOpts, reply *engine.RateProfile) error {
if missing := utils.MissingStructFields(arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
if missing := utils.MissingStructFields(arg, []string{utils.ID}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
rPrf, err := apierSv1.DataManager.GetRateProfile(arg.Tenant, arg.ID, true, true, utils.NonTransactional)
tnt := arg.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
rPrf, err := apierSv1.DataManager.GetRateProfile(tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
if err.Error() != utils.ErrNotFound.Error() {
err = utils.NewErrServerError(err)
@@ -45,10 +49,11 @@ func (apierSv1 *APIerSv1) GetRateProfile(arg *utils.TenantIDWithOpts, reply *eng
// GetRateProfileIDs returns list of rate profile IDs registered for a tenant
func (apierSv1 *APIerSv1) GetRateProfileIDs(args *utils.PaginatorWithTenant, attrPrfIDs *[]string) error {
if missing := utils.MissingStructFields(args, []string{utils.Tenant}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
prfx := utils.RateProfilePrefix + args.Tenant + ":"
prfx := utils.RateProfilePrefix + tnt + ":"
keys, err := apierSv1.DataManager.DataDB().GetKeysForPrefix(prfx)
if err != nil {
return err
@@ -67,11 +72,12 @@ func (apierSv1 *APIerSv1) GetRateProfileIDs(args *utils.PaginatorWithTenant, att
// GetRateProfileIDsCount sets in reply var the total number of RateProfileIDs registered for a tenant
// returns ErrNotFound in case of 0 RateProfileIDs
func (apierSv1 *APIerSv1) GetRateProfileIDsCount(args *utils.TenantWithOpts, reply *int) (err error) {
if missing := utils.MissingStructFields(args, []string{utils.Tenant}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
var keys []string
prfx := utils.RateProfilePrefix + args.Tenant + ":"
prfx := utils.RateProfilePrefix + tnt + ":"
if keys, err = apierSv1.DataManager.DataDB().GetKeysForPrefix(prfx); err != nil {
return err
}
@@ -89,9 +95,12 @@ type RateProfileWithCache struct {
//SetRateProfile add/update a new Rate Profile
func (apierSv1 *APIerSv1) SetRateProfile(rPrf *RateProfileWithCache, reply *string) error {
if missing := utils.MissingStructFields(rPrf.RateProfile, []string{"Tenant", "ID", "Rates"}); len(missing) != 0 {
if missing := utils.MissingStructFields(rPrf.RateProfile, []string{utils.ID, utils.Rates}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if rPrf.Tenant == utils.EmptyString {
rPrf.Tenant = apierSv1.Config.GeneralCfg().DefaultTenant
}
if err := apierSv1.DataManager.SetRateProfile(rPrf.RateProfile, true); err != nil {
return utils.APIErrorHandler(err)
@@ -110,10 +119,12 @@ func (apierSv1 *APIerSv1) SetRateProfile(rPrf *RateProfileWithCache, reply *stri
//SetRateProfileRates add/update Rates from existing RateProfiles
func (apierSv1 *APIerSv1) SetRateProfileRates(rPrf *RateProfileWithCache, reply *string) (err error) {
if missing := utils.MissingStructFields(rPrf.RateProfile, []string{"Tenant", "ID", "Rates"}); len(missing) != 0 {
if missing := utils.MissingStructFields(rPrf.RateProfile, []string{utils.ID, utils.Rates}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if rPrf.Tenant == utils.EmptyString {
rPrf.Tenant = apierSv1.Config.GeneralCfg().DefaultTenant
}
if err = apierSv1.DataManager.SetRateProfileRates(rPrf.RateProfile, true); err != nil {
return utils.APIErrorHandler(err)
}
@@ -138,18 +149,22 @@ type RemoveRPrfRates struct {
}
func (apierSv1 *APIerSv1) RemoveRateProfileRates(args *RemoveRPrfRates, reply *string) (err error) {
if missing := utils.MissingStructFields(args, []string{"Tenant", "ID"}); len(missing) != 0 {
if missing := utils.MissingStructFields(args, []string{utils.ID}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := apierSv1.DataManager.RemoveRateProfileRates(args.Tenant, args.ID, args.RateIDs, true); err != nil {
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
if err := apierSv1.DataManager.RemoveRateProfileRates(tnt, args.ID, args.RateIDs, true); err != nil {
return utils.APIErrorHandler(err)
}
//generate a loadID for CacheRateProfiles and store it in database
if err := apierSv1.DataManager.SetLoadIDs(map[string]int64{utils.CacheRateProfiles: time.Now().UnixNano()}); err != nil {
return utils.APIErrorHandler(err)
}
if err := apierSv1.CallCache(args.Cache, args.Tenant, utils.CacheRateProfiles,
utils.ConcatenatedKey(args.Tenant, args.ID), nil, nil, args.Opts); err != nil {
if err := apierSv1.CallCache(args.Cache, tnt, utils.CacheRateProfiles,
utils.ConcatenatedKey(tnt, args.ID), nil, nil, args.Opts); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK
@@ -158,10 +173,14 @@ func (apierSv1 *APIerSv1) RemoveRateProfileRates(args *RemoveRPrfRates, reply *s
// RemoveRateProfile remove a specific Rate Profile
func (apierSv1 *APIerSv1) RemoveRateProfile(arg *utils.TenantIDWithCache, reply *string) error {
if missing := utils.MissingStructFields(arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
if missing := utils.MissingStructFields(arg, []string{utils.ID}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := apierSv1.DataManager.RemoveRateProfile(arg.Tenant, arg.ID,
tnt := arg.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
if err := apierSv1.DataManager.RemoveRateProfile(tnt, arg.ID,
utils.NonTransactional, true); err != nil {
return utils.APIErrorHandler(err)
}
@@ -169,8 +188,8 @@ func (apierSv1 *APIerSv1) RemoveRateProfile(arg *utils.TenantIDWithCache, reply
if err := apierSv1.DataManager.SetLoadIDs(map[string]int64{utils.CacheRateProfiles: time.Now().UnixNano()}); err != nil {
return utils.APIErrorHandler(err)
}
if err := apierSv1.CallCache(arg.Cache, arg.Tenant, utils.CacheRateProfiles,
utils.ConcatenatedKey(arg.Tenant, arg.ID), nil, nil, arg.Opts); err != nil {
if err := apierSv1.CallCache(arg.Cache, tnt, utils.CacheRateProfiles,
utils.ConcatenatedKey(tnt, arg.ID), nil, nil, arg.Opts); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK

View File

@@ -46,12 +46,18 @@ var (
testV1RatePrfRpcConn,
testV1RatePrfNotFound,
testV1RatePrfFromFolder,
testV1RatePrfGetRateProfileIDs,
testV1RatePrfGetRateProfileIDsCount,
testV1RatePrfVerifyRateProfile,
testV1RatePrfRemoveRateProfile,
testV1RatePrfNotFound,
testV1RatePrfSetRateProfileRates,
testV1RatePrfRemoveRateProfileRates,
testV1RatePing,
testV1RateGetRemoveRateProfileWithoutTenant,
testV1RatePrfRemoveRateProfileWithoutTenant,
testV1RatePrfGetRateProfileRatesWithoutTenant,
testV1RatePrfRemoveRateProfileRatesWithoutTenant,
testV1RatePrfStopEngine,
}
)
@@ -583,3 +589,188 @@ func testV1RatePrfStopEngine(t *testing.T) {
t.Error(err)
}
}
func testV1RateGetRemoveRateProfileWithoutTenant(t *testing.T) {
rateProfile := &engine.RateProfile{
ID: "RPWithoutTenant",
FilterIDs: []string{"*string:~*req.Subject:1001"},
Weight: 0,
ConnectFee: 0.1,
RoundingMethod: "*up",
RoundingDecimals: 4,
MinCost: 0.1,
MaxCost: 0.6,
MaxCostStrategy: "*free",
Rates: map[string]*engine.Rate{
"RT_WEEK": {
ID: "RT_WEEK",
Weight: 0,
ActivationTimes: "* * * * 1-5",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0 * time.Second),
Value: 0.12,
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Minute),
},
},
},
},
}
var reply string
if err := ratePrfRpc.Call(utils.APIerSv1SetRateProfile, rateProfile, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Error("Unexpected reply returned", reply)
}
var result *engine.RateProfile
rateProfile.Tenant = "cgrates.org"
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfile,
&utils.TenantIDWithOpts{TenantID: &utils.TenantID{ID: "RPWithoutTenant"}},
&result); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(result, rateProfile) {
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(rateProfile), utils.ToJSON(result))
}
}
func testV1RatePrfRemoveRateProfileWithoutTenant(t *testing.T) {
var reply string
if err := ratePrfRpc.Call(utils.APIerSv1RemoveRateProfile,
&utils.TenantIDWithCache{ID: "RPWithoutTenant"},
&reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Error("Unexpected reply returned", reply)
}
var result *engine.RateProfile
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfile,
&utils.TenantIDWithOpts{TenantID: &utils.TenantID{ID: "RPWithoutTenant"}},
&result); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}
func testV1RatePrfGetRateProfileIDs(t *testing.T) {
var result []string
expected := []string{"RP1"}
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfileIDs,
&utils.PaginatorWithTenant{},
&result); err != nil {
t.Error(err)
} else if len(result) != len(expected) {
t.Errorf("Expected %+v \n, received %+v", expected, result)
}
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfileIDs,
&utils.PaginatorWithTenant{Tenant: "cgrates.org"},
&result); err != nil {
t.Error(err)
} else if len(result) != len(expected) {
t.Errorf("Expected %+v \n, received %+v", expected, result)
}
}
func testV1RatePrfGetRateProfileIDsCount(t *testing.T) {
var reply int
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfileIDsCount,
&utils.TenantWithOpts{},
&reply); err != nil {
t.Error(err)
} else if reply != 1 {
t.Errorf("Expected 1, received %+v", reply)
}
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfileIDsCount,
&utils.TenantWithOpts{Tenant: "cgrates.org"},
&reply); err != nil {
t.Error(err)
} else if reply != 1 {
t.Errorf("Expected 1, received %+v", reply)
}
}
func testV1RatePrfGetRateProfileRatesWithoutTenant(t *testing.T) {
rPrf := &engine.RateProfile{
ID: "SpecialRate",
FilterIDs: []string{"*string:~*req.Subject:1001"},
Weight: 0,
ConnectFee: 0.1,
RoundingMethod: "*up",
RoundingDecimals: 4,
MinCost: 0.1,
MaxCost: 0.6,
MaxCostStrategy: "*free",
Rates: map[string]*engine.Rate{
"RT_WEEK": {
ID: "RT_WEEK",
Weight: 0,
ActivationTimes: "* * * * 1-5",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0 * time.Second),
Value: 0.12,
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Minute),
},
{
IntervalStart: time.Duration(1 * time.Minute),
Value: 0.06,
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
},
},
},
"RT_WEEKEND": {
ID: "RT_WEEKEND",
Weight: 10,
ActivationTimes: "* * * * 0,6",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0 * time.Second),
Value: 0.06,
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
},
},
},
"RT_CHRISTMAS": {
ID: "RT_CHRISTMAS",
Weight: 30,
ActivationTimes: "* * 24 12 *",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0 * time.Second),
Value: 0.06,
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
},
},
},
},
}
var reply string
if err := ratePrfRpc.Call(utils.APIerSv1SetRateProfileRates, rPrf, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Error("Unexpected reply returned", reply)
}
rPrf.Tenant = "cgrates.org"
var rply *engine.RateProfile
if err := ratePrfRpc.Call(utils.APIerSv1GetRateProfile,
utils.TenantIDWithOpts{TenantID: &utils.TenantID{ID: "SpecialRate"}},
&rply); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(rPrf, rply) {
t.Errorf("Expecting: %+v, \n received: %+v", utils.ToJSON(rPrf), utils.ToJSON(rply))
}
}
func testV1RatePrfRemoveRateProfileRatesWithoutTenant(t *testing.T) {
var reply string
if err := ratePrfRpc.Call(utils.APIerSv1RemoveRateProfileRates,
&RemoveRPrfRates{ID: "SpecialRate"},
&reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Error("Unexpected reply returned", reply)
}
}

View File

@@ -1582,6 +1582,7 @@ const (
APIerSv1SetRateProfile = "APIerSv1.SetRateProfile"
APIerSv1GetRateProfile = "APIerSv1.GetRateProfile"
APIerSv1GetRateProfileIDs = "APIerSv1.GetRateProfileIDs"
APIerSv1GetRateProfileIDsCount = "APIerSv1.GetRateProfileIDsCount"
APIerSv1RemoveRateProfile = "APIerSv1.RemoveRateProfile"
APIerSv1SetRateProfileRates = "APIerSv1.SetRateProfileRates"
APIerSv1RemoveRateProfileRates = "APIerSv1.RemoveRateProfileRates"