From b0e478676128b334a5f0845106198adb0b83c60f Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 25 Feb 2020 09:51:49 +0200 Subject: [PATCH] In case usage missing from Event for suppliers use 1 Minute as default --- apier/v1/suppliers_it_test.go | 55 +++++++++++++++++++++++++++++++++++ engine/suppliers.go | 7 ++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/apier/v1/suppliers_it_test.go b/apier/v1/suppliers_it_test.go index a3f1fa8a8..0d5212fa1 100644 --- a/apier/v1/suppliers_it_test.go +++ b/apier/v1/suppliers_it_test.go @@ -48,6 +48,7 @@ var ( testV1SplSFromFolder, testV1SplSGetWeightSuppliers, testV1SplSGetLeastCostSuppliers, + testV1SplSGetLeastCostSuppliersWithoutUsage, testV1SplSGetLeastCostSuppliersWithMaxCost, testV1SplSGetLeastCostSuppliersWithMaxCost2, testV1SplSGetLeastCostSuppliersWithMaxCostNotFound, @@ -233,6 +234,60 @@ func testV1SplSGetLeastCostSuppliers(t *testing.T) { } } +func testV1SplSGetLeastCostSuppliersWithoutUsage(t *testing.T) { + ev := &engine.ArgsGetSuppliers{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testV1SplSGetLeastCostSuppliers", + Event: map[string]interface{}{ + utils.Account: "1003", + utils.Subject: "1003", + utils.Destination: "1002", + utils.SetupTime: time.Date(2017, 12, 1, 14, 25, 0, 0, time.UTC), + }, + }, + } + eSpls := engine.SortedSuppliers{ + ProfileID: "SPL_LEASTCOST_1", + Sorting: utils.MetaLC, + Count: 3, + SortedSuppliers: []*engine.SortedSupplier{ + { + SupplierID: "supplier3", + SortingData: map[string]interface{}{ + utils.Cost: 0.0102, + utils.RatingPlanID: "RP_SPECIAL_1002", + utils.Weight: 15.0, + }, + }, + { + SupplierID: "supplier1", + SortingData: map[string]interface{}{ + utils.Cost: 0.0102, + utils.RatingPlanID: "RP_SPECIAL_1002", + utils.Weight: 10.0, + }, + }, + { + SupplierID: "supplier2", + SortingData: map[string]interface{}{ + utils.Cost: 1.2, + utils.RatingPlanID: "RP_RETAIL1", + utils.Weight: 20.0, + }, + }, + }, + } + var suplsReply engine.SortedSuppliers + if err := splSv1Rpc.Call(utils.SupplierSv1GetSuppliers, + ev, &suplsReply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eSpls, suplsReply) { + t.Errorf("Expecting: %s, received: %s", + utils.ToJSON(eSpls), utils.ToJSON(suplsReply)) + } +} + func testV1SplSGetLeastCostSuppliersWithMaxCost(t *testing.T) { ev := &engine.ArgsGetSuppliers{ MaxCost: "0.30", diff --git a/engine/suppliers.go b/engine/suppliers.go index 3648aa109..bef8b9b2a 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -227,7 +227,12 @@ func (spS *SupplierService) costForEvent(ev *utils.CGREvent, } var usage time.Duration if usage, err = ev.FieldAsDuration(utils.Usage); err != nil { - return + if err != utils.ErrNotFound { + return + } + // in case usage is missing from event we decide to use 1 minute as default + usage = time.Duration(1 * time.Minute) + err = nil } for _, anctID := range acntIDs { cd := &CallDescriptor{