From d3522ef2e7f8f85f17bca5967da33bae56433955 Mon Sep 17 00:00:00 2001 From: adragusin Date: Wed, 20 Nov 2019 16:17:20 +0200 Subject: [PATCH] Updated engine tests after replacing MapStorage --- engine/account.go | 19 +++-- engine/account_test.go | 16 ++-- engine/action_trigger.go | 33 ++++++++- engine/action_trigger_test.go | 118 ++++++++++++++++++++++++++++++ engine/balance_filter.go | 3 + engine/loader_csv_test.go | 2 +- engine/storage_internal_datadb.go | 2 +- engine/units_counter.go | 42 +++++++++++ utils/coreutils.go | 7 +- utils/coreutils_test.go | 41 +++++++++++ 10 files changed, 258 insertions(+), 25 deletions(-) create mode 100644 engine/action_trigger_test.go diff --git a/engine/account.go b/engine/account.go index 85b11d593..6499882cb 100644 --- a/engine/account.go +++ b/engine/account.go @@ -834,17 +834,22 @@ func (account *Account) GetUniqueSharedGroupMembers(cd *CallDescriptor) (utils.S func (acc *Account) Clone() *Account { newAcc := &Account{ - ID: acc.ID, - UnitCounters: nil, // not used when cloned (dryRun) - ActionTriggers: nil, // not used when cloned (dryRun) - AllowNegative: acc.AllowNegative, - Disabled: acc.Disabled, + ID: acc.ID, + UnitCounters: acc.UnitCounters.Clone(), // not used when cloned (dryRun) + AllowNegative: acc.AllowNegative, + Disabled: acc.Disabled, } if acc.BalanceMap != nil { newAcc.BalanceMap = make(map[string]Balances, len(acc.BalanceMap)) + for key, balanceChain := range acc.BalanceMap { + newAcc.BalanceMap[key] = balanceChain.Clone() + } } - for key, balanceChain := range acc.BalanceMap { - newAcc.BalanceMap[key] = balanceChain.Clone() + if acc.ActionTriggers != nil { + newAcc.ActionTriggers = make(ActionTriggers, len(acc.ActionTriggers)) + for key, actionTrigger := range acc.ActionTriggers { + newAcc.ActionTriggers[key] = actionTrigger.Clone() + } } return newAcc } diff --git a/engine/account_test.go b/engine/account_test.go index d9585bc59..97f656f7a 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -2328,13 +2328,11 @@ func TestAccountAsNavigableMap(t *testing.T) { } func TestAccountClone(t *testing.T) { - //empty check account := &Account{} eOut := &Account{} - if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) { + if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) } - //normal check account = &Account{ ID: "testID", BalanceMap: map[string]Balances{ @@ -2347,9 +2345,8 @@ func TestAccountClone(t *testing.T) { ID: "ActionTriggerID2", }, }, - AllowNegative: true, - Disabled: true, - executingTriggers: true, + AllowNegative: true, + Disabled: true, } eOut = &Account{ ID: "testID", @@ -2363,11 +2360,10 @@ func TestAccountClone(t *testing.T) { ID: "ActionTriggerID2", }, }, - AllowNegative: true, - Disabled: true, - executingTriggers: true, + AllowNegative: true, + Disabled: true, } - + if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) } diff --git a/engine/action_trigger.go b/engine/action_trigger.go index 8fc7e2047..e5a827431 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -152,9 +152,25 @@ func (at *ActionTrigger) CreateBalance() *Balance { // makes a shallow copy of the receiver func (at *ActionTrigger) Clone() *ActionTrigger { - clone := new(ActionTrigger) - *clone = *at - return clone + if at == nil { + return nil + } + return &ActionTrigger{ + ID: at.ID, + UniqueID: at.UniqueID, + ThresholdType: at.ThresholdType, + ThresholdValue: at.ThresholdValue, + Recurrent: at.Recurrent, + MinSleep: at.MinSleep, + ExpirationDate: at.ExpirationDate, + ActivationDate: at.ActivationDate, + Weight: at.Weight, + ActionsID: at.ActionsID, + MinQueuedItems: at.MinQueuedItems, + Executed: at.Executed, + LastExecutionTime: at.LastExecutionTime, + Balance: at.Balance.Clone(), + } } func (at *ActionTrigger) Equals(oat *ActionTrigger) bool { @@ -189,3 +205,14 @@ func (atpl ActionTriggers) Less(j, i int) bool { func (atpl ActionTriggers) Sort() { sort.Sort(atpl) } + +func (atpl ActionTriggers) Clone() ActionTriggers { + if atpl == nil { + return nil + } + clone := make(ActionTriggers, len(atpl)) + for i, at := range atpl { + clone[i] = at.Clone() + } + return clone +} diff --git a/engine/action_trigger_test.go b/engine/action_trigger_test.go new file mode 100644 index 000000000..6dee7e98b --- /dev/null +++ b/engine/action_trigger_test.go @@ -0,0 +1,118 @@ +/* +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 engine + +import ( + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/utils" +) + +func TestActionTriggerClone(t *testing.T) { + var at *ActionTrigger + if rcv := at.Clone(); at != nil { + t.Errorf("Expecting : nil, received: %s", utils.ToJSON(rcv)) + } + at = &ActionTrigger{} + eOut := &ActionTrigger{} + if rcv := at.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + ttime := time.Now() + at = &ActionTrigger{ + ID: "testID", + UniqueID: "testUniqueID", + ThresholdType: "testThresholdType", + ThresholdValue: 0.1, + Recurrent: true, + MinSleep: time.Duration(10), + ExpirationDate: ttime, + ActivationDate: ttime.Add(5), + Weight: 0.1, + ActionsID: "testActionsID", + MinQueuedItems: 7, + Executed: true, + LastExecutionTime: ttime.Add(1), + Balance: &BalanceFilter{ + Uuid: utils.StringPointer("testUuid"), + }, + } + eOut = &ActionTrigger{ + ID: "testID", + UniqueID: "testUniqueID", + ThresholdType: "testThresholdType", + ThresholdValue: 0.1, + Recurrent: true, + MinSleep: time.Duration(10), + ExpirationDate: ttime, + ActivationDate: ttime.Add(5), + Weight: 0.1, + ActionsID: "testActionsID", + MinQueuedItems: 7, + Executed: true, + LastExecutionTime: ttime.Add(1), + Balance: &BalanceFilter{ + Uuid: utils.StringPointer("testUuid"), + }, + } + rcv := at.Clone() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + rcv.Balance.Uuid = utils.StringPointer("modified") + if at.Balance.Uuid == utils.StringPointer("modified") { + t.Errorf("Expedcting: modified, received %s", utils.ToJSON(at.Balance.Uuid)) + } + +} + +func TestActionTriggersClone(t *testing.T) { + var atpl ActionTriggers + if rcv := atpl.Clone(); rcv != nil { + t.Errorf("Expecting : nil, received: %s", utils.ToJSON(rcv)) + } + atpl = make(ActionTriggers, 0) + eOut := make(ActionTriggers, 0) + if rcv := atpl.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + + } + atpl = []*ActionTrigger{ + &ActionTrigger{ + ID: "test1", + }, + &ActionTrigger{ + ID: "test2", + }, + } + eOut = []*ActionTrigger{ + &ActionTrigger{ + ID: "test1", + }, + &ActionTrigger{ + ID: "test2", + }, + } + if rcv := atpl.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + +} diff --git a/engine/balance_filter.go b/engine/balance_filter.go index 290dc5f0b..dcf103f3f 100644 --- a/engine/balance_filter.go +++ b/engine/balance_filter.go @@ -64,6 +64,9 @@ func (bp *BalanceFilter) CreateBalance() *Balance { } func (bf *BalanceFilter) Clone() *BalanceFilter { + if bf == nil { + return nil + } result := &BalanceFilter{} if bf.Uuid != nil { result.Uuid = new(string) diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index ba2444545..21fa3ed77 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -882,7 +882,7 @@ func TestLoadActionTriggers(t *testing.T) { Executed: false, } if !reflect.DeepEqual(atr, expected) { - t.Errorf("Error loading action trigger: %+v", utils.ToIJSON(atr.Balance)) + t.Errorf("Expected: %+v, received %+v", utils.ToJSON(expected), utils.ToJSON(atr)) } atr = csvr.actionsTriggers["STANDARD_TRIGGER"][1] expected = &ActionTrigger{ diff --git a/engine/storage_internal_datadb.go b/engine/storage_internal_datadb.go index ba68d2e9a..b328bca7c 100644 --- a/engine/storage_internal_datadb.go +++ b/engine/storage_internal_datadb.go @@ -486,7 +486,7 @@ func (iDB *InternalDB) GetActionTriggersDrv(id string) (at ActionTriggers, err e if !ok || x == nil { return nil, utils.ErrNotFound } - return x.(ActionTriggers), nil + return x.(ActionTriggers).Clone(), nil } func (iDB *InternalDB) SetActionTriggersDrv(id string, at ActionTriggers) (err error) { diff --git a/engine/units_counter.go b/engine/units_counter.go index c3a30f5ba..15e911658 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -44,6 +44,48 @@ func (cfs CounterFilters) HasCounter(cf *CounterFilter) bool { return false } +// Clone clones *UnitCounter +func (uc *UnitCounter) Clone() (newUnit *UnitCounter) { + if uc == nil { + return + } + newUnit = &UnitCounter{ + CounterType: uc.CounterType, + Counters: make(CounterFilters, len(uc.Counters)), + } + for i, counter := range uc.Counters { + newUnit.Counters[i] = counter.Clone() + } + return newUnit +} + +// Clone clones *CounterFilter +func (cfs *CounterFilter) Clone() *CounterFilter { + if cfs == nil { + return nil + } + return &CounterFilter{ + Value: cfs.Value, + Filter: cfs.Filter.Clone(), + } +} + +// Clone clones *UnitCounters +func (ucs UnitCounters) Clone() UnitCounters { + if ucs == nil { + return nil + } + newUnitCounters := make(UnitCounters) + for key, unitCounter := range ucs { + newal := make([]*UnitCounter, len(unitCounter)) + for i, uc := range unitCounter { + newal[i] = uc.Clone() + } + newUnitCounters[key] = newal + } + return newUnitCounters +} + // Returns true if the counters were of the same type // Copies the value from old balances func (uc *UnitCounter) CopyCounterValues(oldUc *UnitCounter) bool { diff --git a/utils/coreutils.go b/utils/coreutils.go index f5f1fe242..ad9197249 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -118,6 +118,7 @@ func FirstNonEmpty(vals ...string) string { return EmptyString } +// Sha1 generate the SHA1 hash from any string func Sha1(attrs ...string) string { hasher := sha1.New() for _, attr := range attrs { @@ -126,9 +127,9 @@ func Sha1(attrs ...string) string { return fmt.Sprintf("%x", hasher.Sum(nil)) } -func NewTPid() string { - return Sha1(GenUUID()) -} +// func NewTPid() string { +// return Sha1(GenUUID()) +// } // helper function for uuid generation func GenUUID() string { diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index 98015d949..7747a63e5 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -51,11 +51,52 @@ func TestFirstNonEmpty(t *testing.T) { } } +func TestSha1(t *testing.T) { + //empty check + rcv := Sha1(" ") + eOut := "b858cb282617fb0956d960215c8e84d1ccf909c6" + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", eOut, rcv) + } + //normal check + rcv = Sha1("teststring") + eOut = "b8473b86d4c2072ca9b08bd28e373e8253e865c4" + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", eOut, rcv) + } + rcv = Sha1("test1", "test2") + eOut = "dff964f6e3c1761b6288f5c75c319d36fb09b2b9" + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", eOut, rcv) + } +} + func TestUUID(t *testing.T) { uuid := GenUUID() if len(uuid) == 0 { t.Fatalf("GenUUID error %s", uuid) } + uuid2 := GenUUID() + if len(uuid2) == 0 { + t.Fatalf("GenUUID error %s", uuid) + } + if uuid == uuid2 { + t.Error("GenUUID error.") + } +} + +func TestUUIDSha1Prefix(t *testing.T) { + rcv := UUIDSha1Prefix() + if len(rcv) != 7 { + t.Errorf("Expected len: 7, received %d", len(rcv)) + } + rcv2 := UUIDSha1Prefix() + if len(rcv2) != 7 { + t.Errorf("Expected len: 7, received %d", len(rcv)) + } + if rcv == rcv2 { + t.Error("UUIDSha1Prefix error") + } } func TestRoundByMethodUp1(t *testing.T) {