From f70bdc926d398f72a6b45b37032f633ccefc630f Mon Sep 17 00:00:00 2001 From: TeoV Date: Thu, 20 Aug 2020 16:02:57 +0300 Subject: [PATCH] When use APIerSv1.LoadRatingProfile instead of overwrite combine the RatingPlanActivations --- apier/v1/apier2_it_test.go | 117 +++++++++++++++++++++++++++++++++++++ engine/tpreader.go | 14 ++++- 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/apier/v1/apier2_it_test.go b/apier/v1/apier2_it_test.go index b94cc68cf..e259058c4 100644 --- a/apier/v1/apier2_it_test.go +++ b/apier/v1/apier2_it_test.go @@ -62,6 +62,7 @@ var ( testAPIerSetActionPlanDfltTime, testAPIerLoadRatingPlan, testAPIerLoadRatingPlan2, + testAPIerLoadRatingProfile, testAPIerKillEngine, } ) @@ -507,6 +508,122 @@ func testAPIerLoadRatingPlan2(t *testing.T) { } +func testAPIerLoadRatingProfile(t *testing.T) { + var reply string + rpf := &utils.TPRatingProfile{ + TPid: "TP_SAMPLE", + LoadId: "TP_SAMPLE", + Tenant: "cgrates.org", + Category: "call", + Subject: utils.META_ANY, + RatingPlanActivations: []*utils.TPRatingActivation{{ + ActivationTime: "2012-01-01T00:00:00Z", + RatingPlanId: "RPl_SAMPLE_RATING_PLAN", + FallbackSubjects: utils.EmptyString, + }}, + } + // add a TPRatingProfile + if err := apierRPC.Call(utils.APIerSv1SetTPRatingProfile, rpf, &reply); err != nil { + t.Error(err) + } + // load the TPRatingProfile into dataDB + argsRPrf := &utils.TPRatingProfile{ + TPid: "TP_SAMPLE", LoadId: "TP_SAMPLE", + Tenant: "cgrates.org", Category: "call", Subject: "*any"} + if err := apierRPC.Call(utils.APIerSv1LoadRatingProfile, argsRPrf, &reply); err != nil { + t.Error(err) + } + + // verify if was added correctly + var rpl engine.RatingProfile + attrGetRatingPlan := &utils.AttrGetRatingProfile{ + Tenant: "cgrates.org", Category: "call", Subject: utils.META_ANY} + actTime, err := utils.ParseTimeDetectLayout("2012-01-01T00:00:00Z", utils.EmptyString) + if err != nil { + t.Error(err) + } + expected := engine.RatingProfile{ + Id: "*out:cgrates.org:call:*any", + RatingPlanActivations: engine.RatingPlanActivations{ + { + ActivationTime: actTime, + RatingPlanId: "RPl_SAMPLE_RATING_PLAN", + }, + }, + } + if err := apierRPC.Call(utils.APIerSv1GetRatingProfile, attrGetRatingPlan, &rpl); err != nil { + t.Errorf("Got error on APIerSv1.GetRatingProfile: %+v", err) + } else if !reflect.DeepEqual(expected, rpl) { + t.Errorf("Calling APIerSv1.GetRatingProfile expected: %+v, received: %+v", utils.ToJSON(expected), utils.ToJSON(rpl)) + } + + // add new RatingPlan + rp := &utils.TPRatingPlan{TPid: "TP_SAMPLE", ID: "RPl_SAMPLE_RATING_PLAN2", + RatingPlanBindings: []*utils.TPRatingPlanBinding{ + {DestinationRatesId: "DR_SAMPLE_DESTINATION_RATE", TimingId: utils.META_ANY, + Weight: 10}, + }} + + if err := apierRPC.Call(utils.APIerSv1SetTPRatingPlan, rp, &reply); err != nil { + t.Error("Got error on APIerSv1.SetTPRatingPlan: ", err.Error()) + } else if reply != utils.OK { + t.Error("Unexpected reply received when calling APIerSv1.SetTPRatingPlan: ", reply) + } + + if err := apierRPC.Call(utils.APIerSv1LoadRatingPlan, &AttrLoadRatingPlan{TPid: "TP_SAMPLE", RatingPlanId: "RPl_SAMPLE_RATING_PLAN2"}, &reply); err != nil { + t.Error("Got error on APIerSv1.LoadRatingPlan: ", err.Error()) + } else if reply != utils.OK { + t.Error("Calling APIerSv1.LoadRatingPlan got reply: ", reply) + } + + // overwrite the existing TPRatingProfile with a new RatingPlanActivations + rpf = &utils.TPRatingProfile{ + TPid: "TP_SAMPLE", + LoadId: "TP_SAMPLE", + Tenant: "cgrates.org", + Category: "call", + Subject: utils.META_ANY, + RatingPlanActivations: []*utils.TPRatingActivation{{ + ActivationTime: "2012-02-02T00:00:00Z", + RatingPlanId: "RPl_SAMPLE_RATING_PLAN2", + FallbackSubjects: utils.EmptyString, + }}, + } + + if err := apierRPC.Call(utils.APIerSv1SetTPRatingProfile, rpf, &reply); err != nil { + t.Error(err) + } + + // load the TPRatingProfile into dataDB + // because the RatingProfile exists the RatingPlanActivations will be merged + if err := apierRPC.Call(utils.APIerSv1LoadRatingProfile, argsRPrf, &reply); err != nil { + t.Error(err) + } + actTime2, err := utils.ParseTimeDetectLayout("2012-02-02T00:00:00Z", utils.EmptyString) + if err != nil { + t.Error(err) + } + expected = engine.RatingProfile{ + Id: "*out:cgrates.org:call:*any", + RatingPlanActivations: engine.RatingPlanActivations{ + { + ActivationTime: actTime, + RatingPlanId: "RPl_SAMPLE_RATING_PLAN", + }, + { + ActivationTime: actTime2, + RatingPlanId: "RPl_SAMPLE_RATING_PLAN2", + }, + }, + } + if err := apierRPC.Call(utils.APIerSv1GetRatingProfile, attrGetRatingPlan, &rpl); err != nil { + t.Errorf("Got error on APIerSv1.GetRatingProfile: %+v", err) + } else if !reflect.DeepEqual(expected, rpl) { + t.Errorf("Calling APIerSv1.GetRatingProfile expected: %+v, received: %+v", utils.ToJSON(expected), utils.ToJSON(rpl)) + } + +} + func testAPIerKillEngine(t *testing.T) { if err := engine.KillEngine(*waitRater); err != nil { t.Error(err) diff --git a/engine/tpreader.go b/engine/tpreader.go index a996368b4..2fb87b8d4 100644 --- a/engine/tpreader.go +++ b/engine/tpreader.go @@ -339,7 +339,19 @@ func (tpr *TpReader) LoadRatingProfilesFiltered(qriedRpf *utils.TPRatingProfile) return err } for _, tpRpf := range rpfs { - resultRatingProfile = &RatingProfile{Id: tpRpf.KeyId()} + if tpr.dm.dataDB != nil { // check if we have a connection with dataDB and check if RatingProfile exists + if resultRatingProfile, err = tpr.dm.GetRatingProfile(tpRpf.KeyId(), false, utils.NonTransactional); err != nil { + if err == utils.ErrNotFound { + resultRatingProfile = &RatingProfile{Id: tpRpf.KeyId()} + err = nil + } else { + return err + } + } + } else { + resultRatingProfile = &RatingProfile{Id: tpRpf.KeyId()} + } + for _, tpRa := range tpRpf.RatingPlanActivations { at, err := utils.ParseTimeDetectLayout(tpRa.ActivationTime, tpr.timezone) if err != nil {