diff --git a/apis/routes_test.go b/apis/routes_test.go new file mode 100644 index 000000000..085037b84 --- /dev/null +++ b/apis/routes_test.go @@ -0,0 +1,462 @@ +/* +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 +*/ + +package apis + +import ( + "reflect" + "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) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + dataDB := engine.NewInternalDB(nil, nil, true) + 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 := &RouteWithAPIOpts{ + RouteProfile: &engine.RouteProfile{ + Tenant: "cgrates.org", + ID: "routeID", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Weight: 10, + }, + } + + 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.PaginatorWithTenant{}, + &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.GetRouteProfileCount(context.Background(), &utils.TenantWithAPIOpts{}, + &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) + } + + 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) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + dataDB := engine.NewInternalDB(nil, nil, true) + 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) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + adms := &AdminSv1{ + cfg: cfg, + dm: dm, + } + + rtPrf := &RouteWithAPIOpts{ + RouteProfile: &engine.RouteProfile{}, + } + + var reply string + 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: 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, map[string]chan birpc.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): 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) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + adms := &AdminSv1{ + cfg: cfg, + dm: dm, + } + + rtPrf := &RouteWithAPIOpts{ + RouteProfile: &engine.RouteProfile{ + ID: "TestRoutesRemoveRouteProfileCheckErrors", + Tenant: "cgrates.org", + Weight: 10, + }, + } + 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, map[string]chan birpc.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): 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 + }, + } + + 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) { + 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.PaginatorWithTenant{ + 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) { + 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.PaginatorWithTenant{ + Tenant: "cgrates.org", + }, &reply); err == nil || err != utils.ErrNotFound { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } + + dm.DataDB().Flush(utils.EmptyString) +} + +func TestRoutesGetRouteProfileCountErrMock(t *testing.T) { + 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.GetRouteProfileCount(context.Background(), + &utils.TenantWithAPIOpts{ + Tenant: "cgrates.org", + }, &reply); err == nil || err != utils.ErrNotImplemented { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotImplemented, err) + } +} + +func TestRoutesGetRouteProfileCountErrKeys(t *testing.T) { + 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.GetRouteProfileCount(context.Background(), + &utils.TenantWithAPIOpts{ + Tenant: "cgrates.org", + }, &reply); err == nil || err != utils.ErrNotFound { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } +} + +func TestRoutesNewRouteSv1(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + 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") + } +} diff --git a/engine/datadbmock.go b/engine/datadbmock.go index d08bef64b..a7af88a09 100644 --- a/engine/datadbmock.go +++ b/engine/datadbmock.go @@ -57,6 +57,9 @@ type DataDBMock struct { SetAccountDrvF func(ctx *context.Context, profile *utils.Account) error GetAccountDrvF func(ctx *context.Context, str1 string, str2 string) (*utils.Account, error) RemoveAccountDrvF func(ctx *context.Context, str1 string, str2 string) error + GetRouteProfileDrvF func(ctx *context.Context, tnt, id string) (*RouteProfile, error) + SetRouteProfileDrvF func(ctx *context.Context, rtPrf *RouteProfile) error + RemoveRouteProfileDrvF func(ctx *context.Context, tnt, id string) error } //Storage methods @@ -261,15 +264,24 @@ func (dbM *DataDBMock) RemoveFilterDrv(str1 string, str2 string) error { return utils.ErrNotImplemented } -func (dbM *DataDBMock) GetRouteProfileDrv(*context.Context, string, string) (*RouteProfile, error) { +func (dbM *DataDBMock) GetRouteProfileDrv(ctx *context.Context, tnt, id string) (*RouteProfile, error) { + if dbM.GetRouteProfileDrvF != nil { + return dbM.GetRouteProfileDrvF(ctx, tnt, id) + } return nil, utils.ErrNotImplemented } -func (dbM *DataDBMock) SetRouteProfileDrv(*context.Context, *RouteProfile) error { +func (dbM *DataDBMock) SetRouteProfileDrv(ctx *context.Context, rtPrf *RouteProfile) error { + if dbM.SetRouteProfileDrvF != nil { + return dbM.SetRouteProfileDrvF(ctx, rtPrf) + } return utils.ErrNotImplemented } -func (dbM *DataDBMock) RemoveRouteProfileDrv(*context.Context, string, string) error { +func (dbM *DataDBMock) RemoveRouteProfileDrv(ctx *context.Context, tnt, id string) error { + if dbM.RemoveRouteProfileDrvF != nil { + return dbM.RemoveRouteProfileDrvF(ctx, tnt, id) + } return utils.ErrNotImplemented }