diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 0adf44711..a8de8efd7 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -406,17 +406,16 @@ func (apiv1 *APIerSv1) SetRatingProfile(attrs utils.AttrSetRatingProfile, reply return fmt.Errorf("%s:RatingPlanActivation:%v", utils.ErrMandatoryIeMissing.Error(), missing) } } - tpRpf := utils.TPRatingProfile{Tenant: attrs.Tenant, - Category: attrs.Category, Subject: attrs.Subject} - keyId := tpRpf.KeyId() + keyID := utils.ConcatenatedKey(utils.META_OUT, + attrs.Tenant, attrs.Category, attrs.Subject) var rpfl *engine.RatingProfile if !attrs.Overwrite { - if rpfl, err = apiv1.DataManager.GetRatingProfile(keyId, false, utils.NonTransactional); err != nil && err != utils.ErrNotFound { + if rpfl, err = apiv1.DataManager.GetRatingProfile(keyID, false, utils.NonTransactional); err != nil && err != utils.ErrNotFound { return utils.NewErrServerError(err) } } if rpfl == nil { - rpfl = &engine.RatingProfile{Id: keyId, RatingPlanActivations: make(engine.RatingPlanActivations, 0)} + rpfl = &engine.RatingProfile{Id: keyID, RatingPlanActivations: make(engine.RatingPlanActivations, 0)} } for _, ra := range attrs.RatingPlanActivations { at, err := utils.ParseTimeDetectLayout(ra.ActivationTime, @@ -434,8 +433,8 @@ func (apiv1 *APIerSv1) SetRatingProfile(attrs utils.AttrSetRatingProfile, reply &engine.RatingPlanActivation{ ActivationTime: at, RatingPlanId: ra.RatingPlanId, - FallbackKeys: utils.FallbackSubjKeys(tpRpf.Tenant, - tpRpf.Category, ra.FallbackSubjects)}) + FallbackKeys: utils.FallbackSubjKeys(attrs.Tenant, + attrs.Category, ra.FallbackSubjects)}) } if err := apiv1.DataManager.SetRatingProfile(rpfl, utils.NonTransactional); err != nil { return utils.NewErrServerError(err) diff --git a/apier/v1/apier2_it_test.go b/apier/v1/apier2_it_test.go index dac3917e8..5d35c8888 100644 --- a/apier/v1/apier2_it_test.go +++ b/apier/v1/apier2_it_test.go @@ -300,20 +300,17 @@ func testAPIerGetActionPlanIDs(t *testing.T) { func testAPIerGetRatingPlanIDs(t *testing.T) { var reply []string - expected := []string{"RP_1002_LOW", "RP_1003", "RP_1001", "RP_SMS", "RP_1002"} + expected := []string{"RP_1002_LOW", "RP_1003", "RP_1001", "RP_MMS", "RP_SMS", "RP_1002"} if err := apierRPC.Call(utils.APIerSv1GetRatingPlanIDs, utils.TenantArgWithPaginator{TenantArg: utils.TenantArg{Tenant: "cgrates.org"}}, &reply); err != nil { t.Error(err) - } else if len(reply) != 5 { - t.Errorf("Expected: 5 , received: <%+v>", len(reply)) - } else { - sort.Strings(reply) - sort.Strings(expected) - if !reflect.DeepEqual(reply, expected) { - t.Errorf("Expected: <%+v> , received: <%+v>", expected, reply) + } + sort.Strings(reply) + sort.Strings(expected) + if !reflect.DeepEqual(reply, expected) { + t.Errorf("Expected: <%+v> , received: <%+v>", utils.ToJSON(expected), utils.ToJSON(reply)) - } } } diff --git a/apier/v1/apier_it_test.go b/apier/v1/apier_it_test.go index b7e404823..eb4a31760 100644 --- a/apier/v1/apier_it_test.go +++ b/apier/v1/apier_it_test.go @@ -526,13 +526,13 @@ func testApierTPRatingProfile(t *testing.T) { } // Test get var rplyRpf *utils.TPRatingProfile - if err := rater.Call(utils.APIerSv1GetTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: rpfTst.GetId()}, &rplyRpf); err != nil { + if err := rater.Call(utils.APIerSv1GetTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: utils.ConcatenatedKey(rpfTst.LoadId, rpfTst.Tenant, rpfTst.Category, rpfTst.Subject)}, &rplyRpf); err != nil { t.Error("Calling APIerSv1.GetTPRatingProfiles, got error: ", err.Error()) } else if !reflect.DeepEqual(rpfTst, rplyRpf) { t.Errorf("Calling APIerSv1.GetTPRatingProfiles expected: %v, received: %v", rpfTst, rplyRpf) } // Test remove - if err := rater.Call(utils.APIerSv1RemoveTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: rpfTst.GetId()}, &reply); err != nil { + if err := rater.Call(utils.APIerSv1RemoveTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: utils.ConcatenatedKey(rpfTst.LoadId, rpfTst.Tenant, rpfTst.Category, rpfTst.Subject)}, &reply); err != nil { t.Error("Calling APIerSv1.RemoveTPRatingProfile, got error: ", err.Error()) } else if reply != utils.OK { t.Error("Calling APIerSv1.RemoveTPRatingProfile received: ", reply) diff --git a/apier/v1/config_it_test.go b/apier/v1/config_it_test.go index 89d0b725f..2f0a8ffc3 100644 --- a/apier/v1/config_it_test.go +++ b/apier/v1/config_it_test.go @@ -141,6 +141,7 @@ func testConfigSReloadConfigFromJSONSessionS(t *testing.T) { "ReplicationConns": []interface{}{}, "MaxCallDuration": float64(3 * time.Hour), + "MinDurLowBalance": 0., "SessionIndexes": map[string]interface{}{"OriginID": true}, "ClientProtocol": 1., "TerminateAttempts": 5., @@ -161,7 +162,7 @@ func testConfigSReloadConfigFromJSONSessionS(t *testing.T) { }, &rpl); err != nil { t.Error(err) } else if !reflect.DeepEqual(exp, rpl) { - t.Errorf("Expected %+v , received: %+v ", exp, rpl) + t.Errorf("Expected %+v , received: %+v ", utils.ToJSON(exp), utils.ToJSON(rpl)) } } diff --git a/apier/v1/tpratingprofiles.go b/apier/v1/tpratingprofiles.go index 676c7bb4b..1d4a0ad7f 100644 --- a/apier/v1/tpratingprofiles.go +++ b/apier/v1/tpratingprofiles.go @@ -21,7 +21,6 @@ package v1 // This file deals with tp_rate_profiles management over APIs import ( - "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) @@ -91,7 +90,7 @@ func (api *APIerSv1) GetTPRatingProfile(attrs AttrGetTPRatingProfile, reply *uti return utils.NewErrMandatoryIeMissing(missing...) } tmpRpf := &utils.TPRatingProfile{TPid: attrs.TPid} - if err := tmpRpf.SetRatingProfilesId(attrs.RatingProfileID); err != nil { + if err := tmpRpf.SetRatingProfileID(attrs.RatingProfileID); err != nil { return err } rpfs, err := api.StorDb.GetTPRatingProfiles(tmpRpf) @@ -134,13 +133,13 @@ func (api *APIerSv1) RemoveTPRatingProfile(attrs AttrGetTPRatingProfile, reply * if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RatingProfileID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - tmpRpf := engine.TpRatingProfile{} - if err = tmpRpf.SetRatingProfileId(attrs.RatingProfileID); err != nil { + tmpRpf := new(utils.TPRatingProfile) + if err = tmpRpf.SetRatingProfileID(attrs.RatingProfileID); err != nil { return } err = api.StorDb.RemTpData(utils.TBLTPRateProfiles, attrs.TPid, map[string]string{ - "loadid": tmpRpf.Loadid, + "loadid": tmpRpf.LoadId, "tenant": tmpRpf.Tenant, "category": tmpRpf.Category, "subject": tmpRpf.Subject, diff --git a/apier/v1/tpratingprofiles_it_test.go b/apier/v1/tpratingprofiles_it_test.go index 9c170fb5a..d8a1ac15a 100644 --- a/apier/v1/tpratingprofiles_it_test.go +++ b/apier/v1/tpratingprofiles_it_test.go @@ -41,7 +41,7 @@ var ( tpRatingProfile *utils.TPRatingProfile tpRatingProfileDelay int tpRatingProfileConfigDIR string //run tests for specific configuration - tpRatingProfileID = "RPrf:*out:Tenant1:Category:Subject" + tpRatingProfileID = "RPrf:Tenant1:Category:Subject" ) var sTestsTPRatingProfiles = []func(t *testing.T){ @@ -269,7 +269,7 @@ func testTPRatingProfilesGetTPRatingProfileIds(t *testing.T) { func testTPRatingProfilesRemoveTPRatingProfile(t *testing.T) { var resp string if err := tpRatingProfileRPC.Call(utils.APIerSv1RemoveTPRatingProfile, - &AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileID: tpRatingProfile.GetId()}, &resp); err != nil { + &AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileID: utils.ConcatenatedKey(tpRatingProfile.LoadId, tpRatingProfile.Tenant, tpRatingProfile.Category, tpRatingProfile.Subject)}, &resp); err != nil { t.Error(err) } else if resp != utils.OK { t.Error("Unexpected reply returned", resp) diff --git a/apier/v2/apier.go b/apier/v2/apier.go index 5ddc304f6..74c8f5d3c 100644 --- a/apier/v2/apier.go +++ b/apier/v2/apier.go @@ -54,14 +54,13 @@ func (apiv2 *APIerSv2) LoadRatingProfile(attrs AttrLoadRatingProfile, reply *str if len(attrs.TPid) == 0 { return utils.NewErrMandatoryIeMissing("TPid") } - tpRpf := &utils.TPRatingProfile{TPid: attrs.TPid} dbReader, err := engine.NewTpReader(apiv2.DataManager.DataDB(), apiv2.StorDb, attrs.TPid, apiv2.Config.GeneralCfg().DefaultTimezone, apiv2.Config.ApierCfg().CachesConns, apiv2.Config.ApierCfg().SchedulerConns) if err != nil { return utils.NewErrServerError(err) } - if err := dbReader.LoadRatingProfilesFiltered(tpRpf); err != nil { + if err := dbReader.LoadRatingProfilesFiltered(&utils.TPRatingProfile{TPid: attrs.TPid}); err != nil { return utils.NewErrServerError(err) } *reply = utils.OK diff --git a/engine/models.go b/engine/models.go index 49befba33..d86933f39 100644 --- a/engine/models.go +++ b/engine/models.go @@ -97,23 +97,6 @@ type TpRatingProfile struct { CreatedAt time.Time } -func (rpf *TpRatingProfile) SetRatingProfileId(id string) error { - ids := strings.Split(id, utils.CONCATENATED_KEY_SEP) - if len(ids) != 5 { - return fmt.Errorf("Wrong TP Rating Profile Id: %s", id) - } - rpf.Loadid = ids[0] - rpf.Tenant = ids[2] - rpf.Category = ids[3] - rpf.Subject = ids[4] - return nil -} - -func (rpf *TpRatingProfile) GetRatingProfileId() string { - return utils.ConcatenatedKey(rpf.Loadid, utils.META_OUT, - rpf.Tenant, rpf.Category, rpf.Subject) -} - type TpAction struct { Id int64 Tpid string diff --git a/utils/apitpdata.go b/utils/apitpdata.go index c3a4304cb..0cb49e767 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -240,15 +240,15 @@ func (rpf *TPRatingProfile) GetId() string { rpf.Tenant, rpf.Category, rpf.Subject) } -func (rpf *TPRatingProfile) SetRatingProfilesId(id string) error { +func (rpf *TPRatingProfile) SetRatingProfileID(id string) error { ids := strings.Split(id, CONCATENATED_KEY_SEP) - if len(ids) != 5 { + if len(ids) != 4 { return fmt.Errorf("Wrong TPRatingProfileId: %s", id) } rpf.LoadId = ids[0] - rpf.Tenant = ids[2] - rpf.Category = ids[3] - rpf.Subject = ids[4] + rpf.Tenant = ids[1] + rpf.Category = ids[2] + rpf.Subject = ids[3] return nil } diff --git a/utils/apitpdata_test.go b/utils/apitpdata_test.go index a80b6fd6b..0828267eb 100644 --- a/utils/apitpdata_test.go +++ b/utils/apitpdata_test.go @@ -251,13 +251,13 @@ func TestTPRatingProfileKeys(t *testing.T) { func TestTPRatingProfileSetRatingProfilesId(t *testing.T) { //empty check tpRatingProfile := new(TPRatingProfile) - tpRatingProfile.SetRatingProfilesId("") + tpRatingProfile.SetRatingProfileID("") eOut := new(TPRatingProfile) if !reflect.DeepEqual(eOut, tpRatingProfile) { t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile) } //test check - tpRatingProfile.SetRatingProfilesId("1:2:3:4:5") + tpRatingProfile.SetRatingProfileID("1:3:4:5") eOut.LoadId = "1" eOut.Tenant = "3" eOut.Category = "4" @@ -266,7 +266,7 @@ func TestTPRatingProfileSetRatingProfilesId(t *testing.T) { t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile) } //wrong TPRatingProfile sent - err := tpRatingProfile.SetRatingProfilesId("1:2:3:4:5:6") + err := tpRatingProfile.SetRatingProfileID("1:2:3:4:5:6") if err == nil { t.Error("Wrong TPRatingProfileId sent and no error received") }