From 776e8b5057b6cbb2795d2cfb5d2080daff547be3 Mon Sep 17 00:00:00 2001 From: Edwardro22 Date: Tue, 21 Feb 2017 15:57:27 +0200 Subject: [PATCH] small fix --- migrator/action_triggers.go | 111 +++++++++++++++++++++++++++++++ migrator/action_triggers_test.go | 64 ++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 migrator/action_triggers.go create mode 100644 migrator/action_triggers_test.go diff --git a/migrator/action_triggers.go b/migrator/action_triggers.go new file mode 100644 index 000000000..e022ee7df --- /dev/null +++ b/migrator/action_triggers.go @@ -0,0 +1,111 @@ +/* +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 ( + "strings" + "time" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +type v1ActionTriggers []*v1ActionTrigger + +type v1ActionTrigger struct { + Id string + ThresholdType string + ThresholdValue float64 + Recurrent bool + MinSleep time.Duration + BalanceId string + BalanceType string + BalanceDirection string + BalanceDestinationIds string + BalanceWeight float64 + BalanceExpirationDate time.Time + BalanceTimingTags string + BalanceRatingSubject string + BalanceCategory string + BalanceSharedGroup string + BalanceDisabled bool + Weight float64 + ActionsId string + MinQueuedItems int + Executed bool +} + +func (v1Act v1ActionTriggers) AsActionTriggers() (at engine.ActionTriggers, err error) { + at = make(engine.ActionTriggers, len(v1Act)) + for index, oldAct := range v1Act { + atr := &engine.ActionTrigger{ + UniqueID: oldAct.Id, + ThresholdType: oldAct.ThresholdType, + ThresholdValue: oldAct.ThresholdValue, + Recurrent: oldAct.Recurrent, + MinSleep: oldAct.MinSleep, + Weight: oldAct.Weight, + ActionsID: oldAct.ActionsId, + MinQueuedItems: oldAct.MinQueuedItems, + Executed: oldAct.Executed, + } + + bf := &engine.BalanceFilter{} + if oldAct.BalanceId != "" { + bf.ID = utils.StringPointer(oldAct.BalanceId) + } + if oldAct.BalanceType != "" { + bf.Type = utils.StringPointer(oldAct.BalanceType) + } + if oldAct.BalanceRatingSubject != "" { + bf.RatingSubject = utils.StringPointer(oldAct.BalanceRatingSubject) + } + if oldAct.BalanceDirection != "" { + bf.Directions = utils.StringMapPointer(utils.ParseStringMap(oldAct.BalanceDirection)) + } + if oldAct.BalanceDestinationIds != "" { + bf.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(oldAct.BalanceDestinationIds)) + } + if oldAct.BalanceTimingTags != "" { + bf.TimingIDs = utils.StringMapPointer(utils.ParseStringMap(oldAct.BalanceTimingTags)) + } + if oldAct.BalanceCategory != "" { + bf.Categories = utils.StringMapPointer(utils.ParseStringMap(oldAct.BalanceCategory)) + } + if oldAct.BalanceSharedGroup != "" { + bf.SharedGroups = utils.StringMapPointer(utils.ParseStringMap(oldAct.BalanceSharedGroup)) + } + if oldAct.BalanceWeight != 0 { + bf.Weight = utils.Float64Pointer(oldAct.BalanceWeight) + } + if oldAct.BalanceDisabled != false { + bf.Disabled = utils.BoolPointer(oldAct.BalanceDisabled) + } + if !oldAct.BalanceExpirationDate.IsZero() { + bf.ExpirationDate = utils.TimePointer(oldAct.BalanceExpirationDate) + } + atr.Balance = bf + at[index] = atr + if at[index].ThresholdType == "*min_counter" || + at[index].ThresholdType == "*max_counter" { + at[index].ThresholdType = strings.Replace(at[index].ThresholdType, "_", "_event_", 1) + } + + } + return at, nil +} diff --git a/migrator/action_triggers_test.go b/migrator/action_triggers_test.go new file mode 100644 index 000000000..c727dd571 --- /dev/null +++ b/migrator/action_triggers_test.go @@ -0,0 +1,64 @@ +/* +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 ( + "encoding/json" + "reflect" + "testing" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var v1ActionTriggers1 = `[{"BalanceType": "*monetary","BalanceDirection": "*out","ThresholdType":"*max_balance", "ThresholdValue" :2, "ActionsId": "TEST_ACTIONS", "Executed": true}]` + +func TestV1ActionTriggersAsActionTriggers1(t *testing.T) { + atrs := engine.ActionTriggers{&engine.ActionTrigger{ + Balance: &engine.BalanceFilter{ + Type: utils.StringPointer(utils.MONETARY), + Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), + }, + ThresholdType: utils.TRIGGER_MAX_BALANCE, + ThresholdValue: 2, + ActionsID: "TEST_ACTIONS", + Executed: true, + }, + } + var v1actstrgrs v1ActionTriggers + if err := json.Unmarshal([]byte(v1ActionTriggers1), &v1actstrgrs); err != nil { + t.Error(err) + } + if newatrs, err := v1actstrgrs.AsActionTriggers(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(atrs, newatrs) { + 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) +// } +// }