From 530ad1e541d1f2d7960cbdf976f1e4673af26669 Mon Sep 17 00:00:00 2001 From: andronache Date: Mon, 7 Dec 2020 17:49:13 +0200 Subject: [PATCH] Added tests in config --- config/actionscfg_test.go | 83 ++++++++++++++++++++++++++++++++++++++ config/config_json_test.go | 20 +++++++++ config/config_test.go | 51 +++++++++++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/config/actionscfg_test.go b/config/actionscfg_test.go index 6d55b49ee..94da2624f 100644 --- a/config/actionscfg_test.go +++ b/config/actionscfg_test.go @@ -17,3 +17,86 @@ along with this program. If not, see */ package config + +import ( + "reflect" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestActionSCfgLoadFromJSONCfg(t *testing.T) { + jsonCfg := &ActionSJsonCfg{ + Enabled: utils.BoolPointer(true), + Indexed_selects: utils.BoolPointer(false), + String_indexed_fields: &[]string{"*req.index1"}, + Prefix_indexed_fields: &[]string{"*req.index1", "*req.index2"}, + Suffix_indexed_fields: &[]string{"*req.index1"}, + Nested_fields: utils.BoolPointer(true), + } + expected := &ActionSCfg{ + Enabled: true, + IndexedSelects: false, + StringIndexedFields: &[]string{"*req.index1"}, + PrefixIndexedFields: &[]string{"*req.index1", "*req.index2"}, + SuffixIndexedFields: &[]string{"*req.index1"}, + NestedFields: true, + } + jsnCfg := NewDefaultCGRConfig() + if err = jsnCfg.actionSCfg.loadFromJSONCfg(jsonCfg); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, jsnCfg.actionSCfg) { + t.Errorf("\nExpecting <%+v>,\n Received <%+v>", utils.ToJSON(expected), utils.ToJSON(jsnCfg.actionSCfg)) + } +} + +func TestActionSCfgAsMapInterface(t *testing.T) { + cfgJSONStr := `{ +"actions": { + "enabled": true, + "Indexed_selects": false, + "String_indexed_fields": ["*req.index1"], + "Prefix_indexed_fields": ["*req.index1","*req.index2"], + "Suffix_indexed_fields": ["*req.index1"], + "Nested_fields": true, + }, +}` + + eMap := map[string]interface{}{ + utils.EnabledCfg: true, + utils.IndexedSelectsCfg: false, + utils.StringIndexedFieldsCfg: []string{"*req.index1"}, + utils.PrefixIndexedFieldsCfg: []string{"*req.index1", "*req.index2"}, + utils.SuffixIndexedFieldsCfg: []string{"*req.index1"}, + utils.NestedFieldsCfg: true, + } + if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil { + t.Error(err) + } else if rcv := cgrCfg.actionSCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("Expected: %+v\n Received: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } +} + +func TestActionSCfgClone(t *testing.T) { + ban := &ActionSCfg{ + Enabled: true, + IndexedSelects: false, + StringIndexedFields: &[]string{"*req.index1"}, + PrefixIndexedFields: &[]string{"*req.index1", "*req.index2"}, + SuffixIndexedFields: &[]string{"*req.index1"}, + NestedFields: true, + } + rcv := ban.Clone() + if !reflect.DeepEqual(ban, rcv) { + t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(ban), utils.ToJSON(rcv)) + } + if (*rcv.StringIndexedFields)[0] = ""; (*ban.StringIndexedFields)[0] != "*req.index1" { + t.Errorf("Expected clone to not modify the cloned") + } + if (*rcv.PrefixIndexedFields)[0] = ""; (*ban.PrefixIndexedFields)[0] != "*req.index1" { + t.Errorf("Expected clone to not modify the cloned") + } + if (*rcv.SuffixIndexedFields)[0] = ""; (*ban.SuffixIndexedFields)[0] != "*req.index1" { + t.Errorf("Expected clone to not modify the cloned") + } +} diff --git a/config/config_json_test.go b/config/config_json_test.go index 3d9297175..924d99e3b 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -2162,3 +2162,23 @@ func TestDfTemplateSJsonCfg(t *testing.T) { t.Errorf("Expected: %+v \n,received: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg)) } } + +func TestDfActionSJsonCfg(t *testing.T) { + eCfg := &ActionSJsonCfg{ + Enabled: utils.BoolPointer(false), + Indexed_selects: utils.BoolPointer(true), + String_indexed_fields: nil, + Prefix_indexed_fields: &[]string{}, + Suffix_indexed_fields: &[]string{}, + Nested_fields: utils.BoolPointer(false), + } + dfCgrJSONCfg, err := NewCgrJsonCfgFromBytes([]byte(CGRATES_CFG_JSON)) + if err != nil { + t.Error(err) + } + if cfg, err := dfCgrJSONCfg.ActionSCfgJson(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eCfg, cfg) { + t.Error("Received: ", utils.ToJSON(cfg)) + } +} diff --git a/config/config_test.go b/config/config_test.go index e1f27b625..7a9f06e9c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -5868,3 +5868,54 @@ func TestCGRConfigClone(t *testing.T) { t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(cfg.coreSCfg), utils.ToJSON(rcv.coreSCfg)) } } + +func TestActionSConfig(t *testing.T) { + expected := &ActionSCfg{ + Enabled: false, + IndexedSelects: true, + StringIndexedFields: nil, + PrefixIndexedFields: &[]string{}, + SuffixIndexedFields: &[]string{}, + NestedFields: false, + } + cgrConfig := NewDefaultCGRConfig() + newConfig := cgrConfig.ActionSCfg() + if !reflect.DeepEqual(expected, newConfig) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(newConfig)) + } +} + +func TestV1GetConfigSectionActionSJson(t *testing.T) { + var reply map[string]interface{} + expected := map[string]interface{}{ + ActionSJson: map[string]interface{}{ + utils.EnabledCfg: false, + utils.IndexedSelectsCfg: true, + utils.PrefixIndexedFieldsCfg: []string{}, + utils.SuffixIndexedFieldsCfg: []string{}, + utils.NestedFieldsCfg: false, + }, + } + cfgCgr := NewDefaultCGRConfig() + if err := cfgCgr.V1GetConfig(&SectionWithOpts{Section: ActionSJson}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, expected) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(reply)) + } +} + +func TestLoadActionSCfgError(t *testing.T) { + cfgJSONStr := `{ + "actions": { + "string_indexed_fields": "*req.index", + } + }` + expected := "json: cannot unmarshal string into Go struct field ActionSJsonCfg.String_indexed_fields of type []string" + cgrConfig := NewDefaultCGRConfig() + + if cgrCfgJSON, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if err := cgrConfig.loadActionSCfg(cgrCfgJSON); err == nil || err.Error() != expected { + t.Errorf("Expected %+v, received %+v", expected, err) + } +}