diff --git a/data/conf/samples/tutmongojson/cgrates.json b/data/conf/samples/tutmongojson/cgrates.json new file mode 100644 index 000000000..ba8106486 --- /dev/null +++ b/data/conf/samples/tutmongojson/cgrates.json @@ -0,0 +1,69 @@ +{ +// CGRateS Configuration file + + +"general": { + "log_level": 7, + "reply_timeout": "30s", + "dbdata_encoding": "json", +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + + +"data_db": { + "db_type": "mongo", + "db_name": "10", + "db_port": 27017, +}, + + +"stor_db": { + "db_type": "mongo", + "db_port": 27017, +}, + + +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "lcr_rules": {"limit": 10000, "ttl":"0s", "precache": true}, + "cdr_stats": {"limit": 10000, "ttl":"0s", "precache": true}, + "actions": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "account_action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": true}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": true}, + "aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "derived_chargers": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resources": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueues": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueue_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "thresholds": {"limit": 10000, "ttl":"0s", "precache": true}, + "threshold_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "filters": {"limit": 10000, "ttl":"0s", "precache": true}, + "supplier_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "attribute_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_filter_indexes" :{"limit": 10000, "ttl":"0s"}, + "resource_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_revindexes" :{"limit": 10000, "ttl":"0s"}, + "attribute_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "attribute_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, +}, + + +} \ No newline at end of file diff --git a/data/conf/samples/tutmongomsgpack/cgrates.json b/data/conf/samples/tutmongomsgpack/cgrates.json new file mode 100644 index 000000000..5200fd05e --- /dev/null +++ b/data/conf/samples/tutmongomsgpack/cgrates.json @@ -0,0 +1,69 @@ +{ +// CGRateS Configuration file + + +"general": { + "log_level": 7, + "reply_timeout": "30s", + "dbdata_encoding": "msgpack", +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + + +"data_db": { + "db_type": "mongo", + "db_name": "11", + "db_port": 27017, +}, + + +"stor_db": { + "db_type": "mongo", + "db_port": 27017, +}, + + +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "lcr_rules": {"limit": 10000, "ttl":"0s", "precache": true}, + "cdr_stats": {"limit": 10000, "ttl":"0s", "precache": true}, + "actions": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "account_action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": true}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": true}, + "aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "derived_chargers": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resources": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueues": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueue_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "thresholds": {"limit": 10000, "ttl":"0s", "precache": true}, + "threshold_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "filters": {"limit": 10000, "ttl":"0s", "precache": true}, + "supplier_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "attribute_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_filter_indexes" :{"limit": 10000, "ttl":"0s"}, + "resource_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_revindexes" :{"limit": 10000, "ttl":"0s"}, + "attribute_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "attribute_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, +}, + + +} \ No newline at end of file diff --git a/data/conf/samples/tutmysqljson/cgrates.json b/data/conf/samples/tutmysqljson/cgrates.json new file mode 100644 index 000000000..303bdf609 --- /dev/null +++ b/data/conf/samples/tutmysqljson/cgrates.json @@ -0,0 +1,67 @@ +{ +// CGRateS Configuration file +// + + +"general": { + "log_level": 7, + "dbdata_encoding": "json", // encoding used to store object data in strings: +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + +"data_db": { // database used to store runtime data (eg: accounts, cdr stats) + "db_type": "redis", // data_db type: + "db_port": 6379, // data_db port to reach the database + "db_name": "10", // data_db database name to connect to + +}, + +"stor_db": { + "db_password": "CGRateS.org", +}, + + +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "lcr_rules": {"limit": 10000, "ttl":"0s", "precache": true}, + "cdr_stats": {"limit": 10000, "ttl":"0s", "precache": true}, + "actions": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "account_action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": true}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": true}, + "aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "derived_chargers": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resources": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueues": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueue_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "thresholds": {"limit": 10000, "ttl":"0s", "precache": true}, + "threshold_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "filters": {"limit": 10000, "ttl":"0s", "precache": true}, + "supplier_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "attribute_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_filter_indexes" :{"limit": 10000, "ttl":"0s"}, + "resource_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_revindexes" :{"limit": 10000, "ttl":"0s"}, + "attribute_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "attribute_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, +}, + + +} \ No newline at end of file diff --git a/data/conf/samples/tutmysqlmsgpack/cgrates.json b/data/conf/samples/tutmysqlmsgpack/cgrates.json new file mode 100644 index 000000000..3e0f8f411 --- /dev/null +++ b/data/conf/samples/tutmysqlmsgpack/cgrates.json @@ -0,0 +1,67 @@ +{ +// CGRateS Configuration file +// + + +"general": { + "log_level": 7, + "dbdata_encoding": "msgpack", // encoding used to store object data in strings: +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + +"data_db": { // database used to store runtime data (eg: accounts, cdr stats) + "db_type": "redis", // data_db type: + "db_port": 6379, // data_db port to reach the database + "db_name": "11", // data_db database name to connect to + +}, + +"stor_db": { + "db_password": "CGRateS.org", +}, + + +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "lcr_rules": {"limit": 10000, "ttl":"0s", "precache": true}, + "cdr_stats": {"limit": 10000, "ttl":"0s", "precache": true}, + "actions": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "account_action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": true}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": true}, + "aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_aliases": {"limit": 10000, "ttl":"0s", "precache": true}, + "derived_chargers": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resources": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueues": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueue_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "thresholds": {"limit": 10000, "ttl":"0s", "precache": true}, + "threshold_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "filters": {"limit": 10000, "ttl":"0s", "precache": true}, + "supplier_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "attribute_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_filter_indexes" :{"limit": 10000, "ttl":"0s"}, + "resource_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "stat_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_revindexes" :{"limit": 10000, "ttl":"0s"}, + "attribute_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "attribute_filter_revindexes" : {"limit": 10000, "ttl":"0s"}, +}, + + +} \ No newline at end of file diff --git a/migrator/accounts_it_test.go b/migrator/accounts_it_test.go index 3e13e6dac..4c7b8b3a2 100755 --- a/migrator/accounts_it_test.go +++ b/migrator/accounts_it_test.go @@ -102,6 +102,42 @@ func TestAccountITMove(t *testing.T) { } } +func TestAccountITMoveEncoding(t *testing.T) { + var err error + accPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + accCfgIn, err = config.NewCGRConfigFromFolder(accPathIn) + if err != nil { + t.Fatal(err) + } + accPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + accCfgOut, err = config.NewCGRConfigFromFolder(accPathOut) + if err != nil { + t.Fatal(err) + } + accAction = utils.Move + for _, stest := range sTestsAccIT { + t.Run("TestAccountITMove", stest) + } +} + +func TestAccountITMoveEncoding2(t *testing.T) { + var err error + accPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + accCfgIn, err = config.NewCGRConfigFromFolder(accPathIn) + if err != nil { + t.Fatal(err) + } + accPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + accCfgOut, err = config.NewCGRConfigFromFolder(accPathOut) + if err != nil { + t.Fatal(err) + } + accAction = utils.Move + for _, stest := range sTestsAccIT { + t.Run("TestAccountITMove", stest) + } +} + func testAccITConnect(t *testing.T) { dataDBIn, err := NewMigratorDataDB(accCfgIn.DataDbType, accCfgIn.DataDbHost, accCfgIn.DataDbPort, accCfgIn.DataDbName, diff --git a/migrator/action_it_test.go b/migrator/action_it_test.go new file mode 100644 index 000000000..3b6868c65 --- /dev/null +++ b/migrator/action_it_test.go @@ -0,0 +1,251 @@ +// +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 migrator + +import ( + //"flag" + "log" + "path" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + actPathIn string + actPathOut string + actCfgIn *config.CGRConfig + actCfgOut *config.CGRConfig + actMigrator *Migrator + actAction string +) + +var sTestsActIT = []func(t *testing.T){ + testActITConnect, + testActITFlush, + testActITMigrateAndMove, +} + +func TestActionITRedis(t *testing.T) { + var err error + actPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql") + actCfgIn, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actCfgOut, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actAction = utils.Migrate + for _, stest := range sTestsActIT { + t.Run("TestActionITMigrateRedis", stest) + } +} + +func TestActionITMongo(t *testing.T) { + var err error + actPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actCfgIn, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actCfgOut, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actAction = utils.Migrate + for _, stest := range sTestsActIT { + t.Run("TestActionITMigrateMongo", stest) + } +} + +func TestActionITMove(t *testing.T) { + var err error + actPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actCfgIn, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql") + actCfgOut, err = config.NewCGRConfigFromFolder(actPathOut) + if err != nil { + t.Fatal(err) + } + actAction = utils.Move + for _, stest := range sTestsActIT { + t.Run("TestActionITMove", stest) + } +} + +func TestActionITMoveEncoding(t *testing.T) { + var err error + actPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + actCfgIn, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + actCfgOut, err = config.NewCGRConfigFromFolder(actPathOut) + if err != nil { + t.Fatal(err) + } + actAction = utils.Move + for _, stest := range sTestsActIT { + t.Run("TestActionITMoveEncoding", stest) + } +} + +func TestActionITMoveEncoding2(t *testing.T) { + var err error + actPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + actCfgIn, err = config.NewCGRConfigFromFolder(actPathIn) + if err != nil { + t.Fatal(err) + } + actPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + actCfgOut, err = config.NewCGRConfigFromFolder(actPathOut) + if err != nil { + t.Fatal(err) + } + actAction = utils.Move + for _, stest := range sTestsActIT { + t.Run("TestActionITMoveEncoding2", stest) + } +} + +func testActITConnect(t *testing.T) { + dataDBIn, err := NewMigratorDataDB(actCfgIn.DataDbType, + actCfgIn.DataDbHost, actCfgIn.DataDbPort, actCfgIn.DataDbName, + actCfgIn.DataDbUser, actCfgIn.DataDbPass, actCfgIn.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + dataDBOut, err := NewMigratorDataDB(actCfgOut.DataDbType, + actCfgOut.DataDbHost, actCfgOut.DataDbPort, actCfgOut.DataDbName, + actCfgOut.DataDbUser, actCfgOut.DataDbPass, actCfgOut.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + actMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) + if err != nil { + log.Fatal(err) + } +} + +func testActITFlush(t *testing.T) { + actMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(actMigrator.dmOut.DataManager().DataDB()); err != nil { + t.Error("Error ", err.Error()) + } +} + +func testActITMigrateAndMove(t *testing.T) { + v1act := &v1Actions{ + &v1Action{ + Id: "test", + ActionType: "", + BalanceType: "", + Direction: "INBOUND", + ExtraParameters: "", + ExpirationString: "", + Balance: &v1Balance{ + Timings: []*engine.RITiming{ + &engine.RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + }, + }, + }, + }, + } + act := &engine.Actions{ + &engine.Action{ + Id: "test", + ActionType: "", + ExtraParameters: "", + ExpirationString: "", + Weight: 0.00, + Balance: &engine.BalanceFilter{ + Timings: []*engine.RITiming{ + &engine.RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + }, + }, + }, + }, + } + switch accAction { + case utils.Migrate: + err := actMigrator.dmIN.setV1Actions(v1act) + if err != nil { + t.Error("Error when setting v1 Actions ", err.Error()) + } + currentVersion := engine.Versions{utils.StatS: 2, utils.Thresholds: 2, utils.Accounts: 2, utils.Actions: 1, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2} + err = actMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Actions ", err.Error()) + } + err, _ = actMigrator.Migrate([]string{utils.MetaActions}) + if err != nil { + t.Error("Error when migrating Actions ", err.Error()) + } + result, err := actMigrator.dmOut.DataManager().GetActions((*v1act)[0].Id, true, utils.NonTransactional) + if err != nil { + t.Error("Error when getting Actions ", err.Error()) + } + if !reflect.DeepEqual(*act, result) { + t.Errorf("Expecting: %+v, received: %+v", *act, result) + } + case utils.Move: + if err := actMigrator.dmIN.DataManager().SetActions((*v1act)[0].Id, *act, utils.NonTransactional); err != nil { + t.Error("Error when setting ActionPlan ", err.Error()) + } + currentVersion := engine.CurrentDataDBVersions() + err := actMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Actions ", err.Error()) + } + err, _ = actMigrator.Migrate([]string{utils.MetaActions}) + if err != nil { + t.Error("Error when migrating Actions ", err.Error()) + } + result, err := actMigrator.dmOut.DataManager().GetActions((*v1act)[0].Id, true, utils.NonTransactional) + if err != nil { + t.Error("Error when getting Actions ", err.Error()) + } + if !reflect.DeepEqual(*act, result) { + t.Errorf("Expecting: %+v, received: %+v", *act, result) + } + } +} diff --git a/migrator/action_plan_it_test.go b/migrator/action_plan_it_test.go new file mode 100644 index 000000000..01b8472c8 --- /dev/null +++ b/migrator/action_plan_it_test.go @@ -0,0 +1,244 @@ +// +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 migrator + +import ( + //"flag" + "log" + "path" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + actPlnPathIn string + actPlnPathOut string + actPlnCfgIn *config.CGRConfig + actPlnCfgOut *config.CGRConfig + actPlnMigrator *Migrator + actActionPlan string +) + +var sTestsActPlnIT = []func(t *testing.T){ + testActPlnITConnect, + testActPlnITFlush, + testActPlnITMigrateAndMove, +} + +func TestActionPlanITRedis(t *testing.T) { + var err error + actPlnPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql") + actPlnCfgIn, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actPlnCfgOut, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actActionPlan = utils.Migrate + for _, stest := range sTestsActPlnIT { + t.Run("TestActionPlanITMigrateRedis", stest) + } +} + +func TestActionPlanITMongo(t *testing.T) { + var err error + actPlnPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actPlnCfgIn, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actPlnCfgOut, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actActionPlan = utils.Migrate + for _, stest := range sTestsActPlnIT { + t.Run("TestActionPlanITMigrateMongo", stest) + } +} + +func TestActionPlanITMove(t *testing.T) { + var err error + actPlnPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actPlnCfgIn, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actPlnPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql") + actPlnCfgOut, err = config.NewCGRConfigFromFolder(actPlnPathOut) + if err != nil { + t.Fatal(err) + } + actActionPlan = utils.Move + for _, stest := range sTestsActPlnIT { + t.Run("TestActionPlanITMove", stest) + } +} + +func TestActionPlanITMoveEncoding(t *testing.T) { + var err error + actPlnPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + actPlnCfgIn, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actPlnPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + actPlnCfgOut, err = config.NewCGRConfigFromFolder(actPlnPathOut) + if err != nil { + t.Fatal(err) + } + actActionPlan = utils.Move + for _, stest := range sTestsActPlnIT { + t.Run("TestActionPlanITMoveEncoding", stest) + } +} + +func TestActionPlanITMoveEncoding2(t *testing.T) { + var err error + actPlnPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + actPlnCfgIn, err = config.NewCGRConfigFromFolder(actPlnPathIn) + if err != nil { + t.Fatal(err) + } + actPlnPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + actPlnCfgOut, err = config.NewCGRConfigFromFolder(actPlnPathOut) + if err != nil { + t.Fatal(err) + } + actActionPlan = utils.Move + for _, stest := range sTestsActPlnIT { + t.Run("TestActionPlanITMoveEncoding2", stest) + } +} + +func testActPlnITConnect(t *testing.T) { + dataDBIn, err := NewMigratorDataDB(actPlnCfgIn.DataDbType, + actPlnCfgIn.DataDbHost, actPlnCfgIn.DataDbPort, actPlnCfgIn.DataDbName, + actPlnCfgIn.DataDbUser, actPlnCfgIn.DataDbPass, actPlnCfgIn.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + dataDBOut, err := NewMigratorDataDB(actPlnCfgOut.DataDbType, + actPlnCfgOut.DataDbHost, actPlnCfgOut.DataDbPort, actPlnCfgOut.DataDbName, + actPlnCfgOut.DataDbUser, actPlnCfgOut.DataDbPass, actPlnCfgOut.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + actPlnMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) + if err != nil { + log.Fatal(err) + } +} + +func testActPlnITFlush(t *testing.T) { + actPlnMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(actPlnMigrator.dmOut.DataManager().DataDB()); err != nil { + t.Error("Error ", err.Error()) + } +} + +func testActPlnITMigrateAndMove(t *testing.T) { + v1actPln := &v1ActionPlans{ + &v1ActionPlan{ + Id: "test", + AccountIds: []string{"one"}, + Timing: &engine.RateInterval{ + Timing: &engine.RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + }, + }, + }, + } + + actPln := &engine.ActionPlan{ + Id: "test", + AccountIDs: utils.StringMap{"one": true}, + ActionTimings: []*engine.ActionTiming{ + &engine.ActionTiming{ + Timing: &engine.RateInterval{ + Timing: &engine.RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + }, + }, + }, + }, + } + + switch accAction { + case utils.Migrate: + err := actPlnMigrator.dmIN.setV1ActionPlans(v1actPln) + if err != nil { + t.Error("Error when setting v1 ActionPlan ", err.Error()) + } + currentVersion := engine.Versions{utils.StatS: 2, utils.Thresholds: 2, utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 1, utils.SharedGroups: 2} + err = actPlnMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for ActionPlan ", err.Error()) + } + err, _ = actPlnMigrator.Migrate([]string{utils.AccountActionPlansPrefix}) + if err != nil { + t.Error("Error when migrating ActionPlan ", err.Error()) + } + result, err := actPlnMigrator.dmOut.DataManager().DataDB().GetActionPlan((*v1actPln)[0].Id, true, utils.NonTransactional) + if err != nil { + t.Error("Error when getting ActionPlan ", err.Error()) + } + if !reflect.DeepEqual(*actPln, result) { + t.Errorf("Expecting: %+v, received: %+v", *actPln, result) + } + case utils.Move: + if err := actPlnMigrator.dmIN.DataManager().DataDB().SetActionPlan((*v1actPln)[0].Id, actPln, true, utils.NonTransactional); err != nil { + t.Error("Error when setting ActionPlan ", err.Error()) + } + currentVersion := engine.CurrentDataDBVersions() + err := actPlnMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for ActionPlan ", err.Error()) + } + err, _ = actPlnMigrator.Migrate([]string{utils.MetaActions}) + if err != nil { + t.Error("Error when migrating ActionPlan ", err.Error()) + } + result, err := actPlnMigrator.dmOut.DataManager().DataDB().GetActionPlan((*v1actPln)[0].Id, true, utils.NonTransactional) + if err != nil { + t.Error("Error when getting ActionPlan ", err.Error()) + } + if !reflect.DeepEqual(*actPln, result) { + t.Errorf("Expecting: %+v, received: %+v", *actPln, result) + } + } +} diff --git a/migrator/action_trigger_it_test.go b/migrator/action_trigger_it_test.go new file mode 100644 index 000000000..f5b3d743d --- /dev/null +++ b/migrator/action_trigger_it_test.go @@ -0,0 +1,245 @@ +// +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 migrator + +import ( + //"flag" + "log" + "path" + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + actTrgPathIn string + actTrgPathOut string + actTrgCfgIn *config.CGRConfig + actTrgCfgOut *config.CGRConfig + actTrgMigrator *Migrator + actActionTrigger string +) + +var sTestsActTrgIT = []func(t *testing.T){ + testActTrgITConnect, + testActTrgITFlush, + testActTrgITMigrateAndMove, +} + +func TestActionTriggerITRedis(t *testing.T) { + var err error + actTrgPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql") + actTrgCfgIn, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actTrgCfgOut, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actActionTrigger = utils.Migrate + for _, stest := range sTestsActTrgIT { + t.Run("TestActionTriggerITMigrateRedis", stest) + } +} + +func TestActionTriggerITMongo(t *testing.T) { + var err error + actTrgPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actTrgCfgIn, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actTrgCfgOut, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actActionTrigger = utils.Migrate + for _, stest := range sTestsActTrgIT { + t.Run("TestActionTriggerITMigrateMongo", stest) + } +} + +func TestActionTriggerITMove(t *testing.T) { + var err error + actTrgPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + actTrgCfgIn, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actTrgPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql") + actTrgCfgOut, err = config.NewCGRConfigFromFolder(actTrgPathOut) + if err != nil { + t.Fatal(err) + } + actActionTrigger = utils.Move + for _, stest := range sTestsActTrgIT { + t.Run("TestActionTriggerITMove", stest) + } +} + +func TestActionTriggerITMoveEncoding(t *testing.T) { + var err error + actTrgPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + actTrgCfgIn, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actTrgPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + actTrgCfgOut, err = config.NewCGRConfigFromFolder(actTrgPathOut) + if err != nil { + t.Fatal(err) + } + actActionTrigger = utils.Move + for _, stest := range sTestsActTrgIT { + t.Run("TestActionTriggerITMoveEncoding", stest) + } +} + +func TestActionTriggerITMoveEncoding2(t *testing.T) { + var err error + actTrgPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + actTrgCfgIn, err = config.NewCGRConfigFromFolder(actTrgPathIn) + if err != nil { + t.Fatal(err) + } + actTrgPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + actTrgCfgOut, err = config.NewCGRConfigFromFolder(actTrgPathOut) + if err != nil { + t.Fatal(err) + } + actActionTrigger = utils.Move + for _, stest := range sTestsActTrgIT { + t.Run("TestActionTriggerITMoveEncoding2", stest) + } +} + +func testActTrgITConnect(t *testing.T) { + dataDBIn, err := NewMigratorDataDB(actTrgCfgIn.DataDbType, + actTrgCfgIn.DataDbHost, actTrgCfgIn.DataDbPort, actTrgCfgIn.DataDbName, + actTrgCfgIn.DataDbUser, actTrgCfgIn.DataDbPass, actTrgCfgIn.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + dataDBOut, err := NewMigratorDataDB(actTrgCfgOut.DataDbType, + actTrgCfgOut.DataDbHost, actTrgCfgOut.DataDbPort, actTrgCfgOut.DataDbName, + actTrgCfgOut.DataDbUser, actTrgCfgOut.DataDbPass, actTrgCfgOut.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + actTrgMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) + if err != nil { + log.Fatal(err) + } +} + +func testActTrgITFlush(t *testing.T) { + actTrgMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(actTrgMigrator.dmOut.DataManager().DataDB()); err != nil { + t.Error("Error ", err.Error()) + } +} + +func testActTrgITMigrateAndMove(t *testing.T) { + tim := time.Date(2012, time.February, 27, 23, 59, 59, 0, time.UTC) + v1actTrg := &v1ActionTriggers{ + &v1ActionTrigger{ + Id: "Test", + BalanceType: "*monetary", + BalanceDirection: "*out", + ThresholdType: "*max_balance", + ThresholdValue: 2, + ActionsId: "TEST_ACTIONS", + Executed: true, + BalanceExpirationDate: tim, + }, + } + actTrg := engine.ActionTriggers{ + &engine.ActionTrigger{ + ID: "Test", + Balance: &engine.BalanceFilter{ + Timings: []*engine.RITiming{}, + ExpirationDate: utils.TimePointer(tim), + Type: utils.StringPointer(utils.MONETARY), + Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), + }, + ExpirationDate: tim, + LastExecutionTime: tim, + ActivationDate: tim, + ThresholdType: utils.TRIGGER_MAX_BALANCE, + ThresholdValue: 2, + ActionsID: "TEST_ACTIONS", + Executed: true, + }, + } + + switch accAction { + case utils.Migrate: + err := actTrgMigrator.dmIN.setV1ActionTriggers(v1actTrg) + if err != nil { + t.Error("Error when setting v1 ActionTriggers ", err.Error()) + } + currentVersion := engine.Versions{utils.StatS: 2, utils.Thresholds: 2, utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 1, utils.ActionPlans: 2, utils.SharedGroups: 2} + err = actTrgMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for ActionTriggers ", err.Error()) + } + err, _ = actTrgMigrator.Migrate([]string{utils.AccountActionPlansPrefix}) + if err != nil { + t.Error("Error when migrating ActionTriggers ", err.Error()) + } + result, err := actTrgMigrator.dmOut.DataManager().DataDB().GetActionTriggersDrv((*v1actTrg)[0].Id) + if err != nil { + t.Error("Error when getting ActionTriggers ", err.Error()) + } + if !reflect.DeepEqual(&actTrg, result) { + t.Errorf("Expecting: %+v, received: %+v", &actTrg, result) + } + case utils.Move: + if err := actTrgMigrator.dmIN.DataManager().DataDB().SetActionTriggersDrv((*v1actTrg)[0].Id, actTrg); err != nil { + t.Error("Error when setting ActionTriggers ", err.Error()) + } + currentVersion := engine.CurrentDataDBVersions() + err := actTrgMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for ActionTriggers ", err.Error()) + } + err, _ = actTrgMigrator.Migrate([]string{utils.MetaActionTriggers}) + if err != nil { + t.Error("Error when migrating ActionTriggers ", err.Error()) + } + result, err := actTrgMigrator.dmOut.DataManager().DataDB().GetActionTriggersDrv((*v1actTrg)[0].Id) + if err != nil { + t.Error("Error when getting ActionTriggers ", err.Error()) + } + if !reflect.DeepEqual(&actTrg, result) { + t.Errorf("Expecting: %+v, received: %+v", &actTrg, result) + } + } +} diff --git a/migrator/migrator_it_test.go b/migrator/migrator_it_test.go index c801778c0..499be7d7d 100644 --- a/migrator/migrator_it_test.go +++ b/migrator/migrator_it_test.go @@ -69,28 +69,6 @@ var sTestsITMigrator = []func(t *testing.T){ testMigratorTimings, testMigratorThreshold, testMigratorAttributeProfile, - //TPS - testMigratorTPRatingProfile, - testMigratorTPSuppliers, - testMigratorTPActions, - testMigratorTPAccountActions, - testMigratorTpActionTriggers, - testMigratorTpActionPlans, - testMigratorTpUsers, - testMigratorTpTimings, - testMigratorTpThreshold, - testMigratorTpStats, - testMigratorTpSharedGroups, - testMigratorTpResources, - testMigratorTpRatingProfiles, - testMigratorTpRatingPlans, - testMigratorTpRates, - testMigratorTpFilter, - testMigratorTpDestination, - testMigratorTpDestinationRate, - testMigratorTpDerivedChargers, - testMigratorTpCdrStats, - testMigratorTpAliases, testFlush, } @@ -1780,1113 +1758,4 @@ func testMigratorAttributeProfile(t *testing.T) { } } } - -//TP TESTS -func testMigratorTPRatingProfile(t *testing.T) { - tpRatingProfile := []*utils.TPRatingProfile{ - &utils.TPRatingProfile{ - TPid: "TPRProf1", - LoadId: "RPrf", - Direction: "*out", - Tenant: "Tenant1", - Category: "Category", - Subject: "Subject", - RatingPlanActivations: []*utils.TPRatingActivation{ - &utils.TPRatingActivation{ - ActivationTime: "2014-07-29T15:00:00Z", - RatingPlanId: "PlanOne", - FallbackSubjects: "FallBack", - CdrStatQueueIds: "RandomId", - }, - &utils.TPRatingActivation{ - ActivationTime: "2015-07-29T10:00:00Z", - RatingPlanId: "PlanTwo", - FallbackSubjects: "FallOut", - CdrStatQueueIds: "RandomIdTwo", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPRatingProfiles(tpRatingProfile); err != nil { - t.Error("Error when setting Stats ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for stats ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpRatingProfiles}) - if err != nil { - t.Error("Error when migrating Stats ", err.Error()) - } - result, err := mig.OutStorDB().GetTPRatingProfiles(tpRatingProfile[0]) - if err != nil { - t.Error("Error when getting Stats ", err.Error()) - } - if !reflect.DeepEqual(tpRatingProfile[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingProfile[0], result[0]) - } - } -} - -func testMigratorTPSuppliers(t *testing.T) { - tpSplPr := []*utils.TPSupplierProfile{ - &utils.TPSupplierProfile{ - TPid: "SupplierTPID12", - Tenant: "cgrates.org", - ID: "SUPL_1", - FilterIDs: []string{"FLTR_ACNT_dan"}, - ActivationInterval: &utils.TPActivationInterval{ - ActivationTime: "2014-07-29T15:00:00Z", - ExpiryTime: "", - }, - Sorting: "*lowest_cost", - SortingParameters: []string{"Parameter1"}, - Suppliers: []*utils.TPSupplier{ - &utils.TPSupplier{ - ID: "supplier1", - AccountIDs: []string{"Account1"}, - FilterIDs: []string{"FLTR_1"}, - RatingPlanIDs: []string{"RPL_1"}, - ResourceIDs: []string{"ResGroup1"}, - StatIDs: []string{"Stat1"}, - Weight: 10, - Blocker: false, - }, - }, - Weight: 20, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPSuppliers(tpSplPr); err != nil { - t.Error("Error when setting TpSupplier ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpSupplier ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpSuppliers}) - if err != nil { - t.Error("Error when migrating TpSupplier ", err.Error()) - } - result, err := mig.OutStorDB().GetTPSuppliers(tpSplPr[0].TPid, tpSplPr[0].ID) - if err != nil { - t.Error("Error when getting TPSupplier ", err.Error()) - } else if !reflect.DeepEqual(tpSplPr, result) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(tpSplPr), utils.ToJSON(result)) - } - } -} - -func testMigratorTPActions(t *testing.T) { - tpActions := []*utils.TPActions{ - &utils.TPActions{ - TPid: "TPAcc", - ID: "ID", - Actions: []*utils.TPAction{ - &utils.TPAction{ - Identifier: "*topup_reset", - BalanceId: "BalID", - BalanceType: "*data", - Directions: "*out", - Units: "10", - ExpiryTime: "*unlimited", - Filter: "", - TimingTags: "2014-01-14T00:00:00Z", - DestinationIds: "DST_1002", - RatingSubject: "SPECIAL_1002", - Categories: "", - SharedGroups: "SHARED_A", - BalanceWeight: "10", - ExtraParameters: "", - BalanceBlocker: "false", - BalanceDisabled: "false", - Weight: 10, - }, - &utils.TPAction{ - Identifier: "*log", - BalanceId: "BalID", - BalanceType: "*monetary", - Directions: "*out", - Units: "120", - ExpiryTime: "*unlimited", - Filter: "", - TimingTags: "2014-01-14T00:00:00Z", - DestinationIds: "*any", - RatingSubject: "SPECIAL_1002", - Categories: "", - SharedGroups: "SHARED_A", - BalanceWeight: "11", - ExtraParameters: "", - BalanceBlocker: "false", - BalanceDisabled: "false", - Weight: 11, - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPActions(tpActions); err != nil { - t.Error("Error when setting TpActions ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpActions ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpActions}) - if err != nil { - t.Error("Error when migrating TpActions ", err.Error()) - } - result, err := mig.OutStorDB().GetTPActions(tpActions[0].TPid, tpActions[0].ID) - if err != nil { - t.Error("Error when getting TpActions ", err.Error()) - } - if !reflect.DeepEqual(tpActions[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpActions[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpActions[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpActions[0].ID, result[0].ID) - } else if !reflect.DeepEqual(tpActions[0].Actions[0], result[0].Actions[0]) && - !reflect.DeepEqual(tpActions[0].Actions[0], result[0].Actions[1]) { - t.Errorf("Expecting: %+v, received: %+v", tpActions[0].Actions[0], result[0].Actions[0]) - } else if !reflect.DeepEqual(tpActions[0].Actions[1], result[0].Actions[1]) && - !reflect.DeepEqual(tpActions[0].Actions[1], result[0].Actions[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpActions[0].Actions[1], result[0].Actions[1]) - } - } -} - -func testMigratorTPAccountActions(t *testing.T) { - tpAccActions := []*utils.TPAccountActions{ - &utils.TPAccountActions{ - TPid: "TPAcc", - LoadId: "ID", - Tenant: "cgrates.org", - Account: "1001", - ActionPlanId: "PREPAID_10", - ActionTriggersId: "STANDARD_TRIGGERS", - AllowNegative: true, - Disabled: false, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPAccountActions(tpAccActions); err != nil { - t.Error("Error when setting TpAccountActions ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpAccountActions ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpAccountActions}) - if err != nil { - t.Error("Error when migrating TpAccountActions ", err.Error()) - } - result, err := mig.OutStorDB().GetTPAccountActions(&utils.TPAccountActions{TPid: "TPAcc"}) - if err != nil { - t.Error("Error when getting TpAccountActions ", err.Error()) - } - if !reflect.DeepEqual(tpAccActions[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpAccActions[0], result[0]) - } - } -} - -func testMigratorTpActionTriggers(t *testing.T) { - tpActionTriggers := []*utils.TPActionTriggers{ - &utils.TPActionTriggers{ - TPid: "TPAct", - ID: "STANDARD_TRIGGERS", - ActionTriggers: []*utils.TPActionTrigger{ - &utils.TPActionTrigger{ - Id: "STANDARD_TRIGGERS", - UniqueID: "", - ThresholdType: "*min_balance", - ThresholdValue: 2, - Recurrent: false, - MinSleep: "0", - ExpirationDate: "", - ActivationDate: "", - BalanceId: "", - BalanceType: "*monetary", - BalanceDirections: "*out", - BalanceDestinationIds: "FS_USERS", - BalanceWeight: "", - BalanceExpirationDate: "", - BalanceTimingTags: "", - BalanceRatingSubject: "", - BalanceCategories: "", - BalanceSharedGroups: "", - BalanceBlocker: "", - BalanceDisabled: "", - MinQueuedItems: 3, - ActionsId: "LOG_WARNING", - Weight: 10, - }, - &utils.TPActionTrigger{ - Id: "STANDARD_TRIGGERS", - UniqueID: "", - ThresholdType: "*max_event_counter", - ThresholdValue: 5, - Recurrent: false, - MinSleep: "0", - ExpirationDate: "", - ActivationDate: "", - BalanceId: "", - BalanceType: "*monetary", - BalanceDirections: "*out", - BalanceDestinationIds: "FS_USERS", - BalanceWeight: "", - BalanceExpirationDate: "", - BalanceTimingTags: "", - BalanceRatingSubject: "", - BalanceCategories: "", - BalanceSharedGroups: "", - BalanceBlocker: "", - BalanceDisabled: "", - MinQueuedItems: 3, - ActionsId: "LOG_WARNING", - Weight: 10, - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPActionTriggers(tpActionTriggers); err != nil { - t.Error("Error when setting TpActionTriggers ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpActionTriggers ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpActionTriggers}) - if err != nil { - t.Error("Error when migrating TpActionTriggers ", err.Error()) - } - result, err := mig.OutStorDB().GetTPActionTriggers(tpActionTriggers[0].TPid, tpActionTriggers[0].ID) - if err != nil { - t.Error("Error when getting TpAccountActions ", err.Error()) - } - if !reflect.DeepEqual(tpActionTriggers[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpActionTriggers[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpActionTriggers[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpActionTriggers[0].ID, result[0].ID) - } else if !reflect.DeepEqual(tpActionTriggers[0].ActionTriggers[0], result[0].ActionTriggers[0]) && - !reflect.DeepEqual(tpActionTriggers[0].ActionTriggers[0], result[0].ActionTriggers[1]) { - t.Errorf("Expecting: %+v, received: %+v", tpActionTriggers[0].ActionTriggers[0], result[0].ActionTriggers[0]) - } else if !reflect.DeepEqual(tpActionTriggers[0].ActionTriggers[1], result[0].ActionTriggers[1]) && - !reflect.DeepEqual(tpActionTriggers[0].ActionTriggers[1], result[0].ActionTriggers[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpActionTriggers[0].ActionTriggers[1], result[0].ActionTriggers[1]) - } - } -} - -func testMigratorTpActionPlans(t *testing.T) { - tpAccPlan := []*utils.TPActionPlan{ - &utils.TPActionPlan{ - TPid: "TPAcc", - ID: "ID", - ActionPlan: []*utils.TPActionTiming{ - &utils.TPActionTiming{ - ActionsId: "AccId", - TimingId: "TimingID", - Weight: 10, - }, - &utils.TPActionTiming{ - ActionsId: "AccId2", - TimingId: "TimingID2", - Weight: 11, - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPActionPlans(tpAccPlan); err != nil { - t.Error("Error when setting TpActionPlans ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpActionPlans ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpActionPlans}) - if err != nil { - t.Error("Error when migrating TpActionPlans ", err.Error()) - } - result, err := mig.OutStorDB().GetTPActionPlans(tpAccPlan[0].TPid, tpAccPlan[0].ID) - if err != nil { - t.Error("Error when getting TpActionPlans ", err.Error()) - } - if !reflect.DeepEqual(tpAccPlan[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpAccPlan[0], result[0]) - } - } -} - -func testMigratorTpUsers(t *testing.T) { - tpUser := []*utils.TPUsers{ - &utils.TPUsers{ - TPid: "TPU1", - UserName: "User1", - Tenant: "Tenant1", - Masked: true, - Weight: 20, - Profile: []*utils.TPUserProfile{ - &utils.TPUserProfile{ - AttrName: "UserProfile1", - AttrValue: "ValUP1", - }, - &utils.TPUserProfile{ - AttrName: "UserProfile2", - AttrValue: "ValUP2", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPUsers(tpUser); err != nil { - t.Error("Error when setting TpUsers ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpUsers ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpUsers}) - if err != nil { - t.Error("Error when migrating TpUsers ", err.Error()) - } - result, err := mig.OutStorDB().GetTPUsers(&utils.TPUsers{TPid: tpUser[0].TPid}) - if err != nil { - t.Error("Error when getting TpUsers ", err.Error()) - } - if !reflect.DeepEqual(tpUser[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpUser[0], result[0]) - } - } -} - -func testMigratorTpTimings(t *testing.T) { - tpTiming := []*utils.ApierTPTiming{&utils.ApierTPTiming{ - TPid: "TPT1", - ID: "Timing", - Years: "2017", - Months: "05", - MonthDays: "01", - WeekDays: "1", - Time: "15:00:00Z", - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPTimings(tpTiming); err != nil { - t.Error("Error when setting TpTiming ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpTiming ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpTiming}) - if err != nil { - t.Error("Error when migrating TpTiming ", err.Error()) - } - result, err := mig.OutStorDB().GetTPTimings(tpTiming[0].TPid, tpTiming[0].ID) - if err != nil { - t.Error("Error when getting TpTiming ", err.Error()) - } - if !reflect.DeepEqual(tpTiming[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpTiming[0], result[0]) - } - } -} - -func testMigratorTpThreshold(t *testing.T) { - tpThreshold := []*utils.TPThreshold{ - &utils.TPThreshold{ - TPid: "TH1", - Tenant: "cgrates.org", - ID: "Threhold", - FilterIDs: []string{"FLTR_1", "FLTR_2"}, - ActivationInterval: &utils.TPActivationInterval{ - ActivationTime: "2014-07-29T15:00:00Z", - ExpiryTime: "", - }, - Recurrent: true, - MinSleep: "1s", - Blocker: true, - Weight: 10, - ActionIDs: []string{"Thresh1", "Thresh2"}, - Async: true, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPThresholds(tpThreshold); err != nil { - t.Error("Error when setting TpThreshold ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpThreshold ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpThresholds}) - if err != nil { - t.Error("Error when migrating TpThreshold ", err.Error()) - } - result, err := mig.OutStorDB().GetTPThresholds(tpThreshold[0].TPid, tpThreshold[0].ID) - if err != nil { - t.Error("Error when getting TpThreshold ", err.Error()) - } - if !reflect.DeepEqual(tpThreshold[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpThreshold[0], result[0]) - } - } -} - -func testMigratorTpStats(t *testing.T) { - tpStat := []*utils.TPStats{ - &utils.TPStats{ - Tenant: "cgrates.org", - TPid: "TPS1", - ID: "Stat1", - FilterIDs: []string{"FLTR_1"}, - ActivationInterval: &utils.TPActivationInterval{ - ActivationTime: "2014-07-29T15:00:00Z", - ExpiryTime: "", - }, - TTL: "1", - Metrics: []*utils.MetricWithParams{ - &utils.MetricWithParams{MetricID: "MetricValue", Parameters: ""}, - &utils.MetricWithParams{MetricID: "MetricValueTwo", Parameters: ""}, - }, - Blocker: false, - Stored: false, - Weight: 20, - MinItems: 1, - ThresholdIDs: []string{"ThreshValue", "ThreshValueTwo"}, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPStats(tpStat); err != nil { - t.Error("Error when setting TpStats ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpStats ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpStats}) - if err != nil { - t.Error("Error when migrating TpStats ", err.Error()) - } - result, err := mig.OutStorDB().GetTPStats(tpStat[0].TPid, tpStat[0].ID) - if err != nil { - t.Error("Error when getting TpStats ", err.Error()) - } - if !reflect.DeepEqual(tpStat[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpStat[0], result[0]) - } - } -} - -func testMigratorTpSharedGroups(t *testing.T) { - tpSharedGroups := []*utils.TPSharedGroups{ - &utils.TPSharedGroups{ - TPid: "Tpi", - ID: "TpSg", - SharedGroups: []*utils.TPSharedGroup{ - &utils.TPSharedGroup{ - Account: "AccOne", - Strategy: "StrategyOne", - RatingSubject: "SubOne", - }, - &utils.TPSharedGroup{ - Account: "AccTow", - Strategy: "StrategyTwo", - RatingSubject: "SubTwo", - }, - &utils.TPSharedGroup{ - Account: "AccPlus", - Strategy: "StrategyPlus", - RatingSubject: "SubPlus", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPSharedGroups(tpSharedGroups); err != nil { - t.Error("Error when setting TpSharedGroups ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpSharedGroups ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpSharedGroups}) - if err != nil { - t.Error("Error when migrating TpSharedGroups ", err.Error()) - } - result, err := mig.OutStorDB().GetTPSharedGroups(tpSharedGroups[0].TPid, tpSharedGroups[0].ID) - if err != nil { - t.Error("Error when getting TpSharedGroups ", err.Error()) - } - if !reflect.DeepEqual(tpSharedGroups[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpSharedGroups[0], result[0]) - } - } -} - -func testMigratorTpResources(t *testing.T) { - tpRes := []*utils.TPResource{ - &utils.TPResource{ - Tenant: "cgrates.org", - TPid: "TPR1", - ID: "ResGroup1", - FilterIDs: []string{"FLTR_1"}, - ActivationInterval: &utils.TPActivationInterval{ - ActivationTime: "2014-07-29T15:00:00Z", - ExpiryTime: "", - }, - UsageTTL: "1s", - Limit: "7", - AllocationMessage: "", - Blocker: true, - Stored: true, - Weight: 20, - ThresholdIDs: []string{"ValOne", "ValTwo"}, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPResources(tpRes); err != nil { - t.Error("Error when setting TpResources ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpResources ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpResources}) - if err != nil { - t.Error("Error when migrating TpResources ", err.Error()) - } - result, err := mig.OutStorDB().GetTPResources(tpRes[0].TPid, tpRes[0].ID) - if err != nil { - t.Error("Error when getting TpResources ", err.Error()) - } - if !reflect.DeepEqual(tpRes[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpRes[0], result[0]) - } - } -} - -func testMigratorTpRatingProfiles(t *testing.T) { - tpRatingProfile := []*utils.TPRatingProfile{ - &utils.TPRatingProfile{ - TPid: "TPRProf1", - LoadId: "RPrf", - Direction: "*out", - Tenant: "Tenant1", - Category: "Category", - Subject: "Subject", - RatingPlanActivations: []*utils.TPRatingActivation{ - &utils.TPRatingActivation{ - ActivationTime: "2014-07-29T15:00:00Z", - RatingPlanId: "PlanOne", - FallbackSubjects: "FallBack", - CdrStatQueueIds: "RandomId", - }, - &utils.TPRatingActivation{ - ActivationTime: "2015-07-29T10:00:00Z", - RatingPlanId: "PlanTwo", - FallbackSubjects: "FallOut", - CdrStatQueueIds: "RandomIdTwo", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPRatingProfiles(tpRatingProfile); err != nil { - t.Error("Error when setting TpRatingProfiles ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpRatingProfiles ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpRatingProfiles}) - if err != nil { - t.Error("Error when migrating TpRatingProfiles ", err.Error()) - } - result, err := mig.OutStorDB().GetTPRatingProfiles(&utils.TPRatingProfile{TPid: tpRatingProfile[0].TPid}) - if err != nil { - t.Error("Error when getting TpRatingProfiles ", err.Error()) - } - if !reflect.DeepEqual(tpRatingProfile[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingProfile[0], result[0]) - } - } -} - -func testMigratorTpRatingPlans(t *testing.T) { - tpRatingPlan := []*utils.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, - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPRatingPlans(tpRatingPlan); err != nil { - t.Error("Error when setting TpRatingPlans ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpRatingPlans ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpRatingPlans}) - if err != nil { - t.Error("Error when migrating TpRatingPlans ", err.Error()) - } - result, err := mig.OutStorDB().GetTPRatingPlans("TPRP1", "Plan1", nil) - if err != nil { - t.Error("Error when getting TpRatingPlans ", err.Error()) - } - if !reflect.DeepEqual(tpRatingPlan[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingPlan[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpRatingPlan[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingPlan[0].ID, result[0].ID) - } else if !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[0], result[0].RatingPlanBindings[0]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[1], result[0].RatingPlanBindings[1]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[1], result[0].RatingPlanBindings[0]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[0], result[0].RatingPlanBindings[1]) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingPlan[0].RatingPlanBindings[0], result[0].RatingPlanBindings[0]) - } else if !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[0], result[0].RatingPlanBindings[0]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[1], result[0].RatingPlanBindings[1]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[1], result[0].RatingPlanBindings[0]) && - !reflect.DeepEqual(tpRatingPlan[0].RatingPlanBindings[0], result[0].RatingPlanBindings[1]) { - t.Errorf("Expecting: %+v, received: %+v", tpRatingPlan[0].RatingPlanBindings[1], result[0].RatingPlanBindings[1]) - } - - } -} - -func testMigratorTpRates(t *testing.T) { - tpRate := []*utils.TPRate{ - &utils.TPRate{ - TPid: "TPidTpRate", - ID: "RT_FS_USERS", - RateSlots: []*utils.RateSlot{ - &utils.RateSlot{ - ConnectFee: 12, - Rate: 3, - RateUnit: "6s", - RateIncrement: "6s", - GroupIntervalStart: "0s", - }, - &utils.RateSlot{ - ConnectFee: 12, - Rate: 3, - RateUnit: "4s", - RateIncrement: "6s", - GroupIntervalStart: "1s", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPRates(tpRate); err != nil { - t.Error("Error when setting TpRates ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpRates ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpRates}) - if err != nil { - t.Error("Error when migrating TpRates ", err.Error()) - } - result, err := mig.OutStorDB().GetTPRates(tpRate[0].TPid, tpRate[0].ID) - if err != nil { - t.Error("Error when getting TpRates ", err.Error()) - } - if !reflect.DeepEqual(tpRate[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpRate[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].ID, result[0].ID) - } - if !reflect.DeepEqual(tpRate[0].RateSlots[0].ConnectFee, result[0].RateSlots[0].ConnectFee) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[0].ConnectFee, result[0].RateSlots[0].ConnectFee) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[0].Rate, result[0].RateSlots[0].Rate) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[0].Rate, result[0].RateSlots[0].Rate) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[0].RateUnit, result[0].RateSlots[0].RateUnit) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[0].RateUnit, result[0].RateSlots[0].RateUnit) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[0].RateIncrement, result[0].RateSlots[0].RateIncrement) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[0].RateIncrement, result[0].RateSlots[0].RateIncrement) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[0].GroupIntervalStart, result[0].RateSlots[0].GroupIntervalStart) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[0].GroupIntervalStart, result[0].RateSlots[0].GroupIntervalStart) - } - if !reflect.DeepEqual(tpRate[0].RateSlots[1].ConnectFee, result[0].RateSlots[1].ConnectFee) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[1].ConnectFee, result[0].RateSlots[1].ConnectFee) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[1].Rate, result[0].RateSlots[1].Rate) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[1].Rate, result[0].RateSlots[1].Rate) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[1].RateUnit, result[0].RateSlots[1].RateUnit) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[1].RateUnit, result[0].RateSlots[1].RateUnit) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[1].RateIncrement, result[0].RateSlots[1].RateIncrement) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[1].RateIncrement, result[0].RateSlots[1].RateIncrement) - } else if !reflect.DeepEqual(tpRate[0].RateSlots[1].GroupIntervalStart, result[0].RateSlots[1].GroupIntervalStart) { - t.Errorf("Expecting: %+v, received: %+v", tpRate[0].RateSlots[1].GroupIntervalStart, result[0].RateSlots[1].GroupIntervalStart) - } - } -} - -func testMigratorTpFilter(t *testing.T) { - tpFilter := []*utils.TPFilterProfile{ - &utils.TPFilterProfile{ - TPid: "TP1", - Tenant: "cgrates.org", - ID: "Filter", - Filters: []*utils.TPFilter{ - &utils.TPFilter{ - Type: "*string", - FieldName: "Account", - Values: []string{"1001", "1002"}, - }, - }, - ActivationInterval: &utils.TPActivationInterval{ - ActivationTime: "2014-07-29T15:00:00Z", - ExpiryTime: "", - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPFilters(tpFilter); err != nil { - t.Error("Error when setting TpFilter ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpFilter ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpFilters}) - if err != nil { - t.Error("Error when migrating TpFilter ", err.Error()) - } - result, err := mig.OutStorDB().GetTPFilters(tpFilter[0].TPid, tpFilter[0].ID) - if err != nil { - t.Error("Error when getting TpFilter ", err.Error()) - } - if !reflect.DeepEqual(tpFilter[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpFilter[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpFilter[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpFilter[0].ID, result[0].ID) - } else if !reflect.DeepEqual(tpFilter[0].Filters, result[0].Filters) { - t.Errorf("Expecting: %+v, received: %+v", tpFilter[0].Filters, result[0].Filters) - } else if !reflect.DeepEqual(tpFilter[0].ActivationInterval, result[0].ActivationInterval) { - t.Errorf("Expecting: %+v, received: %+v", tpFilter[0].ActivationInterval, result[0].ActivationInterval) - } - } -} - -func testMigratorTpDestination(t *testing.T) { - tpDestination := []*utils.TPDestination{ - &utils.TPDestination{ - TPid: "TPD", - ID: "GERMANY", - Prefixes: []string{"+49", "+4915"}, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPDestinations(tpDestination); err != nil { - t.Error("Error when setting TpDestination ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpDestination ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpDestinations}) - if err != nil { - t.Error("Error when migrating TpDestination ", err.Error()) - } - result, err := mig.OutStorDB().GetTPDestinations(tpDestination[0].TPid, tpDestination[0].ID) - if err != nil { - t.Error("Error when getting TpDestination ", err.Error()) - } - if !reflect.DeepEqual(tpDestination[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpDestination[0], result[0]) - } - } -} - -func testMigratorTpDestinationRate(t *testing.T) { - tpDestRate := []*utils.TPDestinationRate{ - &utils.TPDestinationRate{ - TPid: "testTPid", - ID: "1", - DestinationRates: []*utils.DestinationRate{ - &utils.DestinationRate{ - DestinationId: "GERMANY", - RateId: "RT_1CENT", - RoundingMethod: "*up", - RoundingDecimals: 0, - MaxCost: 0.0, - MaxCostStrategy: "", - }, - }, - }, - } - - switch action { - case Move: - if err := mig.InStorDB().SetTPDestinationRates(tpDestRate); err != nil { - t.Error("Error when setting TpDestinationRate ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpDestinationRate ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpDestinationRates}) - if err != nil { - t.Error("Error when migrating TpDestinationRate ", err.Error()) - } //OutStorDB - result, err := mig.InStorDB().GetTPDestinationRates("testTPid", "", nil) - if err != nil { - t.Error("Error when getting TpDestinationRate ", err.Error()) - } - if !reflect.DeepEqual(tpDestRate[0].TPid, result[0].TPid) { - t.Errorf("Expecting: %+v, received: %+v", tpDestRate[0].TPid, result[0].TPid) - } else if !reflect.DeepEqual(tpDestRate[0].ID, result[0].ID) { - t.Errorf("Expecting: %+v, received: %+v", tpDestRate[0].ID, result[0].ID) - } - if !reflect.DeepEqual(tpDestRate[0].DestinationRates[0].DestinationId, result[0].DestinationRates[0].DestinationId) { - t.Errorf("Expecting: %+v, received: %+v", tpDestRate[0].DestinationRates[0].DestinationId, result[0].DestinationRates[0].DestinationId) - } else if !reflect.DeepEqual(tpDestRate[0].DestinationRates[0].RateId, result[0].DestinationRates[0].RateId) { - t.Errorf("Expecting: %+v, received: %+v", tpDestRate[0].DestinationRates[0].RateId, result[0].DestinationRates[0].RateId) - } else if !reflect.DeepEqual(tpDestRate[0].DestinationRates[0], result[0].DestinationRates[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpDestRate[0].DestinationRates[0], result[0].DestinationRates[0]) - } - } -} - -func testMigratorTpDerivedChargers(t *testing.T) { - tpDerivedChargers := []*utils.TPDerivedChargers{ - &utils.TPDerivedChargers{ - TPid: "TPD", - LoadId: "LoadID", - Direction: "*out", - Tenant: "cgrates.org", - Category: "call", - Account: "1001", - Subject: "1001", - DestinationIds: "", - DerivedChargers: []*utils.TPDerivedCharger{ - &utils.TPDerivedCharger{ - RunId: "derived_run1", - RunFilters: "", - ReqTypeField: "^*rated", - DirectionField: "*default", - TenantField: "*default", - CategoryField: "*default", - AccountField: "*default", - SubjectField: "^1002", - DestinationField: "*default", - SetupTimeField: "*default", - PddField: "*default", - AnswerTimeField: "*default", - UsageField: "*default", - SupplierField: "*default", - DisconnectCauseField: "*default", - CostField: "*default", - RatedField: "*default", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPDerivedChargers(tpDerivedChargers); err != nil { - t.Error("Error when setting TpDerivedChargers ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpDerivedChargers ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpDerivedChargers}) - if err != nil { - t.Error("Error when migrating TpDerivedChargers ", err.Error()) - } - result, err := mig.OutStorDB().GetTPDerivedChargers(&utils.TPDerivedChargers{TPid: tpDerivedChargers[0].TPid}) - if err != nil { - t.Error("Error when getting TpDerivedChargers ", err.Error()) - } - if !reflect.DeepEqual(tpDerivedChargers[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpDerivedChargers[0], result[0]) - } - } -} - -func testMigratorTpCdrStats(t *testing.T) { - tpCdrStats := []*utils.TPCdrStats{ - &utils.TPCdrStats{ - TPid: "TPCdr", - ID: "ID", - CdrStats: []*utils.TPCdrStat{ - &utils.TPCdrStat{ - QueueLength: "10", - TimeWindow: "0", - SaveInterval: "10s", - Metrics: "ASR", - SetupInterval: "", - TORs: "", - CdrHosts: "", - CdrSources: "", - ReqTypes: "", - Directions: "", - Tenants: "cgrates.org", - Categories: "", - Accounts: "", - Subjects: "1001", - DestinationIds: "1003", - PddInterval: "", - UsageInterval: "", - Suppliers: "suppl1", - DisconnectCauses: "", - MediationRunIds: "*default", - RatedAccounts: "", - RatedSubjects: "", - CostInterval: "", - ActionTriggers: "CDRST1_WARN", - }, - &utils.TPCdrStat{ - QueueLength: "10", - TimeWindow: "0", - SaveInterval: "10s", - Metrics: "ACC", - SetupInterval: "", - TORs: "", - CdrHosts: "", - CdrSources: "", - ReqTypes: "", - Directions: "", - Tenants: "cgrates.org", - Categories: "", - Accounts: "", - Subjects: "1002", - DestinationIds: "1003", - PddInterval: "", - UsageInterval: "", - Suppliers: "suppl1", - DisconnectCauses: "", - MediationRunIds: "*default", - RatedAccounts: "", - RatedSubjects: "", - CostInterval: "", - ActionTriggers: "CDRST1_WARN", - }, - }, - }, - } - switch action { - case Move: - if err := mig.InStorDB().SetTPCdrStats(tpCdrStats); err != nil { - t.Error("Error when setting TpCdrStats ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpCdrStats ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpCdrStats}) - if err != nil { - t.Error("Error when migrating TpCdrStats ", err.Error()) - } - result, err := mig.OutStorDB().GetTPCdrStats(tpCdrStats[0].TPid, tpCdrStats[0].ID) - if err != nil { - t.Error("Error when getting TpCdrStats ", err.Error()) - } - if !reflect.DeepEqual(tpCdrStats[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpCdrStats[0], result[0]) - } - } -} - -func testMigratorTpAliases(t *testing.T) { - tpAliases := []*utils.TPAliases{ - &utils.TPAliases{ - TPid: "tpID", - Direction: "*out", - Tenant: "cgrates.org", - Category: "call", - Account: "1001", - Subject: "1002", - Context: "", - Values: []*utils.TPAliasValue{ - &utils.TPAliasValue{ - DestinationId: "1002", - Target: "1002", - Original: "1002", - Alias: "1002", - Weight: 20.0, - }, - }, - }, - } - - switch action { - case Move: - if err := mig.InStorDB().SetTPAliases(tpAliases); err != nil { - t.Error("Error when setting TpAliases ", err.Error()) - } - currentVersion := engine.CurrentStorDBVersions() - err := mig.OutStorDB().SetVersions(currentVersion, false) - if err != nil { - t.Error("Error when setting version for TpAliases ", err.Error()) - } - err, _ = mig.Migrate([]string{utils.MetaTpAliases}) - if err != nil { - t.Error("Error when migrating TpAliases ", err.Error()) - } - result, err := mig.OutStorDB().GetTPAliases(&utils.TPAliases{TPid: tpAliases[0].TPid}) - if err != nil { - t.Error("Error when getting TpAliases ", err.Error()) - } - if !reflect.DeepEqual(tpAliases[0], result[0]) { - t.Errorf("Expecting: %+v, received: %+v", tpAliases[0], result[0]) - } - } -} */ diff --git a/migrator/sharedgroup_it_test.go b/migrator/sharedgroup_it_test.go new file mode 100644 index 000000000..0924dfabb --- /dev/null +++ b/migrator/sharedgroup_it_test.go @@ -0,0 +1,226 @@ +// +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 migrator + +import ( + "log" + "path" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + shrGrpPathIn string + shrGrpPathOut string + shrGrpCfgIn *config.CGRConfig + shrGrpCfgOut *config.CGRConfig + shrGrpMigrator *Migrator + shrSharedGroup string +) + +var sTestsShrGrpIT = []func(t *testing.T){ + testShrGrpITConnect, + testShrGrpITFlush, + testShrGrpITMigrateAndMove, +} + +func TestSharedGroupITRedis(t *testing.T) { + var err error + shrGrpPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql") + shrGrpCfgIn, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrGrpCfgOut, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrSharedGroup = utils.Migrate + for _, stest := range sTestsShrGrpIT { + t.Run("TestSharedGroupITMigrateRedis", stest) + } +} + +func TestSharedGroupITMongo(t *testing.T) { + var err error + shrGrpPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + shrGrpCfgIn, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrGrpCfgOut, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrSharedGroup = utils.Migrate + for _, stest := range sTestsShrGrpIT { + t.Run("TestSharedGroupITMigrateMongo", stest) + } +} + +func TestSharedGroupITMove(t *testing.T) { + var err error + shrGrpPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + shrGrpCfgIn, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrGrpPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql") + shrGrpCfgOut, err = config.NewCGRConfigFromFolder(shrGrpPathOut) + if err != nil { + t.Fatal(err) + } + shrSharedGroup = utils.Move + for _, stest := range sTestsShrGrpIT { + t.Run("TestSharedGroupITMove", stest) + } +} + +func TestSharedGroupITMoveEncoding(t *testing.T) { + var err error + shrGrpPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + shrGrpCfgIn, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrGrpPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + shrGrpCfgOut, err = config.NewCGRConfigFromFolder(shrGrpPathOut) + if err != nil { + t.Fatal(err) + } + shrSharedGroup = utils.Move + for _, stest := range sTestsShrGrpIT { + t.Run("TestSharedGroupITMoveEncoding", stest) + } +} + +func TestSharedGroupITMoveEncoding2(t *testing.T) { + var err error + shrGrpPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + shrGrpCfgIn, err = config.NewCGRConfigFromFolder(shrGrpPathIn) + if err != nil { + t.Fatal(err) + } + shrGrpPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + shrGrpCfgOut, err = config.NewCGRConfigFromFolder(shrGrpPathOut) + if err != nil { + t.Fatal(err) + } + shrSharedGroup = utils.Move + for _, stest := range sTestsShrGrpIT { + t.Run("TestSharedGroupITMoveEncoding2", stest) + } +} + +func testShrGrpITConnect(t *testing.T) { + dataDBIn, err := NewMigratorDataDB(shrGrpCfgIn.DataDbType, + shrGrpCfgIn.DataDbHost, shrGrpCfgIn.DataDbPort, shrGrpCfgIn.DataDbName, + shrGrpCfgIn.DataDbUser, shrGrpCfgIn.DataDbPass, shrGrpCfgIn.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + dataDBOut, err := NewMigratorDataDB(shrGrpCfgOut.DataDbType, + shrGrpCfgOut.DataDbHost, shrGrpCfgOut.DataDbPort, shrGrpCfgOut.DataDbName, + shrGrpCfgOut.DataDbUser, shrGrpCfgOut.DataDbPass, shrGrpCfgOut.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + shrGrpMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) + if err != nil { + log.Fatal(err) + } +} + +func testShrGrpITFlush(t *testing.T) { + shrGrpMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(shrGrpMigrator.dmOut.DataManager().DataDB()); err != nil { + t.Error("Error ", err.Error()) + } +} + +func testShrGrpITMigrateAndMove(t *testing.T) { + v1shrGrp := &v1SharedGroup{ + Id: "Test", + AccountParameters: map[string]*engine.SharingParameters{ + "test": &engine.SharingParameters{Strategy: "*highest"}, + }, + MemberIds: []string{"1", "2", "3"}, + } + shrGrp := &engine.SharedGroup{ + Id: "Test", + AccountParameters: map[string]*engine.SharingParameters{ + "test": &engine.SharingParameters{Strategy: "*highest"}, + }, + MemberIds: utils.NewStringMap("1", "2", "3"), + } + + switch accAction { + case utils.Migrate: + err := shrGrpMigrator.dmIN.setV1SharedGroup(v1shrGrp) + if err != nil { + t.Error("Error when setting v1 SharedGroup ", err.Error()) + } + currentVersion := engine.Versions{utils.StatS: 2, utils.Thresholds: 2, utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 1} + err = shrGrpMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for SharedGroup ", err.Error()) + } + err, _ = shrGrpMigrator.Migrate([]string{utils.MetaSharedGroups}) + if err != nil { + t.Error("Error when migrating SharedGroup ", err.Error()) + } + result, err := shrGrpMigrator.dmOut.DataManager().DataDB().GetSharedGroupDrv(v1shrGrp.Id) + if err != nil { + t.Error("Error when getting SharedGroup ", err.Error()) + } + if !reflect.DeepEqual(&shrGrp, result) { + t.Errorf("Expecting: %+v, received: %+v", &shrGrp, result) + } + case utils.Move: + if err := shrGrpMigrator.dmIN.DataManager().DataDB().SetSharedGroupDrv(shrGrp); err != nil { + t.Error("Error when setting SharedGroup ", err.Error()) + } + currentVersion := engine.CurrentDataDBVersions() + err := shrGrpMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for SharedGroup ", err.Error()) + } + err, _ = shrGrpMigrator.Migrate([]string{utils.MetaSharedGroups}) + if err != nil { + t.Error("Error when migrating SharedGroup ", err.Error()) + } + result, err := shrGrpMigrator.dmOut.DataManager().DataDB().GetSharedGroupDrv(v1shrGrp.Id) + if err != nil { + t.Error("Error when getting SharedGroup ", err.Error()) + } + if !reflect.DeepEqual(&shrGrp, result) { + t.Errorf("Expecting: %+v, received: %+v", &shrGrp, result) + } + } +} diff --git a/migrator/stats_it_test.go b/migrator/stats_it_test.go index d35d99f10..34b39a962 100755 --- a/migrator/stats_it_test.go +++ b/migrator/stats_it_test.go @@ -20,7 +20,6 @@ along with this program. If not, see package migrator -/* import ( "log" "path" @@ -102,38 +101,31 @@ func TestStatsQueueITMove(t *testing.T) { } func testStsITConnect(t *testing.T) { - dataDBIn, err := engine.ConfigureDataStorage(stsCfgIn.DataDbType, + dataDBIn, err := NewMigratorDataDB(stsCfgIn.DataDbType, stsCfgIn.DataDbHost, stsCfgIn.DataDbPort, stsCfgIn.DataDbName, stsCfgIn.DataDbUser, stsCfgIn.DataDbPass, stsCfgIn.DBDataEncoding, config.CgrConfig().CacheCfg(), *loadHistorySize) if err != nil { log.Fatal(err) } - dataDBOut, err := engine.ConfigureDataStorage(stsCfgOut.DataDbType, + dataDBOut, err := NewMigratorDataDB(stsCfgOut.DataDbType, stsCfgOut.DataDbHost, stsCfgOut.DataDbPort, stsCfgOut.DataDbName, stsCfgOut.DataDbUser, stsCfgOut.DataDbPass, stsCfgOut.DBDataEncoding, config.CgrConfig().CacheCfg(), *loadHistorySize) if err != nil { log.Fatal(err) } - oldDataDB, err := ConfigureV1DataStorage(stsCfgIn.DataDbType, - stsCfgIn.DataDbHost, stsCfgIn.DataDbPort, stsCfgIn.DataDbName, - stsCfgIn.DataDbUser, stsCfgIn.DataDbPass, stsCfgIn.DBDataEncoding) - if err != nil { - log.Fatal(err) - } - stsMigrator, err = NewMigrator(dataDBIn, dataDBOut, stsCfgIn.DataDbType, - stsCfgIn.DBDataEncoding, nil, nil, stsCfgIn.StorDBType, oldDataDB, - stsCfgIn.DataDbType, stsCfgIn.DBDataEncoding, nil, stsCfgIn.StorDBType, - false, false, false, false, false) + stsMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) if err != nil { log.Fatal(err) } } func testStsITFlush(t *testing.T) { - stsMigrator.dmOut.DataDB().Flush("") - if err := engine.SetDBVersions(stsMigrator.dmOut.DataDB()); err != nil { + stsMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(stsMigrator.dmOut.DataManager().DataDB()); err != nil { t.Error("Error ", err.Error()) } } @@ -237,7 +229,7 @@ func testStsITMigrateAndMove(t *testing.T) { } switch stsAction { case utils.Migrate: - err := stsMigrator.oldDataDB.setV1Stats(v1Sts) + err := stsMigrator.dmIN.setV1Stats(v1Sts) if err != nil { t.Error("Error when setting v1Stat ", err.Error()) } @@ -249,7 +241,7 @@ func testStsITMigrateAndMove(t *testing.T) { utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2} - err = stsMigrator.dmOut.DataDB().SetVersions(currentVersion, false) + err = stsMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) if err != nil { t.Error("Error when setting version for stats ", err.Error()) } @@ -258,8 +250,7 @@ func testStsITMigrateAndMove(t *testing.T) { t.Error("Error when migrating Stats ", err.Error()) } - result, err := stsMigrator.dmOut.GetStatQueueProfile("cgrates.org", - v1Sts.Id, true, utils.NonTransactional) + result, err := stsMigrator.dmOut.DataManager().DataDB().GetStatQueueProfileDrv("cgrates.org", v1Sts.Id) if err != nil { t.Error("Error when getting Stats ", err.Error()) } @@ -267,8 +258,7 @@ func testStsITMigrateAndMove(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sqp, result) } - result1, err := stsMigrator.dmOut.GetFilter("cgrates.org", - v1Sts.Id, true, utils.NonTransactional) + result1, err := stsMigrator.dmOut.DataManager().DataDB().GetFilterDrv("cgrates.org", v1Sts.Id) if err != nil { t.Error("Error when getting Stats ", err.Error()) } @@ -278,7 +268,7 @@ func testStsITMigrateAndMove(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", len(filter.Rules), len(result1.Rules)) } - result2, err := stsMigrator.dmOut.GetStatQueue("cgrates.org", sq.ID, true, utils.NonTransactional) + result2, err := stsMigrator.dmOut.DataManager().GetStatQueue("cgrates.org", sq.ID, true, utils.NonTransactional) if err != nil { t.Error("Error when getting Stats ", err.Error()) } @@ -287,14 +277,14 @@ func testStsITMigrateAndMove(t *testing.T) { } case utils.Move: - if err := stsMigrator.dmIN.SetStatQueueProfile(sqp, true); err != nil { + if err := stsMigrator.dmIN.DataManager().DataDB().SetStatQueueProfileDrv(sqp); err != nil { t.Error("Error when setting Stats ", err.Error()) } - if err := stsMigrator.dmIN.SetStatQueue(sq); err != nil { + if err := stsMigrator.dmIN.DataManager().SetStatQueue(sq); err != nil { t.Error("Error when setting Stats ", err.Error()) } currentVersion := engine.CurrentDataDBVersions() - err := stsMigrator.dmOut.DataDB().SetVersions(currentVersion, false) + err := stsMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) if err != nil { t.Error("Error when setting version for stats ", err.Error()) } @@ -302,11 +292,11 @@ func testStsITMigrateAndMove(t *testing.T) { if err != nil { t.Error("Error when migrating Stats ", err.Error()) } - result, err := stsMigrator.dmOut.GetStatQueueProfile(sqp.Tenant, sqp.ID, true, utils.NonTransactional) + result, err := stsMigrator.dmOut.DataManager().DataDB().GetStatQueueProfileDrv(sqp.Tenant, sqp.ID) if err != nil { t.Error("Error when getting Stats ", err.Error()) } - result1, err := stsMigrator.dmOut.GetStatQueue(sq.Tenant, sq.ID, true, utils.NonTransactional) + result1, err := stsMigrator.dmOut.DataManager().GetStatQueue(sq.Tenant, sq.ID, true, utils.NonTransactional) if err != nil { t.Error("Error when getting Stats ", err.Error()) } @@ -319,4 +309,3 @@ func testStsITMigrateAndMove(t *testing.T) { } } -*/ diff --git a/migrator/thresholds_it_test.go b/migrator/thresholds_it_test.go new file mode 100644 index 000000000..7a0f8d481 --- /dev/null +++ b/migrator/thresholds_it_test.go @@ -0,0 +1,257 @@ +// +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 migrator + +import ( + "log" + "path" + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + trsPathIn string + trsPathOut string + trsCfgIn *config.CGRConfig + trsCfgOut *config.CGRConfig + trsMigrator *Migrator + trsThresholds string +) + +var sTestsTrsIT = []func(t *testing.T){ + testTrsITConnect, + testTrsITFlush, + testTrsITMigrateAndMove, +} + +func TestThresholdsITRedis(t *testing.T) { + var err error + trsPathIn = path.Join(*dataDir, "conf", "samples", "tutmysql") + trsCfgIn, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsCfgOut, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsThresholds = utils.Migrate + for _, stest := range sTestsTrsIT { + t.Run("TestThresholdsITMigrateRedis", stest) + } +} + +func TestThresholdsITMongo(t *testing.T) { + var err error + trsPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + trsCfgIn, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsCfgOut, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsThresholds = utils.Migrate + for _, stest := range sTestsTrsIT { + t.Run("TestThresholdsITMigrateMongo", stest) + } +} + +func TestThresholdsITMove(t *testing.T) { + var err error + trsPathIn = path.Join(*dataDir, "conf", "samples", "tutmongo") + trsCfgIn, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsPathOut = path.Join(*dataDir, "conf", "samples", "tutmysql") + trsCfgOut, err = config.NewCGRConfigFromFolder(trsPathOut) + if err != nil { + t.Fatal(err) + } + trsThresholds = utils.Move + for _, stest := range sTestsTrsIT { + t.Run("TestThresholdsITMove", stest) + } +} + +func TestThresholdsITMoveEncoding(t *testing.T) { + var err error + trsPathIn = path.Join(*dataDir, "conf", "samples", "tutmongojson") + trsCfgIn, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsPathOut = path.Join(*dataDir, "conf", "samples", "tutmongomsgpack") + trsCfgOut, err = config.NewCGRConfigFromFolder(trsPathOut) + if err != nil { + t.Fatal(err) + } + trsThresholds = utils.Move + for _, stest := range sTestsTrsIT { + t.Run("TestThresholdsITMoveEncoding", stest) + } +} + +func TestThresholdsITMoveEncoding2(t *testing.T) { + var err error + trsPathIn = path.Join(*dataDir, "conf", "samples", "tutmysqljson") + trsCfgIn, err = config.NewCGRConfigFromFolder(trsPathIn) + if err != nil { + t.Fatal(err) + } + trsPathOut = path.Join(*dataDir, "conf", "samples", "tutmysqlmsgpack") + trsCfgOut, err = config.NewCGRConfigFromFolder(trsPathOut) + if err != nil { + t.Fatal(err) + } + trsThresholds = utils.Move + for _, stest := range sTestsTrsIT { + t.Run("TestThresholdsITMoveEncoding2", stest) + } +} + +func testTrsITConnect(t *testing.T) { + dataDBIn, err := NewMigratorDataDB(trsCfgIn.DataDbType, + trsCfgIn.DataDbHost, trsCfgIn.DataDbPort, trsCfgIn.DataDbName, + trsCfgIn.DataDbUser, trsCfgIn.DataDbPass, trsCfgIn.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + dataDBOut, err := NewMigratorDataDB(trsCfgOut.DataDbType, + trsCfgOut.DataDbHost, trsCfgOut.DataDbPort, trsCfgOut.DataDbName, + trsCfgOut.DataDbUser, trsCfgOut.DataDbPass, trsCfgOut.DBDataEncoding, + config.CgrConfig().CacheCfg(), *loadHistorySize) + if err != nil { + log.Fatal(err) + } + trsMigrator, err = NewMigrator(dataDBIn, dataDBOut, + nil, nil, + false, false, false) + if err != nil { + log.Fatal(err) + } +} + +func testTrsITFlush(t *testing.T) { + trsMigrator.dmOut.DataManager().DataDB().Flush("") + if err := engine.SetDBVersions(trsMigrator.dmOut.DataManager().DataDB()); err != nil { + t.Error("Error ", err.Error()) + } +} + +func testTrsITMigrateAndMove(t *testing.T) { + tim := time.Date(2012, time.February, 27, 23, 59, 59, 0, time.UTC) + var filters []*engine.FilterRule + v1trs := &v2ActionTrigger{ + ID: "test2", // original csv tag + UniqueID: "testUUID", // individual id + ThresholdType: "*min_event_counter", //*min_event_counter, *max_event_counter, *min_balance_counter, *max_balance_counter, *min_balance, *max_balance, *balance_expired + ThresholdValue: 5.32, + Recurrent: false, // reset excuted flag each run + MinSleep: time.Duration(5) * time.Second, // Minimum duration between two executions in case of recurrent triggers + ExpirationDate: time.Now(), + ActivationDate: time.Now(), + Balance: &engine.BalanceFilter{ + ID: utils.StringPointer("TESTZ"), + Timings: []*engine.RITiming{}, + ExpirationDate: utils.TimePointer(tim), + Type: utils.StringPointer(utils.MONETARY), + Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), + }, + Weight: 0, + ActionsID: "Action1", + MinQueuedItems: 10, // Trigger actions only if this number is hit (stats only) + Executed: false, + LastExecutionTime: time.Now(), + } + x, err := engine.NewFilterRule(engine.MetaRSR, "Directions", v1trs.Balance.Directions.Slice()) + if err != nil { + t.Error("Error when creating new NewFilterRule", err.Error()) + } + filters = append(filters, x) + + filter := &engine.Filter{Tenant: config.CgrConfig().DefaultTenant, ID: *v1trs.Balance.ID, Rules: filters} + + tresProf := &engine.ThresholdProfile{ + ID: v1trs.ID, + Tenant: config.CgrConfig().DefaultTenant, + FilterIDs: []string{filter.ID}, + Blocker: false, + Weight: v1trs.Weight, + ActivationInterval: &utils.ActivationInterval{v1trs.ExpirationDate, v1trs.ActivationDate}, + MinSleep: v1trs.MinSleep, + } + tres := &engine.Threshold{ + Tenant: config.CgrConfig().DefaultTenant, + ID: v1trs.ID, + } + switch accAction { + case utils.Migrate: + err := trsMigrator.dmIN.setV2ActionTrigger(v1trs) + if err != nil { + t.Error("Error when setting v1 Thresholds ", err.Error()) + } + currentVersion := engine.Versions{utils.StatS: 2, utils.Thresholds: 1, utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2} + err = trsMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Thresholds ", err.Error()) + } + err, _ = trsMigrator.Migrate([]string{utils.MetaThresholds}) + if err != nil { + t.Error("Error when migrating Thresholds ", err.Error()) + } + result, err := trsMigrator.dmOut.DataManager().DataDB().GetThresholdDrv(tresProf.Tenant, tresProf.ID) + if err != nil { + t.Error("Error when getting Thresholds ", err.Error()) + } + if !reflect.DeepEqual(&tres, result) { + t.Errorf("Expecting: %+v, received: %+v", &tres, result) + } + case utils.Move: + if err := trsMigrator.dmIN.DataManager().DataDB().SetThresholdDrv(tres); err != nil { + t.Error("Error when setting Thresholds ", err.Error()) + } + currentVersion := engine.CurrentDataDBVersions() + err := trsMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Thresholds ", err.Error()) + } + err, _ = trsMigrator.Migrate([]string{utils.MetaSharedGroups}) + if err != nil { + t.Error("Error when migrating Thresholds ", err.Error()) + } + result, err := trsMigrator.dmOut.DataManager().DataDB().GetThresholdDrv(tresProf.Tenant, tresProf.ID) + if err != nil { + t.Error("Error when getting Thresholds ", err.Error()) + } + if !reflect.DeepEqual(&tres, result) { + t.Errorf("Expecting: %+v, received: %+v", &tres, result) + } + } +}