tests passing with new balance struct

This commit is contained in:
Radu Ioan Fericean
2013-09-05 23:28:04 +03:00
parent f3f9d03c80
commit 2f6b1f3f84
9 changed files with 141 additions and 126 deletions

View File

@@ -109,7 +109,7 @@ func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error {
attr.Direction = engine.OUTBOUND
}
at.SetActions(engine.Actions{&engine.Action{ActionType: engine.TOPUP, BalanceId: attr.BalanceId, Direction: attr.Direction, Units: attr.Value}})
at.SetActions(engine.Actions{&engine.Action{ActionType: engine.TOPUP, BalanceId: attr.BalanceId, Direction: attr.Direction, Balance: &engine.Balance{Value: attr.Value}}})
if err := at.Execute(); err != nil {
*reply = err.Error()

View File

@@ -35,9 +35,9 @@ type Action struct {
ExpirationString string
Weight float64
Balance *Balance
destinationTag, rateType string // From here for import/load purposes only
expirationDate time.Time
units, rateValue, minutesWeight float64
DestinationTag, RateType string // From here for import/load purposes only
ExpirationDate time.Time
Units, RateValue, MinutesWeight float64
}
const (
@@ -138,7 +138,7 @@ func resetCounterAction(ub *UserBalance, a *Action) (err error) {
ub.UnitCounters = append(ub.UnitCounters, uc)
}
if a.BalanceId == MINUTES {
uc.initMinuteBuckets(ub.ActionTriggers)
uc.initMinuteBalances(ub.ActionTriggers)
} else {
uc.Units = 0
}

View File

@@ -529,7 +529,7 @@ func TestActionTriggerPriotityList(t *testing.T) {
func TestActionResetTriggres(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -555,7 +555,7 @@ func TestActionResetTriggresExecutesThem(t *testing.T) {
func TestActionResetTriggresActionFilter(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -569,7 +569,7 @@ func TestActionSetPostpaid(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -583,7 +583,7 @@ func TestActionSetPrepaid(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -597,7 +597,7 @@ func TestActionResetPrepaid(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -605,7 +605,7 @@ func TestActionResetPrepaid(t *testing.T) {
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 0 ||
len(ub.UnitCounters) != 0 ||
ub.BalanceMap[MINUTES][0].Value != 0 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 0 ||
ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
t.Log(ub.BalanceMap)
t.Error("Reset prepaid action failed!")
@@ -616,7 +616,7 @@ func TestActionResetPostpaid(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -624,7 +624,7 @@ func TestActionResetPostpaid(t *testing.T) {
if ub.Type != UB_TYPE_POSTPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 0 ||
len(ub.UnitCounters) != 0 ||
ub.BalanceMap[MINUTES][0].Value != 0 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 0 ||
ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
t.Error("Reset postpaid action failed!")
}
@@ -634,7 +634,7 @@ func TestActionTopupResetCredit(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -643,7 +643,7 @@ func TestActionTopupResetCredit(t *testing.T) {
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 10 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Errorf("Topup reset action failed: %#v", ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue())
}
@@ -654,20 +654,20 @@ func TestActionTopupResetMinutes(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}},
MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}}
topupResetAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES][0].Value != 5 ||
ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue() != 5 ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 1 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Errorf("Topup reset minutes action failed: %v", ub.BalanceMap)
t.Errorf("Topup reset minutes action failed: %v", ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue())
}
}
@@ -675,7 +675,7 @@ func TestActionTopupCredit(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -684,7 +684,7 @@ func TestActionTopupCredit(t *testing.T) {
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 110 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Error("Topup action failed!", ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue())
}
@@ -694,19 +694,19 @@ func TestActionTopupMinutes(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Balance: &Balance{Value: 5, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}}
topupAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES][0].Value != 15 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 15 ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Error("Topup minutes action failed!", ub.BalanceMap[MINUTES][0])
t.Error("Topup minutes action failed!", ub.BalanceMap[MINUTES+OUTBOUND][0])
}
}
@@ -714,7 +714,7 @@ func TestActionDebitCredit(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -723,7 +723,7 @@ func TestActionDebitCredit(t *testing.T) {
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 90 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Error("Debit action failed!", ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue())
}
@@ -733,27 +733,31 @@ func TestActionDebitMinutes(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Balance: &Balance{Value: 5, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}}
debitAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES][0].Value != 5 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 5 ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true || ub.ActionTriggers[1].Executed != true {
t.Error("Debit minutes action failed!", ub.BalanceMap[MINUTES][0])
t.Error("Debit minutes action failed!", ub.BalanceMap[MINUTES+OUTBOUND][0])
}
}
func TestActionResetAllCounters(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{
&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"},
&Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -764,22 +768,24 @@ func TestActionResetAllCounters(t *testing.T) {
len(ub.UnitCounters[0].MinuteBalances) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
ub.ActionTriggers[0].Executed != true {
t.Error("Reset counters action failed!")
t.Error("Reset counters action failed: ", ub.BalanceMap[MINUTES])
}
if len(ub.UnitCounters) < 1 {
t.FailNow()
}
mb := ub.UnitCounters[0].MinuteBalances[0]
if mb.Weight != 20 || mb.SpecialPrice != 1 || mb.Value != 10 || mb.DestinationId != "NAT" {
t.Errorf("Minute bucked cloned incorrectly: %v!", mb)
if mb.Weight != 20 || mb.SpecialPrice != 1 || mb.Value != 0 || mb.DestinationId != "NAT" {
t.Errorf("Balanxce cloned incorrectly: %v!", mb)
}
}
func TestActionResetCounterMinutes(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -791,13 +797,15 @@ func TestActionResetCounterMinutes(t *testing.T) {
len(ub.UnitCounters[1].MinuteBalances) != 1 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
ub.ActionTriggers[0].Executed != true {
t.Logf("%#v", ub.UnitCounters[1].MinuteBalances[0])
t.Logf("%#v", ub.UnitCounters[1].MinuteBalances[1])
t.Error("Reset counters action failed!", ub.UnitCounters[1].MinuteBalances)
}
if len(ub.UnitCounters) < 2 || len(ub.UnitCounters[1].MinuteBalances) < 1 {
t.FailNow()
}
mb := ub.UnitCounters[1].MinuteBalances[0]
if mb.Weight != 20 || mb.SpecialPrice != 1 || mb.Value != 10 || mb.DestinationId != "NAT" {
if mb.Weight != 20 || mb.SpecialPrice != 1 || mb.Value != 0 || mb.DestinationId != "NAT" {
t.Errorf("Minute bucked cloned incorrectly: %v!", mb)
}
}
@@ -806,7 +814,7 @@ func TestActionResetCounterCREDIT(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}, &UnitsCounter{BalanceId: SMS, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
@@ -815,7 +823,7 @@ func TestActionResetCounterCREDIT(t *testing.T) {
if ub.Type != UB_TYPE_POSTPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 2 ||
len(ub.BalanceMap[MINUTES]) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
ub.ActionTriggers[0].Executed != true {
t.Error("Reset counters action failed!", ub.UnitCounters)
}

View File

@@ -38,18 +38,21 @@ func populateDB() {
minu := &UserBalance{
Id: "*out:vdf:minu",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 0}}, MINUTES: BalanceChain{
&Balance{Value: 200, DestinationId: "NAT", Weight: 10},
&Balance{Value: 100, DestinationId: "RET", Weight: 20},
}},
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 0}},
MINUTES + OUTBOUND: BalanceChain{
&Balance{Value: 200, DestinationId: "NAT", Weight: 10},
&Balance{Value: 100, DestinationId: "RET", Weight: 20},
}},
}
broker := &UserBalance{
Id: "*out:vdf:broker",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{
&Balance{Value: 20, DestinationId: "NAT", Weight: 10, SpecialPrice: 1},
&Balance{Value: 100, DestinationId: "RET", Weight: 20},
}},
BalanceMap: map[string]BalanceChain{
MINUTES + OUTBOUND: BalanceChain{
&Balance{Value: 20, DestinationId: "NAT", Weight: 10, SpecialPrice: 1},
&Balance{Value: 100, DestinationId: "RET", Weight: 20},
}},
}
if storageGetter != nil {
storageGetter.(Storage).Flush()

View File

@@ -541,7 +541,7 @@ func (self *SQLStorage) SetTPActions(tpid string, acts map[string][]*Action) err
}
qry += fmt.Sprintf("('%s','%s','%s','%s','%s',%f,'%s','%s','%s',%f,%f,%f)",
tpid, actId, act.ActionType, act.BalanceId, act.Direction, act.Balance.Value, act.ExpirationString,
act.destinationTag, act.rateType, act.rateValue, act.minutesWeight, act.Weight)
act.DestinationTag, act.RateType, act.RateValue, act.MinutesWeight, act.Weight)
i++
}
}

View File

@@ -317,12 +317,12 @@ func (self *TPCSVImporter) importActions(fn string) error {
ActionType: actionType,
BalanceId: balanceType,
Direction: direction,
units: units,
expirationDate: expiryTime,
destinationTag: destTag,
rateType: rateType,
rateValue: rateValue,
minutesWeight: minutesWeight,
Units: units,
ExpirationDate: expiryTime,
DestinationTag: destTag,
RateType: rateType,
RateValue: rateValue,
MinutesWeight: minutesWeight,
Weight: weight,
}
if err := self.StorDb.SetTPActions(self.TPid, map[string][]*Action{actId: []*Action{act}}); err != nil {

View File

@@ -30,7 +30,7 @@ type UnitsCounter struct {
MinuteBalances BalanceChain
}
func (uc *UnitsCounter) initMinuteBuckets(ats []*ActionTrigger) {
func (uc *UnitsCounter) initMinuteBalances(ats []*ActionTrigger) {
uc.MinuteBalances = make(BalanceChain, 0)
for _, at := range ats {
acs, err := storageGetter.GetActions(at.ActionsId)
@@ -38,8 +38,10 @@ func (uc *UnitsCounter) initMinuteBuckets(ats []*ActionTrigger) {
continue
}
for _, a := range acs {
if a.Balance != nil {
uc.MinuteBalances = append(uc.MinuteBalances, a.Balance.Clone())
if a.BalanceId == MINUTES && a.Balance != nil {
b := a.Balance.Clone()
b.Value = 0
uc.MinuteBalances = append(uc.MinuteBalances, b)
}
}
}

View File

@@ -72,11 +72,11 @@ Returns user's available minutes for the specified destination
*/
func (ub *UserBalance) getSecondsForPrefix(prefix string) (seconds, credit float64, balances BalanceChain) {
credit = ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()
if len(ub.BalanceMap[MINUTES]) == 0 {
if len(ub.BalanceMap[MINUTES+OUTBOUND]) == 0 {
// Logger.Debug("There are no minute buckets to check for user: ", ub.Id)
return
}
for _, b := range ub.BalanceMap[MINUTES] {
for _, b := range ub.BalanceMap[MINUTES+OUTBOUND] {
if b.IsExpired() {
continue
}
@@ -109,7 +109,7 @@ func (ub *UserBalance) debitMinuteBalance(newMb *Balance) error {
ub.BalanceMap = make(map[string]BalanceChain, 0)
}
found := false
for _, mb := range ub.BalanceMap[MINUTES] {
for _, mb := range ub.BalanceMap[MINUTES+OUTBOUND] {
if mb.IsExpired() {
continue
}
@@ -123,7 +123,7 @@ func (ub *UserBalance) debitMinuteBalance(newMb *Balance) error {
// then we add it to the list
if !found && newMb.Value <= 0 {
newMb.Value = -newMb.Value
ub.BalanceMap[MINUTES] = append(ub.BalanceMap[MINUTES], newMb)
ub.BalanceMap[MINUTES+OUTBOUND] = append(ub.BalanceMap[MINUTES+OUTBOUND], newMb)
}
return nil
}
@@ -259,8 +259,8 @@ func (ub *UserBalance) executeActionTriggers(a *Action) {
}
} else { // BALANCE
for _, b := range ub.BalanceMap[at.BalanceId] {
if at.BalanceId == MINUTES && at.DestinationId != "" { // last check adds safety
for _, mb := range ub.BalanceMap[MINUTES] {
if at.BalanceId == MINUTES {
for _, mb := range ub.BalanceMap[MINUTES+OUTBOUND] {
if strings.Contains(at.ThresholdType, "*max") {
if mb.DestinationId == at.DestinationId && mb.Value >= at.ThresholdValue {
// run the actions
@@ -347,7 +347,7 @@ func (ub *UserBalance) initMinuteCounters() {
continue
}
for _, a := range acs {
if a.Balance != nil {
if a.BalanceId == MINUTES && a.Balance != nil {
direction := at.Direction
if direction == "" {
direction = OUTBOUND
@@ -359,7 +359,9 @@ func (ub *UserBalance) initMinuteCounters() {
uc.MinuteBalances = BalanceChain{}
ub.UnitCounters = append(ub.UnitCounters, uc)
}
uc.MinuteBalances = append(uc.MinuteBalances, a.Balance.Clone())
b := a.Balance.Clone()
b.Value = 0
uc.MinuteBalances = append(uc.MinuteBalances, b)
uc.MinuteBalances.Sort()
}
}
@@ -377,9 +379,9 @@ func (ub *UserBalance) CleanExpiredBalancesAndBuckets() {
}
ub.BalanceMap[key] = bm
}
for i := 0; i < len(ub.BalanceMap[MINUTES]); i++ {
if ub.BalanceMap[MINUTES][i].IsExpired() {
ub.BalanceMap[MINUTES] = append(ub.BalanceMap[MINUTES][:i], ub.BalanceMap[MINUTES][i+1:]...)
for i := 0; i < len(ub.BalanceMap[MINUTES+OUTBOUND]); i++ {
if ub.BalanceMap[MINUTES+OUTBOUND][i].IsExpired() {
ub.BalanceMap[MINUTES+OUTBOUND] = append(ub.BalanceMap[MINUTES+OUTBOUND][:i], ub.BalanceMap[MINUTES+OUTBOUND][i+1:]...)
}
}
}

View File

@@ -98,7 +98,7 @@ func TestBalanceChainStoreRestore(t *testing.T) {
func TestUserBalanceStorageStoreRestore(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.01, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
storageGetter.SetUserBalance(rifsBalance)
ub1, err := storageGetter.GetUserBalance("other")
if err != nil || !ub1.BalanceMap[CREDIT+OUTBOUND].Equal(rifsBalance.BalanceMap[CREDIT+OUTBOUND]) {
@@ -110,7 +110,7 @@ func TestUserBalanceStorageStoreRestore(t *testing.T) {
func TestGetSecondsForPrefix(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 200}}}}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 200}}}}
seconds, credit, bucketList := ub1.getSecondsForPrefix("0723")
expected := 110.0
if credit != 200 || seconds != expected || bucketList[0].Weight < bucketList[1].Weight {
@@ -122,7 +122,7 @@ func TestGetSpecialPricedSeconds(t *testing.T) {
b1 := &Balance{Value: 10, SpecialPrice: 10, Weight: 10, DestinationId: "NAT"}
b2 := &Balance{Value: 100, SpecialPrice: 1, Weight: 20, DestinationId: "RET"}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
seconds, credit, bucketList := ub1.getSecondsForPrefix("0723")
expected := 21.0
if credit != 0 || seconds != expected || len(bucketList) < 2 || bucketList[0].Weight < bucketList[1].Weight {
@@ -133,13 +133,13 @@ func TestGetSpecialPricedSeconds(t *testing.T) {
func TestUserBalanceStorageStore(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.01, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
storageGetter.SetUserBalance(rifsBalance)
result, err := storageGetter.GetUserBalance(rifsBalance.Id)
if err != nil || rifsBalance.Id != result.Id ||
len(rifsBalance.BalanceMap[MINUTES]) < 2 || len(result.BalanceMap[MINUTES]) < 2 ||
!(rifsBalance.BalanceMap[MINUTES][0].Equal(result.BalanceMap[MINUTES][0])) ||
!(rifsBalance.BalanceMap[MINUTES][1].Equal(result.BalanceMap[MINUTES][1])) ||
len(rifsBalance.BalanceMap[MINUTES+OUTBOUND]) < 2 || len(result.BalanceMap[MINUTES+OUTBOUND]) < 2 ||
!(rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Equal(result.BalanceMap[MINUTES+OUTBOUND][0])) ||
!(rifsBalance.BalanceMap[MINUTES+OUTBOUND][1].Equal(result.BalanceMap[MINUTES+OUTBOUND][1])) ||
!rifsBalance.BalanceMap[CREDIT+OUTBOUND].Equal(result.BalanceMap[CREDIT+OUTBOUND]) {
t.Errorf("Expected %v was %v", rifsBalance, result)
}
@@ -148,7 +148,7 @@ func TestUserBalanceStorageStore(t *testing.T) {
func TestDebitMoneyBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.01, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
result := rifsBalance.debitBalance(CREDIT, 6, false)
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 15 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", 15, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
@@ -158,7 +158,7 @@ func TestDebitMoneyBalance(t *testing.T) {
func TestDebitAllMoneyBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.01, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance.debitBalance(CREDIT, 21, false)
result := rifsBalance.debitBalance(CREDIT, 0, false)
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 0 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
@@ -169,7 +169,7 @@ func TestDebitAllMoneyBalance(t *testing.T) {
func TestDebitMoreMoneyBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
result := rifsBalance.debitBalance(CREDIT, 22, false)
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -1 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", -1, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
@@ -179,7 +179,7 @@ func TestDebitMoreMoneyBalance(t *testing.T) {
func TestDebitNegativeMoneyBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
result := rifsBalance.debitBalance(CREDIT, -15, false)
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 36 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", 36, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
@@ -189,7 +189,7 @@ func TestDebitNegativeMoneyBalance(t *testing.T) {
func TestDebitMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(6, "0723", false)
if b2.Value != 94 || err != nil {
t.Log(err)
@@ -200,7 +200,7 @@ func TestDebitMinuteBalance(t *testing.T) {
func TestDebitMultipleBucketsMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(105, "0723", false)
if b2.Value != 0 || b1.Value != 5 || err != nil {
t.Log(err)
@@ -211,7 +211,7 @@ func TestDebitMultipleBucketsMinuteBalance(t *testing.T) {
func TestDebitAllMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(110, "0723", false)
if b2.Value != 0 || b1.Value != 0 || err != nil {
t.Errorf("Expected %v was %v", 0, b2.Value)
@@ -221,7 +221,7 @@ func TestDebitAllMinuteBalance(t *testing.T) {
func TestDebitMoreMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(115, "0723", false)
if b2.Value != 100 || b1.Value != 10 || err == nil {
t.Errorf("Expected %v was %v", 1000, b2.Value)
@@ -231,7 +231,7 @@ func TestDebitMoreMinuteBalance(t *testing.T) {
func TestDebitSpecialPriceMinuteBalance0(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(5, "0723", false)
if b2.Value != 95 || b1.Value != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 16 {
t.Errorf("Expected %v was %v", 16, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
@@ -241,7 +241,7 @@ func TestDebitSpecialPriceMinuteBalance0(t *testing.T) {
func TestDebitSpecialPriceAllMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(21, "0723", false)
if b2.Value != 79 || b1.Value != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 0 {
t.Errorf("Expected %v was %v", 0, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
@@ -251,7 +251,7 @@ func TestDebitSpecialPriceAllMinuteBalance(t *testing.T) {
func TestDebitSpecialPriceMoreMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(25, "0723", false)
if b2.Value != 75 || b1.Value != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -4 {
t.Log(b2.Value)
@@ -264,7 +264,7 @@ func TestDebitSpecialPriceMoreMinuteBalance(t *testing.T) {
func TestDebitSpecialPriceMoreMinuteBalancePrepay(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", Type: UB_TYPE_PREPAID, BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", Type: UB_TYPE_PREPAID, BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(25, "0723", false)
expected := 21.0
if b2.Value != 100 || b1.Value != 10 || err != AMOUNT_TOO_BIG || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != expected {
@@ -278,7 +278,7 @@ func TestDebitSpecialPriceMoreMinuteBalancePrepay(t *testing.T) {
func TestDebitSpecialPriceNegativeMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(-15, "0723", false)
if b2.Value != 115 || b1.Value != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 36 {
t.Log(b1, b2, err)
@@ -289,7 +289,7 @@ func TestDebitSpecialPriceNegativeMinuteBalance(t *testing.T) {
func TestDebitNegativeMinuteBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(-15, "0723", false)
if b2.Value != 115 || b1.Value != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 21 {
t.Log(b1, b2, err)
@@ -300,7 +300,7 @@ func TestDebitNegativeMinuteBalance(t *testing.T) {
func TestDebitSMSBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
result := rifsBalance.debitBalance(SMS, 12, false)
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 88 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", 88, rifsBalance.BalanceMap[SMS+OUTBOUND])
@@ -310,7 +310,7 @@ func TestDebitSMSBalance(t *testing.T) {
func TestDebitAllSMSBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
result := rifsBalance.debitBalance(SMS, 100, false)
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 0 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", 0, rifsBalance.BalanceMap[SMS+OUTBOUND])
@@ -320,7 +320,7 @@ func TestDebitAllSMSBalance(t *testing.T) {
func TestDebitMoreSMSBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
result := rifsBalance.debitBalance(SMS, 110, false)
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != -10 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", -10, rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value)
@@ -330,7 +330,7 @@ func TestDebitMoreSMSBalance(t *testing.T) {
func TestDebitNegativeSMSBalance(t *testing.T) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.0, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
result := rifsBalance.debitBalance(SMS, -15, false)
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 115 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
t.Errorf("Expected %v was %v", 115, rifsBalance.BalanceMap[SMS+OUTBOUND])
@@ -341,12 +341,12 @@ func TestUserBalancedebitBalance(t *testing.T) {
ub := &UserBalance{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS: BalanceChain{&Balance{Value: 14}}, TRAFFIC: BalanceChain{&Balance{Value: 1204}}, MINUTES: BalanceChain{&Balance{Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{SMS: BalanceChain{&Balance{Value: 14}}, TRAFFIC: BalanceChain{&Balance{Value: 1204}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
}
newMb := &Balance{Weight: 20, SpecialPrice: 1, DestinationId: "NEW"}
ub.debitMinuteBalance(newMb)
if len(ub.BalanceMap[MINUTES]) != 3 || ub.BalanceMap[MINUTES][2] != newMb {
t.Error("Error adding minute bucket!", len(ub.BalanceMap[MINUTES]), ub.BalanceMap[MINUTES])
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 3 || ub.BalanceMap[MINUTES+OUTBOUND][2] != newMb {
t.Error("Error adding minute bucket!", len(ub.BalanceMap[MINUTES+OUTBOUND]), ub.BalanceMap[MINUTES+OUTBOUND])
}
}
@@ -355,11 +355,11 @@ func TestUserBalancedebitBalanceExists(t *testing.T) {
ub := &UserBalance{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES: BalanceChain{&Balance{Value: 15, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 15, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
}
newMb := &Balance{Value: -10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}
ub.debitMinuteBalance(newMb)
if len(ub.BalanceMap[MINUTES]) != 2 || ub.BalanceMap[MINUTES][0].Value != 25 {
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 25 {
t.Error("Error adding minute bucket!")
}
}
@@ -368,10 +368,10 @@ func TestUserBalanceAddMinuteNil(t *testing.T) {
ub := &UserBalance{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES: BalanceChain{&Balance{Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
}
ub.debitMinuteBalance(nil)
if len(ub.BalanceMap[MINUTES]) != 2 {
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 {
t.Error("Error adding minute bucket!")
}
}
@@ -382,53 +382,53 @@ func TestUserBalanceAddMinutBucketEmpty(t *testing.T) {
mb3 := &Balance{Value: -10, DestinationId: "OTHER"}
ub := &UserBalance{}
ub.debitMinuteBalance(mb1)
if len(ub.BalanceMap[MINUTES]) != 1 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES])
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 1 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
}
ub.debitMinuteBalance(mb2)
if len(ub.BalanceMap[MINUTES]) != 1 || ub.BalanceMap[MINUTES][0].Value != 20 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES])
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 1 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 20 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
}
ub.debitMinuteBalance(mb3)
if len(ub.BalanceMap[MINUTES]) != 2 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES])
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
}
}
func TestUserBalanceExecuteTriggeredActions(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS"}},
}
ub.countUnits(&Action{BalanceId: CREDIT, Balance: &Balance{Value: 1}})
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES][0].Value)
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)
}
// are set to executed
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES][0].Value)
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)
}
// we can reset them
ub.resetActionTriggers(nil)
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 120 || ub.BalanceMap[MINUTES][0].Value != 30 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES][0].Value)
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 120 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 30 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)
}
}
func TestUserBalanceExecuteTriggeredActionsBalance(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, SpecialPrice: 1, DestinationId: "NAT"}, &Balance{Weight: 10, SpecialPrice: 10, SpecialPriceType: PRICE_ABSOLUTE, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Units: 1}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 100, ThresholdType: TRIGGER_MIN_COUNTER, ActionsId: "TEST_ACTIONS"}},
}
ub.countUnits(&Action{BalanceId: CREDIT, Balance: &Balance{Value: 1}})
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES][0].Value)
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 110 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 20 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)
}
}
@@ -451,7 +451,7 @@ func TestCleanExpired(t *testing.T) {
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{
&Balance{ExpirationDate: time.Now().Add(10 * time.Second)},
&Balance{ExpirationDate: time.Date(2013, 7, 18, 14, 33, 0, 0, time.UTC)},
&Balance{ExpirationDate: time.Now().Add(10 * time.Second)}}, MINUTES: BalanceChain{
&Balance{ExpirationDate: time.Now().Add(10 * time.Second)}}, MINUTES + OUTBOUND: BalanceChain{
&Balance{ExpirationDate: time.Date(2013, 7, 18, 14, 33, 0, 0, time.UTC)},
&Balance{ExpirationDate: time.Now().Add(10 * time.Second)},
}},
@@ -460,7 +460,7 @@ func TestCleanExpired(t *testing.T) {
if len(ub.BalanceMap[CREDIT+OUTBOUND]) != 2 {
t.Error("Error cleaning expired balances!")
}
if len(ub.BalanceMap[MINUTES]) != 1 {
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 1 {
t.Error("Error cleaning expired minute buckets!")
}
}
@@ -516,7 +516,7 @@ func BenchmarkGetSecondForPrefix(b *testing.B) {
b1 := &Balance{Value: 10, SpecialPrice: 10, Weight: 10, DestinationId: "NAT"}
b2 := &Balance{Value: 100, SpecialPrice: 1, Weight: 20, DestinationId: "RET"}
ub1 := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
ub1 := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
b.StartTimer()
for i := 0; i < b.N; i++ {
ub1.getSecondsForPrefix("0723")
@@ -526,7 +526,7 @@ func BenchmarkGetSecondForPrefix(b *testing.B) {
func BenchmarkUserBalanceStorageStoreRestore(b *testing.B) {
b1 := &Balance{Value: 10, Weight: 10, SpecialPrice: 0.01, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, SpecialPrice: 0.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
for i := 0; i < b.N; i++ {
storageGetter.SetUserBalance(rifsBalance)
storageGetter.GetUserBalance(rifsBalance.Id)
@@ -536,7 +536,7 @@ func BenchmarkUserBalanceStorageStoreRestore(b *testing.B) {
func BenchmarkGetSecondsForPrefix(b *testing.B) {
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
ub1 := &UserBalance{Id: "OUT:CUSTOMER_1:rif", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
for i := 0; i < b.N; i++ {
ub1.getSecondsForPrefix("0723")
}