mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add test for multiple groups pricing +testsuite update
This commit is contained in:
committed by
Dan Christian Bogos
parent
7eeab045a3
commit
22d7ecf784
@@ -24,7 +24,6 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/birpc"
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
@@ -56,35 +55,17 @@ func TestEscapeCharacters(t *testing.T) {
|
||||
}
|
||||
|
||||
}`
|
||||
cfg, cfgPath, clean, err := initTestCfg(content)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer clean()
|
||||
|
||||
// Flush datadb and stordb.
|
||||
err = engine.InitDataDb(cfg)
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestEscapeCharacters",
|
||||
// Encoding: *encoding,
|
||||
ConfigJSON: content,
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = engine.InitStorDb(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Start the engine.
|
||||
_, err = engine.StopStartEngine(cfgPath, 1000)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.KillEngine(1000)
|
||||
|
||||
// Initialize a jsonrpc client.
|
||||
var client *birpc.Client
|
||||
client, err = newRPCClient(cfg.ListenCfg())
|
||||
if err != nil {
|
||||
t.Fatal("Could not connect to rater: ", err.Error())
|
||||
}
|
||||
defer shutdown()
|
||||
|
||||
/*
|
||||
When escape sequences are written manually, like \u0000 in the csv file, they are not interpreted as
|
||||
|
||||
@@ -81,7 +81,7 @@ func TestBalanceBlocker(t *testing.T) {
|
||||
|
||||
}`
|
||||
|
||||
client, _, shutdown, err := setupTest(t, "TestBalanceBlocker", utils.EmptyString, utils.EmptyString, content, map[string]string{
|
||||
tpFiles := map[string]string{
|
||||
utils.AccountActionsCsv: `#Tenant,Account,ActionPlanId,ActionTriggersId,AllowNegative,Disabled
|
||||
cgrates.org,1001,PACKAGE_1001,,,`,
|
||||
utils.ActionPlansCsv: `#Id,ActionsId,TimingId,Weight
|
||||
@@ -96,10 +96,17 @@ RT_ANY,0,0.6,60s,1s,0s`,
|
||||
RP_ANY,DR_ANY,*any,10`,
|
||||
utils.RatingProfilesCsv: `#Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject
|
||||
cgrates.org,call,1001,2014-01-14T00:00:00Z,RP_ANY,`,
|
||||
})
|
||||
}
|
||||
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestBalanceBlocker",
|
||||
// Encoding: *encoding,
|
||||
ConfigJSON: content,
|
||||
TpFiles: tpFiles,
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to do setup for test: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer shutdown()
|
||||
|
||||
@@ -35,9 +35,13 @@ import (
|
||||
var aSummaryBefore *engine.AccountSummary
|
||||
|
||||
func TestGetAccountCost(t *testing.T) {
|
||||
cfgPath := path.Join(*dataDir, "conf", "samples", "rerate_cdrs_mysql")
|
||||
tpPath := path.Join(*dataDir, "tariffplans", "reratecdrs")
|
||||
client, _, shutdown, err := setupTest(t, "TestRerateCDRs", cfgPath, tpPath, utils.EmptyString, nil)
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestGetAccountCost",
|
||||
// Encoding: *encoding,
|
||||
ConfigPath: path.Join(*dataDir, "conf", "samples", "rerate_cdrs_mysql"),
|
||||
TpPath: path.Join(*dataDir, "tariffplans", "reratecdrs"),
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -21,11 +21,14 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc"
|
||||
"github.com/cgrates/birpc/context"
|
||||
@@ -54,23 +57,31 @@ func newRPCClient(cfg *config.ListenCfg) (c *birpc.Client, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// setupTest prepares the testing environment. It takes optional file paths for
|
||||
// existing configuration files or tariff plans and a content string for generating a new configuration
|
||||
// if no path is provided. It also takes a map of CSV filenames to content strings for loading data.
|
||||
// Returns an RPC client to interact with the engine, the configuration, a shutdown function to close
|
||||
// the engine, and an error if any step of the initialization fails.
|
||||
//
|
||||
// If cfgPath is provided, it loads configuration from the specified file; otherwise, it creates a new
|
||||
// configuration with the content provided. If tpPath is given, it uses the path for CSV loading; if it's
|
||||
// empty but csvFiles is not, it creates a temporary directory with CSV files for loading into the service.
|
||||
func setupTest(t *testing.T, testName, cfgPath, tpPath, content string, csvFiles map[string]string,
|
||||
) (client *birpc.Client, cfg *config.CGRConfig, shutdownFunc func(), err error) {
|
||||
// TestEnvironment holds the setup parameters and configurations
|
||||
// required for running integration tests.
|
||||
type TestEnvironment struct {
|
||||
Name string // usually the name of the test
|
||||
ConfigPath string // file path to the main configuration file
|
||||
ConfigJSON string // contains the configuration JSON content if ConfigPath is missing
|
||||
TpPath string // specifies the path to the tariff plans
|
||||
TpFiles map[string]string // maps CSV filenames to their content for tariff plan loading
|
||||
LogBuffer io.Writer // captures the log output of the test environment
|
||||
// Encoding string // specifies the data encoding type (e.g., JSON, GOB)
|
||||
}
|
||||
|
||||
// Setup initializes the testing environment using the provided configuration. It loads the configuration
|
||||
// from a specified path or creates a new one if the path is not provided. The method starts the engine,
|
||||
// establishes an RPC client connection, and loads CSV data if provided. It returns an RPC client, the
|
||||
// configuration, a shutdown function, and any error encountered.
|
||||
func (env TestEnvironment) Setup(t *testing.T, engineDelay int,
|
||||
) (client *birpc.Client, cfg *config.CGRConfig, shutdownFunc context.CancelFunc, err error) {
|
||||
|
||||
switch {
|
||||
case cfgPath != "":
|
||||
cfg, err = config.NewCGRConfigFromPath(cfgPath)
|
||||
case env.ConfigPath != "":
|
||||
cfg, err = config.NewCGRConfigFromPath(env.ConfigPath)
|
||||
default:
|
||||
var clean func()
|
||||
cfg, cfgPath, clean, err = initTestCfg(content)
|
||||
cfg, env.ConfigPath, clean, err = initCfg(env.ConfigJSON)
|
||||
defer clean() // it is safe to defer clean func before error check
|
||||
}
|
||||
|
||||
@@ -82,44 +93,37 @@ func setupTest(t *testing.T, testName, cfgPath, tpPath, content string, csvFiles
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
_, err = engine.StopStartEngine(cfgPath, *waitRater)
|
||||
exec.Command("pkill", "cgr-engine").Run()
|
||||
time.Sleep(time.Duration(engineDelay) * time.Millisecond)
|
||||
|
||||
cancel, err := startEngine(cfg, env.ConfigPath, engineDelay, env.LogBuffer)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
shutdownFunc = func() {
|
||||
err := engine.KillEngine(*waitRater)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
|
||||
client, err = newRPCClient(cfg.ListenCfg())
|
||||
if err != nil {
|
||||
shutdownFunc()
|
||||
cancel()
|
||||
return nil, nil, nil, fmt.Errorf("could not connect to cgr-engine: %w", err)
|
||||
}
|
||||
|
||||
var customTpPath string
|
||||
if len(csvFiles) != 0 {
|
||||
customTpPath = fmt.Sprintf("/tmp/testTPs/%s", testName)
|
||||
if len(env.TpFiles) != 0 {
|
||||
customTpPath = fmt.Sprintf("/tmp/testTPs/%s", env.Name)
|
||||
}
|
||||
|
||||
if err := loadCSVs(client, tpPath, customTpPath, csvFiles); err != nil {
|
||||
shutdownFunc()
|
||||
if err := loadCSVs(client, env.TpPath, customTpPath, env.TpFiles); err != nil {
|
||||
cancel()
|
||||
return nil, nil, nil, fmt.Errorf("failed to load csvs: %w", err)
|
||||
}
|
||||
|
||||
return client, cfg, shutdownFunc, nil
|
||||
return client, cfg, cancel, nil
|
||||
}
|
||||
|
||||
// initTestCfg creates a new CGRConfig from the provided configuration content string.
|
||||
// It generates a temporary file path, writes the content to a configuration file,
|
||||
// and returns the created CGRConfig, the path to the configuration file,
|
||||
// a cleanup function to remove the temporary configuration file,
|
||||
// and an error if the content is empty or an issue occurs during file creation or
|
||||
// configuration initialization.
|
||||
func initTestCfg(cfgContent string) (cfg *config.CGRConfig, cfgPath string, cleanFunc func(), err error) {
|
||||
// initCfg creates a new CGRConfig from the provided configuration content string. It generates a
|
||||
// temporary directory and file path, writes the content to the configuration file, and returns the
|
||||
// created CGRConfig, the file path, a cleanup function, and any error encountered.
|
||||
func initCfg(cfgContent string) (cfg *config.CGRConfig, cfgPath string, cleanFunc func(), err error) {
|
||||
if cfgContent == utils.EmptyString {
|
||||
return nil, "", func() {}, errors.New("content should not be empty")
|
||||
}
|
||||
@@ -144,11 +148,8 @@ func initTestCfg(cfgContent string) (cfg *config.CGRConfig, cfgPath string, clea
|
||||
return cfg, cfgPath, removeFunc, nil
|
||||
}
|
||||
|
||||
// loadCSVs loads tariff plan data from specified CSV files by calling the 'APIerSv1.LoadTariffPlanFromFolder' method using
|
||||
// the client parameter.
|
||||
// It handles the creation of a custom temporary path if provided and ensures the data from the given CSV files
|
||||
// is written and loaded as well. If no custom path is provided, it will load CSVs from the tpPath if it is not empty.
|
||||
// Returns an error if directory creation, file writing, or data loading fails.
|
||||
// loadCSVs loads tariff plan data from CSV files into the service. It handles directory creation and file writing for custom
|
||||
// paths, and loads data from the specified paths using the provided RPC client. Returns an error if any step fails.
|
||||
func loadCSVs(client *birpc.Client, tpPath, customTpPath string, csvFiles map[string]string) (err error) {
|
||||
paths := make([]string, 0, 2)
|
||||
if customTpPath != "" {
|
||||
@@ -206,3 +207,34 @@ func flushDBs(cfg *config.CGRConfig, flushDataDB, flushStorDB bool) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// startEngine starts the CGR engine process with the provided configuration. It writes engine logs to the provided logBuffer
|
||||
// (if any) and waits for the engine to be ready. Returns a cancel function to stop the engine and any error encountered.
|
||||
func startEngine(cfg *config.CGRConfig, cfgPath string, waitEngine int, logBuffer io.Writer) (context.CancelFunc, error) {
|
||||
enginePath, err := exec.LookPath("cgr-engine")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
engine := exec.CommandContext(ctx, enginePath, "-config_path", cfgPath)
|
||||
if logBuffer != nil {
|
||||
engine.Stdout = logBuffer
|
||||
engine.Stderr = logBuffer
|
||||
}
|
||||
if err := engine.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fib := utils.FibDuration(time.Millisecond, 0)
|
||||
for i := 0; i < 20; i++ {
|
||||
time.Sleep(fib())
|
||||
_, err = jsonrpc.Dial(utils.TCP, cfg.ListenCfg().RPCJSONListen)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("starting cgr-engine on port %s failed: %w", cfg.ListenCfg().RPCJSONListen, err)
|
||||
}
|
||||
time.Sleep(time.Duration(waitEngine) * time.Millisecond)
|
||||
return cancel, nil
|
||||
}
|
||||
|
||||
@@ -46,9 +46,13 @@ func TestRerateCDRs(t *testing.T) {
|
||||
default:
|
||||
t.Fatal("Unknown Database type")
|
||||
}
|
||||
cfgPath := path.Join(*dataDir, "conf", "samples", cfgDir)
|
||||
tpPath := path.Join(*dataDir, "tariffplans", "reratecdrs")
|
||||
client, _, shutdown, err := setupTest(t, "TestRerateCDRs", cfgPath, tpPath, utils.EmptyString, nil)
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestRerateCDRs",
|
||||
// Encoding: *encoding,
|
||||
ConfigPath: path.Join(*dataDir, "conf", "samples", cfgDir),
|
||||
TpPath: path.Join(*dataDir, "tariffplans", "reratecdrs"),
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
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 general_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestRatingSubjectSetDefault(t *testing.T) {
|
||||
switch *dbType {
|
||||
case utils.MetaInternal:
|
||||
case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres:
|
||||
t.SkipNow()
|
||||
default:
|
||||
t.Fatal("unsupported dbtype value")
|
||||
}
|
||||
|
||||
content := `{
|
||||
|
||||
"data_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"stor_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"rals": {
|
||||
"enabled": true,
|
||||
"balance_rating_subject":{
|
||||
"*data":"",
|
||||
}
|
||||
},
|
||||
|
||||
"cdrs": {
|
||||
"enabled": true,
|
||||
"rals_conns": ["*localhost"]
|
||||
},
|
||||
|
||||
"schedulers": {
|
||||
"enabled": true
|
||||
},
|
||||
|
||||
"apiers": {
|
||||
"enabled": true,
|
||||
"scheduler_conns": ["*internal"]
|
||||
}
|
||||
|
||||
}`
|
||||
|
||||
client, _, shutdown, err := setupTest(t, "TestBalanceBlocker", utils.EmptyString, utils.EmptyString, content, map[string]string{
|
||||
utils.AccountActionsCsv: `#Tenant,Account,ActionPlanId,ActionTriggersId,AllowNegative,Disabled
|
||||
cgrates.org,1001,PACKAGE_1001,,,`,
|
||||
utils.ActionPlansCsv: `#Id,ActionsId,TimingId,Weight
|
||||
PACKAGE_1001,ACT_TOPUP_DATA,*asap,10
|
||||
PACKAGE_1001,ACT_TOPUP_MON,*asap,10
|
||||
`,
|
||||
utils.ActionsCsv: `#ActionsId[0],Action[1],ExtraParameters[2],Filter[3],BalanceId[4],BalanceType[5],Categories[6],DestinationIds[7],RatingSubject[8],SharedGroup[9],ExpiryTime[10],TimingIds[11],Units[12],BalanceWeight[13],BalanceBlocker[14],BalanceDisabled[15],Weight[16]
|
||||
ACT_TOPUP_DATA,*topup_reset,,,data1,*data,,*any,,,*unlimited,,102400,10,false,false,10
|
||||
ACT_TOPUP_MON,*topup_reset,,,money1,*monetary,,*any,,,*unlimited,,250,10,false,false,10
|
||||
`,
|
||||
utils.DestinationRatesCsv: `#Id,DestinationId,RatesTag,RoundingMethod,RoundingDecimals,MaxCost,MaxCostStrategy
|
||||
DR_DATA,*any,RT_DATA,*up,0,0,`,
|
||||
utils.RatesCsv: `#Id,ConnectFee,Rate,RateUnit,RateIncrement,GroupIntervalStart
|
||||
RT_DATA,0,1,1024,1024,0`,
|
||||
utils.RatingPlansCsv: `#Id,DestinationRatesId,TimingTag,Weight
|
||||
RP_DATA,DR_DATA,*any,10`,
|
||||
utils.RatingProfilesCsv: `#Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject
|
||||
cgrates.org,data,1001,2022-01-14T00:00:00Z,RP_DATA,`,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to do setup for test: %v", err)
|
||||
}
|
||||
|
||||
defer shutdown()
|
||||
|
||||
t.Run("CheckInitialBalance", func(t *testing.T) {
|
||||
var acnt engine.Account
|
||||
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
|
||||
if err := client.Call(context.Background(), utils.APIerSv2GetAccount, attrs, &acnt); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(acnt.BalanceMap) != 2 {
|
||||
t.Fatalf("expected account to have only one balance of type *data, received %v", utils.ToJSON(acnt))
|
||||
} else if balanceD := acnt.BalanceMap[utils.MetaData][0]; balanceD.ID != "data1" || balanceD.Value != 102400 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceD)
|
||||
} else if balanceM := acnt.BalanceMap[utils.MetaMonetary][0]; balanceM.ID != "money1" || balanceM.Value != 250 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceM)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProcessCDR", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
if err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "data",
|
||||
utils.ToR: utils.MetaData,
|
||||
utils.OriginID: "processCDR",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaPrepaid,
|
||||
utils.AccountField: "1001",
|
||||
utils.Destination: "1002",
|
||||
utils.SetupTime: time.Date(2022, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2022, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 10000,
|
||||
},
|
||||
},
|
||||
}, &reply); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if ev := reply[0].Event; ev[utils.Cost] != 10.0 {
|
||||
t.Fatalf("Expected Cost to be 5,received %v", ev[utils.Cost])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckFinalBalance", func(t *testing.T) {
|
||||
var acnt engine.Account
|
||||
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
|
||||
if err := client.Call(context.Background(), utils.APIerSv2GetAccount, attrs, &acnt); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(acnt.BalanceMap) != 2 {
|
||||
t.Fatalf("expected account to have 2 balances *monetary and *data, received %v", acnt)
|
||||
} else if balanceM := acnt.BalanceMap[utils.MetaMonetary][0]; balanceM.ID != "money1" || balanceM.Value != 240.0 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceM)
|
||||
} else if balanceD := acnt.BalanceMap[utils.MetaData][0]; balanceD.ID != "data1" || balanceD.Value != 92160 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceD)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func TestRatingSubjectSet(t *testing.T) {
|
||||
|
||||
}`
|
||||
|
||||
client, _, shutdown, err := setupTest(t, "TestBalanceBlocker", utils.EmptyString, utils.EmptyString, content, map[string]string{
|
||||
tpFiles := map[string]string{
|
||||
utils.AccountActionsCsv: `#Tenant,Account,ActionPlanId,ActionTriggersId,AllowNegative,Disabled
|
||||
cgrates.org,1001,PACKAGE_1001,,,`,
|
||||
utils.ActionPlansCsv: `#Id,ActionsId,TimingId,Weight
|
||||
@@ -83,10 +83,17 @@ RT_DATA,0,1,1024,1024,0`,
|
||||
RP_DATA,DR_DATA,*any,10`,
|
||||
utils.RatingProfilesCsv: `#Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject
|
||||
cgrates.org,data,RPF_DATA,2022-01-14T00:00:00Z,RP_DATA,`,
|
||||
})
|
||||
}
|
||||
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestRatingSubjectSet",
|
||||
// Encoding: *encoding,
|
||||
ConfigJSON: content,
|
||||
TpFiles: tpFiles,
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to do setup for test: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer shutdown()
|
||||
@@ -148,3 +155,138 @@ cgrates.org,data,RPF_DATA,2022-01-14T00:00:00Z,RP_DATA,`,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRatingSubjectSetDefault(t *testing.T) {
|
||||
switch *dbType {
|
||||
case utils.MetaInternal:
|
||||
case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres:
|
||||
t.SkipNow()
|
||||
default:
|
||||
t.Fatal("unsupported dbtype value")
|
||||
}
|
||||
|
||||
content := `{
|
||||
|
||||
"data_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"stor_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"rals": {
|
||||
"enabled": true,
|
||||
"balance_rating_subject":{
|
||||
"*data":"",
|
||||
}
|
||||
},
|
||||
|
||||
"cdrs": {
|
||||
"enabled": true,
|
||||
"rals_conns": ["*localhost"]
|
||||
},
|
||||
|
||||
"schedulers": {
|
||||
"enabled": true
|
||||
},
|
||||
|
||||
"apiers": {
|
||||
"enabled": true,
|
||||
"scheduler_conns": ["*internal"]
|
||||
}
|
||||
|
||||
}`
|
||||
|
||||
tpFiles := map[string]string{
|
||||
utils.AccountActionsCsv: `#Tenant,Account,ActionPlanId,ActionTriggersId,AllowNegative,Disabled
|
||||
cgrates.org,1001,PACKAGE_1001,,,`,
|
||||
utils.ActionPlansCsv: `#Id,ActionsId,TimingId,Weight
|
||||
PACKAGE_1001,ACT_TOPUP_DATA,*asap,10
|
||||
PACKAGE_1001,ACT_TOPUP_MON,*asap,10
|
||||
`,
|
||||
utils.ActionsCsv: `#ActionsId[0],Action[1],ExtraParameters[2],Filter[3],BalanceId[4],BalanceType[5],Categories[6],DestinationIds[7],RatingSubject[8],SharedGroup[9],ExpiryTime[10],TimingIds[11],Units[12],BalanceWeight[13],BalanceBlocker[14],BalanceDisabled[15],Weight[16]
|
||||
ACT_TOPUP_DATA,*topup_reset,,,data1,*data,,*any,,,*unlimited,,102400,10,false,false,10
|
||||
ACT_TOPUP_MON,*topup_reset,,,money1,*monetary,,*any,,,*unlimited,,250,10,false,false,10
|
||||
`,
|
||||
utils.DestinationRatesCsv: `#Id,DestinationId,RatesTag,RoundingMethod,RoundingDecimals,MaxCost,MaxCostStrategy
|
||||
DR_DATA,*any,RT_DATA,*up,0,0,`,
|
||||
utils.RatesCsv: `#Id,ConnectFee,Rate,RateUnit,RateIncrement,GroupIntervalStart
|
||||
RT_DATA,0,1,1024,1024,0`,
|
||||
utils.RatingPlansCsv: `#Id,DestinationRatesId,TimingTag,Weight
|
||||
RP_DATA,DR_DATA,*any,10`,
|
||||
utils.RatingProfilesCsv: `#Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject
|
||||
cgrates.org,data,1001,2022-01-14T00:00:00Z,RP_DATA,`,
|
||||
}
|
||||
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestRatingSubjectSetDefault",
|
||||
// Encoding: *encoding,
|
||||
ConfigJSON: content,
|
||||
TpFiles: tpFiles,
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer shutdown()
|
||||
|
||||
t.Run("CheckInitialBalance", func(t *testing.T) {
|
||||
var acnt engine.Account
|
||||
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
|
||||
if err := client.Call(context.Background(), utils.APIerSv2GetAccount, attrs, &acnt); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(acnt.BalanceMap) != 2 {
|
||||
t.Fatalf("expected account to have only one balance of type *data, received %v", utils.ToJSON(acnt))
|
||||
} else if balanceD := acnt.BalanceMap[utils.MetaData][0]; balanceD.ID != "data1" || balanceD.Value != 102400 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceD)
|
||||
} else if balanceM := acnt.BalanceMap[utils.MetaMonetary][0]; balanceM.ID != "money1" || balanceM.Value != 250 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceM)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProcessCDR", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
if err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "data",
|
||||
utils.ToR: utils.MetaData,
|
||||
utils.OriginID: "processCDR",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaPrepaid,
|
||||
utils.AccountField: "1001",
|
||||
utils.Destination: "1002",
|
||||
utils.SetupTime: time.Date(2022, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2022, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 10000,
|
||||
},
|
||||
},
|
||||
}, &reply); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if ev := reply[0].Event; ev[utils.Cost] != 10.0 {
|
||||
t.Fatalf("Expected Cost to be 5,received %v", ev[utils.Cost])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckFinalBalance", func(t *testing.T) {
|
||||
var acnt engine.Account
|
||||
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
|
||||
if err := client.Call(context.Background(), utils.APIerSv2GetAccount, attrs, &acnt); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(acnt.BalanceMap) != 2 {
|
||||
t.Fatalf("expected account to have 2 balances *monetary and *data, received %v", acnt)
|
||||
} else if balanceM := acnt.BalanceMap[utils.MetaMonetary][0]; balanceM.ID != "money1" || balanceM.Value != 240.0 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceM)
|
||||
} else if balanceD := acnt.BalanceMap[utils.MetaData][0]; balanceD.ID != "data1" || balanceD.Value != 92160 {
|
||||
t.Fatalf("received account with unexpected balance: %v", balanceD)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
289
general_tests/shared_subject_it_test.go
Normal file
289
general_tests/shared_subject_it_test.go
Normal file
@@ -0,0 +1,289 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
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 general_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
// TestSharedSubject is based on a request posted on the CGRateS google group: https://groups.google.com/g/cgrates/c/3iqrh4CJnow.
|
||||
//
|
||||
// The main idea of the test is to handle a scenario where there are two groups sharing one subject:
|
||||
//
|
||||
// Group 1: 1001,1002,1003,1004,1005
|
||||
// Group 2: 1001,1010,1011,1012,1013
|
||||
//
|
||||
// The goal is to handle two different pricings based on the following rules:
|
||||
// - excluding 1001, any number from Group 1 calling any number from Group 2 will use a pricing of 2 units/s.
|
||||
// - excluding 1001, any number from Group 2 calling any number from Group 1 will use a pricing of 1 unit/s.
|
||||
// - if 1001 calls a number from Group 1 or is called by a number from Group 1, a pricing of 1 unit/s will be used.
|
||||
// - if 1001 calls a number from Group 2 or is called by a number from Group 2, a pricing of 2 units/s will be used.
|
||||
//
|
||||
// To accomplish this, the test does the following setup:
|
||||
// - creates one rating profile with pricing for each group
|
||||
// - attaches the rating profile via changing the event subject using an attribute profile
|
||||
// - sets up needed filters in order to select the correct pricing for the situation.
|
||||
func TestSharedSubject(t *testing.T) {
|
||||
switch *dbType {
|
||||
case utils.MetaInternal:
|
||||
case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres:
|
||||
t.SkipNow()
|
||||
default:
|
||||
t.Fatal("unsupported dbtype value")
|
||||
}
|
||||
|
||||
content := `{
|
||||
|
||||
"data_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"stor_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
|
||||
"attributes":{
|
||||
"enabled": true,
|
||||
},
|
||||
|
||||
"rals": {
|
||||
"enabled": true,
|
||||
},
|
||||
|
||||
"cdrs": {
|
||||
"enabled": true,
|
||||
"attributes_conns": ["*internal"],
|
||||
"rals_conns": ["*internal"]
|
||||
},
|
||||
|
||||
"schedulers": {
|
||||
"enabled": true
|
||||
},
|
||||
|
||||
"apiers": {
|
||||
"enabled": true,
|
||||
"scheduler_conns": ["*internal"]
|
||||
}
|
||||
|
||||
}`
|
||||
|
||||
tpFiles := map[string]string{
|
||||
utils.AttributesCsv: `#Tenant,ID,Contexts,FilterIDs,ActivationInterval,AttributeFilterIDs,Path,Type,Value,Blocker,Weight
|
||||
cgrates.org,ATTR_SET_SUBJECT,*any,,,,,,,false,20
|
||||
cgrates.org,ATTR_SET_SUBJECT,,,,FLTR_SameGroup1,*req.Subject,*constant,Subject1,,
|
||||
cgrates.org,ATTR_SET_SUBJECT,,,,FLTR_DiffGroup1,*req.Subject,*constant,Subject1,,
|
||||
cgrates.org,ATTR_SET_SUBJECT,,,,FLTR_SameGroup2,*req.Subject,*constant,Subject2,,
|
||||
cgrates.org,ATTR_SET_SUBJECT,,,,FLTR_DiffGroup2,*req.Subject,*constant,Subject2,,`,
|
||||
utils.DestinationRatesCsv: `#Id,DestinationId,RatesTag,RoundingMethod,RoundingDecimals,MaxCost,MaxCostStrategy
|
||||
DR_Subject1,*any,RT_Subject1,*up,20,0,
|
||||
DR_Subject2,*any,RT_Subject2,*up,20,0,`,
|
||||
utils.FiltersCsv: `#Tenant[0],ID[1],Type[2],Path[3],Values[4],ActivationInterval[5]
|
||||
cgrates.org,FLTR_SameGroup1,*string,~*req.Subject,1001;1002;1003;1004;1005,
|
||||
cgrates.org,FLTR_SameGroup1,*string,~*req.Destination,1001;1002;1003;1004;1005,
|
||||
cgrates.org,FLTR_SameGroup2,*string,~*req.Subject,1001;1010;1011;1012;1013,
|
||||
cgrates.org,FLTR_SameGroup2,*string,~*req.Destination,1001;1010;1011;1012;1013,
|
||||
cgrates.org,FLTR_DiffGroup1,*string,~*req.Subject,1010;1011;1012;1013,
|
||||
cgrates.org,FLTR_DiffGroup1,*string,~*req.Destination,1002;1003;1004;1005,
|
||||
cgrates.org,FLTR_DiffGroup2,*string,~*req.Subject,1002;1003;1004;1005,
|
||||
cgrates.org,FLTR_DiffGroup2,*string,~*req.Destination,1010;1011;1012;1013,
|
||||
#cgrates.org,FLTR_SET_SUBJECT,*prefix,~*req.Subject,10,`,
|
||||
utils.RatesCsv: `#Id,ConnectFee,Rate,RateUnit,RateIncrement,GroupIntervalStart
|
||||
RT_Subject1,0,1,1s,1s,0s
|
||||
RT_Subject2,0,2,1s,1s,0s`,
|
||||
utils.RatingPlansCsv: `#Id,DestinationRatesId,TimingTag,Weight
|
||||
RP_Subject1,DR_Subject1,*any,10
|
||||
RP_Subject2,DR_Subject2,*any,10`,
|
||||
utils.RatingProfilesCsv: `#Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject
|
||||
cgrates.org,call,Subject1,2014-01-14T00:00:00Z,RP_Subject1,
|
||||
cgrates.org,call,Subject2,2014-01-14T00:00:00Z,RP_Subject2,`,
|
||||
}
|
||||
|
||||
testEnv := TestEnvironment{
|
||||
Name: "TestSharedSubject",
|
||||
// Encoding: *encoding,
|
||||
ConfigJSON: content,
|
||||
TpFiles: tpFiles,
|
||||
}
|
||||
client, _, shutdown, err := testEnv.Setup(t, *waitRater)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer shutdown()
|
||||
|
||||
t.Run("Cost1001->1002", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "call",
|
||||
utils.ToR: utils.MetaVoice,
|
||||
utils.OriginID: "processCDR1",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaRated,
|
||||
utils.AccountField: "1001",
|
||||
utils.Subject: "1001",
|
||||
utils.Destination: "1002",
|
||||
utils.SetupTime: time.Date(2021, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2021, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 2 * time.Minute,
|
||||
},
|
||||
},
|
||||
}, &reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rcvCost, ok := reply[0].Event[utils.Cost].(float64)
|
||||
if !ok {
|
||||
t.Fatal("failed to cast received cost into a float64")
|
||||
}
|
||||
if rcvCost != 120 {
|
||||
t.Errorf("expected cost to be %v, received %v", 120, rcvCost)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Cost1002->1001", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "call",
|
||||
utils.ToR: utils.MetaVoice,
|
||||
utils.OriginID: "processCDR2",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaRated,
|
||||
utils.AccountField: "1002",
|
||||
utils.Subject: "1002",
|
||||
utils.Destination: "1001",
|
||||
utils.SetupTime: time.Date(2021, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2021, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 2 * time.Minute,
|
||||
},
|
||||
},
|
||||
}, &reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rcvCost, ok := reply[0].Event[utils.Cost].(float64)
|
||||
if !ok {
|
||||
t.Fatal("failed to cast received cost into a float64")
|
||||
}
|
||||
if rcvCost != 120 {
|
||||
t.Errorf("expected cost to be %v, received %v", 120, rcvCost)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Cost1001->1010", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "call",
|
||||
utils.ToR: utils.MetaVoice,
|
||||
utils.OriginID: "processCDR3",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaRated,
|
||||
utils.AccountField: "1001",
|
||||
utils.Subject: "1001",
|
||||
utils.Destination: "1010",
|
||||
utils.SetupTime: time.Date(2021, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2021, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 2 * time.Minute,
|
||||
},
|
||||
},
|
||||
}, &reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(reply) != 1 {
|
||||
t.Fatal("expected exactly one event in the reply slice")
|
||||
}
|
||||
rcvCost, ok := reply[0].Event[utils.Cost].(float64)
|
||||
if !ok {
|
||||
t.Fatal("failed to cast received cost into a float64")
|
||||
}
|
||||
if rcvCost != 240 {
|
||||
t.Errorf("expected cost to be %v, received %v", 240, rcvCost)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Cost1010->1001", func(t *testing.T) {
|
||||
var reply []*utils.EventWithFlags
|
||||
err := client.Call(context.Background(), utils.CDRsV2ProcessEvent,
|
||||
&engine.ArgV1ProcessEvent{
|
||||
Flags: []string{utils.MetaRALs},
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.RunID: "run_1",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.Category: "call",
|
||||
utils.ToR: utils.MetaVoice,
|
||||
utils.OriginID: "processCDR4",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.RequestType: utils.MetaRated,
|
||||
utils.AccountField: "1010",
|
||||
utils.Subject: "1010",
|
||||
utils.Destination: "1001",
|
||||
utils.SetupTime: time.Date(2021, time.February, 2, 16, 14, 50, 0, time.UTC),
|
||||
utils.AnswerTime: time.Date(2021, time.February, 2, 16, 15, 0, 0, time.UTC),
|
||||
utils.Usage: 2 * time.Minute,
|
||||
},
|
||||
},
|
||||
}, &reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(reply) != 1 {
|
||||
t.Fatal("expected exactly one event in the reply slice")
|
||||
}
|
||||
rcvCost, ok := reply[0].Event[utils.Cost].(float64)
|
||||
if !ok {
|
||||
t.Fatal("failed to cast received cost into a float64")
|
||||
}
|
||||
if rcvCost != 240 {
|
||||
t.Errorf("expected cost to be %v, received %v", 240, rcvCost)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user