From 2c587c1b6ca223fdb20770ed9c06f0e58a1bb31e Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Fri, 4 Feb 2022 14:50:16 +0200 Subject: [PATCH] Redesigned APIRateProfile + tests --- apis/cache_it_test.go | 50 ++- apis/filter_indexes_it_test.go | 328 ++++++++------- apis/rates.go | 36 +- apis/rates_it_test.go | 715 ++++++++++++++++++++++++++------- apis/rates_test.go | 505 ++++++++++++----------- dispatchers/rates_it_test.go | 78 ++-- utils/librates.go | 42 +- utils/librates_test.go | 110 +---- 8 files changed, 1094 insertions(+), 770 deletions(-) diff --git a/apis/cache_it_test.go b/apis/cache_it_test.go index 4c37f3573..289d9aa33 100644 --- a/apis/cache_it_test.go +++ b/apis/cache_it_test.go @@ -155,21 +155,31 @@ func testCacheSSetAttributeProfile(t *testing.T) { func testCacheSSetRateProfile(t *testing.T) { var reply string rtPrf := &utils.APIRateProfile{ - ID: "DefaultRate", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Weights: ";10", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.12), - Unit: utils.Float64Pointer(float64(time.Minute)), - Increment: utils.Float64Pointer(float64(time.Minute)), + RateProfile: &utils.RateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(12, 2), + Unit: utils.NewDecimal(int64(time.Minute), 0), + Increment: utils.NewDecimal(int64(time.Minute), 0), + }, }, }, }, @@ -181,10 +191,6 @@ func testCacheSSetRateProfile(t *testing.T) { } var result *utils.RateProfile - expRtPrf, err := rtPrf.AsRateProfile() - if err != nil { - t.Error(err) - } if err := chcRPC.Call(context.Background(), utils.AdminSv1GetRateProfile, &utils.TenantIDWithAPIOpts{ TenantID: &utils.TenantID{ @@ -193,10 +199,10 @@ func testCacheSSetRateProfile(t *testing.T) { }, }, &result); err != nil { } else { - expRtPrf.Compile() + rtPrf.Compile() result.Compile() - if !reflect.DeepEqual(expRtPrf, result) { - t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRtPrf), utils.ToJSON(result)) + if !reflect.DeepEqual(rtPrf.RateProfile, result) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(rtPrf), utils.ToJSON(result)) } } } diff --git a/apis/filter_indexes_it_test.go b/apis/filter_indexes_it_test.go index b9bb62013..e2d1cc75e 100644 --- a/apis/filter_indexes_it_test.go +++ b/apis/filter_indexes_it_test.go @@ -2176,20 +2176,22 @@ func testV1FIdxSetRateSProfileWithFltr(t *testing.T) { //we will set a RateProfile with our filter and check the indexes rtPrf := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_1", - FilterIDs: []string{"fltr_for_attr", - "*string:~*req.Account:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_ALWAYS": { - ID: "RT_ALWAYS", - FilterIDs: []string{"*string:~*req.RequestType:*prepaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_1", + FilterIDs: []string{"fltr_for_attr", + "*string:~*req.Account:1001"}, + Rates: map[string]*utils.Rate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + FilterIDs: []string{"*string:~*req.RequestType:*prepaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2277,21 +2279,23 @@ func testV1FIdxSetRateProfileMoreFltrsMoreIndexing(t *testing.T) { // update our RateProfile with our filter rtPrf := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_1", - FilterIDs: []string{"fltr_for_attr", - "fltr_for_attr2", "fltr_for_attr3", - "*string:~*req.Account:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_ALWAYS": { - ID: "RT_ALWAYS", - FilterIDs: []string{"*string:~*req.RequestType:*prepaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_1", + FilterIDs: []string{"fltr_for_attr", + "fltr_for_attr2", "fltr_for_attr3", + "*string:~*req.Account:1001"}, + Rates: map[string]*utils.Rate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + FilterIDs: []string{"*string:~*req.RequestType:*prepaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2388,42 +2392,46 @@ func testV1FIdxRateSProfileComputeIndexes(t *testing.T) { func testV1FIdxRateSProfileMoreProfilesForFilters(t *testing.T) { // we will add more RateProfiles for indexing rtPrf1 := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_2", - FilterIDs: []string{ - "fltr_for_attr2", "fltr_for_attr3", - "*string:~*req.Account:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - FilterIDs: []string{"*string:~*req.RequestType:*pseudoprepaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.001), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_2", + FilterIDs: []string{ + "fltr_for_attr2", "fltr_for_attr3", + "*string:~*req.Account:1001"}, + Rates: map[string]*utils.Rate{ + "RT_CHRISTMAS": { + ID: "RT_CHRISTMAS", + FilterIDs: []string{"*string:~*req.RequestType:*pseudoprepaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 3), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, }, } rtPrf2 := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_3", - FilterIDs: []string{ - "fltr_for_attr", - "*string:~*req.Account:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_ALWAYS": { - ID: "RT_ALWAYS", - FilterIDs: nil, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.5), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_3", + FilterIDs: []string{ + "fltr_for_attr", + "*string:~*req.Account:1001"}, + Rates: map[string]*utils.Rate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + FilterIDs: nil, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(5, 1), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2644,32 +2652,34 @@ func testV1FIdxRateSProfileRatesWithFltr(t *testing.T) { //we will set a RateProfile with our filter and check the indexes rtPrf := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_1", - FilterIDs: []string{"*string:~*req.Account:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_ALWAYS": { - ID: "RT_ALWAYS", - FilterIDs: []string{"fltr_for_attr", - "*string:~*req.RequestType:*prepaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Rates: map[string]*utils.Rate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + FilterIDs: []string{"fltr_for_attr", + "*string:~*req.RequestType:*prepaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, - }, - "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - FilterIDs: []string{"*string:~*req.RequestType:*postpaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + "RT_CHRISTMAS": { + ID: "RT_CHRISTMAS", + FilterIDs: []string{"*string:~*req.RequestType:*postpaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2759,33 +2769,35 @@ func testV1FIdxSetRatePofileRatesMoreFltrsMoreIndexing(t *testing.T) { // update our RateProfileRates with our filters rtPrf := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_1", - FilterIDs: []string{"*string:~*req.Account:1001", "fltr_for_attr"}, - Rates: map[string]*utils.APIRate{ - "RT_ALWAYS": { - ID: "RT_ALWAYS", - FilterIDs: []string{"fltr_for_attr", "fltr_for_attr2", - "*string:~*req.RequestType:*prepaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_1", + FilterIDs: []string{"*string:~*req.Account:1001", "fltr_for_attr"}, + Rates: map[string]*utils.Rate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + FilterIDs: []string{"fltr_for_attr", "fltr_for_attr2", + "*string:~*req.RequestType:*prepaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, - }, - "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - FilterIDs: []string{"fltr_for_attr3", - "*string:~*req.RequestType:*postpaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.01), - FixedFee: utils.Float64Pointer(0), + "RT_CHRISTMAS": { + ID: "RT_CHRISTMAS", + FilterIDs: []string{"fltr_for_attr3", + "*string:~*req.RequestType:*postpaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 2), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2887,32 +2899,34 @@ func testV1FIdxRateProfileRatesMoreRatesForFilters(t *testing.T) { //we will add more rates in RATE_1 for matching filters and also // another RateProfile with new rates rtPrf := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_1", - FilterIDs: []string{"*string:~*req.Account:1001", "fltr_for_attr"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEKEND": { - ID: "RT_WEEKEND", - FilterIDs: []string{"fltr_for_attr3", "fltr_for_attr2", - "*string:~*req.RequestType:*postpaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.1), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_1", + FilterIDs: []string{"*string:~*req.Account:1001", "fltr_for_attr"}, + Rates: map[string]*utils.Rate{ + "RT_WEEKEND": { + ID: "RT_WEEKEND", + FilterIDs: []string{"fltr_for_attr3", "fltr_for_attr2", + "*string:~*req.RequestType:*postpaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 1), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, - }, - "RT_GOLDEN_HOUR": { - ID: "RT_GOLDEN_HOUR", - FilterIDs: []string{"fltr_for_attr"}, - ActivationTimes: "* * 1 * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.001), - FixedFee: utils.Float64Pointer(0), + "RT_GOLDEN_HOUR": { + ID: "RT_GOLDEN_HOUR", + FilterIDs: []string{"fltr_for_attr"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 3), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, @@ -2926,32 +2940,34 @@ func testV1FIdxRateProfileRatesMoreRatesForFilters(t *testing.T) { t.Errorf("UNexpected reply returne") } rtPrf2 := utils.APIRateProfile{ - Tenant: "cgrates.org", - ID: "RATE_2", - FilterIDs: []string{"*string:~*req.Account:1007", "fltr_for_attr"}, - Rates: map[string]*utils.APIRate{ - "RT_CHRISTMAS_2": { - ID: "RT_CHRISTMAS_2", - FilterIDs: []string{"fltr_for_attr3", "fltr_for_attr2", - "*string:~*req.RequestType:*postpaid"}, - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.1), - FixedFee: utils.Float64Pointer(0), + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RATE_2", + FilterIDs: []string{"*string:~*req.Account:1007", "fltr_for_attr"}, + Rates: map[string]*utils.Rate{ + "RT_CHRISTMAS_2": { + ID: "RT_CHRISTMAS_2", + FilterIDs: []string{"fltr_for_attr3", "fltr_for_attr2", + "*string:~*req.RequestType:*postpaid"}, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 1), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, - }, - "RT_ALWAYS_2": { - ID: "RT_ALWAYS_2", - FilterIDs: []string{"fltr_for_attr", "fltr_for_attr2"}, - ActivationTimes: "* * 1 * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.001), - FixedFee: utils.Float64Pointer(0), + "RT_ALWAYS_2": { + ID: "RT_ALWAYS_2", + FilterIDs: []string{"fltr_for_attr", "fltr_for_attr2"}, + ActivationTimes: "* * 1 * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(1, 3), + FixedFee: utils.NewDecimal(0, 0), + }, }, }, }, diff --git a/apis/rates.go b/apis/rates.go index 7c4545726..792144b46 100644 --- a/apis/rates.go +++ b/apis/rates.go @@ -117,26 +117,22 @@ func (admS *AdminSv1) GetRateProfileCount(ctx *context.Context, args *utils.Args } // SetRateProfile add/update a new Rate Profile -func (admS *AdminSv1) SetRateProfile(ctx *context.Context, ext *utils.APIRateProfile, reply *string) error { - if missing := utils.MissingStructFields(ext, []string{utils.ID, utils.Rates}); len(missing) != 0 { +func (admS *AdminSv1) SetRateProfile(ctx *context.Context, args *utils.APIRateProfile, reply *string) error { + if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Rates}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - if ext.Tenant == utils.EmptyString { - ext.Tenant = admS.cfg.GeneralCfg().DefaultTenant + if args.Tenant == utils.EmptyString { + args.Tenant = admS.cfg.GeneralCfg().DefaultTenant } - rPrf, err := ext.AsRateProfile() - if err != nil { - return err - } - if err := admS.dm.SetRateProfile(ctx, rPrf, true); err != nil { + if err := admS.dm.SetRateProfile(ctx, args.RateProfile, true); err != nil { return utils.APIErrorHandler(err) } //generate a loadID for CacheRateProfiles and store it in database if err := admS.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheRateProfiles: time.Now().UnixNano()}); err != nil { return utils.APIErrorHandler(err) } - if err := admS.CallCache(ctx, utils.IfaceAsString(ext.APIOpts[utils.MetaCache]), rPrf.Tenant, utils.CacheRateProfiles, - rPrf.TenantID(), &rPrf.FilterIDs, ext.APIOpts); err != nil { + if err := admS.CallCache(ctx, utils.IfaceAsString(args.APIOpts[utils.MetaCache]), args.Tenant, utils.CacheRateProfiles, + args.TenantID(), &args.FilterIDs, args.APIOpts); err != nil { return utils.APIErrorHandler(err) } *reply = utils.OK @@ -144,26 +140,22 @@ func (admS *AdminSv1) SetRateProfile(ctx *context.Context, ext *utils.APIRatePro } // SetRateProfileRates add/update Rates from existing RateProfiles -func (admS *AdminSv1) SetRateProfileRates(ctx *context.Context, ext *utils.APIRateProfile, reply *string) (err error) { - if missing := utils.MissingStructFields(ext, []string{utils.ID, utils.Rates}); len(missing) != 0 { +func (admS *AdminSv1) SetRateProfileRates(ctx *context.Context, args *utils.APIRateProfile, reply *string) (err error) { + if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Rates}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - if ext.Tenant == utils.EmptyString { - ext.Tenant = admS.cfg.GeneralCfg().DefaultTenant + if args.Tenant == utils.EmptyString { + args.Tenant = admS.cfg.GeneralCfg().DefaultTenant } - rPrf, err := ext.AsRateProfile() - if err != nil { - return err - } - if err = admS.dm.SetRateProfileRates(ctx, rPrf, true); err != nil { + if err = admS.dm.SetRateProfileRates(ctx, args.RateProfile, true); err != nil { return utils.APIErrorHandler(err) } //generate a loadID for CacheRateProfiles and store it in database if err = admS.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheRateProfiles: time.Now().UnixNano()}); err != nil { return utils.APIErrorHandler(err) } - if err = admS.CallCache(ctx, utils.IfaceAsString(ext.APIOpts[utils.MetaCache]), rPrf.Tenant, utils.CacheRateProfiles, - rPrf.TenantID(), &rPrf.FilterIDs, ext.APIOpts); err != nil { + if err = admS.CallCache(ctx, utils.IfaceAsString(args.APIOpts[utils.MetaCache]), args.Tenant, utils.CacheRateProfiles, + args.TenantID(), &args.FilterIDs, args.APIOpts); err != nil { return utils.APIErrorHandler(err) } *reply = utils.OK diff --git a/apis/rates_it_test.go b/apis/rates_it_test.go index 565e107ef..c88f9ea46 100644 --- a/apis/rates_it_test.go +++ b/apis/rates_it_test.go @@ -68,6 +68,9 @@ var ( testRateRemoveRateProfileRates, testRateSetRateProfileRates, testRateSetRateProfilesWithPrefix, + // here we will tests better the create,read,update and delte for the rates inside of a RateProfile + //testRateProfileWithMultipleRates, + //testRateProfileUpdateRates, testRateSKillEngine, } ) @@ -150,19 +153,29 @@ func testGetRateProfilesBeforeSet(t *testing.T) { } func testRateSetRateProfile(t *testing.T) { ratePrf := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -315,19 +328,29 @@ func testGetRateProfileBeforeSet2(t *testing.T) { func testRateSetRateProfile2(t *testing.T) { ratePrf := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST_SECOND", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_SECOND", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -457,18 +480,13 @@ func testRateGetRateProfiles2(t *testing.T) { ID: "RT_WEEK", Weights: []*utils.DynamicWeight{ { - FilterIDs: nil, - Weight: 0, + Weight: 0, }, }, ActivationTimes: "* * * * 1-5", IntervalRates: []*utils.IntervalRate{ { IntervalStart: utils.NewDecimal(0, 0), - FixedFee: nil, - RecurrentFee: nil, - Unit: nil, - Increment: nil, }, }, }, @@ -542,8 +560,7 @@ func testRateGetRateProfilesAfterRemove(t *testing.T) { FilterIDs: []string{"*string:~*req.Account:dan"}, Weights: []*utils.DynamicWeight{ { - FilterIDs: nil, - Weight: 0, + Weight: 0, }, }, MaxCostStrategy: "*free", @@ -552,18 +569,13 @@ func testRateGetRateProfilesAfterRemove(t *testing.T) { ID: "RT_WEEK", Weights: []*utils.DynamicWeight{ { - FilterIDs: nil, - Weight: 0, + Weight: 0, }, }, ActivationTimes: "* * * * 1-5", IntervalRates: []*utils.IntervalRate{ { IntervalStart: utils.NewDecimal(0, 0), - FixedFee: nil, - RecurrentFee: nil, - Unit: nil, - Increment: nil, }, }, }, @@ -581,19 +593,29 @@ func testRateGetRateProfilesAfterRemove(t *testing.T) { func testRateSetAttributeProfileBrokenReference(t *testing.T) { ratePrf := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST_SECOND", - FilterIDs: []string{"invalid_filter_format", "*string:~*opts.*context:*sessions"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_SECOND", + FilterIDs: []string{"invalid_filter_format", "*string:~*opts.*context:*sessions"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -609,29 +631,43 @@ func testRateSetAttributeProfileBrokenReference(t *testing.T) { func testRateSetRateProfile3(t *testing.T) { ratePrf := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST_THIRD", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - }, + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_THIRD", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, }, }, - "RT_MONTH": { - ID: "RT_MONTH", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, + }, + }, + "RT_MONTH": { + ID: "RT_MONTH", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -758,20 +794,30 @@ func testRateRemoveRateProfileRates(t *testing.T) { // as we removed our RT_WEEK, we will add it back to our profile argsRate := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST_THIRD", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - // RT_WEEK wich we added back we added back - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_THIRD", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + // RT_WEEK wich we added back we added back + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -787,38 +833,53 @@ func testRateRemoveRateProfileRates(t *testing.T) { func testRateSetRateProfileRates(t *testing.T) { argsRate := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "TEST_RATE_IT_TEST_THIRD", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - // RT_MONTH rate will be updated - "RT_MONTH": { - ID: "RT_MONTH", - Weights: "*exists:~*req.Destination:;25", - ActivationTimes: "* 10 * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - FixedFee: utils.Float64Pointer(0.4), - Increment: utils.Float64Pointer(float64(time.Second)), - RecurrentFee: utils.Float64Pointer(float64(time.Second)), - Unit: utils.Float64Pointer(1), - }, + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_THIRD", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, }, }, - // RT_CHRISTMAS will be added as a new rate into our profile - "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weights: ";10", - ActivationTimes: "1 * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.2), - Unit: utils.Float64Pointer(float64(time.Minute)), - Increment: utils.Float64Pointer(0), + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + // RT_MONTH rate will be updated + "RT_MONTH": { + ID: "RT_MONTH", + Weights: utils.DynamicWeights{ + { + FilterIDs: []string{"*exists:~*req.Destination:"}, + Weight: 25, + }, + }, + ActivationTimes: "* 10 * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(4, 1), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(1, 0), + }, + }, + }, + // RT_CHRISTMAS will be added as a new rate into our profile + "RT_CHRISTMAS": { + ID: "RT_CHRISTMAS", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + ActivationTimes: "1 * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(2, 1), + Unit: utils.NewDecimal(int64(time.Minute), 0), + Increment: utils.NewDecimal(0, 0), + }, }, }, }, @@ -914,19 +975,29 @@ func testRateSetRateProfileRates(t *testing.T) { func testRateSetRateProfilesWithPrefix(t *testing.T) { ratePrf := &utils.APIRateProfile{ - Tenant: utils.CGRateSorg, - ID: "PrefixTEST_RATE_IT_TEST", - FilterIDs: []string{"*string:~*req.Account:dan"}, - Weights: ";0", - MaxCostStrategy: "*free", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * 1-5", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "PrefixTEST_RATE_IT_TEST", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * 1-5", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + }, }, }, }, @@ -1003,18 +1074,13 @@ func testRateSetRateProfilesWithPrefix(t *testing.T) { ID: "RT_WEEK", Weights: []*utils.DynamicWeight{ { - FilterIDs: nil, - Weight: 0, + Weight: 0, }, }, ActivationTimes: "* * * * 1-5", IntervalRates: []*utils.IntervalRate{ { IntervalStart: utils.NewDecimal(0, 0), - FixedFee: nil, - RecurrentFee: nil, - Unit: nil, - Increment: nil, }, }, }, @@ -1029,6 +1095,381 @@ func testRateSetRateProfilesWithPrefix(t *testing.T) { } } +func testRateProfileWithMultipleRates(t *testing.T) { + ratePrf := &utils.APIRateProfile{ + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "MultipleRates", + FilterIDs: []string{"*exists:~*req.CGRID:", "*prefix:~*req.Destination:12354"}, + Weights: utils.DynamicWeights{ + { + Weight: 100, + }, + }, + MinCost: utils.NewDecimal(2, 1), + MaxCost: utils.NewDecimal(20244, 3), + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + "RT_MONDAY": { + ID: "RT_MONDAY", + Weights: utils.DynamicWeights{ + { + Weight: 50, + }, + }, + ActivationTimes: "* * * * 0", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(33, 2), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(60*time.Second), 0), + FixedFee: utils.NewDecimal(1, 1), + Increment: utils.NewDecimal(int64(time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + "RT_THUESDAY": { + ID: "RT_THUESDAY", + Weights: utils.DynamicWeights{ + { + Weight: 40, + }, + }, + FilterIDs: []string{"*string:~*opts.*rates:true"}, + ActivationTimes: "* * * * 1", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(20, 2), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(45*time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + Increment: utils.NewDecimal(int64(time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + "RT_WEDNESDAY": { + ID: "RT_WEDNESDAY", + Weights: utils.DynamicWeights{ + { + Weight: 30, + }, + }, + ActivationTimes: "* * * * 2", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(1, 1), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(45*time.Second), 0), + FixedFee: utils.NewDecimal(2, 3), + Increment: utils.NewDecimal(int64(time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + "RT_THURSDAY": { + ID: "RT_THURSDAY", + Weights: utils.DynamicWeights{ + { + Weight: 20, + }, + }, + ActivationTimes: "* * * * 3", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(2, 1), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(2*time.Minute), 0), + FixedFee: utils.NewDecimal(1, 3), + Increment: utils.NewDecimal(int64(time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + "RT_FRIDAY": { + ID: "RT_FRIDAY", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + ActivationTimes: "* * * * 4", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(5, 1), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(time.Minute+30*time.Second), 0), + FixedFee: utils.NewDecimal(21, 3), + Increment: utils.NewDecimal(int64(time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + }, + }, + } + var reply string + if err := rateSRPC.Call(context.Background(), utils.AdminSv1SetRateProfile, + ratePrf, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error(err) + } + + // as we created our profile, count the rates + var result *utils.RateProfile + if err := rateSRPC.Call(context.Background(), utils.AdminSv1GetRateProfile, + &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: utils.CGRateSorg, + ID: "MultipleRates", + }, + }, &result); err != nil { + t.Error(err) + } else if len(result.Rates) != 5 { + t.Errorf("Unexpected reply returned") + } +} + +func testRateProfileUpdateRates(t *testing.T) { + argsRate := &utils.APIRateProfile{ + RateProfile: &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "MultipleRates", + FilterIDs: []string{"*exists:~*req.CGRID:", "*prefix:~*req.Destination:12354"}, + Weights: utils.DynamicWeights{ + { + Weight: 100, + }, + }, + MinCost: utils.NewDecimal(2, 1), + MaxCost: utils.NewDecimal(20244, 3), + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + // RT_MMONDAY Modified the interval rates + "RT_MONDAY": { + ID: "RT_MONDAY", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*lt:~*req.*usage:6"}, + ActivationTimes: "* 12 * * 0", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + Blocker: true, + }, + // on rate_friday we will modify just the activationTimes and one intervalRate, in all weekend + "RT_FRIDAY": { + ID: "RT_FRIDAY", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + ActivationTimes: "* * * * 4-6", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(5, 1), + Increment: utils.NewDecimal(int64(time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(time.Second), 0), + Unit: utils.NewDecimal(int64(time.Minute), 0), + }, + }, + }, + }, + }, + } + var result *string + if err := rateSRPC.Call(context.Background(), utils.AdminSv1SetRateProfileRates, + argsRate, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(result, utils.StringPointer("OK")) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON("OK"), utils.ToJSON(result)) + } + + // so, RT_MONDAY, RT_FRIDAY were update + // RT_THURSDAY, RT_WEDNESDAY AND RATE_THUESDAY are the same + expectedRate := &utils.RateProfile{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_THIRD", + FilterIDs: []string{"*string:~*req.Account:dan"}, + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + MaxCostStrategy: "*free", + Rates: map[string]*utils.Rate{ + // RT_WEEK that remains the same + "RT_MONDAY": { + ID: "RT_MONDAY", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*lt:~*req.*usage:6"}, + ActivationTimes: "* 12 * * 0", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + Blocker: true, + }, + "RT_THUESDAY": { + ID: "RT_THUESDAY", + Weights: utils.DynamicWeights{ + { + Weight: 40, + }, + }, + FilterIDs: []string{"*string:~*opts.*rates:true"}, + ActivationTimes: "* * * * 1", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(20, 2), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(45*time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + Increment: utils.NewDecimal(int64(2*time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + }, + "RT_WEDNESDAY": { + ID: "RT_WEDNESDAY", + Weights: utils.DynamicWeights{ + { + Weight: 30, + }, + }, + ActivationTimes: "* * * * 2", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(1, 1), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(45*time.Second), 0), + FixedFee: utils.NewDecimal(2, 3), + Increment: utils.NewDecimal(int64(2*time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + }, + "RT_THURSDAY": { + ID: "RT_THURSDAY", + Weights: utils.DynamicWeights{ + { + Weight: 20, + }, + }, + ActivationTimes: "* * * * 3", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(2, 1), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + { + IntervalStart: utils.NewDecimal(int64(2*time.Minute), 0), + FixedFee: utils.NewDecimal(1, 3), + Increment: utils.NewDecimal(int64(2*time.Minute), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + }, + "RT_FRIDAY": { + ID: "RT_FRIDAY", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + ActivationTimes: "* * * * 4-6", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + FixedFee: utils.NewDecimal(5, 1), + Increment: utils.NewDecimal(int64(2*time.Second), 0), + RecurrentFee: utils.NewDecimal(int64(2*time.Second), 0), + Unit: utils.NewDecimal(int64(2*time.Minute), 0), + }, + }, + }, + }, + } + var result2 *utils.RateProfile + if err := rateSRPC.Call(context.Background(), utils.AdminSv1GetRateProfile, + &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: utils.CGRateSorg, + ID: "TEST_RATE_IT_TEST_THIRD", + }, + }, &result2); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(result2, expectedRate) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expectedRate), utils.ToJSON(result2)) + } +} + //Kill the engine when it is about to be finished func testRateSKillEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { diff --git a/apis/rates_test.go b/apis/rates_test.go index a03988c1a..e0b5debf7 100644 --- a/apis/rates_test.go +++ b/apis/rates_test.go @@ -59,13 +59,15 @@ func TestRatesGetRateProfile1(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "DefaultRate", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -142,13 +144,15 @@ func TestRatesGetRateProfileIDs(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "RP1", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "RP1", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -179,13 +183,15 @@ func TestRatesGetRateProfile2(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "RP2", - Tenant: "tenant", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "RP2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -258,17 +264,18 @@ func TestRatesGetRateProfileCount(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "RP3", - Tenant: "tenant", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "RP3", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } - var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) if err != nil { @@ -297,13 +304,15 @@ func TestRatesGetRateProfileCountEmptyTenant(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "RP4", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "RP4", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -370,12 +379,14 @@ func TestRatesSetRateProfileMissingStructFieldError(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -396,12 +407,14 @@ func TestRatesSetRateProfileEmptyTenant(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -457,15 +470,19 @@ func TestRatesSetRateProfileError(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext := &utils.APIRateProfile{ - ID: "RP6", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "RP6", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } + var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) if err != nil { @@ -518,16 +535,19 @@ func TestRatesSetRateProfile(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } + var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) if err != nil { @@ -626,13 +646,15 @@ func TestRatesRemoveRateProfile(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -679,16 +701,19 @@ func TestRatesRemoveRateProfileMissing(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } + var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) if err != nil { @@ -719,12 +744,14 @@ func TestRatesRemoveRateProfileEmptyTenant(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -769,16 +796,19 @@ func TestRatesSetGetRateProfileError(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } + expected := "SERVER_ERROR: NOT_IMPLEMENTED" var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) @@ -808,16 +838,19 @@ func TestRatesSetRemoveRateProfileError(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } + expected := "SERVER_ERROR: NOT_IMPLEMENTED" var rtRply string err := admS.SetRateProfile(context.Background(), ext, &rtRply) @@ -851,42 +884,6 @@ func TestRatesSetRemoveRateProfileError(t *testing.T) { } } -func TestRatesSetRateProfileError2(t *testing.T) { - engine.Cache.Clear(nil) - cfg := config.NewDefaultCGRConfig() - cfg.GeneralCfg().DefaultCaching = utils.MetaNone - connMgr := engine.NewConnManager(cfg) - dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items) - 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: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "error", - FixedFee: nil, - RecurrentFee: nil, - Unit: nil, - Increment: nil, - }, - }, - }, - }, - } - expected := "can't convert to decimal" - var rtRply string - err := admS.SetRateProfile(context.Background(), ext, &rtRply) - if err == nil || err.Error() != expected { - t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) - } -} - func TestRatesSetRateProfileRates(t *testing.T) { engine.Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() @@ -896,13 +893,15 @@ func TestRatesSetRateProfileRates(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext1 := &utils.APIRateProfile{ - ID: "2", - Tenant: "tenant", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -912,13 +911,15 @@ func TestRatesSetRateProfileRates(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) } ext2 := &utils.APIRateProfile{ - ID: "2", - Tenant: "tenant", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -967,12 +968,14 @@ func TestRatesSetRateProfileRatesNoTenant(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext1 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -982,12 +985,14 @@ func TestRatesSetRateProfileRatesNoTenant(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) } ext2 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1035,11 +1040,13 @@ func TestRatesSetRateProfileRatesMissingField(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext2 := &utils.APIRateProfile{ - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1064,13 +1071,15 @@ func TestRatesSetRateProfileRatesErr(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext2 := &utils.APIRateProfile{ - ID: "2", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1086,42 +1095,6 @@ func TestRatesSetRateProfileRatesErr(t *testing.T) { } -func TestRatesSetRateProfileRatesErr2(t *testing.T) { - engine.Cache.Clear(nil) - cfg := config.NewDefaultCGRConfig() - cfg.GeneralCfg().DefaultCaching = utils.MetaNone - connMgr := engine.NewConnManager(cfg) - dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items) - dm := engine.NewDataManager(dataDB, nil, connMgr) - admS := NewAdminSv1(cfg, dm, connMgr) - ext2 := &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", - }, - }, - }, - }, - } - var rtRply string - expected := "can't convert to decimal" - err := admS.SetRateProfileRates(context.Background(), ext2, &rtRply) - if err == nil || err.Error() != expected { - t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) - } else if rtRply != utils.EmptyString { - t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected), utils.ToJSON(rtRply)) - } - -} - -/* func TestRatesRemoveRateProfileRate(t *testing.T) { engine.Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() @@ -1131,12 +1104,14 @@ func TestRatesRemoveRateProfileRate(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext1 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1146,12 +1121,14 @@ func TestRatesRemoveRateProfileRate(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) } ext2 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1197,7 +1174,6 @@ func TestRatesRemoveRateProfileRate(t *testing.T) { if !reflect.DeepEqual(utils.ToJSON(expected2), utils.ToJSON(&result)) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected2), utils.ToJSON(&result)) } - } func TestRatesRemoveRateProfileRateEmptyTenant(t *testing.T) { @@ -1209,12 +1185,14 @@ func TestRatesRemoveRateProfileRateEmptyTenant(t *testing.T) { dm := engine.NewDataManager(dataDB, nil, connMgr) admS := NewAdminSv1(cfg, dm, connMgr) ext1 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1224,12 +1202,14 @@ func TestRatesRemoveRateProfileRateEmptyTenant(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) } ext2 := &utils.APIRateProfile{ - ID: "2", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - ActivationTimes: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1275,7 +1255,6 @@ func TestRatesRemoveRateProfileRateEmptyTenant(t *testing.T) { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected2), utils.ToJSON(&result)) } } -*/ func TestRatesRemoveRateProfileRateError(t *testing.T) { engine.Cache.Clear(nil) @@ -1342,13 +1321,15 @@ func TestRatesSetRateProfileErrorSetLoadIDs(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1388,13 +1369,15 @@ func TestRatesSetRateProfileRatesErrorSetLoadIDs(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } @@ -1517,13 +1500,15 @@ func TestRatesSetRateProfileErrorCache(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, APIOpts: map[string]interface{}{ @@ -1571,13 +1556,15 @@ func TestRatesSetRateProfileRatesErrorCache(t *testing.T) { 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: "* * * * *", + RateProfile: &utils.RateProfile{ + ID: "2", + Tenant: "tenant", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, }, }, } diff --git a/dispatchers/rates_it_test.go b/dispatchers/rates_it_test.go index bdc24eed0..4344e2855 100644 --- a/dispatchers/rates_it_test.go +++ b/dispatchers/rates_it_test.go @@ -82,21 +82,31 @@ func testDspRPrfPing(t *testing.T) { func testDspRPrfCostForEvent(t *testing.T) { rPrf := &utils.APIRateProfile{ - ID: "DefaultRate", - Tenant: "cgrates.org", - FilterIDs: []string{"*string:~*req.Subject:1001"}, - Weights: ";10", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.12), - Unit: utils.Float64Pointer(float64(time.Minute)), - Increment: utils.Float64Pointer(float64(time.Minute)), + RateProfile: &utils.RateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(12, 1), + Unit: utils.NewDecimal(int64(time.Minute), 0), + Increment: utils.NewDecimal(int64(time.Minute), 0), + }, }, }, }, @@ -154,20 +164,30 @@ func testDspRPrfCostForEvent(t *testing.T) { func testDspRPrfCostForEventWithoutFilters(t *testing.T) { rPrf := &utils.APIRateProfile{ - ID: "ID_RP", - Tenant: "cgrates.org", - Weights: ";10", - Rates: map[string]*utils.APIRate{ - "RT_WEEK": { - ID: "RT_WEEK", - Weights: ";0", - ActivationTimes: "* * * * *", - IntervalRates: []*utils.APIIntervalRate{ - { - IntervalStart: "0", - RecurrentFee: utils.Float64Pointer(0.25), - Unit: utils.Float64Pointer(float64(time.Minute)), - Increment: utils.Float64Pointer(float64(time.Second)), + RateProfile: &utils.RateProfile{ + ID: "ID_RP", + Tenant: "cgrates.org", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weights: utils.DynamicWeights{ + { + Weight: 0, + }, + }, + ActivationTimes: "* * * * *", + IntervalRates: []*utils.IntervalRate{ + { + IntervalStart: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(25, 2), + Unit: utils.NewDecimal(int64(time.Minute), 0), + Increment: utils.NewDecimal(int64(time.Second), 0), + }, }, }, }, diff --git a/utils/librates.go b/utils/librates.go index 367c1d751..38510785e 100644 --- a/utils/librates.go +++ b/utils/librates.go @@ -455,47 +455,9 @@ func (rIcr *RateSIncrement) Cost(rts map[string]*IntervalRate) (cost *decimal.Bi // func CompressIntervals(rtIvls []*RateSInterval) { // } -// AsRateProfile converts APIRateProfile to RateProfile -func (ext *APIRateProfile) AsRateProfile() (rp *RateProfile, err error) { - rp = &RateProfile{ - Tenant: ext.Tenant, - ID: ext.ID, - FilterIDs: ext.FilterIDs, - MaxCostStrategy: ext.MaxCostStrategy, - } - if ext.Weights != EmptyString { - if rp.Weights, err = NewDynamicWeightsFromString(ext.Weights, ";", "&"); err != nil { - return nil, err - } - } - if ext.MinCost != nil { - rp.MinCost = NewDecimalFromFloat64(*ext.MinCost) - } - if ext.MaxCost != nil { - rp.MaxCost = NewDecimalFromFloat64(*ext.MaxCost) - } - if len(ext.Rates) != 0 { - rp.Rates = make(map[string]*Rate) - for key, extRate := range ext.Rates { - if rp.Rates[key], err = extRate.AsRate(); err != nil { - return - } - } - } - err = rp.Compile() - return -} - type APIRateProfile struct { - Tenant string - ID string - FilterIDs []string - Weights string - MinCost *float64 - MaxCost *float64 - MaxCostStrategy string - Rates map[string]*APIRate - APIOpts map[string]interface{} + *RateProfile + APIOpts map[string]interface{} } // AsRate converts APIRate to Rate diff --git a/utils/librates_test.go b/utils/librates_test.go index 22f6ac491..2365f14d7 100644 --- a/utils/librates_test.go +++ b/utils/librates_test.go @@ -198,9 +198,8 @@ func TestLibratesRunTimes(t *testing.T) { } } -/* func TestLibratesCorrectCost(t *testing.T) { - //CorrectCost does nothing + //CorrectCost does nothing in this case rPc := &RateProfileCost{ Cost: NewDecimal(1234, 3), MinCost: NewDecimal(1, 0), @@ -216,7 +215,7 @@ func TestLibratesCorrectCost(t *testing.T) { } rPc.CorrectCost(nil, "") - if !reflect.DeepEqual(rPc, expected) { + if !rPc.Equals(expected) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, rPc) } @@ -230,7 +229,7 @@ func TestLibratesCorrectCost(t *testing.T) { rPc.CorrectCost(IntPointer(2), MetaRoundingUp) - if !reflect.DeepEqual(rPc, expected) { + if !rPc.Equals(expected) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(rPc)) } @@ -244,7 +243,7 @@ func TestLibratesCorrectCost(t *testing.T) { rPc.Cost = NewDecimal(234, 2) rPc.CorrectCost(nil, "") - if !reflect.DeepEqual(rPc, expected) { + if !rPc.Equals(expected) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, rPc) } @@ -259,11 +258,10 @@ func TestLibratesCorrectCost(t *testing.T) { rPc.Cost = NewDecimal(12, 2) rPc.CorrectCost(nil, "") - if !reflect.DeepEqual(rPc, expected) { + if !rPc.Equals(expected) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, rPc) } } -*/ func TestRateProfileSort(t *testing.T) { minDecimal, err := NewDecimalFromUsage("1m") @@ -1003,104 +1001,6 @@ func TestRateSIntervalCompressEqualsCase3(t *testing.T) { } } -func TestLibratesAsRateProfile(t *testing.T) { - // Invalid DynamicWeights string - ext := &APIRateProfile{ - Weights: "testWeight", - } - rp := &RateProfile{ - Tenant: ext.Tenant, - ID: ext.ID, - FilterIDs: ext.FilterIDs, - MaxCostStrategy: ext.MaxCostStrategy, - } - - received, err := ext.AsRateProfile() - experr := "invalid DynamicWeight format for string " - - if err == nil || err.Error() != experr { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", experr, err) - } - - if received != nil { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", nil, received) - } - - // No changes - ext.Weights = EmptyString - - expected := rp - received, err = ext.AsRateProfile() - - if err != nil { - t.Errorf("\nExpected nil, got <%+v>", err) - } - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } - - // assign MinCost to rp - ext.MinCost = Float64Pointer(1) - - expected.MinCost = NewDecimal(1, 0) - received, err = ext.AsRateProfile() - - if err != nil { - t.Errorf("\nExpected nil, got <%+v>", err) - } - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } - - // assign MaxCost to rp - ext.MaxCost = Float64Pointer(2) - - expected.MaxCost = NewDecimal(2, 0) - received, err = ext.AsRateProfile() - - if err != nil { - t.Errorf("\nExpected nil, got <%+v>", err) - } - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } -} - -func TestLibratesAsRateProfileNon0Len(t *testing.T) { - id := "testID" - ext := &APIRateProfile{ - Rates: map[string]*APIRate{ - "testKey": { - ID: id, - Weights: "testWeight", - }, - }, - } - rp := &RateProfile{ - Tenant: ext.Tenant, - ID: ext.ID, - FilterIDs: ext.FilterIDs, - MaxCostStrategy: ext.MaxCostStrategy, - } - - expected := rp - expected.Rates = map[string]*Rate{ - "testKey": nil, - } - received, err := ext.AsRateProfile() - - if err.Error() != "invalid DynamicWeight format for string " { - t.Errorf("\nExpected nil, got <%+v>", err) - } - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%v>, \nReceived: <%v>", expected, received) - } -} - func TestRatesIntervalEquals(t *testing.T) { rtInt1 := &RateSInterval{ IntervalStart: NewDecimal(int64(10*time.Second), 0),