/* 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 config import ( "reflect" "strings" "testing" "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/", } if cgrCfg, err := NewDefaultCGRConfig(); err != nil { t.Error(err) } else 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) } } 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, err := NewDefaultCGRConfig() if err != nil { t.Fatal(err) } if err = cfg.loadConfigFromReader(strings.NewReader(cfgsJSONStr), []func(*CgrJsonCfg) error{cfg.loadFromJSONCfg}, true); err != nil { t.Fatal(err) } exp := "*env:NODE_ID" if cfg.GeneralCfg().NodeID != exp { t.Errorf("Expected %+v, received %+v", exp, cfg.GeneralCfg().NodeID) } }