diff --git a/apier/v1/tp_it_test.go b/apier/v1/tp_it_test.go new file mode 100644 index 000000000..172965254 --- /dev/null +++ b/apier/v1/tp_it_test.go @@ -0,0 +1,155 @@ +// +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 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 ( + tpCfgPath string + tpCfg *config.CGRConfig + tpRPC *rpc.Client + tpDataDir = "/usr/share/cgrates" + tpDelay int + tpConfigDIR string //run tests for specific configuration +) + +var sTestsTP = []func(t *testing.T){ + testTPInitCfg, + testTPResetStorDb, + testTPStartEngine, + testTPRpcConn, + testTPImportTPFromFolderPath, + testTPExportTPToFolder, + testTPKillEngine, +} + +//Test start here +func TestTPITMySql(t *testing.T) { + tpConfigDIR = "tutmysql" + for _, stest := range sTestsTP { + t.Run(tpConfigDIR, stest) + } +} + +func TestTPITMongo(t *testing.T) { + tpConfigDIR = "tutmongo" + for _, stest := range sTestsTP { + t.Run(tpConfigDIR, stest) + } +} + +func TestTPITPG(t *testing.T) { + tpConfigDIR = "tutpostgres" + for _, stest := range sTestsTP { + t.Run(tpConfigDIR, stest) + } +} + +func testTPInitCfg(t *testing.T) { + utils.Logger.Debug("init config") + var err error + tpCfgPath = path.Join(tpDataDir, "conf", "samples", tpConfigDIR) + tpCfg, err = config.NewCGRConfigFromFolder(tpCfgPath) + if err != nil { + t.Error(err) + } + tpCfg.DataFolderPath = tpDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(tpCfg) + switch tpConfigDIR { + case "tutmongo": // Mongo needs more time to reset db, need to investigate + tpDelay = 2000 + default: + tpDelay = 2000 + } +} + +// Wipe out the cdr database +func testTPResetStorDb(t *testing.T) { + utils.Logger.Debug("ResetStorDB") + if err := engine.InitStorDb(tpCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testTPStartEngine(t *testing.T) { + utils.Logger.Debug("StartEngine") + if _, err := engine.StopStartEngine(tpCfgPath, tpDelay); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testTPRpcConn(t *testing.T) { + utils.Logger.Debug("RPCCONN") + var err error + tpRPC, err = jsonrpc.Dial("tcp", tpCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testTPImportTPFromFolderPath(t *testing.T) { + var reply string + if err := tpRPC.Call("ApierV1.ImportTariffPlanFromFolder", utils.AttrImportTPFromFolder{TPid: "TEST_TPID2", FolderPath: path.Join(tpDataDir, "tariffplans", "tutorial")}, &reply); err != nil { + t.Error("Got error on ApierV1.ImportTarrifPlanFromFolder: ", err.Error()) + } else if reply != utils.OK { + t.Error("Calling ApierV1.ImportTarrifPlanFromFolder got reply: ", reply) + } +} + +func testTPExportTPToFolder(t *testing.T) { + var reply *utils.ExportedTPStats + expectedTPStas := &utils.ExportedTPStats{ + Compressed: true, + ExportPath: "/tmp/", + ExportedFiles: []string{"RatingProfiles.csv", "CdrStats.csv", "Users.csv", "RatingPlans.csv", "Actions.csv", "AccountActions.csv", + "Timings.csv", "SharedGroups.csv", "ActionPlans.csv", "ActionTriggers.cs", "DerivedChargers.csv", "Resources.csv", "Stats.csv", "Thresholds.csv", "Destinations.csv", "Rates.csv", "DestinationRates.csv"}, + } + tpid := "TEST_TPID2" + compress := true + exportPath := "/tmp/" + if err := tpRPC.Call("ApierV1.ExportTPToFolder", &utils.AttrDirExportTP{TPid: &tpid, ExportPath: &exportPath, Compress: &compress}, &reply); err != nil { + t.Error("Got error on ApierV1.ExportTPToFolder: ", err.Error()) + } else if !reflect.DeepEqual(reply.ExportPath, expectedTPStas.ExportPath) { + t.Errorf("Expecting : %+v, received: %+v", expectedTPStas.ExportPath, reply.ExportPath) + } else if !reflect.DeepEqual(reply.Compressed, expectedTPStas.Compressed) { + t.Errorf("Expecting : %+v, received: %+v", expectedTPStas.Compressed, reply.Compressed) + } else if !reflect.DeepEqual(len(reply.ExportedFiles), len(expectedTPStas.ExportedFiles)) { + t.Errorf("Expecting : %+v, received: %+v", len(reply.ExportedFiles), len(expectedTPStas.ExportedFiles)) + } + +} + +func testTPKillEngine(t *testing.T) { + utils.Logger.Debug("KillEngine") + if err := engine.KillEngine(tpDelay); err != nil { + t.Error(err) + } +}