From 9b9cdb860d8d888a36f3c26ae2606c941ce5dc13 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 10 Nov 2015 10:31:37 +0200 Subject: [PATCH] test for *exp_balance trigger fixes #269 --- engine/account.go | 24 ++++++++++-------------- engine/account_test.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/engine/account.go b/engine/account.go index 8d537b884..a7843b012 100644 --- a/engine/account.go +++ b/engine/account.go @@ -489,39 +489,35 @@ func (ub *Account) executeActionTriggers(a *Action) { for _, mb := range uc.Balances { if strings.HasPrefix(at.ThresholdType, "*max") { if mb.MatchActionTrigger(at) && mb.GetValue() >= at.ThresholdValue { - // run the actions at.Execute(ub, nil) } } else { //MIN if mb.MatchActionTrigger(at) && mb.GetValue() <= at.ThresholdValue { - // run the actions at.Execute(ub, nil) } } } } } - } else if at.ThresholdType == utils.TRIGGER_MIN_BALANCE || at.ThresholdType == utils.TRIGGER_MAX_BALANCE { // BALANCE THRESHOLD + } else { // BALANCE for _, b := range ub.BalanceMap[at.BalanceType] { - if !b.dirty { // do not check clean balances + if !b.dirty && at.ThresholdType != utils.TRIGGER_EXP_BALANCE { // do not check clean balances continue } - if strings.HasPrefix(at.ThresholdType, "*max") { + switch at.ThresholdType { + case utils.TRIGGER_MAX_BALANCE: + if b.MatchActionTrigger(at) && b.GetValue() >= at.ThresholdValue { - // run the actions at.Execute(ub, nil) } - } else { //MIN + case utils.TRIGGER_MIN_BALANCE: if b.MatchActionTrigger(at) && b.GetValue() <= at.ThresholdValue { - // run the actions at.Execute(ub, nil) } - } - } - } else if at.ThresholdType == utils.TRIGGER_EXP_BALANCE { - for _, b := range ub.BalanceMap[at.BalanceType] { - if b.MatchActionTrigger(at) && b.IsExpired() { - at.Execute(ub, nil) + case utils.TRIGGER_EXP_BALANCE: + if b.MatchActionTrigger(at) && b.IsExpired() { + at.Execute(ub, nil) + } } } } diff --git a/engine/account_test.go b/engine/account_test.go index 1647497a7..6b0ccdfff 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -963,7 +963,24 @@ func TestAccountExecuteTriggeredDayWeek(t *testing.T) { ub.UnitCounters[0].Balances[1].Value != 1 { t.Error("Error reseting both counters", ub.UnitCounters[0].Balances[0].Value, ub.UnitCounters[0].Balances[1].Value) } +} +func TestAccountExpActionTrigger(t *testing.T) { + ub := &Account{ + Id: "TEST_UB", + BalanceMap: map[string]BalanceChain{utils.MONETARY: BalanceChain{&Balance{Directions: utils.NewStringMap(utils.OUT), Value: 100, ExpirationDate: time.Date(2015, time.November, 9, 9, 48, 0, 0, time.UTC)}}, utils.VOICE: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationIds: utils.StringMap{"NAT": true}, Directions: utils.StringMap{utils.OUT: true}}, &Balance{Weight: 10, DestinationIds: utils.StringMap{"RET": true}}}}, + ActionTriggers: ActionTriggers{ + &ActionTrigger{Id: "check expired balances", BalanceType: utils.MONETARY, BalanceDirections: utils.StringMap{utils.OUT: true}, ThresholdValue: 10, ThresholdType: utils.TRIGGER_EXP_BALANCE, ActionsId: "TEST_ACTIONS"}, + }, + } + ub.executeActionTriggers(nil) + if ub.BalanceMap[utils.MONETARY][0].IsExpired() || + ub.BalanceMap[utils.MONETARY][0].GetValue() != 10 || // expired was cleaned + ub.BalanceMap[utils.VOICE][0].GetValue() != 20 || + ub.ActionTriggers[0].Executed != true { + t.Log(ub.BalanceMap[utils.MONETARY][0].IsExpired()) + t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY][0].GetValue(), ub.BalanceMap[utils.VOICE][0].GetValue(), len(ub.BalanceMap[utils.MONETARY])) + } } func TestCleanExpired(t *testing.T) {