From 648911d82b01d4fe11b5d5913964fe9d9b870a01 Mon Sep 17 00:00:00 2001 From: TeoV Date: Thu, 16 May 2019 09:57:28 +0300 Subject: [PATCH] Add test for apier with dispatcher --- apier/v1/attributes.go | 3 +- dispatchers/dispatchers.go | 6 +- dispatchers/dispatchers_it_test.go | 109 +++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/apier/v1/attributes.go b/apier/v1/attributes.go index c48207a07..00eb64405 100644 --- a/apier/v1/attributes.go +++ b/apier/v1/attributes.go @@ -26,7 +26,7 @@ import ( ) // GetAttributeProfile returns an Attribute Profile -func (apierV1 *ApierV1) GetAttributeProfile(arg utils.TenantID, reply *engine.AttributeProfile) error { +func (apierV1 *ApierV1) GetAttributeProfile(arg utils.TenantIDWithArgDispatcher, reply *engine.AttributeProfile) error { if missing := utils.MissingStructFields(&arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } @@ -65,6 +65,7 @@ func (apierV1 *ApierV1) GetAttributeProfileIDs(args utils.TenantArgWithPaginator type AttributeWithCache struct { *engine.AttributeProfile Cache *string + *utils.ArgDispatcher } //SetAttributeProfile add/update a new Attribute Profile diff --git a/dispatchers/dispatchers.go b/dispatchers/dispatchers.go index 434afb578..a12612fe7 100755 --- a/dispatchers/dispatchers.go +++ b/dispatchers/dispatchers.go @@ -205,15 +205,15 @@ func (dS *DispatcherService) V1Apier(apier interface{}, args *utils.MethodParame var argD *utils.ArgDispatcher //check if we have APIKey in event and in case it has add it in ArgDispatcher apiKeyIface, hasApiKey := parameters[utils.APIKey] - if hasApiKey { + if hasApiKey && apiKeyIface != nil { argD = &utils.ArgDispatcher{ APIKey: utils.StringPointer(apiKeyIface.(string)), } } //check if we have RouteID in event and in case it has add it in ArgDispatcher routeIDIface, hasRouteID := parameters[utils.RouteID] - if hasRouteID { - if !hasApiKey { //in case we don't have APIKey, but we have RouteID we need to initialize the struct + if hasRouteID && routeIDIface != nil { + if !hasApiKey || apiKeyIface == nil { //in case we don't have APIKey, but we have RouteID we need to initialize the struct argD = &utils.ArgDispatcher{ RouteID: utils.StringPointer(routeIDIface.(string)), } diff --git a/dispatchers/dispatchers_it_test.go b/dispatchers/dispatchers_it_test.go index 20b0034f7..18f8ae338 100644 --- a/dispatchers/dispatchers_it_test.go +++ b/dispatchers/dispatchers_it_test.go @@ -19,3 +19,112 @@ along with this program. If not, see */ package dispatchers + +import ( + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var sTestsDspApier = []func(t *testing.T){ + testDspApierSetAttributes, + testDspApierGetAttributes, + testDspApierUnkownAPiKey, +} + +//Test start here +func TestDspApierITMySQL(t *testing.T) { + testDsp(t, sTestsDspApier, "TestDspApier", "all", "all2", "dispatchers", "tutorial", "oldtutorial", "dispatchers") +} + +func TestDspApierITMongo(t *testing.T) { + testDsp(t, sTestsDspApier, "TestDspApier", "all", "all2", "dispatchers_mongo", "tutorial", "oldtutorial", "dispatchers") +} + +//because we import dispatchers in apierV1 we will send information as map[string]interface{} +func testDspApierSetAttributes(t *testing.T) { + ev := &map[string]interface{}{ + utils.Tenant: "cgrates.org", + "ID": "ATTR_Dispatcher", + "Contexts": []string{utils.MetaSessionS}, + "FilterIDs": []string{"*string:~Account:1234"}, + "ActivationInterval": &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + "Attributes": []*engine.Attribute{ + { + FieldName: utils.Subject, + Value: config.RSRParsers{ + &config.RSRParser{ + Rules: "roam", + AllFiltersMatch: true, + }, + }, + }, + }, + "Weight": 10, + utils.APIKey: utils.StringPointer("apier12345"), + } + var result string + if err := dispEngine.RCP.Call("ApierV1.SetAttributeProfile", ev, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + +} + +func testDspApierGetAttributes(t *testing.T) { + var reply *engine.AttributeProfile + alsPrf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_Dispatcher", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:~Account:1234"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Attributes: []*engine.Attribute{ + { + FieldName: utils.Subject, + Value: config.RSRParsers{ + &config.RSRParser{ + Rules: "roam", + AllFiltersMatch: true, + }, + }, + }, + }, + Weight: 10, + } + alsPrf.Compile() + if err := dispEngine.RCP.Call("ApierV1.GetAttributeProfile", + utils.TenantIDWithArgDispatcher{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_Dispatcher"}, + ArgDispatcher: &utils.ArgDispatcher{APIKey: utils.StringPointer("apier12345")}}, &reply); err != nil { + t.Fatal(err) + } + reply.Compile() + if !reflect.DeepEqual(alsPrf, reply) { + t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply) + } + +} + +func testDspApierUnkownAPiKey(t *testing.T) { + var reply *engine.AttributeProfile + + if err := dispEngine.RCP.Call("ApierV1.GetAttributeProfile", + utils.TenantIDWithArgDispatcher{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_Dispatcher"}, + ArgDispatcher: &utils.ArgDispatcher{APIKey: utils.StringPointer("RandomApiKey")}}, &reply); err == nil || err.Error() != utils.ErrUnknownApiKey.Error() { + t.Fatal(err) + } + +}