Files
cgrates/config/configscfg_test.go
2021-10-12 18:11:25 +03:00

175 lines
4.5 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 config
import (
"reflect"
"strings"
"testing"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
)
func TestConfigsloadFromJsonCfg(t *testing.T) {
jsonCfgs := &ConfigSCfgJson{
Enabled: utils.BoolPointer(true),
Url: utils.StringPointer("/randomURL/"),
Root_dir: utils.StringPointer("/randomPath/"),
}
expectedCfg := &ConfigSCfg{
Enabled: true,
URL: "/randomURL/",
RootDir: "/randomPath/",
}
cgrCfg := NewDefaultCGRConfig()
if err := cgrCfg.configSCfg.loadFromJSONCfg(jsonCfgs); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(cgrCfg.configSCfg, expectedCfg) {
t.Errorf("Expected %+v, received %+v", expectedCfg, cgrCfg.configSCfg)
}
jsonCfgs = nil
if err := cgrCfg.configSCfg.loadFromJSONCfg(jsonCfgs); err != nil {
t.Error(err)
}
}
func TestConfigsAsMapInterface(t *testing.T) {
cfgsJSONStr := `{
"configs": {
"enabled": true,
"url": "",
"root_dir": "/var/spool/cgrates/configs"
},
}`
eMap := map[string]interface{}{
utils.EnabledCfg: true,
utils.URLCfg: "",
utils.RootDirCfg: "/var/spool/cgrates/configs",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgsJSONStr); err != nil {
t.Error(err)
} else if rcv := cgrCfg.configSCfg.AsMapInterface(""); !reflect.DeepEqual(rcv, eMap) {
t.Errorf("Expected %+v, received %+v", eMap, rcv)
}
}
func TestConfigsAsMapInterface2(t *testing.T) {
cfgsJSONStr := `{
"configs":{}
}`
eMap := map[string]interface{}{
utils.EnabledCfg: false,
utils.URLCfg: "/configs/",
utils.RootDirCfg: "/var/spool/cgrates/configs",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgsJSONStr); err != nil {
t.Error(err)
} else if rcv := cgrCfg.configSCfg.AsMapInterface(""); !reflect.DeepEqual(rcv, eMap) {
t.Errorf("Expected %+v, received %+v", eMap, rcv)
}
}
func TestNewCGRConfigFromPathWithoutEnv(t *testing.T) {
cfgsJSONStr := `{
"general": {
"node_id": "*env:NODE_ID",
},
}`
cfg := NewDefaultCGRConfig()
if err = loadConfigFromReader(context.Background(), strings.NewReader(cfgsJSONStr), cfg.sections, true, cfg); err != nil {
t.Fatal(err)
}
exp := "*env:NODE_ID"
if cfg.GeneralCfg().NodeID != exp {
t.Errorf("Expected %+v, received %+v", exp, cfg.GeneralCfg().NodeID)
}
}
func TestConfigSCfgClone(t *testing.T) {
cS := &ConfigSCfg{
Enabled: true,
URL: "/randomURL/",
RootDir: "/randomPath/",
}
rcv := cS.Clone()
if !reflect.DeepEqual(cS, rcv) {
t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(cS), utils.ToJSON(rcv))
}
if rcv.URL = ""; cS.URL != "/randomURL/" {
t.Errorf("Expected clone to not modify the cloned")
}
}
func TestDiffConfigSCfgJson(t *testing.T) {
var d *ConfigSCfgJson
v1 := &ConfigSCfg{
Enabled: false,
URL: "localhost:8080",
RootDir: "/usr/share/cgrates",
}
v2 := &ConfigSCfg{
Enabled: true,
URL: "localhost:7777",
RootDir: "/another/dir",
}
expected := &ConfigSCfgJson{
Enabled: utils.BoolPointer(true),
Url: utils.StringPointer("localhost:7777"),
Root_dir: utils.StringPointer("/another/dir"),
}
rcv := diffConfigSCfgJson(d, v1, v2)
if !reflect.DeepEqual(rcv, expected) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv))
}
v2_2 := v1
expected2 := &ConfigSCfgJson{}
rcv = diffConfigSCfgJson(d, v1, v2_2)
if !reflect.DeepEqual(rcv, expected2) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected2), utils.ToJSON(rcv))
}
}
func TestConfigSCloneSection(t *testing.T) {
cfgS := &ConfigSCfg{
Enabled: false,
URL: "localhost:8080",
RootDir: "/usr/share/cgrates",
}
exp := &ConfigSCfg{
Enabled: false,
URL: "localhost:8080",
RootDir: "/usr/share/cgrates",
}
rcv := cfgS.CloneSection()
if !reflect.DeepEqual(rcv, exp) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(rcv))
}
}