From f42153961e70f7e19057f6507a20db70a787294f Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 15 Mar 2019 16:22:45 +0200 Subject: [PATCH] Add Integration test for ApierV2.SetExternalAttributeProfile --- apier/v2/attributes.go | 2 +- apier/v2/attributes_it_test.go | 231 +++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 apier/v2/attributes_it_test.go diff --git a/apier/v2/attributes.go b/apier/v2/attributes.go index 3e8e17a7a..f472fdb0e 100644 --- a/apier/v2/attributes.go +++ b/apier/v2/attributes.go @@ -24,7 +24,7 @@ import ( ) //SetAttributeProfile add/update a new Attribute Profile -func (apierV1 *ApierV2) SetAttributeProfile(extAlsPrf *engine.ExternalAttributeProfile, reply *string) error { +func (apierV1 *ApierV2) SetExternalAttributeProfile(extAlsPrf *engine.ExternalAttributeProfile, reply *string) error { if missing := utils.MissingStructFields(extAlsPrf, []string{"Tenant", "ID"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } diff --git a/apier/v2/attributes_it_test.go b/apier/v2/attributes_it_test.go new file mode 100644 index 000000000..d30ab548c --- /dev/null +++ b/apier/v2/attributes_it_test.go @@ -0,0 +1,231 @@ +// +build integration + +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package v2 + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + alsPrfCfgPath string + alsPrfCfg *config.CGRConfig + attrSRPC *rpc.Client + alsPrfDataDir = "/usr/share/cgrates" + alsPrf *engine.AttributeProfile + alsPrfConfigDIR string //run tests for specific configuration +) + +var sTestsAlsPrf = []func(t *testing.T){ + testAttributeSInitCfg, + testAttributeSInitDataDb, + testAttributeSResetStorDb, + testAttributeSStartEngine, + testAttributeSRPCConn, + testAttributeSSetAlsPrf, + testAttributeSUpdateAlsPrf, + testAttributeSKillEngine, +} + +//Test start here +func TestAttributeSITMySql(t *testing.T) { + alsPrfConfigDIR = "tutmysql" + for _, stest := range sTestsAlsPrf { + t.Run(alsPrfConfigDIR, stest) + } +} + +func TestAttributeSITMongo(t *testing.T) { + alsPrfConfigDIR = "tutmongo" + for _, stest := range sTestsAlsPrf { + t.Run(alsPrfConfigDIR, stest) + } +} + +func testAttributeSInitCfg(t *testing.T) { + var err error + alsPrfCfgPath = path.Join(alsPrfDataDir, "conf", "samples", alsPrfConfigDIR) + alsPrfCfg, err = config.NewCGRConfigFromPath(alsPrfCfgPath) + if err != nil { + t.Error(err) + } + alsPrfCfg.DataFolderPath = alsPrfDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(alsPrfCfg) +} + +func testAttributeSInitDataDb(t *testing.T) { + if err := engine.InitDataDb(alsPrfCfg); err != nil { + t.Fatal(err) + } +} + +// Wipe out the cdr database +func testAttributeSResetStorDb(t *testing.T) { + if err := engine.InitStorDb(alsPrfCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testAttributeSStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(alsPrfCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testAttributeSRPCConn(t *testing.T) { + var err error + attrSRPC, err = jsonrpc.Dial("tcp", alsPrfCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testAttributeSSetAlsPrf(t *testing.T) { + extAlsPrf := &engine.ExternalAttributeProfile{ + Tenant: "cgrates.org", + ID: "ExternalAttribute", + Contexts: []string{utils.MetaSessionS, utils.MetaCDRs}, + FilterIDs: []string{"*string:Account:1001"}, + 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.ExternalAttribute{ + { + FieldName: "Account", + Substitute: "1001", + }, + }, + Weight: 20, + } + var result string + if err := attrSRPC.Call("ApierV2.SetExternalAttributeProfile", extAlsPrf, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + alsPrf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "ExternalAttribute", + Contexts: []string{utils.MetaSessionS, utils.MetaCDRs}, + FilterIDs: []string{"*string:Account:1001"}, + 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: "Account", + Substitute: config.NewRSRParsersMustCompile("1001", true, utils.INFIELD_SEP), + }, + }, + Weight: 20, + } + alsPrf.Compile() + var reply *engine.AttributeProfile + if err := attrSRPC.Call("ApierV1.GetAttributeProfile", + &utils.TenantID{Tenant: "cgrates.org", ID: "ExternalAttribute"}, &reply); err != nil { + t.Fatal(err) + } + reply.Compile() + if !reflect.DeepEqual(alsPrf, reply) { + t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply) + } +} + +func testAttributeSUpdateAlsPrf(t *testing.T) { + extAlsPrf := &engine.ExternalAttributeProfile{ + Tenant: "cgrates.org", + ID: "ExternalAttribute", + Contexts: []string{utils.MetaSessionS, utils.MetaCDRs}, + FilterIDs: []string{"*string:Account:1001"}, + 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.ExternalAttribute{ + { + FieldName: "Account", + Substitute: "1001", + }, + { + FieldName: "Subject", + Substitute: "~Account", + }, + }, + Weight: 20, + } + var result string + if err := attrSRPC.Call("ApierV2.SetExternalAttributeProfile", extAlsPrf, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + alsPrf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "ExternalAttribute", + Contexts: []string{utils.MetaSessionS, utils.MetaCDRs}, + FilterIDs: []string{"*string:Account:1001"}, + 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: "Account", + Substitute: config.NewRSRParsersMustCompile("1001", true, utils.INFIELD_SEP), + }, + { + FieldName: "Subject", + Substitute: config.NewRSRParsersMustCompile("~Account", true, utils.INFIELD_SEP), + }, + }, + Weight: 20, + } + alsPrf.Compile() + var reply *engine.AttributeProfile + if err := attrSRPC.Call("ApierV1.GetAttributeProfile", + &utils.TenantID{Tenant: "cgrates.org", ID: "ExternalAttribute"}, &reply); err != nil { + t.Fatal(err) + } + reply.Compile() + if !reflect.DeepEqual(alsPrf, reply) { + t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply) + } +} + +func testAttributeSKillEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +}