From b6e1d6cd3179fa2f5e9dd6b4c50cc48672c4fa30 Mon Sep 17 00:00:00 2001 From: TeoV Date: Mon, 7 Jan 2019 08:28:05 -0500 Subject: [PATCH] Add test for tpdestinationrates --- apier/v1/tpchargers.go | 5 +- apier/v1/tpdestinationrates_it_test.go | 208 +++++++++++++++++++++++++ apier/v1/versions.go | 4 +- 3 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 apier/v1/tpdestinationrates_it_test.go diff --git a/apier/v1/tpchargers.go b/apier/v1/tpchargers.go index 223ca1a91..2c6028634 100755 --- a/apier/v1/tpchargers.go +++ b/apier/v1/tpchargers.go @@ -35,8 +35,9 @@ func (self *ApierV1) SetTPCharger(attr utils.TPChargerProfile, reply *string) er } type AttrGetTPCharger struct { - TPid string // Tariff plan id - ID string + TPid string // Tariff plan id + Tenant string + ID string } // Queries specific ChargerProfile on Tariff plan diff --git a/apier/v1/tpdestinationrates_it_test.go b/apier/v1/tpdestinationrates_it_test.go new file mode 100644 index 000000000..bbe58e27d --- /dev/null +++ b/apier/v1/tpdestinationrates_it_test.go @@ -0,0 +1,208 @@ +// +build offline_tp + +/* +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 v1 + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + tpDstRateCfgPath string + tpDstRateCfg *config.CGRConfig + tpDstRateRPC *rpc.Client + tpDstRateDataDir = "/usr/share/cgrates" + tpDstRate *utils.TPDestinationRate + tpDstRateDelay int + tpDstRateConfigDIR string //run tests for specific configuration +) + +var sTestsTPDstRates = []func(t *testing.T){ + testTPDstRateInitCfg, + testTPDstRateResetStorDb, + testTPDstRateStartEngine, + testTPDstRateRpcConn, + testTPDstRateGetTPDstRateBeforeSet, + testTPDstRateSetTPDstRate, + testTPDstRateGetTPDstRateAfterSet, + testTPDstRateGetTPDstRateIds, + testTPDstRateUpdateTPDstRate, + testTPDstRateGetTPDstRateAfterUpdate, + testTPDstRateRemTPDstRate, + testTPDstRateGetTPDstRateAfterRemove, + testTPDstRateKillEngine, +} + +//Test start here +func TestTPDstRateITMySql(t *testing.T) { + tpDstRateConfigDIR = "tutmysql" + for _, stest := range sTestsTPDstRates { + t.Run(tpDstRateConfigDIR, stest) + } +} + +func TestTPDstRateITMongo(t *testing.T) { + tpDstRateConfigDIR = "tutmongo" + for _, stest := range sTestsTPDstRates { + t.Run(tpDstRateConfigDIR, stest) + } +} + +func TestTPDstRateITPG(t *testing.T) { + tpDstRateConfigDIR = "tutpostgres" + for _, stest := range sTestsTPDstRates { + t.Run(tpDstRateConfigDIR, stest) + } +} + +func testTPDstRateInitCfg(t *testing.T) { + var err error + tpDstRateCfgPath = path.Join(tpDstRateDataDir, "conf", "samples", tpDstRateConfigDIR) + tpDstRateCfg, err = config.NewCGRConfigFromFolder(tpDstRateCfgPath) + if err != nil { + t.Error(err) + } + tpDstRateCfg.DataFolderPath = tpDstRateDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(tpDstRateCfg) + tpDstRateDelay = 1000 +} + +// Wipe out the cdr database +func testTPDstRateResetStorDb(t *testing.T) { + if err := engine.InitStorDb(tpDstRateCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testTPDstRateStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(tpDstRateCfgPath, tpDstRateDelay); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testTPDstRateRpcConn(t *testing.T) { + var err error + tpDstRateRPC, err = jsonrpc.Dial("tcp", tpDstRateCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testTPDstRateGetTPDstRateBeforeSet(t *testing.T) { + var reply *utils.TPDestinationRate + if err := tpDstRateRPC.Call("ApierV1.GetTPDestinationRate", + &AttrGetTPDestinationRate{TPid: "TP1", ID: "DR_FREESWITCH_USERS"}, &reply); err == nil || + err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } + +} + +func testTPDstRateSetTPDstRate(t *testing.T) { + tpDstRate = &utils.TPDestinationRate{ + TPid: "TP1", + ID: "DR_FREESWITCH_USERS", + DestinationRates: []*utils.DestinationRate{ + &utils.DestinationRate{ + DestinationId: "FS_USERS", + RateId: "RT_FS_USERS", + RoundingMethod: "*up", + RoundingDecimals: 2}, + }, + } + var result string + if err := tpDstRateRPC.Call("ApierV1.SetTPDestinationRate", tpDstRate, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } +} + +func testTPDstRateGetTPDstRateAfterSet(t *testing.T) { + var reply *utils.TPDestinationRate + if err := tpDstRateRPC.Call("ApierV1.GetTPDestinationRate", + &AttrGetTPDestinationRate{TPid: "TP1", ID: "DR_FREESWITCH_USERS"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpDstRate, reply) { + t.Errorf("Expecting : %+v, received: %+v", tpDstRate, reply) + } +} + +func testTPDstRateGetTPDstRateIds(t *testing.T) { + var result []string + expectedTPID := []string{"DR_FREESWITCH_USERS"} + if err := tpDstRateRPC.Call("ApierV1.GetTPDestinationRateIds", + &AttrTPDestinationRateIds{TPid: "TP1"}, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expectedTPID, result) { + t.Errorf("Expecting: %+v, received: %+v", expectedTPID, result) + } + +} + +func testTPDstRateUpdateTPDstRate(t *testing.T) { + +} + +func testTPDstRateGetTPDstRateAfterUpdate(t *testing.T) { + var reply *utils.TPDestinationRate + if err := tpDstRateRPC.Call("ApierV1.GetTPDestinationRate", + &AttrGetTPDestinationRate{TPid: "TP1", ID: "DR_FREESWITCH_USERS"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpDstRate, reply) { + t.Errorf("Expecting : %+v, received: %+v", tpDstRate, reply) + } +} + +func testTPDstRateRemTPDstRate(t *testing.T) { + var resp string + if err := tpDstRateRPC.Call("ApierV1.RemTPDestinationRate", + &AttrGetTPDestinationRate{TPid: "TP1", ID: "DR_FREESWITCH_USERS"}, &resp); err != nil { + t.Error(err) + } else if resp != utils.OK { + t.Error("Unexpected reply returned", resp) + } + +} + +func testTPDstRateGetTPDstRateAfterRemove(t *testing.T) { + var reply *utils.TPDestinationRate + if err := tpDstRateRPC.Call("ApierV1.GetTPDestinationRate", + &AttrGetTPDestinationRate{TPid: "TP1", ID: "DR_FREESWITCH_USERS"}, &reply); err == nil || + err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testTPDstRateKillEngine(t *testing.T) { + if err := engine.KillEngine(tpDstRateDelay); err != nil { + t.Error(err) + } +} diff --git a/apier/v1/versions.go b/apier/v1/versions.go index fb3f85696..39fd60be2 100644 --- a/apier/v1/versions.go +++ b/apier/v1/versions.go @@ -24,7 +24,7 @@ import ( ) // Queries all versions from dataDB -func (self *ApierV1) GetDataDBVersions(arg string, reply *engine.Versions) error { +func (self *ApierV1) GetDataDBVersions(ign string, reply *engine.Versions) error { if vrs, err := self.DataManager.DataDB().GetVersions(""); err != nil { return utils.NewErrServerError(err) } else if len(vrs) == 0 { @@ -36,7 +36,7 @@ func (self *ApierV1) GetDataDBVersions(arg string, reply *engine.Versions) error } // Queries all versions from stordb -func (self *ApierV1) GetStorDBVersions(arg string, reply *engine.Versions) error { +func (self *ApierV1) GetStorDBVersions(ign string, reply *engine.Versions) error { if vrs, err := self.StorDb.GetVersions(""); err != nil { return utils.NewErrServerError(err) } else if len(vrs) == 0 {