mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-13 11:06:25 +05:00
813 lines
22 KiB
Go
813 lines
22 KiB
Go
/*
|
|
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 apis
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/cgrates/birpc"
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/engine"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
func TestRoutesSetGetRemRouteProfile(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
arg := &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "routeID",
|
|
},
|
|
}
|
|
var result engine.RouteProfile
|
|
var reply string
|
|
|
|
rtPrf := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "routeID",
|
|
FilterIDs: []string{"*string:~*req.Account:1001"},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
Routes: []*engine.Route{{}},
|
|
},
|
|
}
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err != nil {
|
|
t.Error(err)
|
|
} else if reply != utils.OK {
|
|
t.Errorf("\nexpected: <%+v>, received: <%+v>", utils.OK, reply)
|
|
}
|
|
|
|
if err := adms.GetRouteProfile(context.Background(), arg, &result); err != nil {
|
|
t.Error(err)
|
|
} else if !reflect.DeepEqual(result, *rtPrf.RouteProfile) {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>",
|
|
utils.ToJSON(rtPrf.RouteProfile), utils.ToJSON(result))
|
|
}
|
|
|
|
var rtPrfIDs []string
|
|
exprtPrfIDs := []string{"routeID"}
|
|
|
|
if err := adms.GetRouteProfileIDs(context.Background(), &utils.ArgsItemIDs{},
|
|
&rtPrfIDs); err != nil {
|
|
t.Error(err)
|
|
} else if !reflect.DeepEqual(rtPrfIDs, exprtPrfIDs) {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exprtPrfIDs, rtPrfIDs)
|
|
}
|
|
|
|
var rplyCount int
|
|
|
|
if err := adms.GetRouteProfilesCount(context.Background(), &utils.ArgsItemIDs{},
|
|
&rplyCount); err != nil {
|
|
t.Error(err)
|
|
} else if rplyCount != len(rtPrfIDs) {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", len(rtPrfIDs), rplyCount)
|
|
}
|
|
|
|
if err := adms.RemoveRouteProfile(context.Background(), &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "routeID",
|
|
},
|
|
}, &reply); err != nil {
|
|
t.Error(err)
|
|
}
|
|
engine.Cache.Clear(nil)
|
|
|
|
if err := adms.GetRouteProfile(context.Background(), arg, &result); err == nil ||
|
|
err != utils.ErrNotFound {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfileCheckErrors(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
var rcv engine.RouteProfile
|
|
experr := "MANDATORY_IE_MISSING: [ID]"
|
|
|
|
if err := adms.GetRouteProfile(context.Background(), &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{},
|
|
}, &rcv); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
adms.dm = nil
|
|
experr = "SERVER_ERROR: NO_DATABASE_CONNECTION"
|
|
|
|
if err := adms.GetRouteProfile(context.Background(), &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "TestRoutesGetRouteProfileCheckErrors",
|
|
}}, &rcv); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesSetRouteProfileCheckErrors(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
rtPrf := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
ID: "ROUTE1",
|
|
},
|
|
}
|
|
|
|
var reply string
|
|
experr := "MANDATORY_IE_MISSING: [Routes]"
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
rtPrf = &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Routes: []*engine.Route{{}},
|
|
},
|
|
}
|
|
|
|
experr = "MANDATORY_IE_MISSING: [ID]"
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
rtPrf.ID = "TestRoutesSetRouteProfileCheckErrors"
|
|
rtPrf.FilterIDs = []string{"invalid_filter_format"}
|
|
experr = "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TestRoutesSetRouteProfileCheckErrors"
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
rtPrf.FilterIDs = []string{}
|
|
adms.connMgr = engine.NewConnManager(cfg)
|
|
adms.connMgr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches), utils.CacheSv1, make(chan birpc.ClientConnector))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
|
experr = "SERVER_ERROR: context deadline exceeded"
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaRemove
|
|
if err := adms.SetRouteProfile(ctx, rtPrf, &reply); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected <%+v>,\nreceived <%+v>", experr, err)
|
|
}
|
|
cancel()
|
|
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
rtPrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return rtPrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return nil, nil
|
|
},
|
|
}
|
|
|
|
adms.dm = engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
experr = "SERVER_ERROR: NOT_IMPLEMENTED"
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err == nil ||
|
|
err.Error() != experr {
|
|
t.Errorf("\nexpected <%+v>, \nreceived <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesRemoveRouteProfileCheckErrors(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
rtPrf := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
ID: "TestRoutesRemoveRouteProfileCheckErrors",
|
|
Tenant: "cgrates.org",
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
Routes: []*engine.Route{{}},
|
|
},
|
|
}
|
|
var reply string
|
|
|
|
if err := adms.SetRouteProfile(context.Background(), rtPrf, &reply); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
|
adms.cfg.GeneralCfg().DefaultCaching = "not_a_caching_type"
|
|
adms.connMgr = engine.NewConnManager(cfg)
|
|
adms.connMgr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches), utils.CacheSv1, make(chan birpc.ClientConnector))
|
|
experr := "SERVER_ERROR: context deadline exceeded"
|
|
|
|
if err := adms.RemoveRouteProfile(ctx, &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "TestRoutesRemoveRouteProfileCheckErrors",
|
|
},
|
|
}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
cancel()
|
|
|
|
adms.cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
var rcv engine.RouteProfile
|
|
|
|
if err := adms.GetRouteProfile(context.Background(), &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "TestRoutesRemoveRouteProfileCheckErrors",
|
|
},
|
|
}, &rcv); err == nil || err != utils.ErrNotFound {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
|
|
}
|
|
|
|
experr = "MANDATORY_IE_MISSING: [ID]"
|
|
|
|
if err := adms.RemoveRouteProfile(context.Background(),
|
|
&utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{}}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
adms.dm = nil
|
|
experr = "SERVER_ERROR: NO_DATABASE_CONNECTION"
|
|
|
|
if err := adms.RemoveRouteProfile(context.Background(), &utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "TestRoutesRemoveRouteProfileCheckErrors",
|
|
}}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
rtPrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return rtPrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return nil, nil
|
|
},
|
|
SetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) {
|
|
return nil
|
|
},
|
|
GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error) {
|
|
return map[string]utils.StringSet{}, nil
|
|
},
|
|
}
|
|
engine.Cache.Clear(nil)
|
|
|
|
adms.dm = engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
experr = "SERVER_ERROR: NOT_IMPLEMENTED"
|
|
|
|
if err := adms.RemoveRouteProfile(context.Background(),
|
|
&utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
ID: "TestRoutesRemoveRouteProfileCheckErrors",
|
|
}}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfileIDsErrMock(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
rtPrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return rtPrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply []string
|
|
experr := "NOT_IMPLEMENTED"
|
|
|
|
if err := adms.GetRouteProfileIDs(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfileIDsErrKeys(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return []string{}, nil
|
|
},
|
|
}
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply []string
|
|
|
|
if err := adms.GetRouteProfileIDs(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
}, &reply); err == nil || err != utils.ErrNotFound {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfilesCountErrMock(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
rtPrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return rtPrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
}
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply int
|
|
|
|
if err := adms.GetRouteProfilesCount(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
}, &reply); err == nil || err != utils.ErrNotImplemented {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotImplemented, err)
|
|
}
|
|
}
|
|
|
|
func TestRoutesGetRouteProfilesCountErrKeys(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return []string{}, nil
|
|
},
|
|
}
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply int
|
|
|
|
if err := adms.GetRouteProfilesCount(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
}, &reply); err == nil || err != utils.ErrNotFound {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
|
|
}
|
|
}
|
|
|
|
func TestRoutesNewRouteSv1(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
rS := engine.NewRouteService(dm, nil, cfg, nil)
|
|
|
|
exp := &RouteSv1{
|
|
rS: rS,
|
|
}
|
|
rcv := NewRouteSv1(rS)
|
|
|
|
if !reflect.DeepEqual(rcv, exp) {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exp, rcv)
|
|
}
|
|
}
|
|
|
|
func TestRoutesSv1Ping(t *testing.T) {
|
|
rS := new(RouteSv1)
|
|
var reply string
|
|
if err := rS.Ping(nil, nil, &reply); err != nil {
|
|
t.Error(err)
|
|
} else if reply != utils.Pong {
|
|
t.Errorf("Unexpected reply error")
|
|
}
|
|
}
|
|
|
|
func TestRoutesGetRouteProfilesOK(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
connMgr := engine.NewConnManager(cfg)
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, nil, connMgr)
|
|
admS := NewAdminSv1(cfg, dm, connMgr, nil)
|
|
args1 := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "test_ID1",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE1",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
APIOpts: nil,
|
|
}
|
|
|
|
var setReply string
|
|
if err := admS.SetRouteProfile(context.Background(), args1, &setReply); err != nil {
|
|
t.Error(err)
|
|
} else if setReply != "OK" {
|
|
t.Error("Unexpected reply returned:", setReply)
|
|
}
|
|
|
|
args2 := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "test_ID2",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE2",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
APIOpts: nil,
|
|
}
|
|
|
|
if err := admS.SetRouteProfile(context.Background(), args2, &setReply); err != nil {
|
|
t.Error(err)
|
|
} else if setReply != "OK" {
|
|
t.Error("Unexpected reply returned:", setReply)
|
|
}
|
|
|
|
// this profile will not match
|
|
args3 := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "test2_ID1",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE1",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
APIOpts: nil,
|
|
}
|
|
|
|
if err := admS.SetRouteProfile(context.Background(), args3, &setReply); err != nil {
|
|
t.Error(err)
|
|
} else if setReply != "OK" {
|
|
t.Error("Unexpected reply returned:", setReply)
|
|
}
|
|
|
|
argsGet := &utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
ItemsPrefix: "test_ID",
|
|
}
|
|
exp := []*engine.RouteProfile{
|
|
{
|
|
Tenant: "cgrates.org",
|
|
ID: "test_ID1",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE1",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Tenant: "cgrates.org",
|
|
ID: "test_ID2",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE2",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var getReply []*engine.RouteProfile
|
|
if err := admS.GetRouteProfiles(context.Background(), argsGet, &getReply); err != nil {
|
|
t.Error(err)
|
|
} else {
|
|
sort.Slice(getReply, func(i, j int) bool {
|
|
return getReply[i].ID < getReply[j].ID
|
|
})
|
|
if !reflect.DeepEqual(getReply, exp) {
|
|
t.Errorf("expected: <%+v>, \nreceived: <%+v>",
|
|
utils.ToJSON(exp), utils.ToJSON(getReply))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRoutesGetRouteProfilesGetIDsErr(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
connMgr := engine.NewConnManager(cfg)
|
|
dataDB := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
|
dm := engine.NewDataManager(dataDB, nil, connMgr)
|
|
admS := NewAdminSv1(cfg, dm, connMgr, nil)
|
|
args := &engine.RouteProfileWithAPIOpts{
|
|
RouteProfile: &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "test_ID1",
|
|
Sorting: utils.MetaWeight,
|
|
Routes: []*engine.Route{
|
|
{
|
|
ID: "ROUTE1",
|
|
},
|
|
},
|
|
Weights: utils.DynamicWeights{
|
|
{
|
|
Weight: 10,
|
|
},
|
|
},
|
|
},
|
|
APIOpts: nil,
|
|
}
|
|
|
|
var setReply string
|
|
if err := admS.SetRouteProfile(context.Background(), args, &setReply); err != nil {
|
|
t.Error(err)
|
|
} else if setReply != "OK" {
|
|
t.Error("Unexpected reply returned:", setReply)
|
|
}
|
|
|
|
argsGet := &utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
ItemsPrefix: "test_ID",
|
|
APIOpts: map[string]interface{}{
|
|
utils.PageLimitOpt: 2,
|
|
utils.PageOffsetOpt: 4,
|
|
utils.PageMaxItemsOpt: 5,
|
|
},
|
|
}
|
|
|
|
experr := `SERVER_ERROR: maximum number of items exceeded`
|
|
var getReply []*engine.RouteProfile
|
|
if err := admS.GetRouteProfiles(context.Background(), argsGet, &getReply); err == nil || err.Error() != experr {
|
|
t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
}
|
|
|
|
func TestRoutesGetRouteProfilesGetProfileErr(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return []string{"rpp_cgrates.org:TEST"}, nil
|
|
},
|
|
}
|
|
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply []*engine.RouteProfile
|
|
experr := "SERVER_ERROR: NOT_IMPLEMENTED"
|
|
|
|
if err := adms.GetRouteProfiles(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
ItemsPrefix: "TEST",
|
|
}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfileIDsGetOptsErr(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
routePrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return routePrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return []string{"rpp_cgrates.org:key1", "rpp_cgrates.org:key2", "rpp_cgrates.org:key3"}, nil
|
|
},
|
|
}
|
|
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply []string
|
|
experr := "cannot convert field<bool>: true to int"
|
|
|
|
if err := adms.GetRouteProfileIDs(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
APIOpts: map[string]interface{}{
|
|
utils.PageLimitOpt: true,
|
|
},
|
|
}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|
|
|
|
func TestRoutesGetRouteProfileIDsPaginateErr(t *testing.T) {
|
|
engine.Cache.Clear(nil)
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
|
|
dbMock := &engine.DataDBMock{
|
|
GetRouteProfileDrvF: func(*context.Context, string, string) (*engine.RouteProfile, error) {
|
|
routePrf := &engine.RouteProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "TEST",
|
|
}
|
|
return routePrf, nil
|
|
},
|
|
SetRouteProfileDrvF: func(*context.Context, *engine.RouteProfile) error {
|
|
return nil
|
|
},
|
|
RemoveRouteProfileDrvF: func(*context.Context, string, string) error {
|
|
return nil
|
|
},
|
|
GetKeysForPrefixF: func(c *context.Context, s string) ([]string, error) {
|
|
return []string{"rpp_cgrates.org:key1", "rpp_cgrates.org:key2", "rpp_cgrates.org:key3"}, nil
|
|
},
|
|
}
|
|
|
|
dm := engine.NewDataManager(dbMock, cfg.CacheCfg(), nil)
|
|
adms := &AdminSv1{
|
|
cfg: cfg,
|
|
dm: dm,
|
|
}
|
|
|
|
var reply []string
|
|
experr := `SERVER_ERROR: maximum number of items exceeded`
|
|
|
|
if err := adms.GetRouteProfileIDs(context.Background(),
|
|
&utils.ArgsItemIDs{
|
|
Tenant: "cgrates.org",
|
|
APIOpts: map[string]interface{}{
|
|
utils.PageLimitOpt: 2,
|
|
utils.PageOffsetOpt: 4,
|
|
utils.PageMaxItemsOpt: 5,
|
|
},
|
|
}, &reply); err == nil || err.Error() != experr {
|
|
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
|
}
|
|
|
|
dm.DataDB().Flush(utils.EmptyString)
|
|
}
|