From 90971e5b56f635cd897d98e111fb657f256d6b6b Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 15 Jul 2020 15:43:13 +0300 Subject: [PATCH] Add integration test for *reset_triggers Action --- apier/v1/accounts_it_test.go | 152 +++++++++++++++++++++++++++++++++++ engine/action_plan.go | 12 ++- engine/action_trigger.go | 1 - 3 files changed, 162 insertions(+), 3 deletions(-) diff --git a/apier/v1/accounts_it_test.go b/apier/v1/accounts_it_test.go index 17b624267..3640e0e1c 100644 --- a/apier/v1/accounts_it_test.go +++ b/apier/v1/accounts_it_test.go @@ -63,6 +63,7 @@ var ( testAccITCountAccounts, testAccITTPFromFolder, testAccITAddBalanceWithDestinations, + //testAccITAccountWithTriggers, testAccITStopCgrEngine, } ) @@ -777,3 +778,154 @@ func testAccITAddBalanceWithDestinations(t *testing.T) { t.Errorf("Calling Responder.MaxDebit got callcost: %v", utils.ToIJSON(cc)) } } + +/* Uncomment this test when found a solution for SetActions +func testAccITAccountWithTriggers(t *testing.T) { + var reply string + args := &utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "testAccITAccountWithTriggers", + BalanceType: utils.MONETARY, + Balance: map[string]interface{}{ + utils.ID: "testAccITAccountWithTriggers", + utils.Weight: 10, + utils.Value: 5, + }, + } + if err := accRPC.Call(utils.APIerSv1SetBalance, args, &reply); err != nil { + t.Error("Got error on SetBalance: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling SetBalance received: %s", reply) + } + + // add an action that contains topup and reset_triggers + topupAction := &utils.AttrSetActions{ActionsId: "TOPUP_WITH_RESET_TRIGGER", Actions: []*utils.TPAction{ + {Identifier: utils.TOPUP_RESET, BalanceId: "testAccITAccountWithTriggers", + BalanceType: utils.MONETARY, Units: "5", Weight: 10.0}, + {Identifier: utils.RESET_TRIGGERS}, + }} + + if err := accRPC.Call(utils.APIerSv2SetActions, topupAction, &reply); err != nil { + t.Error("Got error on APIerSv2.SetActions: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv2.SetActions received: %s", reply) + } + + // add an action to be executed when the trigger is triggered + actTrigger := &utils.AttrSetActions{ActionsId: "ACT_TRIGGER", Actions: []*utils.TPAction{ + {Identifier: utils.TOPUP, BalanceId: "CustomBanalce", + BalanceType: utils.MONETARY, Units: "5", Weight: 10.0}, + }} + + if err := accRPC.Call(utils.APIerSv2SetActions, actTrigger, &reply); err != nil { + t.Error("Got error on APIerSv2.SetActions: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv2.SetActions received: %s", reply) + } + + attrsAddTrigger := &AttrAddActionTrigger{Tenant: "cgrates.org", Account: "testAccITAccountWithTriggers", BalanceType: utils.MONETARY, + ThresholdType: "*min_balance", ThresholdValue: 2, Weight: 10, ActionsId: "ACT_TRIGGER"} + if err := accRPC.Call(utils.APIerSv1AddTriggeredAction, attrsAddTrigger, &reply); err != nil { + t.Error("Got error on APIerSv1.AddTriggeredAction: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv1.AddTriggeredAction received: %s", reply) + } + + var acnt engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: accTenant, + Account: "testAccITAccountWithTriggers", + } + + if err := accRPC.Call(utils.APIerSv2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } else { + for _, value := range acnt.BalanceMap[utils.MONETARY] { + if value.ID == "testAccITAccountWithTriggers" { + if value.GetValue() != 5 { + t.Errorf("Expecting %+v, received: %+v", 5, value.GetValue()) + } + if value.Weight != 10 { + t.Errorf("Expecting %+v, received: %+v", 10, value.Weight) + } + break + } + } + if len(acnt.ActionTriggers) != 1 { + t.Errorf("Expected 1, received: %+v", len(acnt.ActionTriggers)) + } else { + if acnt.ActionTriggers[0].Executed != false { + t.Errorf("Expected false, received: %+v", acnt.ActionTriggers[0].Executed) + } + } + } + + // Debit balance will trigger the Trigger from the account + if err := accRPC.Call(utils.APIerSv1DebitBalance, &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "testAccITAccountWithTriggers", + BalanceType: utils.MONETARY, + Value: 3, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + + if err := accRPC.Call(utils.APIerSv2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } else { + for _, value := range acnt.BalanceMap[utils.MONETARY] { + if value.ID == "testAccITAccountWithTriggers" { + if value.GetValue() != 2 { + t.Errorf("Expecting %+v, received: %+v", 2, value.GetValue()) + } + } else if value.ID == "CustomBanalce" { + if value.GetValue() != 5 { + t.Errorf("Expecting %+v, received: %+v", 5, value.GetValue()) + } + } + } + if len(acnt.ActionTriggers) != 1 { + t.Errorf("Expected 1, received: %+v", len(acnt.ActionTriggers)) + } else { + if acnt.ActionTriggers[0].Executed != true { + t.Errorf("Expected true, received: %+v", acnt.ActionTriggers[0].Executed) + } + } + } + + // execute the action that topup_reset the balance and reset the trigger + attrsEA := &utils.AttrExecuteAction{Tenant: "cgrates.org", Account: "testAccITAccountWithTriggers", + ActionsId: "TOPUP_WITH_RESET_TRIGGER"} + if err := accRPC.Call(utils.APIerSv1ExecuteAction, attrsEA, &reply); err != nil { + t.Error("Got error on APIerSv1.ExecuteAction: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv1.ExecuteAction received: %s", reply) + } + + if err := accRPC.Call(utils.APIerSv2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } else { + for _, value := range acnt.BalanceMap[utils.MONETARY] { + if value.ID == "testAccITAccountWithTriggers" { + if value.GetValue() != 2 { + t.Errorf("Expecting %+v, received: %+v", 2, value.GetValue()) + } + } else if value.ID == "CustomBanalce" { + if value.GetValue() != 5 { + t.Errorf("Expecting %+v, received: %+v", 5, value.GetValue()) + } + } + } + if len(acnt.ActionTriggers) != 1 { + t.Errorf("Expected 1, received: %+v", len(acnt.ActionTriggers)) + } else { + if acnt.ActionTriggers[0].Executed != true { + t.Errorf("Expected true, received: %+v", acnt.ActionTriggers[0].Executed) + } + } + } + +} +*/ diff --git a/engine/action_plan.go b/engine/action_plan.go index 391faf94e..37fe05ed2 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -109,7 +109,7 @@ func (at *ActionTiming) Clone() (cln *ActionTiming) { return } -func (at *ActionTiming) GetNextStartTime(now time.Time) (t time.Time) { +func (at *ActionTiming) GetNextStartTime(t1 time.Time) (t time.Time) { if !at.stCache.IsZero() { return at.stCache } @@ -127,7 +127,15 @@ func (at *ActionTiming) GetNextStartTime(now time.Time) (t time.Time) { if len(i.Timing.Months) > 0 && len(i.Timing.MonthDays) == 0 { i.Timing.MonthDays = append(i.Timing.MonthDays, 1) } - at.stCache = cronexpr.MustParse(i.Timing.CronString()).Next(now) + + at.stCache = cronexpr.MustParse(i.Timing.CronString()).Next(t1) + //if // timingId is *monthly_estimated && at.stCache.Month != t1.Month + 1 + //{ + // // clone la timing i.Clone() + // // i.Clone().Timing.MonthDays -1 + // // at .stCache = cronexpr.MustParse(i.Timing.CronString()).Next(t1) + //} + return at.stCache } diff --git a/engine/action_trigger.go b/engine/action_trigger.go index ab3b5b81b..06ffd8600 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -139,7 +139,6 @@ func (at *ActionTrigger) Match(a *Action) bool { } thresholdType = t.ThresholdType == "" || at.ThresholdType == t.ThresholdType } - return thresholdType && at.Balance.CreateBalance().MatchFilter(a.Balance, false, false) }