From 5b10f63c949f9fae7115f3b7ea232d76afc9952c Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 5 Mar 2014 19:10:27 +0100 Subject: [PATCH] Reload cache on SetRatingProfile API --- apier/apier.go | 6 +++- apier/apier_local_test.go | 61 +++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/apier/apier.go b/apier/apier.go index dafe360b3..cb7f075d2 100644 --- a/apier/apier.go +++ b/apier/apier.go @@ -200,7 +200,7 @@ type AttrSetRatingProfile struct { Direction string // Traffic direction, OUT is the only one supported for now Subject string // Rating subject, usually the same as account Overwrite bool // Overwrite if exists - RatingPlanActivations []*utils.TPRatingActivation // Activate rate profiles at specific time + RatingPlanActivations []*utils.TPRatingActivation // Activate rating plans at specific time } // Sets a specific rating profile working with data directly in the RatingDb without involving storDb @@ -239,6 +239,10 @@ func (self *ApierV1) SetRatingProfile(attrs AttrSetRatingProfile, reply *string) if err := self.RatingDb.SetRatingProfile(rpfl); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } + //Automatic cache of the newly inserted rating profile + if err := self.RatingDb.CacheRating(nil, nil, []string{engine.RATING_PROFILE_PREFIX + keyId}, nil); err != nil { + return err + } *reply = OK return nil } diff --git a/apier/apier_local_test.go b/apier/apier_local_test.go index c33835a01..2bdf298ca 100644 --- a/apier/apier_local_test.go +++ b/apier/apier_local_test.go @@ -738,6 +738,48 @@ func TestApierLoadRatingPlan(t *testing.T) { } } +// Test here SetRatingProfile +func TestApierSetRatingProfile(t *testing.T) { + if !*testLocal { + return + } + reply := "" + rpa := &utils.TPRatingActivation{ActivationTime: "2012-01-01T00:00:00Z", RatingPlanId: "RETAIL1", FallbackSubjects: "dan2"} + rpf := &AttrSetRatingProfile{Tenant: "cgrates.org", TOR: "call", Direction: "*out", Subject: "dan", RatingPlanActivations: []*utils.TPRatingActivation{rpa}} + if err := rater.Call("ApierV1.SetRatingProfile", rpf, &reply); err != nil { + t.Error("Got error on ApierV1.SetRatingProfile: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling ApierV1.SetRatingProfile got reply: ", reply) + } + // Calling the second time should raise EXISTS + if err := rater.Call("ApierV1.SetRatingProfile", rpf, &reply); err == nil || err.Error() != "EXISTS" { + t.Error("Unexpected result on duplication: ", err.Error()) + } + time.Sleep(10 * time.Millisecond) // Give time for cache reload + // Make sure rates were loaded for account dan + // Test here ResponderGetCost + tStart, _ := utils.ParseDate("2013-08-07T17:30:00Z") + tEnd, _ := utils.ParseDate("2013-08-07T17:31:30Z") + cd := engine.CallDescriptor{ + Direction: "*out", + TOR: "call", + Tenant: "cgrates.org", + Subject: "dan", + Account: "dan", + Destination: "+4917621621391", + CallDuration: 90, + TimeStart: tStart, + TimeEnd: tEnd, + } + var cc engine.CallCost + // Simple test that command is executed without errors + if err := rater.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } +} + // Test here LoadRatingProfile func TestApierLoadRatingProfile(t *testing.T) { if !*testLocal { @@ -752,25 +794,6 @@ func TestApierLoadRatingProfile(t *testing.T) { } } -// Test here SetRatingProfile -func TestApierSetRatingProfile(t *testing.T) { - if !*testLocal { - return - } - reply := "" - rpa := &utils.TPRatingActivation{ActivationTime: "2012-01-01T00:00:00Z", RatingPlanId: "RETAIL1", FallbackSubjects: "dan2;*any"} - rpf := &AttrSetRatingProfile{Tenant: "cgrates.org", TOR: "call", Direction: "*out", Subject: "dan", RatingPlanActivations: []*utils.TPRatingActivation{rpa}} - if err := rater.Call("ApierV1.SetRatingProfile", rpf, &reply); err != nil { - t.Error("Got error on ApierV1.SetRatingProfile: ", err.Error()) - } else if reply != "OK" { - t.Error("Calling ApierV1.SetRatingProfile got reply: ", reply) - } - // Calling the second time should raise EXISTS - if err := rater.Call("ApierV1.SetRatingProfile", rpf, &reply); err == nil || err.Error() != "EXISTS" { - t.Error("Unexpected result on duplication: ", err.Error()) - } -} - // Test here LoadAccountActions func TestApierLoadAccountActions(t *testing.T) { if !*testLocal {