mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added new method and tests for it
This commit is contained in:
@@ -18,6 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package migrator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
@@ -70,3 +73,150 @@ type v1UnitsCounter struct {
|
||||
// Units float64
|
||||
Balances v1BalanceChain // first balance is the general one (no destination)
|
||||
}
|
||||
|
||||
func (v1Acc v1Account) AsAccount() (ac engine.Account, err error) {
|
||||
// transfer data into new structurse
|
||||
ac = engine.Account{
|
||||
ID: v1Acc.Id,
|
||||
BalanceMap: make(map[string]engine.Balances, len(v1Acc.BalanceMap)),
|
||||
UnitCounters: make(engine.UnitCounters, len(v1Acc.UnitCounters)),
|
||||
ActionTriggers: make(engine.ActionTriggers, len(v1Acc.ActionTriggers)),
|
||||
AllowNegative: v1Acc.AllowNegative,
|
||||
Disabled: v1Acc.Disabled,
|
||||
}
|
||||
idElements := strings.Split(ac.ID, utils.CONCATENATED_KEY_SEP)
|
||||
if len(idElements) != 3 {
|
||||
log.Printf("Malformed account ID %s", v1Acc.Id)
|
||||
}
|
||||
ac.ID = fmt.Sprintf("%s:%s", idElements[1], idElements[2])
|
||||
// balances
|
||||
for oldBalKey, oldBalChain := range v1Acc.BalanceMap {
|
||||
keyElements := strings.Split(oldBalKey, "*")
|
||||
newBalKey := "*" + keyElements[1]
|
||||
newBalDirection := "*" + idElements[0]
|
||||
ac.BalanceMap[newBalKey] = make(engine.Balances, len(oldBalChain))
|
||||
for index, oldBal := range oldBalChain {
|
||||
// check default to set new id
|
||||
ac.BalanceMap[newBalKey][index] = &engine.Balance{
|
||||
Uuid: oldBal.Uuid,
|
||||
ID: oldBal.Id,
|
||||
Value: oldBal.Value,
|
||||
Directions: utils.ParseStringMap(newBalDirection),
|
||||
ExpirationDate: oldBal.ExpirationDate,
|
||||
Weight: oldBal.Weight,
|
||||
DestinationIDs: utils.ParseStringMap(oldBal.DestinationIds),
|
||||
RatingSubject: oldBal.RatingSubject,
|
||||
Categories: utils.ParseStringMap(oldBal.Category),
|
||||
SharedGroups: utils.ParseStringMap(oldBal.SharedGroup),
|
||||
Timings: oldBal.Timings,
|
||||
TimingIDs: utils.ParseStringMap(oldBal.TimingIDs),
|
||||
Disabled: oldBal.Disabled,
|
||||
}
|
||||
}
|
||||
}
|
||||
// unit counters
|
||||
for _, oldUc := range v1Acc.UnitCounters {
|
||||
newUc := &engine.UnitCounter{Counters: make(engine.CounterFilters, len(oldUc.Balances))}
|
||||
for index, oldUcBal := range oldUc.Balances {
|
||||
bf := &engine.BalanceFilter{}
|
||||
if oldUcBal.Uuid != "" {
|
||||
bf.Uuid = utils.StringPointer(oldUcBal.Uuid)
|
||||
}
|
||||
if oldUcBal.Id != "" {
|
||||
bf.ID = utils.StringPointer(oldUcBal.Id)
|
||||
}
|
||||
if oldUc.BalanceType != "" {
|
||||
bf.Type = utils.StringPointer(oldUc.BalanceType)
|
||||
}
|
||||
if oldUc.Direction != "" {
|
||||
bf.Directions = utils.StringMapPointer(utils.ParseStringMap(oldUc.Direction))
|
||||
}
|
||||
if !oldUcBal.ExpirationDate.IsZero() {
|
||||
bf.ExpirationDate = utils.TimePointer(oldUcBal.ExpirationDate)
|
||||
}
|
||||
if oldUcBal.Weight != 0 {
|
||||
bf.Weight = utils.Float64Pointer(oldUcBal.Weight)
|
||||
}
|
||||
if oldUcBal.DestinationIds != "" {
|
||||
bf.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(oldUcBal.DestinationIds))
|
||||
}
|
||||
if oldUcBal.RatingSubject != "" {
|
||||
bf.RatingSubject = utils.StringPointer(oldUcBal.RatingSubject)
|
||||
}
|
||||
if oldUcBal.Category != "" {
|
||||
bf.Categories = utils.StringMapPointer(utils.ParseStringMap(oldUcBal.Category))
|
||||
}
|
||||
if oldUcBal.SharedGroup != "" {
|
||||
bf.SharedGroups = utils.StringMapPointer(utils.ParseStringMap(oldUcBal.SharedGroup))
|
||||
}
|
||||
if oldUcBal.TimingIDs != "" {
|
||||
bf.TimingIDs = utils.StringMapPointer(utils.ParseStringMap(oldUcBal.TimingIDs))
|
||||
}
|
||||
if oldUcBal.Disabled != false {
|
||||
bf.Disabled = utils.BoolPointer(oldUcBal.Disabled)
|
||||
}
|
||||
bf.Timings = oldUcBal.Timings
|
||||
cf := &engine.CounterFilter{
|
||||
Value: oldUcBal.Value,
|
||||
Filter: bf,
|
||||
}
|
||||
newUc.Counters[index] = cf
|
||||
}
|
||||
ac.UnitCounters[oldUc.BalanceType] = append(ac.UnitCounters[oldUc.BalanceType], newUc)
|
||||
}
|
||||
//action triggers
|
||||
for index, oldAtr := range v1Acc.ActionTriggers {
|
||||
at := &engine.ActionTrigger{
|
||||
UniqueID: oldAtr.Id,
|
||||
ThresholdType: oldAtr.ThresholdType,
|
||||
ThresholdValue: oldAtr.ThresholdValue,
|
||||
Recurrent: oldAtr.Recurrent,
|
||||
MinSleep: oldAtr.MinSleep,
|
||||
Weight: oldAtr.Weight,
|
||||
ActionsID: oldAtr.ActionsId,
|
||||
MinQueuedItems: oldAtr.MinQueuedItems,
|
||||
Executed: oldAtr.Executed,
|
||||
}
|
||||
bf := &engine.BalanceFilter{}
|
||||
if oldAtr.BalanceId != "" {
|
||||
bf.ID = utils.StringPointer(oldAtr.BalanceId)
|
||||
}
|
||||
if oldAtr.BalanceType != "" {
|
||||
bf.Type = utils.StringPointer(oldAtr.BalanceType)
|
||||
}
|
||||
if oldAtr.BalanceRatingSubject != "" {
|
||||
bf.RatingSubject = utils.StringPointer(oldAtr.BalanceRatingSubject)
|
||||
}
|
||||
if oldAtr.BalanceDirection != "" {
|
||||
bf.Directions = utils.StringMapPointer(utils.ParseStringMap(oldAtr.BalanceDirection))
|
||||
}
|
||||
if oldAtr.BalanceDestinationIds != "" {
|
||||
bf.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(oldAtr.BalanceDestinationIds))
|
||||
}
|
||||
if oldAtr.BalanceTimingTags != "" {
|
||||
bf.TimingIDs = utils.StringMapPointer(utils.ParseStringMap(oldAtr.BalanceTimingTags))
|
||||
}
|
||||
if oldAtr.BalanceCategory != "" {
|
||||
bf.Categories = utils.StringMapPointer(utils.ParseStringMap(oldAtr.BalanceCategory))
|
||||
}
|
||||
if oldAtr.BalanceSharedGroup != "" {
|
||||
bf.SharedGroups = utils.StringMapPointer(utils.ParseStringMap(oldAtr.BalanceSharedGroup))
|
||||
}
|
||||
if oldAtr.BalanceWeight != 0 {
|
||||
bf.Weight = utils.Float64Pointer(oldAtr.BalanceWeight)
|
||||
}
|
||||
if oldAtr.BalanceDisabled != false {
|
||||
bf.Disabled = utils.BoolPointer(oldAtr.BalanceDisabled)
|
||||
}
|
||||
if !oldAtr.BalanceExpirationDate.IsZero() {
|
||||
bf.ExpirationDate = utils.TimePointer(oldAtr.BalanceExpirationDate)
|
||||
}
|
||||
at.Balance = bf
|
||||
ac.ActionTriggers[index] = at
|
||||
if ac.ActionTriggers[index].ThresholdType == "*min_counter" ||
|
||||
ac.ActionTriggers[index].ThresholdType == "*max_counter" {
|
||||
ac.ActionTriggers[index].ThresholdType = strings.Replace(ac.ActionTriggers[index].ThresholdType, "_", "_event_", 1)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
49
migrator/accounts_test.go
Normal file
49
migrator/accounts_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package migrator
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestV1AccountAsAccount(t *testing.T) {
|
||||
v1b1 := &v1Balance{Value: 10, Weight: 10, DestinationIds: "NAT"}
|
||||
v1Acc := &v1Account{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]v1BalanceChain{utils.VOICE: v1BalanceChain{v1b1}, utils.MONETARY: v1BalanceChain{&v1Balance{Value: 21}}}}
|
||||
|
||||
v2 := &engine.Balance{Uuid: "", ID: "", Value: 10, Directions: utils.StringMap{"*OUT": true}, Weight: 10, DestinationIDs: utils.StringMap{"NAT": true}, RatingSubject: "", Categories: utils.NewStringMap(""), SharedGroups: utils.NewStringMap(""), TimingIDs: utils.NewStringMap("")}
|
||||
m2 := &engine.Balance{Uuid: "", ID: "", Value: 21, Directions: utils.StringMap{"*OUT": true}, DestinationIDs: utils.NewStringMap(""), RatingSubject: "", Categories: utils.NewStringMap(""), SharedGroups: utils.NewStringMap(""), TimingIDs: utils.NewStringMap("")}
|
||||
testAccount := &engine.Account{ID: "CUSTOMER_1:rif", BalanceMap: map[string]engine.Balances{utils.VOICE: engine.Balances{v2}, utils.MONETARY: engine.Balances{m2}}, UnitCounters: engine.UnitCounters{}, ActionTriggers: engine.ActionTriggers{}}
|
||||
|
||||
def := v1b1.IsDefault()
|
||||
if def != false {
|
||||
t.Errorf("Expecting: false, received: true")
|
||||
}
|
||||
|
||||
newAcc, err := v1Acc.AsAccount()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(*testAccount, newAcc) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", *testAccount, newAcc)
|
||||
t.Errorf(" \n")
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
|
||||
var v1ActionTriggers1 = `[{"BalanceType": "*monetary","BalanceDirection": "*out","ThresholdType":"*max_balance", "ThresholdValue" :2, "ActionsId": "TEST_ACTIONS", "Executed": true}]`
|
||||
|
||||
func TestV1ActionTriggersAsActionTriggers1(t *testing.T) {
|
||||
func TestV1ActionTriggersAsActionTriggers(t *testing.T) {
|
||||
atrs := engine.ActionTriggers{&engine.ActionTrigger{
|
||||
Balance: &engine.BalanceFilter{
|
||||
Type: utils.StringPointer(utils.MONETARY),
|
||||
@@ -50,15 +50,3 @@ func TestV1ActionTriggersAsActionTriggers1(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", atrs, newatrs)
|
||||
}
|
||||
}
|
||||
|
||||
//var v1ActionTriggers2 = `[{"BalanceType": "utils.MONETARY", "ThresholdValue" :2, "ActionsId": "TEST_ACTIONS", "Executed": true}]`
|
||||
// func TestV1ActionTriggersAsActionTriggers2(t *testing.T) {
|
||||
// var v1actstrgrs v1ActionTriggers
|
||||
// if err := json.Unmarshal([]byte(v1ActionTriggers2), &v1actstrgrs); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// v1Act := &v1actstrgrs
|
||||
// if _, err := v1Act.AsActionTriggers(); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user