mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
182 lines
5.7 KiB
Go
182 lines
5.7 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 Affero 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 Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"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 TestACExecuteAccountsSetBalance(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
internalChan := make(chan birpc.ClientConnector, 1)
|
|
connMngr := engine.NewConnManager(cfg)
|
|
connMngr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts), utils.AccountSv1, internalChan)
|
|
apAction := &utils.APAction{
|
|
ID: "TestACExecuteAccounts",
|
|
Type: utils.MetaSetBalance,
|
|
Diktats: []*utils.APDiktat{
|
|
{
|
|
Opts: map[string]any{
|
|
"*balancePath": "~*balance.TestBalance.Value",
|
|
"*balanceValue": "\"constant;`>;q=0.7;expires=3600constant\"",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
dataStorage := utils.MapStorage{
|
|
utils.MetaReq: map[string]any{
|
|
utils.AccountField: "1001",
|
|
},
|
|
utils.MetaOpts: map[string]any{
|
|
utils.Usage: 10 * time.Minute,
|
|
},
|
|
}
|
|
|
|
actCdrLG := &actSetBalance{
|
|
config: cfg,
|
|
connMgr: connMngr,
|
|
aCfg: apAction,
|
|
}
|
|
|
|
expected := "no connection with AccountS"
|
|
if err := actCdrLG.execute(context.Background(), dataStorage, utils.MetaBalanceLimit); err == nil || err.Error() != expected {
|
|
t.Errorf("Expected %+v, received %+v", expected, err)
|
|
}
|
|
|
|
actCdrLG.config.ActionSCfg().AccountSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts)}
|
|
expected = "Closed unspilit syntax"
|
|
if err := actCdrLG.execute(context.Background(), dataStorage, utils.MetaBalanceLimit); err == nil || err.Error() != expected {
|
|
t.Errorf("Expected %+v, received %+v", expected, err)
|
|
}
|
|
|
|
//invalid to parse a value from diktats
|
|
actCdrLG.aCfg.Diktats[0].Opts["*balanceValue"] = "10"
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
|
expected = context.DeadlineExceeded.Error()
|
|
if err := actCdrLG.execute(ctx, dataStorage, utils.MetaBalanceLimit); err == nil || err.Error() != expected {
|
|
t.Errorf("Expected %+v, received %+v", expected, err)
|
|
}
|
|
cancel()
|
|
}
|
|
|
|
func TestACExecuteAccountsRemBalance(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
internalChan := make(chan birpc.ClientConnector, 1)
|
|
connMngr := engine.NewConnManager(cfg)
|
|
connMngr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts), utils.AccountSv1, internalChan)
|
|
apAction := &utils.APAction{
|
|
ID: "TestACExecuteAccountsRemBalance",
|
|
Type: utils.MetaSetBalance,
|
|
Diktats: []*utils.APDiktat{
|
|
{
|
|
Opts: map[string]any{
|
|
"*balancePath": "~*balance.TestBalance.Value",
|
|
"*balanceValue": "10",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
idb, err := engine.NewInternalDB(nil, nil, nil, cfg.DbCfg().Items)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dbCM := engine.NewDBConnManager(map[string]engine.DataDB{utils.MetaDefault: idb}, cfg.DbCfg())
|
|
actRemBal := &actRemBalance{
|
|
config: cfg,
|
|
connMgr: connMngr,
|
|
fltrS: engine.NewFilterS(cfg, connMngr, engine.NewDataManager(dbCM, cfg, connMngr)),
|
|
aCfg: apAction,
|
|
tnt: "cgrates.org",
|
|
}
|
|
|
|
expected := "no connection with AccountS"
|
|
if err := actRemBal.execute(context.Background(), nil, utils.MetaRemBalance); err == nil || err.Error() != expected {
|
|
t.Errorf("Expected %+v, received %+v", expected, err)
|
|
}
|
|
|
|
actRemBal.config.ActionSCfg().AccountSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts)}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
|
expected = context.DeadlineExceeded.Error()
|
|
if err := actRemBal.execute(ctx, nil, utils.MetaRemBalance); err == nil || err.Error() != expected {
|
|
t.Errorf("Expected %+v, received %+v", expected, err)
|
|
}
|
|
cancel()
|
|
}
|
|
|
|
func TestACExecuteAccountsParseError(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.ActionSCfg().AccountSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts)}
|
|
internalChan := make(chan birpc.ClientConnector, 1)
|
|
connMngr := engine.NewConnManager(cfg)
|
|
connMngr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts), utils.AccountSv1, internalChan)
|
|
apAction := &utils.APAction{
|
|
ID: "TestACExecuteAccountsRemBalance",
|
|
Type: utils.MetaSetBalance,
|
|
Diktats: []*utils.APDiktat{
|
|
{
|
|
Opts: map[string]any{
|
|
"*balancePath": "~*balance.TestBalance.Value",
|
|
"*balanceValue": "~10",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
actsetBal := &actSetBalance{
|
|
config: cfg,
|
|
connMgr: connMngr,
|
|
aCfg: apAction,
|
|
tnt: "cgrates.org",
|
|
}
|
|
dataStorage := utils.MapStorage{}
|
|
|
|
if err := actsetBal.execute(nil, dataStorage, utils.MetaRemBalance); err == nil || err != utils.ErrNotFound {
|
|
t.Errorf("Expected %+v, received %+v", utils.ErrNotFound, err)
|
|
}
|
|
}
|
|
|
|
func TestACAccountsGetIDs(t *testing.T) {
|
|
apAction := &utils.APAction{
|
|
ID: "TestACExecuteAccountsRemBalance",
|
|
}
|
|
|
|
actRemBal := &actRemBalance{
|
|
aCfg: apAction,
|
|
}
|
|
if rcv := actRemBal.id(); rcv != apAction.ID {
|
|
t.Errorf("Expected %+v, received %+v", apAction.ID, rcv)
|
|
}
|
|
|
|
actSeTBal := &actSetBalance{
|
|
aCfg: apAction,
|
|
}
|
|
if rcv := actSeTBal.id(); rcv != apAction.ID {
|
|
t.Errorf("Expected %+v, received %+v", apAction.ID, rcv)
|
|
}
|
|
}
|