Blockers + tests

This commit is contained in:
porosnicuadrian
2022-04-20 19:03:56 +03:00
committed by Dan Christian Bogos
parent 7b763b56dd
commit 5662444a10
5 changed files with 163 additions and 17 deletions

View File

@@ -818,7 +818,6 @@ func testStatsProcessEvent(t *testing.T) {
utils.AccountField: "1001",
},
APIOpts: map[string]interface{}{
utils.MetaUsage: 30 * time.Second,
utils.OptsStatsProfileIDs: []string{"SQ_3"},
utils.MetaUsage: 30 * time.Second,
},

View File

@@ -19,24 +19,10 @@
"db_name": "10",
},
"rals": {
"enabled": true,
},
"schedulers": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns": ["*localhost"],
"rals_conns": ["*internal"],
"rates_conns": ["*internal"],
"session_cost_retries": 0,
},
@@ -530,7 +516,6 @@
"admins": {
"enabled": true,
"scheduler_conns": ["*internal"],
},

View File

@@ -1170,6 +1170,7 @@ func TestCDRProcessRatesCostForEvent(t *testing.T) {
},
},
},
utils.MetaCost: utils.NewDecimal(int64(15*time.Second)/10, 0),
utils.MetaUsage: 15 * time.Second,
},
}

78
utils/blockers.go Normal file
View File

@@ -0,0 +1,78 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import (
"fmt"
"strconv"
"strings"
)
type Blockers []*DynamicBlocker
// NewBlockersFromString will build the Blockers that contains slices of FilterIDs and Blocker from a single value. This process is helped by separators
func NewBlockersFromString(value, blkSep, fltrSep string) (blkrs Blockers, err error) {
if len(value) == 0 {
return Blockers{{}}, nil
}
valSeparated := strings.Split(value, blkSep)
lenVals := len(valSeparated)
if lenVals%2 != 0 {
return nil, fmt.Errorf("invalid DynamicBlocker format for string <%s>", value)
}
for idx := 0; idx < lenVals; idx += 2 {
var fltrIDs []string
if len(valSeparated[idx]) != 0 {
fltrIDs = strings.Split(valSeparated[idx], fltrSep)
}
var blocker bool
if len(valSeparated[idx+1]) != 0 {
if blocker, err = strconv.ParseBool(valSeparated[idx+1]); err != nil {
return nil, fmt.Errorf("cannot convert bool with value: <%v> into Blocker", valSeparated[idx+1])
}
}
blkrs = append(blkrs, &DynamicBlocker{FilterIDs: fltrIDs, Blocker: blocker})
}
return
}
// String
func (blkrs Blockers) String(blkSep, fltrSep string) (value string) {
if len(blkrs) == 0 {
return
}
strBlockers := make([]string, len(blkrs))
for idx, val := range blkrs {
strBlockers[idx] = val.String(blkSep, fltrSep)
}
return strings.Join(strBlockers, blkSep)
}
type DynamicBlocker struct {
FilterIDs []string
Blocker bool
}
func (blckr DynamicBlocker) String(blkSep, fltrSep string) (out string) {
blocker := "false"
if blckr.Blocker == true {
blocker = "true"
}
return strings.Join(blckr.FilterIDs, fltrSep) + blkSep + blocker
}

83
utils/blockers_test.go Normal file
View File

@@ -0,0 +1,83 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import (
"reflect"
"testing"
)
func TestNewBlockersFromString(t *testing.T) {
blkrs := Blockers{
{
FilterIDs: []string{"*string:~*opts.*cost:0"},
Blocker: false,
},
{
FilterIDs: []string{"*suffix:~*req.Destination:+4432", "eq:~*opts.*usage:10s"},
Blocker: false,
},
{
FilterIDs: []string{"*notstring:~*req.RequestType:*prepaid"},
Blocker: true,
},
{
Blocker: false,
},
}
blkrsStr := "*string:~*opts.*cost:0;false;*suffix:~*req.Destination:+4432&eq:~*opts.*usage:10s;false;*notstring:~*req.RequestType:*prepaid;true;;false"
if rcv, err := NewBlockersFromString(blkrsStr, InfieldSep, ANDSep); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(rcv, blkrs) {
t.Errorf("Expected %v \n received %v", ToJSON(blkrs), ToJSON(rcv))
}
}
func TestNewBlockersFromString2(t *testing.T) {
blkrs := Blockers{
{
FilterIDs: []string{"*string:~*opts.*cost:0"},
Blocker: false,
},
{},
}
blkrsStr := "*string:~*opts.*cost:0;false;;"
if rcv, err := NewBlockersFromString(blkrsStr, InfieldSep, ANDSep); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(rcv, blkrs) {
t.Errorf("Expected %+v \n ,received %+v", ToJSON(blkrs), ToJSON(rcv))
}
}
func TestNewBlockersFromStringErrSeparator(t *testing.T) {
blkrsStr := "*string:~*opts.*cost:0;false;;;"
exp := "invalid DynamicBlocker format for string <*string:~*opts.*cost:0;false;;;>"
if _, err := NewBlockersFromString(blkrsStr, InfieldSep, ANDSep); err.Error() != exp {
t.Errorf("Expected %s \n received %s", exp, err.Error())
}
}
func TestNewBlockersFromStringFormatBool(t *testing.T) {
blkrsStr := "*string:~*opts.*cost:0;tttrrruuue"
exp := "cannot convert bool with value: <tttrrruuue> into Blocker"
if _, err := NewBlockersFromString(blkrsStr, InfieldSep, ANDSep); err.Error() != exp {
t.Errorf("Expected %s \n received %s", exp, err.Error())
}
}