mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add TPFilter in apier/v1 and test for it
This commit is contained in:
96
apier/v1/tpfilters.go
Normal file
96
apier/v1/tpfilters.go
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
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 Filter within a tariff plan
|
||||
func (self *ApierV1) SetTPFilter(attrs utils.TPFilter, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := self.StorDb.SetTPFilter([]*utils.TPFilter{&attrs}); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
}
|
||||
*reply = utils.OK
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrGetTPFilter struct {
|
||||
TPid string // Tariff plan id
|
||||
ID string // Filter id
|
||||
}
|
||||
|
||||
// Queries specific Filter on tariff plan
|
||||
func (self *ApierV1) GetTPFilter(attr AttrGetTPFilter, reply *utils.TPFilter) error {
|
||||
if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if filter, err := self.StorDb.GetTPFilter(attr.TPid, attr.ID); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
*reply = *filter[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrGetTPFilterIds struct {
|
||||
TPid string // Tariff plan id
|
||||
utils.Paginator
|
||||
}
|
||||
|
||||
// Queries Filter identities on specific tariff plan.
|
||||
func (self *ApierV1) GetTPFilterIds(attrs AttrGetTPFilterIds, 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.TBLTPFilters, 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
|
||||
}
|
||||
|
||||
type AttrRemTPFilter struct {
|
||||
TPid string // Tariff plan id
|
||||
Tenant string
|
||||
ID string // Filter id
|
||||
}
|
||||
|
||||
// Removes specific Filter on Tariff plan
|
||||
func (self *ApierV1) RemTPFilter(attrs AttrRemTPFilter, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := self.StorDb.RemTpData(utils.TBLTPFilters, attrs.TPid, map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
} else {
|
||||
*reply = utils.OK
|
||||
}
|
||||
return nil
|
||||
}
|
||||
215
apier/v1/tpfilters_it_test.go
Normal file
215
apier/v1/tpfilters_it_test.go
Normal file
@@ -0,0 +1,215 @@
|
||||
// +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 (
|
||||
tpFilterCfgPath string
|
||||
tpFilterCfg *config.CGRConfig
|
||||
tpFilterRPC *rpc.Client
|
||||
tpFilterDataDir = "/usr/share/cgrates"
|
||||
tpFilter *utils.TPFilter
|
||||
tpFilterDelay int
|
||||
tpFilterConfigDIR string //run tests for specific configuration
|
||||
)
|
||||
|
||||
var sTestsTPFilters = []func(t *testing.T){
|
||||
testTPFilterInitCfg,
|
||||
testTPFilterResetStorDb,
|
||||
testTPFilterStartEngine,
|
||||
testTPFilterRpcConn,
|
||||
ttestTPFilterGetTPFilterBeforeSet,
|
||||
testTPFilterSetTPFilter,
|
||||
testTPFilterGetTPFilterAfterSet,
|
||||
testTPFilterGetFilterIds,
|
||||
testTPFilterUpdateTPFilter,
|
||||
testTPFilterGetTPFilterAfterUpdate,
|
||||
testTPFilterRemTPFilter,
|
||||
testTPFilterGetTPFilterAfterRemove,
|
||||
testTPFilterKillEngine,
|
||||
}
|
||||
|
||||
//Test start here
|
||||
func TestTPFilterITMySql(t *testing.T) {
|
||||
tpFilterConfigDIR = "tutmysql"
|
||||
for _, stest := range sTestsTPFilters {
|
||||
t.Run(tpFilterConfigDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPFilterITMongo(t *testing.T) {
|
||||
tpFilterConfigDIR = "tutmongo"
|
||||
for _, stest := range sTestsTPFilters {
|
||||
t.Run(tpFilterConfigDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPFilterITPG(t *testing.T) {
|
||||
tpFilterConfigDIR = "tutpostgres"
|
||||
for _, stest := range sTestsTPFilters {
|
||||
t.Run(tpFilterConfigDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterInitCfg(t *testing.T) {
|
||||
var err error
|
||||
tpFilterCfgPath = path.Join(tpFilterDataDir, "conf", "samples", tpFilterConfigDIR)
|
||||
tpFilterCfg, err = config.NewCGRConfigFromFolder(tpFilterCfgPath)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpFilterCfg.DataFolderPath = tpFilterDataDir // Share DataFolderPath through config towards StoreDb for Flush()
|
||||
config.SetCgrConfig(tpFilterCfg)
|
||||
switch tpFilterConfigDIR {
|
||||
case "tutmongo": // Mongo needs more time to reset db, need to investigate
|
||||
tpFilterDelay = 2000
|
||||
default:
|
||||
tpFilterDelay = 1000
|
||||
}
|
||||
}
|
||||
|
||||
// Wipe out the cdr database
|
||||
func testTPFilterResetStorDb(t *testing.T) {
|
||||
if err := engine.InitStorDb(tpFilterCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start CGR Engine
|
||||
func testTPFilterStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(tpFilterCfgPath, tpFilterDelay); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func testTPFilterRpcConn(t *testing.T) {
|
||||
var err error
|
||||
tpFilterRPC, err = jsonrpc.Dial("tcp", tpFilterCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ttestTPFilterGetTPFilterBeforeSet(t *testing.T) {
|
||||
var reply *utils.TPFilter
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilter", &AttrGetTPFilter{TPid: "TP1", ID: "Filter"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterSetTPFilter(t *testing.T) {
|
||||
tpFilter = &utils.TPFilter{
|
||||
TPid: "TP1",
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Filter",
|
||||
FilterType: "*string",
|
||||
FilterFieldName: "Account",
|
||||
FilterFielValues: []string{"1001", "1002"},
|
||||
}
|
||||
|
||||
var result string
|
||||
if err := tpFilterRPC.Call("ApierV1.SetTPFilter", tpFilter, &result); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != utils.OK {
|
||||
t.Error("Unexpected reply returned", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterGetTPFilterAfterSet(t *testing.T) {
|
||||
var reply *utils.TPFilter
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilter", &AttrGetTPFilter{TPid: "TP1", ID: "Filter"}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(tpFilter, reply) {
|
||||
t.Errorf("Expecting : %+v, received: %+v", tpFilter, reply)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testTPFilterGetFilterIds(t *testing.T) {
|
||||
var result []string
|
||||
expectedTPID := []string{"Filter"}
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilterIds", &AttrGetTPFilterIds{TPid: "TP1"}, &result); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expectedTPID, result) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", expectedTPID, result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testTPFilterUpdateTPFilter(t *testing.T) {
|
||||
tpFilter = &utils.TPFilter{
|
||||
TPid: "TP1",
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Filter",
|
||||
FilterType: "*string_prefix",
|
||||
FilterFieldName: "Account",
|
||||
FilterFielValues: []string{"10", "20"},
|
||||
}
|
||||
|
||||
var result string
|
||||
if err := tpFilterRPC.Call("ApierV1.SetTPFilter", tpFilter, &result); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != utils.OK {
|
||||
t.Error("Unexpected reply returned", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterGetTPFilterAfterUpdate(t *testing.T) {
|
||||
var reply *utils.TPFilter
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilter", &AttrGetTPFilter{TPid: "TP1", ID: "Filter"}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(tpFilter, reply) {
|
||||
t.Errorf("Expecting : %+v, received: %+v", tpFilter, reply)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterRemTPFilter(t *testing.T) {
|
||||
var resp string
|
||||
if err := tpFilterRPC.Call("ApierV1.RemTPFilter", &AttrRemTPFilter{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"}, &resp); err != nil {
|
||||
t.Error(err)
|
||||
} else if resp != utils.OK {
|
||||
t.Error("Unexpected reply returned", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterGetTPFilterAfterRemove(t *testing.T) {
|
||||
var reply *utils.TPFilter
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilter", &AttrGetTPFilter{TPid: "TP1", ID: "Filter"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterKillEngine(t *testing.T) {
|
||||
if err := engine.KillEngine(tpFilterDelay); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ var sTestsTPLcrRules = []func(t *testing.T){
|
||||
testTPLcrRulesGetTPLcrRulesBeforeSet,
|
||||
testTPLcrRulesSetTPLcrRules,
|
||||
testTPLcrRulesGetTPLcrRulesAfterSet,
|
||||
testTPLcrRulesGetTPDestinationIds,
|
||||
testTPLcrRulesGetTPLcrRuleIds,
|
||||
testTPLcrRulesUpdateTPLcrRules,
|
||||
testTPLcrRulesGetTPLcrRulesAfterUpdate,
|
||||
testTPLcrRulesRemTPLcrRules,
|
||||
@@ -176,7 +176,7 @@ func testTPLcrRulesGetTPLcrRulesAfterSet(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func testTPLcrRulesGetTPDestinationIds(t *testing.T) {
|
||||
func testTPLcrRulesGetTPLcrRuleIds(t *testing.T) {
|
||||
var result []string
|
||||
expectedTPID := []string{"*out:cgrates.org:call:1001:*any"}
|
||||
if err := tpLcrRuleRPC.Call("ApierV1.GetTPLcrRuleIds", &AttrGetTPLcrIds{TPid: "TPLRC1"}, &result); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user