From f3f257eff11c2be6965ea7871c0e92f11e53094a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sat, 2 Apr 2016 22:53:47 +0300 Subject: [PATCH] balance destinationIDs exceptions --- data/tariffplans/testtp/AccountActions.csv | 1 + data/tariffplans/testtp/ActionPlans.csv | 1 + data/tariffplans/testtp/Actions.csv | 3 +- engine/account.go | 19 +++++++++---- general_tests/tp_it_test.go | 31 ++++++++++++++++++++ utils/map.go | 12 ++++++-- utils/map_test.go | 33 ++++++++++++++++++++++ 7 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 utils/map_test.go diff --git a/data/tariffplans/testtp/AccountActions.csv b/data/tariffplans/testtp/AccountActions.csv index fc5722aa3..917d8d0b9 100644 --- a/data/tariffplans/testtp/AccountActions.csv +++ b/data/tariffplans/testtp/AccountActions.csv @@ -8,3 +8,4 @@ cgrates.org,1009,TEST_EXE,,, cgrates.org,1010,TEST_DATA_r,,true, cgrates.org,1011,TEST_VOICE,,, cgrates.org,1012,PREPAID_10,,, +cgrates.org,1013,TEST_NEG,,, \ No newline at end of file diff --git a/data/tariffplans/testtp/ActionPlans.csv b/data/tariffplans/testtp/ActionPlans.csv index 45e11a89d..54731428a 100644 --- a/data/tariffplans/testtp/ActionPlans.csv +++ b/data/tariffplans/testtp/ActionPlans.csv @@ -4,3 +4,4 @@ PREPAID_10,BONUS_1,ASAP,10 TEST_EXE,TOPUP_EXE,ALWAYS,10 TEST_DATA_r,TOPUP_DATA_r,ASAP,10 TEST_VOICE,TOPUP_VOICE,ASAP,10 +TEST_NEG,TOPUP_NEG,ASAP,10 diff --git a/data/tariffplans/testtp/Actions.csv b/data/tariffplans/testtp/Actions.csv index e85a4781e..98a48be14 100644 --- a/data/tariffplans/testtp/Actions.csv +++ b/data/tariffplans/testtp/Actions.csv @@ -7,4 +7,5 @@ CDRST_LOG,*log,,,,,,,,,,,,,,false,false,10 TOPUP_EXE,*topup,,,,*monetary,*out,,*any,,,*unlimited,,5,10,false,false,10 TOPUP_DATA_r,*topup,,,,*monetary,*out,,DATA_DEST,,,*unlimited,,5000000,10,false,false,10 TOPUP_DATA_r,*topup,,,,*data,*out,,DATA_DEST,datar,,*unlimited,,50000000000,10,false,false,10 -TOPUP_VOICE,*topup,,,,*voice,*out,,GERMANY_MOBILE,,,*unlimited,,50000,10,false,false,10 \ No newline at end of file +TOPUP_VOICE,*topup,,,,*voice,*out,,GERMANY_MOBILE,,,*unlimited,,50000,10,false,false,10 +TOPUP_NEG,*topup,,,,*voice,*out,,GERMANY;!GERMANY_MOBILE,*zero1m,,*unlimited,,100,10,false,false,10 diff --git a/engine/account.go b/engine/account.go index b356a8ab3..ca02216d3 100644 --- a/engine/account.go +++ b/engine/account.go @@ -306,14 +306,21 @@ func (ub *Account) getBalancesForPrefix(prefix, category, direction, tor string, if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) for dId, _ := range destIds { - if b.DestinationIDs[dId.(string)] == true { - b.precision = len(p) - usefulBalances = append(usefulBalances, b) - break + includeDest, found := b.DestinationIDs[dId.(string)] + utils.Logger.Debug("DID: " + dId.(string)) + if found { + if includeDest { + b.precision = len(p) + usefulBalances = append(usefulBalances, b) + break + } else { // the balance had !, so now equals false => exclude balance + b.precision = 1 // fake to exit the outer loop + break + } } - if b.precision > 0 { + /*if b.precision > 0 { break - } + }*/ } } if b.precision > 0 { diff --git a/general_tests/tp_it_test.go b/general_tests/tp_it_test.go index cbea917f3..1ad58d833 100644 --- a/general_tests/tp_it_test.go +++ b/general_tests/tp_it_test.go @@ -187,3 +187,34 @@ func TestTpZeroCost(t *testing.T) { t.Errorf("Calling ApierV2.GetAccount received: %s", utils.ToIJSON(acnt)) } } + +func TestTpZeroNegativeCost(t *testing.T) { + if !*testIntegration { + return + } + tStart := time.Date(2016, 3, 31, 0, 0, 0, 0, time.UTC) + cd := engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "free", + Account: "1013", + Destination: "+4915", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tStart.Add(time.Duration(20) * time.Second), + } + var cc engine.CallCost + if err := tpRPC.Call("Responder.Debit", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.GetDuration() != 20*time.Second { + t.Errorf("Calling Responder.MaxDebit got callcost: %v", utils.ToIJSON(cc)) + } + var acnt *engine.Account + attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1013"} + if err := tpRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error("Got error on ApierV2.GetAccount: ", err.Error()) + } else if acnt.BalanceMap[utils.VOICE][0].Value != 100.0 { + t.Errorf("Calling ApierV2.GetAccount received: %s", utils.ToIJSON(acnt)) + } +} diff --git a/utils/map.go b/utils/map.go index 0bf197a7a..f9f63df76 100644 --- a/utils/map.go +++ b/utils/map.go @@ -67,7 +67,11 @@ func NewStringMap(s ...string) StringMap { for _, v := range s { v = strings.TrimSpace(v) if v != "" { - result[v] = true + if strings.HasPrefix(v, "!") { + result[v[1:]] = false + } else { + result[v] = true + } } } return result @@ -128,7 +132,11 @@ func StringMapFromSlice(s []string) StringMap { for _, v := range s { v = strings.TrimSpace(v) if v != "" { - result[v] = true + if strings.HasPrefix(v, "!") { + result[v[1:]] = false + } else { + result[v] = true + } } } return result diff --git a/utils/map_test.go b/utils/map_test.go new file mode 100644 index 000000000..a8d629492 --- /dev/null +++ b/utils/map_test.go @@ -0,0 +1,33 @@ +package utils + +import "testing" + +func TestStringMapParse(t *testing.T) { + sm := ParseStringMap("1;2;3;4") + if len(sm) != 4 { + t.Error("Error pasring map: ", sm) + } +} + +func TestStringMapParseNegative(t *testing.T) { + sm := ParseStringMap("1;2;!3;4") + if len(sm) != 4 { + t.Error("Error pasring map: ", sm) + } + if sm["3"] != false { + t.Error("Error parsing negative: ", sm) + } +} + +func TestStringMapCompare(t *testing.T) { + sm := ParseStringMap("1;2;!3;4") + if include, found := sm["2"]; include != true && found != true { + t.Error("Error detecting positive: ", sm) + } + if include, found := sm["3"]; include != false && found != true { + t.Error("Error detecting negative: ", sm) + } + if include, found := sm["5"]; include != false && found != false { + t.Error("Error detecting missing: ", sm) + } +}