From 96fa9f7991883ae6f97a5178fe848979e80db274 Mon Sep 17 00:00:00 2001 From: adragusin Date: Mon, 17 Feb 2020 16:16:04 +0200 Subject: [PATCH] Added test for ResourceSv1GetResource --- data/tariffplans/dispatchers/Attributes.csv | 2 +- data/tariffplans/dispatchers_gob/Attributes.csv | 2 +- dispatchers/resources.go | 4 ++-- dispatchers/resources_it_test.go | 16 ++++++++++++++++ engine/eventcost_test.go | 4 ++-- engine/suppliers.go | 5 ++--- utils/apitpdata.go | 4 ++-- utils/consts.go | 1 - 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index dfd14fb5e..819bc67d9 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -8,7 +8,7 @@ cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:~*req.APIKey:chrg12345,,,*req.APIMe cgrates.org,ATTR_API_THR_AUTH,*auth,*string:~*req.APIKey:thr12345,,,*req.APIMethods,*constant,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent&ThresholdSv1.GetThreshold&ThresholdSv1.GetThresholdIDs,false,20 cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~*req.APIKey:sup12345,,,*req.APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:~*req.APIKey:stat12345,,,*req.APIMethods,*constant,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics,false,20 -cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources,false,20 +cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources&ResourceSv1.GetResource,false,20 cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent,false,20 cgrates.org,ATTR_API_RSP_AUTH,*auth,*string:~*req.APIKey:rsp12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&Responder.Shutdown&Responder.Ping,false,20 cgrates.org,ATTR_API_CHC_AUTH,*auth,*string:~*req.APIKey:chc12345,,,*req.APIMethods,*constant,CacheSv1.Ping&CacheSv1.GetCacheStats&CacheSv1.LoadCache&CacheSv1.PrecacheStatus&CacheSv1.GetItemIDs&CacheSv1.HasItem&CacheSv1.GetItemExpiryTime&CacheSv1.ReloadCache&CacheSv1.RemoveItem&CacheSv1.FlushCache&CacheSv1.Clear,false,20 diff --git a/data/tariffplans/dispatchers_gob/Attributes.csv b/data/tariffplans/dispatchers_gob/Attributes.csv index 9d1a47e62..cfc93aded 100644 --- a/data/tariffplans/dispatchers_gob/Attributes.csv +++ b/data/tariffplans/dispatchers_gob/Attributes.csv @@ -8,7 +8,7 @@ cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:~*req.APIKey:chrg12345,,,*req.APIMe cgrates.org,ATTR_API_THR_AUTH,*auth,*string:~*req.APIKey:thr12345,,,*req.APIMethods,*constant,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent&ThresholdSv1.GetThreshold&ThresholdSv1.GetThresholdIDs,false,20 cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~*req.APIKey:sup12345,,,*req.APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:~*req.APIKey:stat12345,,,*req.APIMethods,*constant,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics,false,20 -cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources,false,20 +cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources&ResourceSv1.GetResource,false,20 cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent,false,20 cgrates.org,ATTR_API_RSP_AUTH,*auth,*string:~*req.APIKey:rsp12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&Responder.Shutdown&Responder.Ping,false,20 cgrates.org,ATTR_API_CHC_AUTH,*auth,*string:~*req.APIKey:chc12345,,,*req.APIMethods,*constant,CacheSv1.Ping&CacheSv1.GetCacheStats&CacheSv1.LoadCache&CacheSv1.PrecacheStatus&CacheSv1.GetItemIDs&CacheSv1.HasItem&CacheSv1.GetItemExpiryTime&CacheSv1.ReloadCache&CacheSv1.RemoveItem&CacheSv1.FlushCache&CacheSv1.Clear,false,20 diff --git a/dispatchers/resources.go b/dispatchers/resources.go index 20551df72..7062059e5 100755 --- a/dispatchers/resources.go +++ b/dispatchers/resources.go @@ -152,7 +152,7 @@ func (dS *DispatcherService) ResourceSv1GetResource(args *utils.TenantIDWithArgD return utils.NewErrMandatoryIeMissing(utils.ArgDispatcherField) } if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { - if err = dS.authorize(utils.ResourceSv1GetResources, tnt, + if err = dS.authorize(utils.ResourceSv1GetResource, tnt, args.APIKey, utils.TimePointer(time.Now())); err != nil { return } @@ -161,5 +161,5 @@ func (dS *DispatcherService) ResourceSv1GetResource(args *utils.TenantIDWithArgD return dS.Dispatch(&utils.CGREvent{ Tenant: tnt, ID: args.ID, - }, utils.MetaResources, routeID, utils.ResourceSv1GetResources, args, reply) + }, utils.MetaResources, routeID, utils.ResourceSv1GetResource, args, reply) } diff --git a/dispatchers/resources_it_test.go b/dispatchers/resources_it_test.go index ecd371da1..e807d36d4 100755 --- a/dispatchers/resources_it_test.go +++ b/dispatchers/resources_it_test.go @@ -301,4 +301,20 @@ func testDspResTestAuthKey3(t *testing.T) { t.Errorf("Unexpected resource: %+v", utils.ToJSON(r)) } } + var r *engine.Resource + argsGetResource := &utils.TenantIDWithArgDispatcher{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ResGroup1"}, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("res12345"), + }, + } + if err := dispEngine.RPC.Call(utils.ResourceSv1GetResource, argsGetResource, &r); err != nil { + t.Fatal(err) + } + // make sure Resource1 have no more active resources + if r.ID == "ResGroup1" && + (len(r.Usages) != 1 || len(r.TTLIdx) != 0) { + t.Errorf("Unexpected resource: %+v", utils.ToJSON(r)) + } + } diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index 73e3588bc..1d9268c52 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -2992,8 +2992,8 @@ func TestEventCostgetChargesForPath(t *testing.T) { eBalanceCharge := &BalanceCharge{AccountID: "AccountID"} if rcv, err := eventCost.getChargesForPath([]string{"Increments[0]", utils.Accounting}, chargingInterval); err != nil { t.Errorf("Expecting: nil, received: %+v", err) - } else if !reflect.DeepEqual(eBalanceCharge,rcv){ - t.Errorf("Expecting: %+v, received: %+v",eBalanceCharge, rcv) + } else if !reflect.DeepEqual(eBalanceCharge, rcv) { + t.Errorf("Expecting: %+v, received: %+v", eBalanceCharge, rcv) } } diff --git a/engine/suppliers.go b/engine/suppliers.go index 3aca525a7..2332e18d5 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -368,9 +368,8 @@ func (spS *SupplierService) resourceUsage(resIDs []string, tenant string) (tUsag for _, resID := range resIDs { var res Resource if err = spS.connMgr.Call(spS.cgrcfg.SupplierSCfg().ResourceSConns, nil, utils.ResourceSv1GetResource, - &utils.TenantIDWithArgDispatcher{TenantID: &utils.TenantID{ Tenant: tenant, ID: resID}}, &res); - err != nil && err.Error() != utils.ErrNotFound.Error() { - utils.Logger.Warning( + &utils.TenantIDWithArgDispatcher{TenantID: &utils.TenantID{Tenant: tenant, ID: resID}}, &res); err != nil && err.Error() != utils.ErrNotFound.Error() { + utils.Logger.Warning( fmt.Sprintf(" error: %s getting resource for ID : %s", err.Error(), resID)) continue } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 5325c9e31..f0ae6202d 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1388,7 +1388,7 @@ type RatingPlanCostArg struct { *ArgDispatcher } type SessionIDsWithArgsDispatcher struct { - IDs []string + IDs []string Tenant string *ArgDispatcher -} \ No newline at end of file +} diff --git a/utils/consts.go b/utils/consts.go index ec8061304..c95bfc6bc 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1265,7 +1265,6 @@ const ( const ( ResourceSv1AuthorizeResources = "ResourceSv1.AuthorizeResources" ResourceSv1GetResourcesForEvent = "ResourceSv1.GetResourcesForEvent" - ResourceSv1GetResources = "ResourceSv1.GetResources" ResourceSv1AllocateResources = "ResourceSv1.AllocateResources" ResourceSv1ReleaseResources = "ResourceSv1.ReleaseResources" ResourceSv1Ping = "ResourceSv1.Ping"