From 1f519c0516cbdf6b7dc96034274fcdc8709eb623 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 13 Sep 2017 14:08:01 +0300 Subject: [PATCH] Add tests for TPRatingPlans and TPDestinations --- apier/v1/tpdestinations_it_test.go | 215 ++++++++++++++++++++++++ apier/v1/tprates_it_test.go | 3 +- apier/v1/tpratingplans.go | 7 +- apier/v1/tpratingplans_it_test.go | 236 +++++++++++++++++++++++++++ apier/v1/tpratingprofiles_it_test.go | 2 +- apier/v1/tpresources_it_test.go | 2 +- apier/v1/tpsharedgroups_it_test.go | 2 +- apier/v1/tpstats_it_test.go | 2 +- apier/v1/tptimings_it_test.go | 2 +- apier/v1/tpusers_it_test.go | 2 +- offline_tp_test.sh | 4 +- 11 files changed, 464 insertions(+), 13 deletions(-) create mode 100644 apier/v1/tpdestinations_it_test.go create mode 100644 apier/v1/tpratingplans_it_test.go diff --git a/apier/v1/tpdestinations_it_test.go b/apier/v1/tpdestinations_it_test.go new file mode 100644 index 000000000..fb85da7db --- /dev/null +++ b/apier/v1/tpdestinations_it_test.go @@ -0,0 +1,215 @@ +// +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 ( + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" + "net/rpc" + "net/rpc/jsonrpc" + "path" + "reflect" + "testing" +) + +var ( + tpDestinationCfgPath string + tpDestinationCfg *config.CGRConfig + tpDestinationRPC *rpc.Client + tpDestinationDataDir = "/usr/share/cgrates" + tpDestination *utils.TPDestination + tpDestinationDelay int + tpDestinationConfigDIR string //run tests for specific configuration +) + +var sTestsTPDestinations = []func(t *testing.T){ + testTPDestinationsInitCfg, + testTPDestinationsResetStorDb, + testTPDestinationsStartEngine, + testTPDestinationsRpcConn, + testTPDestinationsGetTPDestinationBeforeSet, + testTPDestinationsSetTPDestination, + testTPDestinationsGetTPDestinationAfterSet, + testTPDestinationsGetTPDestinationIds, + testTPDestinationsUpdateTPDestination, + testTPDestinationsGetTPDestinationAfterUpdate, + testTPDestinationsRemTPDestination, + testTPDestinationsGetTPDestinationAfterRemove, + testTPDestinationsKillEngine, +} + +//Test start here +func TestTPDestinationsITMySql(t *testing.T) { + tpDestinationConfigDIR = "tutmysql" + for _, stest := range sTestsTPDestinations { + t.Run(tpDestinationConfigDIR, stest) + } +} + +func TestTPDestinationsITMongo(t *testing.T) { + tpDestinationConfigDIR = "tutmongo" + for _, stest := range sTestsTPDestinations { + t.Run(tpDestinationConfigDIR, stest) + } +} + +func TestTPDestinationsITPG(t *testing.T) { + tpDestinationConfigDIR = "tutpostgres" + for _, stest := range sTestsTPDestinations { + t.Run(tpDestinationConfigDIR, stest) + } +} + +func testTPDestinationsInitCfg(t *testing.T) { + var err error + tpDestinationCfgPath = path.Join(tpDestinationDataDir, "conf", "samples", tpDestinationConfigDIR) + tpDestinationCfg, err = config.NewCGRConfigFromFolder(tpDestinationCfgPath) + if err != nil { + t.Error(err) + } + tpDestinationCfg.DataFolderPath = tpDestinationDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(tpDestinationCfg) + switch tpDestinationConfigDIR { + case "tutmongo": // Mongo needs more time to reset db, need to investigate + tpDestinationDelay = 2000 + default: + tpDestinationDelay = 1000 + } +} + +// Wipe out the cdr database +func testTPDestinationsResetStorDb(t *testing.T) { + if err := engine.InitStorDb(tpDestinationCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testTPDestinationsStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(tpDestinationCfgPath, tpDestinationDelay); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testTPDestinationsRpcConn(t *testing.T) { + var err error + tpDestinationRPC, err = jsonrpc.Dial("tcp", tpDestinationCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testTPDestinationsGetTPDestinationBeforeSet(t *testing.T) { + var reply *utils.TPDestination + if err := tpDestinationRPC.Call("ApierV1.GetTPDestination", &AttrGetTPDestination{TPid: "TPD", ID: "GERMANY"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } + +} + +func testTPDestinationsSetTPDestination(t *testing.T) { + tpDestination = &utils.TPDestination{ + TPid: "TPD", + ID: "GERMANY", + Prefixes: []string{"+49", "+4915"}, + } + var result string + if err := tpDestinationRPC.Call("ApierV1.SetTPDestination", tpDestination, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } +} + +func testTPDestinationsGetTPDestinationAfterSet(t *testing.T) { + var reply *utils.TPDestination + if err := tpDestinationRPC.Call("ApierV1.GetTPDestination", &AttrGetTPDestination{TPid: "TPD", ID: "GERMANY"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpDestination.TPid, reply.TPid) { + t.Errorf("Expecting : %+v, received: %+v", tpDestination.TPid, reply.TPid) + } else if !reflect.DeepEqual(tpDestination.ID, reply.ID) { + t.Errorf("Expecting : %+v, received: %+v", tpDestination.ID, reply.ID) + } else if !reflect.DeepEqual(len(tpDestination.Prefixes), len(reply.Prefixes)) { + t.Errorf("Expecting : %+v, received: %+v", len(tpDestination.Prefixes), len(reply.Prefixes)) + } + +} + +func testTPDestinationsGetTPDestinationIds(t *testing.T) { + var result []string + expectedTPID := []string{"GERMANY"} + if err := tpDestinationRPC.Call("ApierV1.GetTPDestinationIDs", &AttrGetTPDestinationIds{TPid: "TPD"}, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expectedTPID, result) { + t.Errorf("Expecting: %+v, received: %+v", expectedTPID, result) + } + +} + +func testTPDestinationsUpdateTPDestination(t *testing.T) { + tpDestination.Prefixes = []string{"+49", "+4915", "+4916"} + var result string + if err := tpDestinationRPC.Call("ApierV1.SetTPDestination", tpDestination, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + +} + +func testTPDestinationsGetTPDestinationAfterUpdate(t *testing.T) { + var reply *utils.TPDestination + if err := tpDestinationRPC.Call("ApierV1.GetTPDestination", &AttrGetTPDestination{TPid: "TPD", ID: "GERMANY"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpDestination.TPid, reply.TPid) { + t.Errorf("Expecting : %+v, received: %+v", tpDestination.TPid, reply.TPid) + } else if !reflect.DeepEqual(tpDestination.ID, reply.ID) { + t.Errorf("Expecting : %+v, received: %+v", tpDestination.ID, reply.ID) + } else if !reflect.DeepEqual(len(tpDestination.Prefixes), len(reply.Prefixes)) { + t.Errorf("Expecting : %+v, received: %+v", len(tpDestination.Prefixes), len(reply.Prefixes)) + } + +} + +func testTPDestinationsRemTPDestination(t *testing.T) { + var resp string + if err := tpDestinationRPC.Call("ApierV1.RemTPDestination", &AttrGetTPDestination{TPid: "TPD", ID: "GERMANY"}, &resp); err != nil { + t.Error(err) + } else if resp != utils.OK { + t.Error("Unexpected reply returned", resp) + } + +} + +func testTPDestinationsGetTPDestinationAfterRemove(t *testing.T) { + var reply *utils.TPDestination + if err := tpDestinationRPC.Call("ApierV1.GetTPDestination", &AttrGetTPDestination{TPid: "TPD", ID: "GERMANY"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testTPDestinationsKillEngine(t *testing.T) { + if err := engine.KillEngine(tpDestinationDelay); err != nil { + t.Error(err) + } +} diff --git a/apier/v1/tprates_it_test.go b/apier/v1/tprates_it_test.go index 848375c03..282cd9df2 100644 --- a/apier/v1/tprates_it_test.go +++ b/apier/v1/tprates_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments @@ -39,7 +39,6 @@ var ( tpRate *utils.TPRate tpRateDelay int tpRateConfigDIR string //run tests for specific configuration - ) var sTestsTPRates = []func(t *testing.T){ diff --git a/apier/v1/tpratingplans.go b/apier/v1/tpratingplans.go index fc52776da..3a7531b2c 100644 --- a/apier/v1/tpratingplans.go +++ b/apier/v1/tpratingplans.go @@ -48,9 +48,10 @@ func (self *ApierV1) GetTPRatingPlan(attrs AttrGetTPRatingPlan, reply *utils.TPR return utils.NewErrMandatoryIeMissing(missing...) } if rps, err := self.StorDb.GetTPRatingPlans(attrs.TPid, attrs.ID, &attrs.Paginator); err != nil { - return utils.NewErrServerError(err) - } else if len(rps) == 0 { - return utils.ErrNotFound + if err.Error() != utils.ErrNotFound.Error() { + err = utils.NewErrServerError(err) + } + return err } else { *reply = *rps[0] } diff --git a/apier/v1/tpratingplans_it_test.go b/apier/v1/tpratingplans_it_test.go new file mode 100644 index 000000000..e2390f98d --- /dev/null +++ b/apier/v1/tpratingplans_it_test.go @@ -0,0 +1,236 @@ +// +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 ( + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" + "net/rpc" + "net/rpc/jsonrpc" + "path" + "reflect" + "testing" +) + +var ( + tpRatingPlanCfgPath string + tpRatingPlanCfg *config.CGRConfig + tpRatingPlanRPC *rpc.Client + tpRatingPlanDataDir = "/usr/share/cgrates" + tpRatingPlan *utils.TPRatingPlan + tpRatingPlanDelay int + tpRatingPlanConfigDIR string //run tests for specific configuration +) + +var sTestsTPRatingPlans = []func(t *testing.T){ + testTPRatingPlansInitCfg, + testTPRatingPlansResetStorDb, + testTPRatingPlansStartEngine, + testTPRatingPlansRpcConn, + testTPRatingPlansGetTPRatingPlanBeforeSet, + testTPRatingPlansSetTPRatingPlan, + testTPRatingPlansGetTPRatingPlanAfterSet, + testTPRatingPlansGetTPRatingPlanIds, + testTPRatingPlansUpdateTPRatingPlan, + testTPRatingPlansGetTPRatingPlanAfterUpdate, + testTPRatingPlansRemTPRatingPlan, + testTPRatingPlansGetTPRatingPlanAfterRemove, + testTPRatingPlansKillEngine, +} + +//Test start here +func TestTPRatingPlansITMySql(t *testing.T) { + tpRatingPlanConfigDIR = "tutmysql" + for _, stest := range sTestsTPRatingPlans { + t.Run(tpRatingPlanConfigDIR, stest) + } +} + +func TestTPRatingPlansITMongo(t *testing.T) { + tpRatingPlanConfigDIR = "tutmongo" + for _, stest := range sTestsTPRatingPlans { + t.Run(tpRatingPlanConfigDIR, stest) + } +} + +func TestTPRatingPlansITPG(t *testing.T) { + tpRatingPlanConfigDIR = "tutpostgres" + for _, stest := range sTestsTPRatingPlans { + t.Run(tpRatingPlanConfigDIR, stest) + } +} + +func testTPRatingPlansInitCfg(t *testing.T) { + var err error + tpRatingPlanCfgPath = path.Join(tpRatingPlanDataDir, "conf", "samples", tpRatingPlanConfigDIR) + tpRatingPlanCfg, err = config.NewCGRConfigFromFolder(tpRatingPlanCfgPath) + if err != nil { + t.Error(err) + } + tpRatingPlanCfg.DataFolderPath = tpRatingPlanDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(tpRatingPlanCfg) + switch tpRatingPlanConfigDIR { + case "tutmongo": // Mongo needs more time to reset db, need to investigate + tpRatingPlanDelay = 2000 + default: + tpRatingPlanDelay = 1000 + } +} + +// Wipe out the cdr database +func testTPRatingPlansResetStorDb(t *testing.T) { + if err := engine.InitStorDb(tpRatingPlanCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testTPRatingPlansStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(tpRatingPlanCfgPath, tpRatingPlanDelay); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testTPRatingPlansRpcConn(t *testing.T) { + var err error + tpRatingPlanRPC, err = jsonrpc.Dial("tcp", tpRatingPlanCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testTPRatingPlansGetTPRatingPlanBeforeSet(t *testing.T) { + var reply *utils.TPRatingPlan + if err := tpRatingPlanRPC.Call("ApierV1.GetTPRatingPlan", &AttrGetTPRatingPlan{TPid: "TPRP1", ID: "Plan1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testTPRatingPlansSetTPRatingPlan(t *testing.T) { + tpRatingPlan = &utils.TPRatingPlan{ + TPid: "TPRP1", + ID: "Plan1", + RatingPlanBindings: []*utils.TPRatingPlanBinding{ + &utils.TPRatingPlanBinding{ + DestinationRatesId: "RateId", + TimingId: "TimingID", + Weight: 12, + }, + &utils.TPRatingPlanBinding{ + DestinationRatesId: "DR_FREESWITCH_USERS", + TimingId: "ALWAYS", + Weight: 10, + }, + }, + } + var result string + if err := tpRatingPlanRPC.Call("ApierV1.SetTPRatingPlan", tpRatingPlan, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } +} + +func testTPRatingPlansGetTPRatingPlanAfterSet(t *testing.T) { + var respond *utils.TPRatingPlan + if err := tpRatingPlanRPC.Call("ApierV1.GetTPRatingPlan", &AttrGetTPRatingPlan{TPid: "TPRP1", ID: "Plan1"}, &respond); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpRatingPlan.TPid, respond.TPid) { + t.Errorf("Expecting : %+v, received: %+v", tpRatingPlan.TPid, respond.TPid) + } else if !reflect.DeepEqual(tpRatingPlan.ID, respond.ID) { + t.Errorf("Expecting : %+v, received: %+v", tpRatingPlan, respond) + } else if !reflect.DeepEqual(len(tpRatingPlan.RatingPlanBindings), len(respond.RatingPlanBindings)) { + t.Errorf("Expecting : %+v, received: %+v", len(tpRatingPlan.RatingPlanBindings), len(respond.RatingPlanBindings)) + } +} + +func testTPRatingPlansGetTPRatingPlanIds(t *testing.T) { + var result []string + expected := []string{"Plan1"} + if err := tpRatingPlanRPC.Call("ApierV1.GetTPRatingPlanIds", &AttrGetTPRatingPlanIds{TPid: tpRatingPlan.TPid}, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, result) { + t.Errorf("Expecting: %+v, received: %+v", expected, result) + } +} + +func testTPRatingPlansUpdateTPRatingPlan(t *testing.T) { + tpRatingPlan.RatingPlanBindings = []*utils.TPRatingPlanBinding{ + &utils.TPRatingPlanBinding{ + DestinationRatesId: "RateId", + TimingId: "TimingID", + Weight: 12, + }, + &utils.TPRatingPlanBinding{ + DestinationRatesId: "DR_FREESWITCH_USERS", + TimingId: "ALWAYS", + Weight: 10, + }, + &utils.TPRatingPlanBinding{ + DestinationRatesId: "RateID2", + TimingId: "ALWAYS", + Weight: 11, + }, + } + var result string + if err := tpRatingPlanRPC.Call("ApierV1.SetTPRatingPlan", tpRatingPlan, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } +} + +func testTPRatingPlansGetTPRatingPlanAfterUpdate(t *testing.T) { + var respond *utils.TPRatingPlan + if err := tpRatingPlanRPC.Call("ApierV1.GetTPRatingPlan", &AttrGetTPRatingPlan{TPid: "TPRP1", ID: "Plan1"}, &respond); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(tpRatingPlan.TPid, respond.TPid) { + t.Errorf("Expecting : %+v, received: %+v", tpRatingPlan.TPid, respond.TPid) + } else if !reflect.DeepEqual(tpRatingPlan.ID, respond.ID) { + t.Errorf("Expecting : %+v, received: %+v", tpRatingPlan, respond) + } else if !reflect.DeepEqual(len(tpRatingPlan.RatingPlanBindings), len(respond.RatingPlanBindings)) { + t.Errorf("Expecting : %+v, received: %+v", len(tpRatingPlan.RatingPlanBindings), len(respond.RatingPlanBindings)) + } +} + +func testTPRatingPlansRemTPRatingPlan(t *testing.T) { + var resp string + if err := tpRatingPlanRPC.Call("ApierV1.RemTPRatingPlan", &AttrGetTPRatingPlan{TPid: "TPRP1", ID: "Plan1"}, &resp); err != nil { + t.Error(err) + } else if resp != utils.OK { + t.Error("Unexpected reply returned", resp) + } +} + +func testTPRatingPlansGetTPRatingPlanAfterRemove(t *testing.T) { + var respond *utils.TPRatingPlan + if err := tpRatingPlanRPC.Call("ApierV1.GetTPRatingPlan", &AttrGetTPRatingPlan{TPid: "TPRP1", ID: "Plan1"}, &respond); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testTPRatingPlansKillEngine(t *testing.T) { + if err := engine.KillEngine(tpRatingPlanDelay); err != nil { + t.Error(err) + } +} diff --git a/apier/v1/tpratingprofiles_it_test.go b/apier/v1/tpratingprofiles_it_test.go index a01e02de7..f754a35a1 100644 --- a/apier/v1/tpratingprofiles_it_test.go +++ b/apier/v1/tpratingprofiles_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/apier/v1/tpresources_it_test.go b/apier/v1/tpresources_it_test.go index 7d645577d..37f7c62a8 100644 --- a/apier/v1/tpresources_it_test.go +++ b/apier/v1/tpresources_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/apier/v1/tpsharedgroups_it_test.go b/apier/v1/tpsharedgroups_it_test.go index ea17a923c..e6000e30e 100644 --- a/apier/v1/tpsharedgroups_it_test.go +++ b/apier/v1/tpsharedgroups_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/apier/v1/tpstats_it_test.go b/apier/v1/tpstats_it_test.go index 28e3e9a26..68e0e27e4 100644 --- a/apier/v1/tpstats_it_test.go +++ b/apier/v1/tpstats_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/apier/v1/tptimings_it_test.go b/apier/v1/tptimings_it_test.go index e58df05e0..194b35c2a 100644 --- a/apier/v1/tptimings_it_test.go +++ b/apier/v1/tptimings_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/apier/v1/tpusers_it_test.go b/apier/v1/tpusers_it_test.go index 1e797e8e0..08b1368ac 100644 --- a/apier/v1/tpusers_it_test.go +++ b/apier/v1/tpusers_it_test.go @@ -1,4 +1,4 @@ -// +build offline_TP +// +build offline_tp /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments diff --git a/offline_tp_test.sh b/offline_tp_test.sh index c2c7cc216..6eb76a4ee 100755 --- a/offline_tp_test.sh +++ b/offline_tp_test.sh @@ -1,6 +1,6 @@ ./test.sh gen=$? -echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline_TP' -go test github.com/cgrates/cgrates/apier/v1 -tags=offline_TP +echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline_tp' +go test github.com/cgrates/cgrates/apier/v1 -tags=offline_tp exit $gen