From 94abb7d7d0906c7b965f44e10aec1f6ceac92794 Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Mon, 10 Jul 2023 16:56:07 +0200 Subject: [PATCH] Add coverage tests for utils --- config/config_json_test.go | 2 + config/config_test.go | 231 ++++++++++++++++++++++++++++++++++++ config/httpcfg_test.go | 30 +++++ config/kamagentcfg_test.go | 27 +++++ config/listencfg_test.go | 30 +++++ config/loadercgrcfg_test.go | 37 ++++++ config/resourcescfg_test.go | 67 +++++++++++ config/schedulercfg_test.go | 38 ++++++ 8 files changed, 462 insertions(+) diff --git a/config/config_json_test.go b/config/config_json_test.go index b23ecc770..4e622a833 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -1735,3 +1735,5 @@ func TestDfEventReaderCfg(t *testing.T) { t.Errorf("Expected: %+v, \nreceived: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg)) } } + + diff --git a/config/config_test.go b/config/config_test.go index b7abe1094..6ee0dcd62 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package config import ( + "encoding/json" "path" "reflect" "testing" @@ -1947,3 +1948,233 @@ func TestGeneralCfg(t *testing.T) { t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestConfigDBFields(t *testing.T) { + d := dbDefaults{} + str := "test" + + tests := []struct { + name string + rcv any + exp any + }{ + { + name: "dbName diff from dynamic", + rcv: d.dbName(str, str), + exp: str, + }, + { + name: "dbUser diff from dynamic", + rcv: d.dbUser(str, str), + exp: str, + }, + { + name: "dbHost diff from dynamic", + rcv: d.dbHost(str, str), + exp: str, + }, + { + name: "dbPass diff from dynamic", + rcv: d.dbPass(str, str), + exp: str, + }, + { + name: "isHidden true", + rcv: isHidden(".test"), + exp: true, + }, + { + name: "isHidden false", + rcv: isHidden("."), + exp: false, + }, + } + + for _, tt := range tests { + if tt.rcv != tt.exp { + t.Errorf("Expected: %+v\nReceived: %+v", tt.exp, tt.rcv) + } + } +} + +func TestConfigLoadErrors(t *testing.T) { + c := CGRConfig{} + js := json.RawMessage([]byte(`test`)) + + tests := []struct { + name string + rcv error + exp string + }{ + { + name: "load general cfg error check", + rcv: c.loadGeneralCfg(&CgrJsonCfg{GENERAL_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load cache cfg error check", + rcv: c.loadCacheCfg(&CgrJsonCfg{CACHE_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load listen cfg error check", + rcv: c.loadListenCfg(&CgrJsonCfg{LISTEN_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load HTTP cfg error check", + rcv: c.loadHTTPCfg(&CgrJsonCfg{HTTP_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load StorDB cfg error check", + rcv: c.loadStorDBCfg(&CgrJsonCfg{STORDB_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load FilterS cfg error check", + rcv: c.loadFilterSCfg(&CgrJsonCfg{FilterSjsn: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load RalS cfg error check", + rcv: c.loadRalSCfg(&CgrJsonCfg{RALS_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load Scheduler cfg error check", + rcv: c.loadSchedulerCfg(&CgrJsonCfg{SCHEDULER_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load drs cfg error check", + rcv: c.loadCdrsCfg(&CgrJsonCfg{CDRS_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load SessionS cfg error check", + rcv: c.loadSessionSCfg(&CgrJsonCfg{SessionSJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load KamAgent cfg error check", + rcv: c.loadKamAgentCfg(&CgrJsonCfg{KamailioAgentJSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load FreeswitchAgent cfg error check", + rcv: c.loadFreeswitchAgentCfg(&CgrJsonCfg{FreeSWITCHAgentJSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load AsteriskAgent cfg error check", + rcv: c.loadAsteriskAgentCfg(&CgrJsonCfg{AsteriskAgentJSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load DiameterAgent cfg error check", + rcv: c.loadDiameterAgentCfg(&CgrJsonCfg{DA_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load RadiusAgent cfg error check", + rcv: c.loadRadiusAgentCfg(&CgrJsonCfg{RA_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load DNSAgent cfg error check", + rcv: c.loadDNSAgentCfg(&CgrJsonCfg{DNSAgentJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load HttpAgent cfg error check", + rcv: c.loadHttpAgentCfg(&CgrJsonCfg{HttpAgentJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load AttributeS cfg error check", + rcv: c.loadAttributeSCfg(&CgrJsonCfg{ATTRIBUTE_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load ChargerS cfg error check", + rcv: c.loadChargerSCfg(&CgrJsonCfg{ChargerSCfgJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load ResourceS cfg error check", + rcv: c.loadResourceSCfg(&CgrJsonCfg{RESOURCES_JSON: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load StatS cfg error check", + rcv: c.loadStatSCfg(&CgrJsonCfg{STATS_JSON: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load ThresholdS cfg error check", + rcv: c.loadThresholdSCfg(&CgrJsonCfg{THRESHOLDS_JSON: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load SupplierS cfg error check", + rcv: c.loadSupplierSCfg(&CgrJsonCfg{SupplierSJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load Mailer cfg error check", + rcv: c.loadMailerCfg(&CgrJsonCfg{MAILER_JSN: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load SureTax cfg error check", + rcv: c.loadSureTaxCfg(&CgrJsonCfg{SURETAX_JSON: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load DispatcherS cfg error check", + rcv: c.loadDispatcherSCfg(&CgrJsonCfg{DispatcherSJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load MigratorCgr cfg error check", + rcv: c.loadMigratorCgrCfg(&CgrJsonCfg{CgrMigratorCfgJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load TlsCgr cfg error check", + rcv: c.loadTlsCgrCfg(&CgrJsonCfg{TlsCfgJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load AnalyzerCgr cfg error check", + rcv: c.loadAnalyzerCgrCfg(&CgrJsonCfg{AnalyzerCfgJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load Apier cfg error check", + rcv: c.loadApierCfg(&CgrJsonCfg{ApierS: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load Apier cfg error check", + rcv: c.loadApierCfg(&CgrJsonCfg{ApierS: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "load Ers cfg error check", + rcv: c.loadErsCfg(&CgrJsonCfg{ERsJson: &js}), + exp: "invalid character 'e' in literal true (expecting 'r')", + }, + } + + for _, tt := range tests { + if tt.rcv != nil { + if tt.rcv.Error() != tt.exp { + t.Errorf("Expected: %+v\nReceived: %+v", tt.exp, tt.rcv) + } + } else { + t.Error("was expecting an error") + } + } +} \ No newline at end of file diff --git a/config/httpcfg_test.go b/config/httpcfg_test.go index 35b7e3f84..7e5ec02c8 100644 --- a/config/httpcfg_test.go +++ b/config/httpcfg_test.go @@ -97,3 +97,33 @@ func TestHTTPCfgAsMapInterface(t *testing.T) { t.Errorf("Expected: %+v ,\n received: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestHTTPCfgAsMapInterface2(t *testing.T) { + str := "test" + bl := true + mp := map[string]string{"test1": "val1", "test": "val2"} + + httpcfg := HTTPCfg{ + HTTPJsonRPCURL: str, + HTTPWSURL: str, + HTTPFreeswitchCDRsURL: str, + HTTPCDRsURL: str, + HTTPUseBasicAuth: bl, + HTTPAuthUsers: mp, + } + + exp := map[string]any{ + utils.HTTPJsonRPCURLCfg: httpcfg.HTTPJsonRPCURL, + utils.HTTPWSURLCfg: httpcfg.HTTPWSURL, + utils.HTTPFreeswitchCDRsURLCfg: httpcfg.HTTPFreeswitchCDRsURL, + utils.HTTPCDRsURLCfg: httpcfg.HTTPCDRsURL, + utils.HTTPUseBasicAuthCfg: httpcfg.HTTPUseBasicAuth, + utils.HTTPAuthUsersCfg: map[string]any{"test1": "val1", "test": "val2"}, + } + + rcv := httpcfg.AsMapInterface() + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("Expected: %+v ,\n received: %+v", exp, rcv) + } +} \ No newline at end of file diff --git a/config/kamagentcfg_test.go b/config/kamagentcfg_test.go index e6d69b50f..6df0a4d95 100644 --- a/config/kamagentcfg_test.go +++ b/config/kamagentcfg_test.go @@ -151,3 +151,30 @@ func TestKamAgentCfgAsMapInterface(t *testing.T) { t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestKamAgentCfgloadFromJsonCfg2(t *testing.T) { + nm := 1 + str := "test" + self := KamConnCfg{} + + js := KamConnJsonCfg{ + Alias: &str, + Address: &str, + Reconnects: &nm, + } + + exp := KamConnCfg{ + Alias: str, + Address: str, + Reconnects: nm, + } + + err := self.loadFromJsonCfg(&js) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(self, exp) { + t.Errorf("\nExpected: %+v\nReceived: %+v", exp, self) + } +} diff --git a/config/listencfg_test.go b/config/listencfg_test.go index 067e41474..26b4a3926 100644 --- a/config/listencfg_test.go +++ b/config/listencfg_test.go @@ -20,6 +20,8 @@ package config import ( "reflect" "testing" + + "github.com/cgrates/cgrates/utils" ) func TestListenCfgloadFromJsonCfg(t *testing.T) { @@ -62,3 +64,31 @@ func TestListenCfgloadFromJsonCfg(t *testing.T) { t.Errorf("Expected: %+v , received: %+v", expected, lstcfg) } } + +func TestListenCfgAsMapInterface(t *testing.T) { + str := "test" + + lstcfg := ListenCfg{ + RPCJSONListen: str, + RPCGOBListen: str, + HTTPListen: str, + RPCJSONTLSListen: str, + RPCGOBTLSListen: str, + HTTPTLSListen: str, + } + + exp := map[string]any{ + utils.RPCJSONListenCfg: lstcfg.RPCJSONListen, + utils.RPCGOBListenCfg: lstcfg.RPCGOBListen, + utils.HTTPListenCfg: lstcfg.HTTPListen, + utils.RPCJSONTLSListenCfg: lstcfg.RPCJSONTLSListen, + utils.RPCGOBTLSListenCfg: lstcfg.RPCGOBTLSListen, + utils.HTTPTLSListenCfg: lstcfg.HTTPTLSListen, + } + + rcv := lstcfg.AsMapInterface() + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("Expected: %+v , received: %+v", exp, rcv) + } +} diff --git a/config/loadercgrcfg_test.go b/config/loadercgrcfg_test.go index ca504a0b1..6324669ff 100644 --- a/config/loadercgrcfg_test.go +++ b/config/loadercgrcfg_test.go @@ -106,3 +106,40 @@ func TestLoaderCgrCfgAsMapInterface(t *testing.T) { t.Errorf("Expected: %+v, Received: %+v, at field: %s", utils.ToJSON(eMap["scheduler_conns"]), utils.ToJSON(rcv["scheduler_conns"]), "scheduler_conns") } } + +func TestLoaderCgrCfgloadFromJsonCfg2(t *testing.T) { + str := "test" + bl := false + slc := []string{"val1", utils.MetaInternal} + ld := LoaderCgrCfg{} + + js := LoaderCfgJson{ + Tpid: &str, + Data_path: &str, + Disable_reverse: &bl, + Field_separator: &str, + Caches_conns: &slc, + Scheduler_conns: &slc, + } + + slc2 := []string{"val1", utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)} + slc3 := []string{"val1", utils.ConcatenatedKey(utils.MetaInternal, utils.MetaScheduler)} + + exp := LoaderCgrCfg{ + TpID: str, + DataPath: str, + DisableReverse: bl, + FieldSeparator: 't', + CachesConns: slc2, + SchedulerConns: slc3, + } + + err := ld.loadFromJsonCfg(&js) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(ld, exp) { + t.Errorf("Expected: %+v , received: %+v", exp, ld) + } +} diff --git a/config/resourcescfg_test.go b/config/resourcescfg_test.go index 16c71e71f..00201c36b 100644 --- a/config/resourcescfg_test.go +++ b/config/resourcescfg_test.go @@ -124,3 +124,70 @@ func TestResourceSConfigAsMapInterface(t *testing.T) { t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestResourcesCfgloadFromJson(t *testing.T) { + str := "err" + s := ResourceSConfig{} + + tests := []struct { + name string + arg *ResourceSJsonCfg + err string + }{ + { + name: "conns different from *internal", + arg: &ResourceSJsonCfg{Thresholds_conns: &[]string{str}}, + err: "", + }, + { + name: "conns different from *internal", + arg: &ResourceSJsonCfg{Store_interval: &str}, + err: `time: invalid duration "err"`, + }, + { + name: "conns different from *internal", + arg: &ResourceSJsonCfg{String_indexed_fields: &[]string{str}}, + err: "", + }, + } + + for _, tt := range tests { + err := s.loadFromJsonCfg(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Errorf("\nExpected: %+v\nReceived: %+v", tt.err, err) + } + } + } +} + +func TestResourcesCfgAsMapInterface(t *testing.T) { + slc := []string{"test"} + + rlcfg := ResourceSConfig{ + Enabled: true, + IndexedSelects: true, + ThresholdSConns: slc, + StoreInterval: 1 * time.Second, + StringIndexedFields: &slc, + PrefixIndexedFields: &slc, + NestedFields: true, + } + + exp := map[string]any{ + utils.EnabledCfg: rlcfg.Enabled, + utils.IndexedSelectsCfg: rlcfg.IndexedSelects, + utils.ThresholdSConnsCfg: slc, + utils.StoreIntervalCfg: "1s", + utils.StringIndexedFieldsCfg: slc, + utils.PrefixIndexedFieldsCfg: slc, + utils.NestedFieldsCfg: rlcfg.NestedFields, + } + + rcv := rlcfg.AsMapInterface() + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("\nExpected: %+v\nReceived: %+v", exp, rcv) + } +} diff --git a/config/schedulercfg_test.go b/config/schedulercfg_test.go index be000c734..c6b943cbc 100644 --- a/config/schedulercfg_test.go +++ b/config/schedulercfg_test.go @@ -81,3 +81,41 @@ func TestSchedulerCfgAsMapInterface(t *testing.T) { t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestSchedulerCfgloadFromJsonCfg2(t *testing.T) { + s := SchedulerCfg{} + + tests := []struct{ + name string + arg *SchedulerJsonCfg + err string + }{ + { + name: "cdrs conns diff from *internal", + arg: &SchedulerJsonCfg{Cdrs_conns: &[]string{"test"}}, + err: "", + }, + { + name: "cdrs conns equal to *internal", + arg: &SchedulerJsonCfg{Cdrs_conns: &[]string{"*internal"}}, + err: "", + }, + { + name: "filers with value", + arg: &SchedulerJsonCfg{Filters: &[]string{"test"}}, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := s.loadFromJsonCfg(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Errorf("\nExpected: %+v\nReceived: %+v", tt.err, err) + } + } + }) + } +}