Removed error from AsMapInterface function's signature

This commit is contained in:
porosnicuadrian
2020-12-02 15:33:15 +02:00
committed by Dan Christian Bogos
parent 1085569409
commit d220ab88b9
7 changed files with 85 additions and 77 deletions

View File

@@ -1484,17 +1484,13 @@ func (cfg *CGRConfig) reloadSections(sections ...string) {
}
// AsMapInterface returns the config as a map[string]interface{}
func (cfg *CGRConfig) AsMapInterface(separator string) (mp map[string]interface{}, err error) {
var datadb map[string]interface{}
if datadb, err = cfg.dataDbCfg.AsMapInterface(); err != nil {
return
}
func (cfg *CGRConfig) AsMapInterface(separator string) (mp map[string]interface{}) {
return map[string]interface{}{
LoaderJson: cfg.loaderCfg.AsMapInterface(separator),
HttpAgentJson: cfg.httpAgentCfg.AsMapInterface(separator),
RPCConnsJsonName: cfg.rpcConns.AsMapInterface(),
GENERAL_JSN: cfg.generalCfg.AsMapInterface(),
DATADB_JSN: datadb,
DATADB_JSN: cfg.dataDbCfg.AsMapInterface(),
STORDB_JSN: cfg.storDbCfg.AsMapInterface(),
TlsCfgJson: cfg.tlsCfg.AsMapInterface(),
CACHE_JSN: cfg.cacheCfg.AsMapInterface(),
@@ -1533,7 +1529,7 @@ func (cfg *CGRConfig) AsMapInterface(separator string) (mp map[string]interface{
TemplatesJson: cfg.templates.AsMapInterface(separator),
ConfigSJson: cfg.configSCfg.AsMapInterface(),
CoreSCfgJson: cfg.coreSCfg.AsMapInterface(),
}, nil
}
}
// ReloadArgs the API params for V1ReloadConfig
@@ -1590,13 +1586,13 @@ func (cfg *CGRConfig) V1GetConfig(args *SectionWithOpts, reply *map[string]inter
var mp interface{}
switch args.Section {
case utils.EmptyString:
*reply, err = cfg.AsMapInterface(cfg.GeneralCfg().RSRSep)
*reply = cfg.AsMapInterface(cfg.GeneralCfg().RSRSep)
return
case GENERAL_JSN:
mp = cfg.GeneralCfg().AsMapInterface()
case DATADB_JSN:
var datadb map[string]interface{}
if datadb, err = cfg.DataDbCfg().AsMapInterface(); err != nil {
if datadb = cfg.DataDbCfg().AsMapInterface(); err != nil {
return
}
mp = datadb
@@ -1739,17 +1735,13 @@ func (cfg *CGRConfig) V1GetConfigAsJSON(args *SectionWithOpts, reply *string) (e
var mp interface{}
switch args.Section {
case utils.EmptyString:
if mp, err = cfg.AsMapInterface(cfg.GeneralCfg().RSRSep); err != nil {
return
}
mp = cfg.AsMapInterface(cfg.GeneralCfg().RSRSep)
*reply = utils.ToJSON(mp)
return
case GENERAL_JSN:
mp = cfg.GeneralCfg().AsMapInterface()
case DATADB_JSN:
if mp, err = cfg.DataDbCfg().AsMapInterface(); err != nil {
return
}
mp = cfg.DataDbCfg().AsMapInterface()
case STORDB_JSN:
mp = cfg.StorDbCfg().AsMapInterface()
case TlsCfgJson:

View File

@@ -1054,10 +1054,8 @@ func testHttpHandlerConfigSForFolder(t *testing.T) {
if err != nil {
t.Error(err)
}
mp, err := cfg.AsMapInterface(cfg.generalCfg.RSRSep)
if err != nil {
t.Fatal(err)
}
mp := cfg.AsMapInterface(cfg.generalCfg.RSRSep)
str := utils.ToJSON(mp)
// we compare the length of the string because flags is a map and we receive it in different order
if len(str) != len(string(body)) {

View File

@@ -3752,13 +3752,6 @@ func TestV1GetConfigDataDB(t *testing.T) {
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(reply))
}
}
expectedErr := "strconv.Atoi: parsing \"6579s\": invalid syntax"
cfgCgr = NewDefaultCGRConfig()
cfgCgr.dataDbCfg.DataDbPort = "6579s"
if err := cfgCgr.V1GetConfig(&SectionWithOpts{Section: DATADB_JSN}, &reply); err == nil || err.Error() != expectedErr {
t.Error(err)
}
}
func TestV1GetConfigStorDB(t *testing.T) {
@@ -4864,16 +4857,6 @@ func TestV1GetConfigAsJSONDataDB(t *testing.T) {
}
}
func TestV1GetConfigAsJSONDataDBError(t *testing.T) {
var reply string
expectedErr := "strconv.Atoi: parsing \"6579s\": invalid syntax"
cfgCgr := NewDefaultCGRConfig()
cfgCgr.dataDbCfg.DataDbPort = "6579s"
if err := cfgCgr.V1GetConfigAsJSON(&SectionWithOpts{Section: DATADB_JSN}, &reply); err == nil || err.Error() != expectedErr {
t.Error(err)
}
}
func TestV1GetConfigAsJSONStorDB(t *testing.T) {
var reply string
expected := `{"stor_db":{"db_host":"127.0.0.1","db_name":"cgrates","db_password":"","db_port":3306,"db_type":"*mysql","db_user":"cgrates","items":{"*cdrs":{"remote":false,"replicate":false},"*session_costs":{"remote":false,"replicate":false},"*tp_account_actions":{"remote":false,"replicate":false},"*tp_action_plans":{"remote":false,"replicate":false},"*tp_action_triggers":{"remote":false,"replicate":false},"*tp_actions":{"remote":false,"replicate":false},"*tp_attributes":{"remote":false,"replicate":false},"*tp_chargers":{"remote":false,"replicate":false},"*tp_destination_rates":{"remote":false,"replicate":false},"*tp_destinations":{"remote":false,"replicate":false},"*tp_dispatcher_hosts":{"remote":false,"replicate":false},"*tp_dispatcher_profiles":{"remote":false,"replicate":false},"*tp_filters":{"remote":false,"replicate":false},"*tp_rate_profiles":{"remote":false,"replicate":false},"*tp_rates":{"remote":false,"replicate":false},"*tp_rating_plans":{"remote":false,"replicate":false},"*tp_rating_profiles":{"remote":false,"replicate":false},"*tp_resources":{"remote":false,"replicate":false},"*tp_routes":{"remote":false,"replicate":false},"*tp_shared_groups":{"remote":false,"replicate":false},"*tp_stats":{"remote":false,"replicate":false},"*tp_thresholds":{"remote":false,"replicate":false},"*tp_timings":{"remote":false,"replicate":false},"*versions":{"remote":false,"replicate":false}},"opts":{"conn_max_lifetime":0,"max_idle_conns":10,"max_open_conns":100,"query_timeout":"10s","sslmode":"disable"},"prefix_indexed_fields":[],"remote_conns":null,"replication_conns":null,"string_indexed_fields":[]}}`
@@ -5391,16 +5374,6 @@ func TestV1GetConfigAsJSONAllConfig(t *testing.T) {
}
}
func TestV1GetConfigAsJSONAllConfigError(t *testing.T) {
var reply string
expected := "strconv.Atoi: parsing \"6379s\": invalid syntax"
cgrCfg := NewDefaultCGRConfig()
cgrCfg.dataDbCfg.DataDbPort = "6379s"
if err := cgrCfg.V1GetConfigAsJSON(&SectionWithOpts{Section: utils.EmptyString}, &reply); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}
}
func TestV1ReloadConfigFromJSONEmptyConfig(t *testing.T) {
var reply string
cgrCfg := NewDefaultCGRConfig()

View File

@@ -84,14 +84,9 @@ func handleConfigSFolder(path string, w http.ResponseWriter) {
fmt.Fprintf(w, err.Error())
return
}
mp, err := cfg.AsMapInterface(cfg.generalCfg.RSRSep)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, err.Error())
return
}
// convert the config into a json and send it
if _, err := w.Write([]byte(utils.ToJSON(mp))); err != nil {
if _, err := w.Write([]byte(utils.ToJSON(cfg.AsMapInterface(cfg.generalCfg.RSRSep)))); err != nil {
utils.Logger.Warning(fmt.Sprintf("<%s> Failed to write resonse because: %s",
utils.ConfigSv1, err))
}

View File

@@ -140,7 +140,7 @@ func (dbcfg *DataDbCfg) Clone() (cln *DataDbCfg) {
}
// AsMapInterface returns the config as a map[string]interface{}
func (dbcfg *DataDbCfg) AsMapInterface() (initialMP map[string]interface{}, err error) {
func (dbcfg *DataDbCfg) AsMapInterface() (initialMP map[string]interface{}) {
initialMP = map[string]interface{}{
utils.DataDbTypeCfg: utils.Meta + dbcfg.DataDbType,
utils.DataDbHostCfg: dbcfg.DataDbHost,
@@ -159,11 +159,7 @@ func (dbcfg *DataDbCfg) AsMapInterface() (initialMP map[string]interface{}, err
initialMP[utils.ItemsCfg] = items
}
if dbcfg.DataDbPort != "" {
var dbPort int
if dbPort, err = strconv.Atoi(dbcfg.DataDbPort); err != nil {
return nil, err
}
initialMP[utils.DataDbPortCfg] = dbPort
initialMP[utils.DataDbPortCfg], _ = strconv.Atoi(dbcfg.DataDbPort)
}
return
}

View File

@@ -508,7 +508,7 @@ func TestDataDbCfgAsMapInterface(t *testing.T) {
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
t.Error(err)
} else {
rcv, err := cgrCfg.dataDbCfg.AsMapInterface()
rcv := cgrCfg.dataDbCfg.AsMapInterface()
if err != nil {
t.Error(err)
}
@@ -524,23 +524,6 @@ func TestDataDbCfgAsMapInterface(t *testing.T) {
}
}
func TestDataDBPortStrconvAtoi(t *testing.T) {
cfgJSONSTR := `{
"data_db": {
"db_port": 6079,
}
}`
if cfgJSON, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONSTR); err != nil {
t.Error(err)
} else {
cfgJSON.dataDbCfg.DataDbPort = "6o79"
expected := "strconv.Atoi: parsing \"6o79\": invalid syntax"
if _, err := cfgJSON.dataDbCfg.AsMapInterface(); err == nil || err.Error() != expected {
t.Errorf("Expected %+q \n,received %+q", expected, err)
}
}
}
func TestCloneDataDB(t *testing.T) {
jsonCfg := &DbJsonCfg{
Db_type: utils.StringPointer("redis"),

View File

@@ -0,0 +1,71 @@
/*
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 utils
import (
"testing"
"time"
)
var (
stirShakenTests = []func(t *testing.T){
testGetReaderFromPathGetError,
testGetReaderFromPathStatusCode,
testNewECDSAPrvKey,
testNewECDSAPublicKey,
}
)
func TestStirShakenUtils(t *testing.T) {
for _, test := range stirShakenTests {
t.Run("StirShakenUtils", test)
}
}
func testGetReaderFromPathGetError(t *testing.T) {
urlPath := "https://www.example.org/cert.cer"
expErr := "Get \"https://www.example.org/cert.cer\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)"
if _, err := GetReaderFromPath(urlPath, time.Duration(10)); err == nil || err.Error() != expErr {
t.Errorf("Expected %+v, received %+v", expErr, err)
}
}
func testGetReaderFromPathStatusCode(t *testing.T) {
urlPath := "https://www.example.org/cert.cer"
expErr := "http status error: 404"
if _, err := GetReaderFromPath(urlPath, time.Duration(0)); err == nil || err.Error() != expErr {
t.Errorf("Expected %+v, received %+v", expErr, err)
}
}
func testNewECDSAPrvKey(t *testing.T) {
urlPath := "https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm"
expPrvKey := "Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key"
if _, err := NewECDSAPrvKey(urlPath, time.Duration(0)); err == nil || err.Error() != expPrvKey {
t.Errorf("Expected %+v, received %+v", expPrvKey, err)
}
}
func testNewECDSAPublicKey(t *testing.T) {
urlPath := "https://en.wikipedia.org/wiki/Wiki"
expPublKey := "Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key"
if _, err := NewECDSAPubKey(urlPath, 0); err == nil || err.Error() != expPublKey {
t.Errorf("Expected %+v, received %+v", expPublKey, err)
}
}