mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 06:09:53 +05:00
Updated RemoveTPRatingProfile API. Closes #2062
This commit is contained in:
committed by
Dan Christian Bogos
parent
9d5a6035ee
commit
72dbb523d0
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user