renamed action's BalanceId to BalanceType

also added a topup test
This commit is contained in:
Radu Ioan Fericean
2014-02-21 13:14:06 +02:00
parent 164b9f8945
commit f701a42948
19 changed files with 201 additions and 179 deletions

View File

@@ -63,10 +63,10 @@ func (self *ApierV1) GetRatingPlan(rplnId string, reply *engine.RatingPlan) erro
}
type AttrGetUserBalance struct {
Tenant string
Account string
BalanceId string
Direction string
Tenant string
Account string
BalanceType string
Direction string
}
// Get balance
@@ -84,7 +84,7 @@ func (self *ApierV1) GetUserBalance(attr *AttrGetUserBalance, reply *engine.User
type AttrAddBalance struct {
Tenant string
Account string
BalanceId string
BalanceType string
Direction string
Value float64
ExpirationDate time.Time
@@ -119,9 +119,9 @@ func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error {
}
at.SetActions(engine.Actions{
&engine.Action{
ActionType: aType,
BalanceId: attr.BalanceId,
Direction: attr.Direction,
ActionType: aType,
BalanceType: attr.BalanceType,
Direction: attr.Direction,
Balance: &engine.Balance{
Value: attr.Value,
ExpirationDate: attr.ExpirationDate,
@@ -274,7 +274,7 @@ func (self *ApierV1) SetActions(attrs AttrSetActions, reply *string) error {
a := &engine.Action{
Id: utils.GenUUID(),
ActionType: apiAct.Identifier,
BalanceId: apiAct.BalanceType,
BalanceType: apiAct.BalanceType,
Direction: apiAct.Direction,
Weight: apiAct.Weight,
ExpirationString: apiAct.ExpiryTime,
@@ -370,7 +370,7 @@ type AttrAddActionTrigger struct {
Tenant string
Account string
Direction string
BalanceId string
BalanceType string
ThresholdType string
ThresholdValue float64
DestinationId string
@@ -385,7 +385,7 @@ func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string
at := &engine.ActionTrigger{
Id: utils.GenUUID(),
BalanceId: attr.BalanceId,
BalanceType: attr.BalanceType,
Direction: attr.Direction,
ThresholdType: attr.ThresholdType,
ThresholdValue: attr.ThresholdValue,

View File

@@ -887,43 +887,43 @@ func TestApierAddBalance(t *testing.T) {
return
}
reply := ""
attrs := &AttrAddBalance{Tenant: "cgrates.org", Account: "1001", BalanceId: "*monetary", Direction: "*out", Value: 1.5}
attrs := &AttrAddBalance{Tenant: "cgrates.org", Account: "1001", BalanceType: "*monetary", Direction: "*out", Value: 1.5}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan", BalanceId: "*monetary", Direction: "*out", Value: 1.5}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan", BalanceType: "*monetary", Direction: "*out", Value: 1.5}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan2", BalanceId: "*monetary", Direction: "*out", Value: 1.5}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan2", BalanceType: "*monetary", Direction: "*out", Value: 1.5}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out", Value: 1.5}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceType: "*monetary", Direction: "*out", Value: 1.5}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out", Value: 2.1}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceType: "*monetary", Direction: "*out", Value: 2.1}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out", Value: 2.1}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceType: "*monetary", Direction: "*out", Value: 2.1}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
t.Errorf("Calling ApierV1.AddBalance received: %s", reply)
}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out", Value: 1, Overwrite: true}
attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceType: "*monetary", Direction: "*out", Value: 1, Overwrite: true}
if err := rater.Call("ApierV1.AddBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {
@@ -996,7 +996,7 @@ func TestApierAddTriggeredAction(t *testing.T) {
}
reply := ""
// Add balance to a previously known account
attrs := &AttrAddActionTrigger{Tenant: "cgrates.org", Account: "dan2", Direction: "*out", BalanceId: "*monetary",
attrs := &AttrAddActionTrigger{Tenant: "cgrates.org", Account: "dan2", Direction: "*out", BalanceType: "*monetary",
ThresholdType: "*min_balance", ThresholdValue: 2, DestinationId: "*any", Weight: 10, ActionsId: "WARN_VIA_HTTP"}
if err := rater.Call("ApierV1.AddTriggeredAction", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.AddTriggeredAction: ", err.Error())
@@ -1121,35 +1121,35 @@ func TestApierGetUserBalance(t *testing.T) {
return
}
var reply *engine.UserBalance
attrs := &AttrGetUserBalance{Tenant: "cgrates.org", Account: "1001", BalanceId: "*monetary", Direction: "*out"}
attrs := &AttrGetUserBalance{Tenant: "cgrates.org", Account: "1001", BalanceType: "*monetary", Direction: "*out"}
if err := rater.Call("ApierV1.GetUserBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetUserBalance: ", err.Error())
} else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 11.5 { // We expect 11.5 since we have added in the previous test 1.5
} else if reply.BalanceMap[attrs.BalanceType+attrs.Direction].GetTotalValue() != 11.5 { // We expect 11.5 since we have added in the previous test 1.5
t.Errorf("Calling ApierV1.GetBalance expected: 11.5, received: %f", reply)
}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan", BalanceId: "*monetary", Direction: "*out"}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan", BalanceType: "*monetary", Direction: "*out"}
if err := rater.Call("ApierV1.GetUserBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetUserBalance: ", err.Error())
} else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 1.5 {
} else if reply.BalanceMap[attrs.BalanceType+attrs.Direction].GetTotalValue() != 1.5 {
t.Errorf("Calling ApierV1.GetUserBalance expected: 1.5, received: %f", reply)
}
// The one we have topped up though executeAction
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan2", BalanceId: "*monetary", Direction: "*out"}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan2", BalanceType: "*monetary", Direction: "*out"}
if err := rater.Call("ApierV1.GetUserBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetUserBalance: ", err.Error())
} else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 10 {
} else if reply.BalanceMap[attrs.BalanceType+attrs.Direction].GetTotalValue() != 10 {
t.Errorf("Calling ApierV1.GetUserBalance expected: 10, received: %f", reply)
}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out"}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan3", BalanceType: "*monetary", Direction: "*out"}
if err := rater.Call("ApierV1.GetUserBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetUserBalance: ", err.Error())
} else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 3.6 {
} else if reply.BalanceMap[attrs.BalanceType+attrs.Direction].GetTotalValue() != 3.6 {
t.Errorf("Calling ApierV1.GetUserBalance expected: 3.6, received: %f", reply)
}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out"}
attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan6", BalanceType: "*monetary", Direction: "*out"}
if err := rater.Call("ApierV1.GetUserBalance", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetUserBalance: ", err.Error())
} else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 1 {
} else if reply.BalanceMap[attrs.BalanceType+attrs.Direction].GetTotalValue() != 1 {
t.Errorf("Calling ApierV1.GetUserBalance expected: 1, received: %f", reply)
}
}
@@ -1166,7 +1166,7 @@ func TestTriggersExecute(t *testing.T) {
} else if reply != "OK" {
t.Errorf("Calling ApierV1.SetAccount received: %s", reply)
}
attrAddBlnc := &AttrAddBalance{Tenant: "cgrates.org", Account: "1008", BalanceId: "*monetary", Direction: "*out", Value: 2}
attrAddBlnc := &AttrAddBalance{Tenant: "cgrates.org", Account: "1008", BalanceType: "*monetary", Direction: "*out", Value: 2}
if err := rater.Call("ApierV1.AddBalance", attrAddBlnc, &reply); err != nil {
t.Error("Got error on ApierV1.AddBalance: ", err.Error())
} else if reply != "OK" {

View File

@@ -45,7 +45,7 @@ func (self *CmdAddBalance) Usage(name string) string {
// set param defaults
func (self *CmdAddBalance) defaults() error {
self.rpcMethod = "ApierV1.AddBalance"
self.rpcParams = &apier.AttrAddBalance{BalanceId: engine.CREDIT}
self.rpcParams = &apier.AttrAddBalance{BalanceType: engine.CREDIT}
self.rpcParams.Direction = "*out"
return nil
}
@@ -66,7 +66,7 @@ func (self *CmdAddBalance) FromArgs(args []string) error {
}
self.rpcParams.Value = value
if len(args) > 5 {
self.rpcParams.BalanceId = args[5]
self.rpcParams.BalanceType = args[5]
}
if len(args) > 6 {
self.rpcParams.DestinationId = args[6]

View File

@@ -20,8 +20,9 @@ package console
import (
"fmt"
"github.com/cgrates/cgrates/apier"
"strconv"
"github.com/cgrates/cgrates/apier"
)
func init() {
@@ -56,7 +57,7 @@ func (self *CmdAddTriggeredAction) FromArgs(args []string) error {
self.defaults()
self.rpcParams.Tenant = args[2]
self.rpcParams.Account = args[3]
self.rpcParams.BalanceId = args[4]
self.rpcParams.BalanceType = args[4]
thresholdvalue, err := strconv.ParseFloat(args[5], 64)
if err != nil {
return err

View File

@@ -44,7 +44,7 @@ func (self *CmdGetBalances) Usage(name string) string {
// set param defaults
func (self *CmdGetBalances) defaults() error {
self.rpcMethod = "ApierV1.GetUserBalance"
self.rpcParams = &apier.AttrGetUserBalance{BalanceId: engine.CREDIT}
self.rpcParams = &apier.AttrGetUserBalance{BalanceType: engine.CREDIT}
self.rpcParams.Direction = "*out"
return nil
}

View File

@@ -39,7 +39,7 @@
- name: add cgrates repo
apt_repository: repo='deb http://apt.itsyscom.com/debian wheezy main' state=present
- name: install freeswitch
- name: install cgrates
apt: pkg=cgrates update_cache=yes state=latest
- name: update mysql root password for root account

View File

@@ -39,7 +39,7 @@ Structure to be filled for each tariff plan with the bonus value for received ca
type Action struct {
Id string
ActionType string
BalanceId string
BalanceType string
Direction string
ExtraParameters string
ExpirationString string
@@ -142,7 +142,7 @@ func topupResetAction(ub *UserBalance, a *Action) (err error) {
if ub.BalanceMap == nil { // Init the map since otherwise will get error if nil
ub.BalanceMap = make(map[string]BalanceChain, 0)
}
ub.BalanceMap[a.BalanceId+a.Direction] = BalanceChain{}
ub.BalanceMap[a.BalanceType+a.Direction] = BalanceChain{}
genericMakeNegative(a)
genericDebit(ub, a)
return
@@ -161,7 +161,7 @@ func debitAction(ub *UserBalance, a *Action) (err error) {
func resetCounterAction(ub *UserBalance, a *Action) (err error) {
uc := ub.getUnitCounter(a)
if uc == nil {
uc = &UnitsCounter{BalanceId: a.BalanceId, Direction: a.Direction}
uc = &UnitsCounter{BalanceType: a.BalanceType, Direction: a.Direction}
ub.UnitCounters = append(ub.UnitCounters, uc)
}
uc.initBalances(ub.ActionTriggers)

View File

@@ -28,7 +28,7 @@ import (
type ActionTrigger struct {
Id string // uniquely identify the trigger
BalanceId string
BalanceType string
Direction string
ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance
ThresholdValue float64
@@ -82,7 +82,7 @@ func (at *ActionTrigger) Match(a *Action) bool {
if a == nil {
return true
}
id := a.BalanceId == "" || at.BalanceId == a.BalanceId
id := a.BalanceType == "" || at.BalanceType == a.BalanceType
direction := a.Direction == "" || at.Direction == a.Direction
thresholdType, thresholdValue := true, true
if a.ExtraParameters != "" {

View File

@@ -360,9 +360,9 @@ func TestActionTimingOneTimeRun(t *testing.T) {
func TestActionTimingLogFunction(t *testing.T) {
a := &Action{
ActionType: "*log",
BalanceId: "test",
Balance: &Balance{Value: 1.1},
ActionType: "*log",
BalanceType: "test",
Balance: &Balance{Value: 1.1},
}
at := &ActionTiming{
actions: []*Action{a},
@@ -461,7 +461,7 @@ func TestActionTimingsRemoveMember(t *testing.T) {
func TestActionTriggerMatchNil(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
@@ -474,7 +474,7 @@ func TestActionTriggerMatchNil(t *testing.T) {
func TestActionTriggerMatchAllBlank(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
@@ -487,11 +487,11 @@ func TestActionTriggerMatchAllBlank(t *testing.T) {
func TestActionTriggerMatchMinuteBucketBlank(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
a := &Action{Direction: OUTBOUND, BalanceId: CREDIT}
a := &Action{Direction: OUTBOUND, BalanceType: CREDIT}
if !at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
@@ -500,7 +500,7 @@ func TestActionTriggerMatchMinuteBucketBlank(t *testing.T) {
func TestActionTriggerMatchMinuteBucketFull(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
@@ -513,11 +513,11 @@ func TestActionTriggerMatchMinuteBucketFull(t *testing.T) {
func TestActionTriggerMatchAllFull(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
a := &Action{Direction: OUTBOUND, BalanceId: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 2)}
a := &Action{Direction: OUTBOUND, BalanceType: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 2)}
if !at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
@@ -526,11 +526,11 @@ func TestActionTriggerMatchAllFull(t *testing.T) {
func TestActionTriggerMatchSomeFalse(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
a := &Action{Direction: INBOUND, BalanceId: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 2)}
a := &Action{Direction: INBOUND, BalanceType: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 2)}
if at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
@@ -539,11 +539,11 @@ func TestActionTriggerMatchSomeFalse(t *testing.T) {
func TestActionTriggerMatcBalanceFalse(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
a := &Action{Direction: OUTBOUND, BalanceId: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 3.0)}
a := &Action{Direction: OUTBOUND, BalanceType: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_BALANCE, 3.0)}
if at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
@@ -552,11 +552,11 @@ func TestActionTriggerMatcBalanceFalse(t *testing.T) {
func TestActionTriggerMatcAllFalse(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceId: CREDIT,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
}
a := &Action{Direction: INBOUND, BalanceId: MINUTES, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_COUNTER, 3)}
a := &Action{Direction: INBOUND, BalanceType: MINUTES, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v}`, TRIGGER_MAX_COUNTER, 3)}
if at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
@@ -578,8 +578,8 @@ func TestActionResetTriggres(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetTriggersAction(ub, nil)
if ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
@@ -591,8 +591,8 @@ func TestActionResetTriggresExecutesThem(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetTriggersAction(ub, nil)
if ub.ActionTriggers[0].Executed == true || ub.BalanceMap[CREDIT][0].Value == 12 {
@@ -604,10 +604,10 @@ func TestActionResetTriggresActionFilter(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 10}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetTriggersAction(ub, &Action{BalanceId: SMS})
resetTriggersAction(ub, &Action{BalanceType: SMS})
if ub.ActionTriggers[0].Executed == false || ub.ActionTriggers[1].Executed == false {
t.Error("Reset triggers action failed!")
}
@@ -618,8 +618,8 @@ func TestActionSetPostpaid(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
setPostpaidAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID {
@@ -632,8 +632,8 @@ func TestActionSetPrepaid(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
setPrepaidAction(ub, nil)
if ub.Type != UB_TYPE_PREPAID {
@@ -646,8 +646,8 @@ func TestActionResetPrepaid(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetPrepaidAction(ub, nil)
if ub.Type != UB_TYPE_PREPAID ||
@@ -665,8 +665,8 @@ func TestActionResetPostpaid(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetPostpaidAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID ||
@@ -683,10 +683,10 @@ func TestActionTopupResetCredit(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 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}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
topupResetAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 10 ||
@@ -704,10 +704,10 @@ func TestActionTopupResetMinutes(t *testing.T) {
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}},
MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 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}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
topupResetAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue() != 5 ||
@@ -724,10 +724,10 @@ func TestActionTopupCredit(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 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}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
topupAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 110 ||
@@ -743,10 +743,10 @@ func TestActionTopupMinutes(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
topupAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue() != 15 ||
@@ -763,10 +763,10 @@ func TestActionDebitCredit(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 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}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
debitAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 90 ||
@@ -782,10 +782,10 @@ func TestActionDebitMinutes(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
debitAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 5 ||
@@ -806,8 +806,8 @@ func TestActionResetAllCounters(t *testing.T) {
MINUTES: BalanceChain{
&Balance{Value: 10, Weight: 20, DestinationId: "NAT"},
&Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetCountersAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID ||
@@ -834,10 +834,10 @@ func TestActionResetCounterMinutes(t *testing.T) {
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: MINUTES}
a := &Action{BalanceType: MINUTES}
resetCounterAction(ub, a)
if ub.Type != UB_TYPE_POSTPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
@@ -864,10 +864,10 @@ func TestActionResetCounterCREDIT(t *testing.T) {
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}, &UnitsCounter{BalanceId: SMS, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}, &UnitsCounter{BalanceType: SMS, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceId: CREDIT, Direction: OUTBOUND}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND}
resetCounterAction(ub, a)
if ub.Type != UB_TYPE_POSTPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
@@ -881,7 +881,7 @@ func TestActionResetCounterCREDIT(t *testing.T) {
func TestActionTriggerLogging(t *testing.T) {
at := &ActionTrigger{
Id: "some_uuid",
BalanceId: CREDIT,
BalanceType: CREDIT,
Direction: OUTBOUND,
ThresholdValue: 100.0,
DestinationId: "NAT",

View File

@@ -181,7 +181,7 @@ func (b *Balance) DebitMinutes(cc *CallCost, count bool, ub *UserBalance, moneyB
inc.Cost = 0
inc.paid = true
if count {
ub.countUnits(&Action{BalanceId: MINUTES, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
ub.countUnits(&Action{BalanceType: MINUTES, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
}
}
continue
@@ -222,8 +222,8 @@ func (b *Balance) DebitMinutes(cc *CallCost, count bool, ub *UserBalance, moneyB
nInc.MinuteInfo = &MinuteInfo{newCC.Destination, seconds}
nInc.paid = true
if count {
ub.countUnits(&Action{BalanceId: MINUTES, Direction: newCC.Direction, Balance: &Balance{Value: seconds, DestinationId: newCC.Destination}})
ub.countUnits(&Action{BalanceId: CREDIT, Direction: newCC.Direction, Balance: &Balance{Value: cost, DestinationId: newCC.Destination}})
ub.countUnits(&Action{BalanceType: MINUTES, Direction: newCC.Direction, Balance: &Balance{Value: seconds, DestinationId: newCC.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: newCC.Direction, Balance: &Balance{Value: cost, DestinationId: newCC.Destination}})
}
} else {
increment.paid = false
@@ -285,7 +285,7 @@ func (b *Balance) DebitMoney(cc *CallCost, count bool, ub *UserBalance) error {
increment.SetMoneyBalance(b.Uuid)
increment.paid = true
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
}
}
} else {
@@ -313,7 +313,7 @@ func (b *Balance) DebitMoney(cc *CallCost, count bool, ub *UserBalance) error {
nInc.SetMoneyBalance(b.Uuid)
nInc.paid = true
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: newCC.Direction, Balance: &Balance{Value: amount, DestinationId: newCC.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: newCC.Direction, Balance: &Balance{Value: amount, DestinationId: newCC.Destination}})
}
} else {
increment.paid = false

View File

@@ -41,7 +41,7 @@ func populateDB() {
Id: "*out:vdf:minu",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 0}},
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 50}},
MINUTES + OUTBOUND: BalanceChain{
&Balance{Value: 200, DestinationId: "NAT", Weight: 10},
&Balance{Value: 100, DestinationId: "RET", Weight: 20},

View File

@@ -478,7 +478,7 @@ func (csvr *CSVReader) LoadActions() (err error) {
a := &Action{
Id: utils.GenUUID(),
ActionType: record[1],
BalanceId: record[2],
BalanceType: record[2],
Direction: record[3],
Weight: weight,
ExpirationString: record[5],
@@ -565,7 +565,7 @@ func (csvr *CSVReader) LoadActionTriggers() (err error) {
}
at := &ActionTrigger{
Id: utils.GenUUID(),
BalanceId: record[1],
BalanceType: record[1],
Direction: record[2],
ThresholdType: record[3],
ThresholdValue: value,

View File

@@ -571,7 +571,7 @@ func TestLoadActions(t *testing.T) {
&Action{
Id: as[0].Id,
ActionType: TOPUP_RESET,
BalanceId: CREDIT,
BalanceType: CREDIT,
Direction: OUTBOUND,
ExpirationString: UNLIMITED,
ExtraParameters: "",
@@ -585,7 +585,7 @@ func TestLoadActions(t *testing.T) {
&Action{
Id: as[1].Id,
ActionType: TOPUP,
BalanceId: MINUTES,
BalanceType: MINUTES,
Direction: OUTBOUND,
ExpirationString: UNLIMITED,
ExtraParameters: "",
@@ -637,7 +637,7 @@ func TestLoadActionTriggers(t *testing.T) {
atr := csvr.actionsTriggers["STANDARD_TRIGGER"][0]
expected := &ActionTrigger{
Id: atr.Id,
BalanceId: MINUTES,
BalanceType: MINUTES,
Direction: OUTBOUND,
ThresholdType: TRIGGER_MIN_COUNTER,
ThresholdValue: 10,
@@ -652,7 +652,7 @@ func TestLoadActionTriggers(t *testing.T) {
atr = csvr.actionsTriggers["STANDARD_TRIGGER"][1]
expected = &ActionTrigger{
Id: atr.Id,
BalanceId: MINUTES,
BalanceType: MINUTES,
Direction: OUTBOUND,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 200,

View File

@@ -398,7 +398,7 @@ func (dbr *DbReader) LoadActions() (err error) {
acts[idx] = &Action{
Id: utils.GenUUID(),
ActionType: tpact.Identifier,
BalanceId: tpact.BalanceType,
BalanceType: tpact.BalanceType,
Direction: tpact.Direction,
Weight: tpact.Weight,
ExtraParameters: tpact.ExtraParameters,
@@ -462,7 +462,7 @@ func (dbr *DbReader) LoadActionTriggers() (err error) {
atrs := make([]*ActionTrigger, len(atrsLst))
for idx, apiAtr := range atrsLst {
atrs[idx] = &ActionTrigger{Id: utils.GenUUID(),
BalanceId: apiAtr.BalanceType,
BalanceType: apiAtr.BalanceType,
Direction: apiAtr.Direction,
ThresholdType: apiAtr.ThresholdType,
ThresholdValue: apiAtr.ThresholdValue,
@@ -594,7 +594,7 @@ func (dbr *DbReader) LoadAccountActionsFiltered(qriedAA *utils.TPAccountActions)
atrs := make([]*ActionTrigger, len(atrsLst))
for idx, apiAtr := range atrsLst {
atrs[idx] = &ActionTrigger{Id: utils.GenUUID(),
BalanceId: apiAtr.BalanceType,
BalanceType: apiAtr.BalanceType,
Direction: apiAtr.Direction,
ThresholdType: apiAtr.ThresholdType,
ThresholdValue: apiAtr.ThresholdValue,
@@ -625,7 +625,7 @@ func (dbr *DbReader) LoadAccountActionsFiltered(qriedAA *utils.TPAccountActions)
enacts[idx] = &Action{
Id: utils.GenUUID(),
ActionType: tpact.Identifier,
BalanceId: tpact.BalanceType,
BalanceType: tpact.BalanceType,
Direction: tpact.Direction,
Weight: tpact.Weight,
ExtraParameters: tpact.ExtraParameters,

View File

@@ -117,18 +117,17 @@ func TestStoreInterfaces(t *testing.T) {
var _ LogStorage = sql
}
/************************** Benchmarks *****************************/
func GetUB() *UserBalance {
uc := &UnitsCounter{
Direction: OUTBOUND,
BalanceId: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
Direction: OUTBOUND,
BalanceType: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
}
at := &ActionTrigger{
Id: "some_uuid",
BalanceId: CREDIT,
BalanceType: CREDIT,
Direction: OUTBOUND,
ThresholdValue: 100.0,
DestinationId: "NAT",

View File

@@ -25,8 +25,8 @@ import (
// Amount of a trafic of a certain type
type UnitsCounter struct {
Direction string
BalanceId string
Direction string
BalanceType string
// Units float64
Balances BalanceChain // first balance is the general one (no destination)
}

View File

@@ -24,9 +24,9 @@ import (
func TestUnitsCounterAddBalance(t *testing.T) {
uc := &UnitsCounter{
Direction: OUTBOUND,
BalanceId: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
Direction: OUTBOUND,
BalanceType: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
}
uc.addUnits(20, "test")
if len(uc.Balances) != 3 {
@@ -36,9 +36,9 @@ func TestUnitsCounterAddBalance(t *testing.T) {
func TestUnitsCounterAddBalanceExists(t *testing.T) {
uc := &UnitsCounter{
Direction: OUTBOUND,
BalanceId: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
Direction: OUTBOUND,
BalanceType: SMS,
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}},
}
uc.addUnits(5, "0723")
if len(uc.Balances) != 3 || uc.Balances[1].Value != 15 {

View File

@@ -93,10 +93,10 @@ func (ub *UserBalance) debitBalanceAction(a *Action) error {
a.Balance.Uuid = utils.GenUUID()
}
if ub.BalanceMap == nil {
ub.BalanceMap = make(map[string]BalanceChain, 0)
ub.BalanceMap = make(map[string]BalanceChain, 1)
}
found := false
id := a.BalanceId + a.Direction
id := a.BalanceType + a.Direction
for _, b := range ub.BalanceMap[id] {
if b.IsExpired() {
continue // we can clean expired balances balances here
@@ -107,9 +107,8 @@ func (ub *UserBalance) debitBalanceAction(a *Action) error {
break
}
}
// if it is not found and the Seconds are negative (topup)
// then we add it to the list
if !found && a.Balance.Value <= 0 {
// if it is not found then we add it to the list
if !found {
a.Balance.Value = -a.Balance.Value
ub.BalanceMap[id] = append(ub.BalanceMap[id], a.Balance)
}
@@ -199,7 +198,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error {
cost := increment.Cost
moneyBalance.Value -= cost
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: cost, DestinationId: cc.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: cost, DestinationId: cc.Destination}})
}
returnError = insuficientCreditError
}
@@ -214,7 +213,7 @@ CONNECT_FEE:
b.Value -= amount
// the conect fee is not refundable!
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
}
connectFeePaid = true
break
@@ -226,7 +225,7 @@ CONNECT_FEE:
moneyBalance.Value -= amount
// the conect fee is not refundable!
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
}
}
}
@@ -255,7 +254,7 @@ func (ub *UserBalance) refundIncrements(increments Increments, direction string,
}
balance.Value += increment.Duration.Seconds()
if count {
ub.countUnits(&Action{BalanceId: MINUTES, Direction: direction, Balance: &Balance{Value: -increment.Duration.Seconds()}})
ub.countUnits(&Action{BalanceType: MINUTES, Direction: direction, Balance: &Balance{Value: -increment.Duration.Seconds()}})
}
}
// check money too
@@ -265,7 +264,7 @@ func (ub *UserBalance) refundIncrements(increments Increments, direction string,
}
balance.Value += increment.Cost
if count {
ub.countUnits(&Action{BalanceId: CREDIT, Direction: direction, Balance: &Balance{Value: -increment.Cost}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: direction, Balance: &Balance{Value: -increment.Cost}})
}
}
}
@@ -276,7 +275,7 @@ Debits some amount of user's specified balance. Returns the remaining credit in
*/
func (ub *UserBalance) debitGenericBalance(balanceId string, direction string, amount float64, count bool) float64 {
if count {
ub.countUnits(&Action{BalanceId: balanceId, Direction: direction, Balance: &Balance{Value: amount}})
ub.countUnits(&Action{BalanceType: balanceId, Direction: direction, Balance: &Balance{Value: amount}})
}
ub.BalanceMap[balanceId+direction].Debit(amount)
return ub.BalanceMap[balanceId+direction].GetTotalValue()
@@ -296,7 +295,7 @@ func (ub *UserBalance) executeActionTriggers(a *Action) {
}
if strings.Contains(at.ThresholdType, "counter") {
for _, uc := range ub.UnitCounters {
if uc.BalanceId == at.BalanceId {
if uc.BalanceType == at.BalanceType {
for _, mb := range uc.Balances {
if strings.Contains(at.ThresholdType, "*max") {
if mb.MatchDestination(at.DestinationId) && mb.Value >= at.ThresholdValue {
@@ -313,7 +312,7 @@ func (ub *UserBalance) executeActionTriggers(a *Action) {
}
}
} else { // BALANCE
for _, b := range ub.BalanceMap[at.BalanceId+at.Direction] {
for _, b := range ub.BalanceMap[at.BalanceType+at.Direction] {
if strings.Contains(at.ThresholdType, "*max") {
if b.MatchDestination(at.DestinationId) && b.Value >= at.ThresholdValue {
// run the actions
@@ -349,7 +348,7 @@ func (ub *UserBalance) getUnitCounter(a *Action) *UnitsCounter {
if direction == "" {
direction = OUTBOUND
}
if uc.BalanceId == a.BalanceId && uc.Direction == direction {
if uc.BalanceType == a.BalanceType && uc.Direction == direction {
return uc
}
}
@@ -366,7 +365,7 @@ func (ub *UserBalance) countUnits(a *Action) {
if direction == "" {
direction = OUTBOUND
}
unitsCounter = &UnitsCounter{BalanceId: a.BalanceId, Direction: direction}
unitsCounter = &UnitsCounter{BalanceType: a.BalanceType, Direction: direction}
ub.UnitCounters = append(ub.UnitCounters, unitsCounter)
}
@@ -390,7 +389,7 @@ func (ub *UserBalance) initCounters() {
}
uc, exists := ucTempMap[direction]
if !exists {
uc = &UnitsCounter{BalanceId: a.BalanceId, Direction: direction}
uc = &UnitsCounter{BalanceType: a.BalanceType, Direction: direction}
ucTempMap[direction] = uc
uc.Balances = BalanceChain{}
ub.UnitCounters = append(ub.UnitCounters, uc)

View File

@@ -34,12 +34,12 @@ func init() {
func populateTestActionsForTriggers() {
ats := []*Action{
&Action{ActionType: "*topup", BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}},
&Action{ActionType: "*topup", BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Weight: 20, Value: 10, DestinationId: "NAT"}},
&Action{ActionType: "*topup", BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}},
&Action{ActionType: "*topup", BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Weight: 20, Value: 10, DestinationId: "NAT"}},
}
accountingStorage.SetActions("TEST_ACTIONS", ats)
ats1 := []*Action{
&Action{ActionType: "*topup", BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}, Weight: 20},
&Action{ActionType: "*topup", BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}, Weight: 20},
&Action{ActionType: "*reset_prepaid", Weight: 10},
}
accountingStorage.SetActions("TEST_ACTIONS_ORDER", ats1)
@@ -853,7 +853,7 @@ func TestUserBalancedebitBalance(t *testing.T) {
BalanceMap: map[string]BalanceChain{SMS: BalanceChain{&Balance{Value: 14}}, TRAFFIC: BalanceChain{&Balance{Value: 1204}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
}
newMb := &Balance{Weight: 20, DestinationId: "NEW"}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: newMb}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: newMb}
ub.debitBalanceAction(a)
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])
@@ -868,7 +868,7 @@ func TestUserBalancedebitBalanceExists(t *testing.T) {
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 15, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
}
newMb := &Balance{Value: -10, Weight: 20, DestinationId: "NAT"}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: newMb}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: newMb}
ub.debitBalanceAction(a)
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 25 {
t.Error("Error adding minute bucket!")
@@ -892,17 +892,17 @@ func TestUserBalanceAddMinutBucketEmpty(t *testing.T) {
mb2 := &Balance{Value: -10, DestinationId: "NAT"}
mb3 := &Balance{Value: -10, DestinationId: "OTHER"}
ub := &UserBalance{}
a := &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: mb1}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: mb1}
ub.debitBalanceAction(a)
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 1 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
}
a = &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: mb2}
a = &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: mb2}
ub.debitBalanceAction(a)
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 1 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 20 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
}
a = &Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: mb3}
a = &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: mb3}
ub.debitBalanceAction(a)
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 {
t.Error("Error adding minute bucket: ", ub.BalanceMap[MINUTES+OUTBOUND])
@@ -913,21 +913,21 @@ func TestUserBalanceExecuteTriggeredActions(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS"}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS"}},
}
ub.countUnits(&Action{BalanceId: CREDIT, Balance: &Balance{Value: 1}})
ub.countUnits(&Action{BalanceType: CREDIT, Balance: &Balance{Value: 1}})
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}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
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}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
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)
}
@@ -937,10 +937,10 @@ func TestUserBalanceExecuteTriggeredActionsBalance(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 100, ThresholdType: TRIGGER_MIN_COUNTER, ActionsId: "TEST_ACTIONS"}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 100, ThresholdType: TRIGGER_MIN_COUNTER, ActionsId: "TEST_ACTIONS"}},
}
ub.countUnits(&Action{BalanceId: CREDIT, Balance: &Balance{Value: 1}})
ub.countUnits(&Action{BalanceType: CREDIT, Balance: &Balance{Value: 1}})
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)
}
@@ -950,10 +950,10 @@ func TestUserBalanceExecuteTriggeredActionsOrder(t *testing.T) {
ub := &UserBalance{
Id: "TEST_UB_OREDER",
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceId: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS_ORDER"}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS_ORDER"}},
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
if len(ub.BalanceMap[CREDIT+OUTBOUND]) != 1 || ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 10 {
t.Error("Error executing triggered actions in order", ub.BalanceMap[CREDIT+OUTBOUND])
}
@@ -981,44 +981,44 @@ func TestCleanExpired(t *testing.T) {
func TestUserBalanceUnitCounting(t *testing.T) {
ub := &UserBalance{}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
t.Error("Error counting units")
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
t.Error("Error counting units")
}
}
func TestUserBalanceUnitCountingOutbound(t *testing.T) {
ub := &UserBalance{}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
t.Error("Error counting units")
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
t.Error("Error counting units")
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 30 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 30 {
t.Error("Error counting units")
}
}
func TestUserBalanceUnitCountingOutboundInbound(t *testing.T) {
ub := &UserBalance{}
ub.countUnits(&Action{BalanceId: CREDIT, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
ub.countUnits(&Action{BalanceType: CREDIT, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 10 {
t.Errorf("Error counting units: %+v", ub.UnitCounters[0])
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 {
t.Error("Error counting units")
}
ub.countUnits(&Action{BalanceId: CREDIT, Direction: INBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 2 && ub.UnitCounters[1].BalanceId != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 || ub.UnitCounters[1].Balances[0].Value != 10 {
ub.countUnits(&Action{BalanceType: CREDIT, Direction: INBOUND, Balance: &Balance{Value: 10}})
if len(ub.UnitCounters) != 2 && ub.UnitCounters[1].BalanceType != CREDIT || ub.UnitCounters[0].Balances[0].Value != 20 || ub.UnitCounters[1].Balances[0].Value != 10 {
t.Error("Error counting units")
}
}
@@ -1049,6 +1049,29 @@ func TestUserBalanceRefund(t *testing.T) {
}
}
func TestTopupAction(t *testing.T) {
initialUb, _ := accountingStorage.GetUserBalance("*out:vdf:minu")
a := &Action{
ActionType: "*topup",
BalanceType: CREDIT,
Direction: OUTBOUND,
Balance: &Balance{Value: 25, DestinationId: "RET", Weight: 20},
}
at := &ActionTiming{
UserBalanceIds: []string{"*out:vdf:minu"},
actions: Actions{a},
}
at.Execute()
afterUb, _ := accountingStorage.GetUserBalance("*out:vdf:minu")
initialValue := initialUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()
afterValue := afterUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()
if initialValue != 50 || afterValue != 75 {
t.Error("Bad topup before and after: ", initialValue, afterValue)
}
}
/*********************************** Benchmarks *******************************/
func BenchmarkGetSecondForPrefix(b *testing.B) {