diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 3342ec306..cfa8587e6 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -1301,7 +1301,7 @@ func (dR *DispatcherRateSv1) Ping(args *utils.CGREventWithOpts, reply *string) e } func (dR *DispatcherRateSv1) CostForEvent(args *utils.ArgsCostForEvent, rpCost *engine.RateProfileCost) error { - return dR.dR.RateSV1CostForEvent(args, rpCost) + return dR.dR.RateSv1CostForEvent(args, rpCost) } func NewDispatcherActionSv1(dps *dispatchers.DispatcherService) *DispatcherActionSv1 { diff --git a/dispatchers/rates.go b/dispatchers/rates.go index 73016fea7..2f148391e 100644 --- a/dispatchers/rates.go +++ b/dispatchers/rates.go @@ -37,7 +37,7 @@ func (dS *DispatcherService) RateSv1Ping(args *utils.CGREventWithOpts, rpl *stri return dS.Dispatch(args, utils.RateS, utils.RateSv1Ping, args, rpl) } -func (dS *DispatcherService) RateSV1CostForEvent(args *utils.ArgsCostForEvent, rpCost *engine.RateProfileCost) (err error) { +func (dS *DispatcherService) RateSv1CostForEvent(args *utils.ArgsCostForEvent, rpCost *engine.RateProfileCost) (err error) { if args == nil { args = new(utils.ArgsCostForEvent) } diff --git a/dispatchers/rates_it_test.go b/dispatchers/rates_it_test.go index b2a35458e..18a88d233 100644 --- a/dispatchers/rates_it_test.go +++ b/dispatchers/rates_it_test.go @@ -21,13 +21,19 @@ along with this program. If not, see package dispatchers import ( + "reflect" "testing" + "time" + + "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) var sTestsDspRPrf = []func(t *testing.T){ testDspRPrfPing, + testDspRPrfCostForEvent, + testDspRPrfCostForEventWithoutFilters, } //Test start here @@ -77,3 +83,142 @@ func testDspRPrfPing(t *testing.T) { t.Errorf("Received: %s", reply) } } + +func testDspRPrfCostForEvent(t *testing.T) { + rPrf := &engine.RateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Weight: 10, + Rates: map[string]*engine.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weight: 0, + ActivationTimes: "* * * * *", + IntervalRates: []*engine.IntervalRate{ + { + IntervalStart: 0, + RecurrentFee: 0.12, + Unit: time.Minute, + Increment: time.Minute, + }, + }, + }, + }, + } + var reply string + if err := allEngine.RPC.Call(utils.APIerSv1SetRateProfile, rPrf, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expected OK, received %+v", reply) + } + var rply *engine.RateProfile + if err := allEngine.RPC.Call(utils.APIerSv1GetRateProfile, &utils.TenantID{ + Tenant: "cgrates.org", + ID: "DefaultRate", + }, &rply); err != nil { + t.Error(err) + } + exp := &engine.RateProfileCost{ + ID: "DefaultRate", + Cost: 0.12, + RateSIntervals: []*engine.RateSInterval{{ + UsageStart: 0, + Increments: []*engine.RateSIncrement{{ + UsageStart: 0, + Usage: time.Minute, + Rate: rPrf.Rates["RT_WEEK"], + IntervalRateIndex: 0, + CompressFactor: 1, + }}, + CompressFactor: 1, + }}, + } + + var rpCost *engine.RateProfileCost + if err := dispEngine.RPC.Call(utils.RateSv1CostForEvent, &utils.ArgsCostForEvent{ + CGREventWithOpts: &utils.CGREventWithOpts{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "DefaultRate", + Event: map[string]interface{}{ + utils.Subject: "1001", + }, + }, + Opts: map[string]interface{}{ + utils.OptsAPIKey: "rPrf12345", + }}}, &rpCost); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rpCost, exp) { + t.Errorf("Expected %+v, received %+v", exp, rpCost) + } +} + +func testDspRPrfCostForEventWithoutFilters(t *testing.T) { + rPrf := &engine.RateProfile{ + ID: "ID_RP", + Tenant: "cgrates.org", + Weight: 10, + Rates: map[string]*engine.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + Weight: 0, + ActivationTimes: "* * * * *", + IntervalRates: []*engine.IntervalRate{ + { + IntervalStart: 0, + RecurrentFee: 0.25, + Unit: time.Minute, + Increment: time.Second, + }, + }, + }, + }, + } + var reply string + if err := allEngine.RPC.Call(utils.APIerSv1SetRateProfile, rPrf, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expected OK, received %+v", reply) + } + var rply *engine.RateProfile + if err := allEngine.RPC.Call(utils.APIerSv1GetRateProfile, &utils.TenantID{ + Tenant: "cgrates.org", + ID: "ID_RP", + }, &rply); err != nil { + t.Error(err) + } + exp := &engine.RateProfileCost{ + ID: "ID_RP", + Cost: 0.25, + RateSIntervals: []*engine.RateSInterval{{ + UsageStart: 0, + Increments: []*engine.RateSIncrement{{ + UsageStart: 0, + Usage: time.Minute, + Rate: rPrf.Rates["RT_WEEK"], + IntervalRateIndex: 0, + CompressFactor: 60, + }}, + CompressFactor: 1, + }}, + } + + var rpCost *engine.RateProfileCost + if err := dispEngine.RPC.Call(utils.RateSv1CostForEvent, &utils.ArgsCostForEvent{ + CGREventWithOpts: &utils.CGREventWithOpts{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "EVENT_RATE", + Event: map[string]interface{}{ + utils.Subject: "1002", + }, + }, + Opts: map[string]interface{}{ + utils.OptsAPIKey: "rPrf12345", + }}}, &rpCost); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rpCost, exp) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(exp), utils.ToJSON(rpCost)) + } +}