Add tpthreshold.go in apier/v1 and test for it

This commit is contained in:
TeoV
2017-10-02 11:17:42 +03:00
parent 3ed3cde528
commit d3ea0e1bf8
4 changed files with 327 additions and 6 deletions

View File

@@ -0,0 +1,228 @@
// +build offline_tp
/*
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 v1
import (
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"net/rpc"
"net/rpc/jsonrpc"
"path"
"reflect"
"testing"
)
var (
tpThresholdCfgPath string
tpThresholdCfg *config.CGRConfig
tpThresholdRPC *rpc.Client
tpThresholdDataDir = "/usr/share/cgrates"
tpThreshold *utils.TPThreshold
tpThresholdDelay int
tpThresholdConfigDIR string //run tests for specific configuration
)
var sTestsTPThreshold = []func(t *testing.T){
testTPThreholdInitCfg,
testTPThreholdResetStorDb,
testTPThreholdStartEngine,
testTPThreholdRpcConn,
testTPThreholdGetTPThreholdBeforeSet,
testTPThreholdSetTPThrehold,
testTPThreholdGetTPThreholdAfterSet,
testTPThreholdGetTPThreholdIds,
testTPThreholdUpdateTPThrehold,
testTPThreholdGetTPThreholdAfterUpdate,
testTPThreholdRemTPThrehold,
testTPThreholdGetTPThreholdAfterRemove,
testTPThreholdKillEngine,
}
//Test start here
func TestTPThreholdITMySql(t *testing.T) {
tpThresholdConfigDIR = "tutmysql"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
}
}
func TestTPThreholdITMongo(t *testing.T) {
tpThresholdConfigDIR = "tutmongo"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
}
}
func TestTPThreholdITPG(t *testing.T) {
tpThresholdConfigDIR = "tutpostgres"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
}
}
func testTPThreholdInitCfg(t *testing.T) {
var err error
tpThresholdCfgPath = path.Join(tpThresholdDataDir, "conf", "samples", tpThresholdConfigDIR)
tpThresholdCfg, err = config.NewCGRConfigFromFolder(tpThresholdCfgPath)
if err != nil {
t.Error(err)
}
tpThresholdCfg.DataFolderPath = tpThresholdDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(tpThresholdCfg)
switch tpThresholdConfigDIR {
case "tutmongo": // Mongo needs more time to reset db
tpThresholdDelay = 2000
default:
tpThresholdDelay = 1000
}
}
// Wipe out the cdr database
func testTPThreholdResetStorDb(t *testing.T) {
if err := engine.InitStorDb(tpThresholdCfg); err != nil {
t.Fatal(err)
}
}
// Start CGR Engine
func testTPThreholdStartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(tpThresholdCfgPath, tpThresholdDelay); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func testTPThreholdRpcConn(t *testing.T) {
var err error
tpThresholdRPC, err = jsonrpc.Dial("tcp", tpThresholdCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}
}
func testTPThreholdGetTPThreholdBeforeSet(t *testing.T) {
var reply *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", AttrGetTPThreshold{TPid: "TH1", ID: "Threshold"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}
func testTPThreholdSetTPThrehold(t *testing.T) {
tpThreshold = &utils.TPThreshold{
TPid: "TH1",
Tenant: "cgrates.org",
ID: "Threhold",
Filters: []*utils.TPRequestFilter{
&utils.TPRequestFilter{
Type: "*string",
FieldName: "Account",
Values: []string{"1001", "1002"},
},
},
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: "2014-07-29T15:00:00Z",
ExpiryTime: "",
},
Recurrent: true,
MinSleep: "1s",
Blocker: true,
Weight: 10,
ActionIDs: []string{"Thresh1", "Thresh2"},
}
var result string
if err := tpThresholdRPC.Call("ApierV1.SetTPThreshold", tpThreshold, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Error("Unexpected reply returned", result)
}
}
func testTPThreholdGetTPThreholdAfterSet(t *testing.T) {
var respond *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", &AttrGetTPThreshold{TPid: tpThreshold.TPid, ID: tpThreshold.ID}, &respond); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(tpThreshold, respond) {
t.Errorf("Expecting: %+v, received: %+v", tpThreshold, respond)
}
}
func testTPThreholdGetTPThreholdIds(t *testing.T) {
var result []string
expectedTPID := []string{"Threhold"}
if err := tpThresholdRPC.Call("ApierV1.GetTPThresholdIDs", &AttrGetTPThresholdIds{TPid: tpThreshold.TPid}, &result); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(result, expectedTPID) {
t.Errorf("Expecting: %+v, received: %+v", result, expectedTPID)
}
}
func testTPThreholdUpdateTPThrehold(t *testing.T) {
var result string
tpThreshold.Filters = []*utils.TPRequestFilter{
&utils.TPRequestFilter{
Type: "*string",
FieldName: "Account",
Values: []string{"1001", "1002"},
},
&utils.TPRequestFilter{
Type: "*string_prefix",
FieldName: "Destination",
Values: []string{"10", "20"},
},
}
if err := tpThresholdRPC.Call("ApierV1.SetTPThreshold", tpThreshold, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Error("Unexpected reply returned", result)
}
}
func testTPThreholdGetTPThreholdAfterUpdate(t *testing.T) {
var respond *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", &AttrGetTPThreshold{TPid: tpThreshold.TPid, ID: tpThreshold.ID}, &respond); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(tpThreshold, respond) {
t.Errorf("Expecting: %+v, received: %+v", tpThreshold, respond)
}
}
func testTPThreholdRemTPThrehold(t *testing.T) {
var resp string
if err := tpThresholdRPC.Call("ApierV1.RemTPThreshold", &AttrGetTPThreshold{TPid: "TH1", ID: "Threhold"}, &resp); err != nil {
t.Error(err)
} else if resp != utils.OK {
t.Error("Unexpected reply returned", resp)
}
}
func testTPThreholdGetTPThreholdAfterRemove(t *testing.T) {
var reply *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", AttrGetTPThreshold{TPid: "TH1", ID: "Threshold"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}
func testTPThreholdKillEngine(t *testing.T) {
if err := engine.KillEngine(tpThresholdDelay); err != nil {
t.Error(err)
}
}

93
apier/v1/tpthresholds.go Normal file
View File

@@ -0,0 +1,93 @@
/*
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 v1
import (
"github.com/cgrates/cgrates/utils"
)
// Creates a new threshold within a tariff plan
func (self *ApierV1) SetTPThreshold(attr utils.TPThreshold, reply *string) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.SetTPThreshold([]*utils.TPThreshold{&attr}); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK
return nil
}
type AttrGetTPThreshold struct {
TPid string // Tariff plan id
Tenant string
ID string
}
// Queries specific Stat on Tariff plan
func (self *ApierV1) GetTPThreshold(attr AttrGetTPThreshold, reply *utils.TPThreshold) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if rls, err := self.StorDb.GetTPThreshold(attr.TPid, attr.ID); err != nil {
if err.Error() != utils.ErrNotFound.Error() {
err = utils.NewErrServerError(err)
}
return err
} else {
*reply = *rls[0]
}
return nil
}
type AttrGetTPThresholdIds struct {
TPid string // Tariff plan id
Tenant string
utils.Paginator
}
// Queries Threshold identities on specific tariff plan.
func (self *ApierV1) GetTPThresholdIDs(attrs AttrGetTPThresholdIds, reply *[]string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPThresholds, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
if err.Error() != utils.ErrNotFound.Error() {
err = utils.NewErrServerError(err)
}
return err
} else {
*reply = ids
}
return nil
}
// Removes specific Threshold on Tariff plan
func (self *ApierV1) RemTPThreshold(attrs AttrGetTPThreshold, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.RemTpData(utils.TBLTPThresholds, attrs.TPid, map[string]string{"id": attrs.ID}); err != nil {
return utils.NewErrServerError(err)
} else {
*reply = utils.OK
}
return nil
}

View File

@@ -2179,8 +2179,9 @@ func APItoModelTPThreshold(th *utils.TPThreshold) (mdls TpThresholdS) {
}
for i, fltr := range th.Filters {
mdl := &TpThreshold{
Tpid: th.TPid,
ID: th.ID,
Tpid: th.TPid,
Tenant: th.Tenant,
ID: th.ID,
}
if i == 0 {
mdl.Blocker = th.Blocker

View File

@@ -2091,10 +2091,9 @@ func testOnStorITCRUDThresholdProfile(t *testing.T) {
func testOnStorITCRUDThreshold(t *testing.T) {
res := &Threshold{
Tenant: "cgrates.org",
ID: "TH1",
LastExecuted: time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC).Local(),
WakeupTime: time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC).Local(),
Tenant: "cgrates.org",
ID: "TH1",
Snooze: time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC).Local(),
}
if _, rcvErr := onStor.GetThreshold("cgrates.org", "TH1", true, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
t.Error(rcvErr)