From 8ab7fc1325ce641098d4740f9e8ff3040fce5a12 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 8 Nov 2017 14:46:41 +0200 Subject: [PATCH] Add test for ApierV1.RemoveAccount in general_tests and update method --- apier/v1/accounts.go | 7 +- general_tests/accounts_it_test.go | 160 ++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 general_tests/accounts_it_test.go diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index f21265c7a..a152d6cda 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -339,10 +339,11 @@ func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) return utils.NewErrServerError(err) } if err = self.DataManager.DataDB().RemAccountActionPlans(accID, nil); err != nil { - return + return err } - if err = self.DataManager.CacheDataFromDB(utils.AccountActionPlansPrefix, []string{accID}, true); err != nil { - return + + if err = self.DataManager.CacheDataFromDB(utils.AccountActionPlansPrefix, []string{accID}, true); err.Error() != utils.ErrNotFound.Error() { + return err } *reply = OK return nil diff --git a/general_tests/accounts_it_test.go b/general_tests/accounts_it_test.go new file mode 100644 index 000000000..505382f2c --- /dev/null +++ b/general_tests/accounts_it_test.go @@ -0,0 +1,160 @@ +// +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 general_tests + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + //"reflect" + "testing" + "time" + + //"github.com/cgrates/cgrates/apier/v2" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + accCfgPath string + accCfg *config.CGRConfig + accRpc *rpc.Client + accConfDIR string //run tests for specific configuration + account *engine.Account + accDelay int +) + +var sTestsAcc = []func(t *testing.T){ + testV1AccLoadConfig, + testV1AccInitDataDb, + testAccResetStorDb, + testV1AccStartEngine, + testV1AccRpcConn, + testV1AccGetAccountBeforeSet, + testV1AccLoadTarrifPlans, + testV1AccGetAccountAfterLoad, + testV1AccRemAccount, + testV1AccGetAccountAfterDelete, + testV1AccStopEngine, +} + +// Test start here +func TestAccITMySQL(t *testing.T) { + accConfDIR = "tutmysql" + for _, stest := range sTestsAcc { + t.Run(accConfDIR, stest) + } +} + +func TestAccITMongo(t *testing.T) { + accConfDIR = "tutmongo" + for _, stest := range sTestsAcc { + t.Run(accConfDIR, stest) + } +} + +func testV1AccLoadConfig(t *testing.T) { + var err error + accCfgPath = path.Join(*dataDir, "conf", "samples", accConfDIR) + if accCfg, err = config.NewCGRConfigFromFolder(accCfgPath); err != nil { + t.Error(err) + } + switch accConfDIR { + case "tutmongo": // Mongo needs more time to reset db, need to investigate + accDelay = 2000 + default: + accDelay = 1000 + } +} + +func testV1AccInitDataDb(t *testing.T) { + if err := engine.InitDataDb(accCfg); err != nil { + t.Fatal(err) + } +} + +func testAccResetStorDb(t *testing.T) { + if err := engine.InitStorDb(accCfg); err != nil { + t.Fatal(err) + } +} + +func testV1AccStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(accCfgPath, accDelay); err != nil { + t.Fatal(err) + } +} + +func testV1AccRpcConn(t *testing.T) { + var err error + accRpc, err = jsonrpc.Dial("tcp", accCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal("Could not connect to rater: ", err.Error()) + } +} + +func testV1AccGetAccountBeforeSet(t *testing.T) { + var reply *engine.Account + if err := accRpc.Call("ApierV2.GetAccount", &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testV1AccLoadTarrifPlans(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := accRpc.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + time.Sleep(500 * time.Millisecond) +} + +func testV1AccGetAccountAfterLoad(t *testing.T) { + var reply *engine.Account + if err := accRpc.Call("ApierV2.GetAccount", &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}, &reply); err != nil { + t.Error(err) + } +} + +func testV1AccRemAccount(t *testing.T) { + var reply string + if err := accRpc.Call("ApierV1.RemoveAccount", &utils.AttrRemoveAccount{Tenant: "cgrates.org", Account: "1001"}, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + +} + +func testV1AccGetAccountAfterDelete(t *testing.T) { + var reply *engine.Account + if err := accRpc.Call("ApierV2.GetAccount", &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testV1AccStopEngine(t *testing.T) { + if err := engine.KillEngine(accDelay); err != nil { + t.Error(err) + } +}