mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
219 lines
6.4 KiB
Go
219 lines
6.4 KiB
Go
// +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 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package migrator
|
|
|
|
import (
|
|
"log"
|
|
"path"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/engine"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
var (
|
|
rtplPathIn string
|
|
rtplPathOut string
|
|
rtplCfgIn *config.CGRConfig
|
|
rtplCfgOut *config.CGRConfig
|
|
rtplMigrator *Migrator
|
|
rtplAction string
|
|
)
|
|
|
|
var sTestsRtPlIT = []func(t *testing.T){
|
|
testRtPlITConnect,
|
|
testRtPlITFlush,
|
|
testRtPlITMigrateAndMove,
|
|
}
|
|
|
|
func TestRatingPlanITMove1(t *testing.T) {
|
|
var err error
|
|
rtplPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo")
|
|
rtplCfgIn, err = config.NewCGRConfigFromPath(rtplPathIn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql")
|
|
rtplCfgOut, err = config.NewCGRConfigFromPath(rtplPathOut)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplAction = utils.Move
|
|
for _, stest := range sTestsRtPlIT {
|
|
t.Run("TestRatingPlanITMove", stest)
|
|
}
|
|
}
|
|
|
|
func TestRatingPlanITMove2(t *testing.T) {
|
|
var err error
|
|
rtplPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql")
|
|
rtplCfgIn, err = config.NewCGRConfigFromPath(rtplPathIn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplPathOut = path.Join(*dataDir, "conf", "samples", "tutmongo")
|
|
rtplCfgOut, err = config.NewCGRConfigFromPath(rtplPathOut)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplAction = utils.Move
|
|
for _, stest := range sTestsRtPlIT {
|
|
t.Run("TestRatingPlanITMove", stest)
|
|
}
|
|
}
|
|
|
|
func TestRatingPlanITMoveEncoding(t *testing.T) {
|
|
var err error
|
|
rtplPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo")
|
|
rtplCfgIn, err = config.NewCGRConfigFromPath(rtplPathIn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplPathOut = path.Join(*dataDir, "conf", "samples", "tutmongojson")
|
|
rtplCfgOut, err = config.NewCGRConfigFromPath(rtplPathOut)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplAction = utils.Move
|
|
for _, stest := range sTestsRtPlIT {
|
|
t.Run("TestRatingPlanITMoveEncoding", stest)
|
|
}
|
|
}
|
|
|
|
func TestRatingPlanITMoveEncoding2(t *testing.T) {
|
|
var err error
|
|
rtplPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql")
|
|
rtplCfgIn, err = config.NewCGRConfigFromPath(rtplPathIn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqljson")
|
|
rtplCfgOut, err = config.NewCGRConfigFromPath(rtplPathOut)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rtplAction = utils.Move
|
|
for _, stest := range sTestsRtPlIT {
|
|
t.Run("TestRatingPlanITMoveEncoding2", stest)
|
|
}
|
|
}
|
|
|
|
func testRtPlITConnect(t *testing.T) {
|
|
dataDBIn, err := NewMigratorDataDB(rtplCfgIn.DataDbCfg().DataDbType,
|
|
rtplCfgIn.DataDbCfg().DataDbHost, rtplCfgIn.DataDbCfg().DataDbPort,
|
|
rtplCfgIn.DataDbCfg().DataDbName, rtplCfgIn.DataDbCfg().DataDbUser,
|
|
rtplCfgIn.DataDbCfg().DataDbPass, rtplCfgIn.GeneralCfg().DBDataEncoding,
|
|
config.CgrConfig().CacheCfg(), "")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
dataDBOut, err := NewMigratorDataDB(rtplCfgOut.DataDbCfg().DataDbType,
|
|
rtplCfgOut.DataDbCfg().DataDbHost, rtplCfgOut.DataDbCfg().DataDbPort,
|
|
rtplCfgOut.DataDbCfg().DataDbName, rtplCfgOut.DataDbCfg().DataDbUser,
|
|
rtplCfgOut.DataDbCfg().DataDbPass, rtplCfgOut.GeneralCfg().DBDataEncoding,
|
|
config.CgrConfig().CacheCfg(), "")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
rtplMigrator, err = NewMigrator(dataDBIn, dataDBOut,
|
|
nil, nil,
|
|
false, false, false, false)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func testRtPlITFlush(t *testing.T) {
|
|
if err := rtplMigrator.dmOut.DataManager().DataDB().Flush(""); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if isEmpty, err := rtplMigrator.dmOut.DataManager().DataDB().IsDBEmpty(); err != nil {
|
|
t.Error(err)
|
|
} else if isEmpty != true {
|
|
t.Errorf("\nExpecting: true got :%+v", isEmpty)
|
|
}
|
|
if err := engine.SetDBVersions(rtplMigrator.dmOut.DataManager().DataDB()); err != nil {
|
|
t.Error("Error ", err.Error())
|
|
}
|
|
if err := rtplMigrator.dmIN.DataManager().DataDB().Flush(""); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if isEmpty, err := rtplMigrator.dmIN.DataManager().DataDB().IsDBEmpty(); err != nil {
|
|
t.Error(err)
|
|
} else if isEmpty != true {
|
|
t.Errorf("\nExpecting: true got :%+v", isEmpty)
|
|
}
|
|
if err := engine.SetDBVersions(rtplMigrator.dmIN.DataManager().DataDB()); err != nil {
|
|
t.Error("Error ", err.Error())
|
|
}
|
|
}
|
|
|
|
func testRtPlITMigrateAndMove(t *testing.T) {
|
|
rtplPlan := &engine.RatingPlan{
|
|
Id: "RT_PLAN1",
|
|
Timings: map[string]*engine.RITiming{},
|
|
Ratings: map[string]*engine.RIRate{
|
|
"asjkilj": &engine.RIRate{
|
|
ConnectFee: 10,
|
|
RoundingMethod: utils.ROUNDING_UP,
|
|
RoundingDecimals: 1,
|
|
MaxCost: 10,
|
|
},
|
|
},
|
|
DestinationRates: map[string]engine.RPRateList{},
|
|
}
|
|
switch rtplAction {
|
|
case utils.Migrate: // for the momment only one version of rating plans exists
|
|
case utils.Move:
|
|
if err := rtplMigrator.dmIN.DataManager().SetRatingPlan(rtplPlan, utils.NonTransactional); err != nil {
|
|
t.Error(err)
|
|
}
|
|
currentVersion := engine.CurrentDataDBVersions()
|
|
err := rtplMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false)
|
|
if err != nil {
|
|
t.Error("Error when setting version for RatingPlan ", err.Error())
|
|
}
|
|
|
|
_, err = rtplMigrator.dmOut.DataManager().GetRatingPlan("RT_PLAN1", true, utils.NonTransactional)
|
|
if err != utils.ErrNotFound {
|
|
t.Error(err)
|
|
}
|
|
|
|
err, _ = rtplMigrator.Migrate([]string{utils.MetaRatingPlans})
|
|
if err != nil {
|
|
t.Error("Error when migrating RatingPlan ", err.Error())
|
|
}
|
|
result, err := rtplMigrator.dmOut.DataManager().GetRatingPlan("RT_PLAN1", true, utils.NonTransactional)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(result, rtplPlan) {
|
|
t.Errorf("Expecting: %+v, received: %+v", rtplPlan, result)
|
|
}
|
|
result, err = rtplMigrator.dmIN.DataManager().GetRatingPlan("RT_PLAN1", true, utils.NonTransactional)
|
|
if err != utils.ErrNotFound {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
}
|