From 1c09bac041f3af3502df9720342b49c32da617a8 Mon Sep 17 00:00:00 2001 From: nickolasdaniel Date: Wed, 19 May 2021 14:03:36 +0300 Subject: [PATCH] Added unit tests for config --- config/eescfg_test.go | 362 ++++++++++++++++++++++++++++++++++++++++++ config/erscfg_test.go | 287 +++++++++++++++++++++++++++++++++ 2 files changed, 649 insertions(+) diff --git a/config/eescfg_test.go b/config/eescfg_test.go index 19a32c3ab..af57dfec7 100644 --- a/config/eescfg_test.go +++ b/config/eescfg_test.go @@ -712,3 +712,365 @@ func TestEEsCfgAsMapInterface(t *testing.T) { } } } + +func TestDiffEventExporterJsonCfg(t *testing.T) { + var d *EventExporterJsonCfg + + v1 := &EventExporterCfg{ + ID: "EES_ID", + Type: "xml", + ExportPath: "/tmp/ees", + Opts: map[string]interface{}{}, + Tenant: RSRParsers{ + { + Rules: "Rule1", + }, + }, + + Timezone: "UTC", + Filters: []string{"Filter1"}, + Flags: utils.FlagsWithParams{ + "FLAG_1": { + "PARAM_1": []string{"param1"}, + }, + }, + AttributeSIDs: []string{"ATTR_PRF"}, + AttributeSCtx: "*sessions", + Synchronous: false, + Attempts: 2, + FieldSep: "", + Fields: []*FCTemplate{ + { + Type: "*string", + }, + }, + headerFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + contentFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + trailerFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + } + + v2 := &EventExporterCfg{ + ID: "EES_ID2", + Type: "http", + ExportPath: "/var/tmp/ees", + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: RSRParsers{ + { + Rules: "cgrates.org", + }, + }, + + Timezone: "EEST", + Filters: []string{"Filter2"}, + Flags: utils.FlagsWithParams{ + "FLAG_2": { + "PARAM_2": []string{"param2"}, + }, + }, + AttributeSIDs: []string{"ATTR_PRF_2"}, + AttributeSCtx: "*actions", + Synchronous: true, + Attempts: 3, + FieldSep: ";", + Fields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + headerFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + contentFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + trailerFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + } + + expected := &EventExporterJsonCfg{ + Id: utils.StringPointer("EES_ID2"), + Type: utils.StringPointer("http"), + Export_path: utils.StringPointer("/var/tmp/ees"), + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: utils.StringPointer("cgrates.org"), + Timezone: utils.StringPointer("EEST"), + Filters: &[]string{"Filter2"}, + Flags: &[]string{"FLAG_2:PARAM_2:param2"}, + Attribute_ids: &[]string{"ATTR_PRF_2"}, + Attribute_context: utils.StringPointer("*actions"), + Synchronous: utils.BoolPointer(true), + Attempts: utils.IntPointer(3), + Field_separator: utils.StringPointer(";"), + Fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*prefix"), + Layout: utils.StringPointer(""), + }, + }, + } + + rcv := diffEventExporterJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} + +func TestGetEventExporterJsonCfg(t *testing.T) { + d := []*EventExporterJsonCfg{ + { + Id: utils.StringPointer("EES_ID"), + }, + } + + expected := &EventExporterJsonCfg{ + Id: utils.StringPointer("EES_ID"), + } + + rcv, idx := getEventExporterJsonCfg(d, "EES_ID") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } else if idx != 0 { + t.Errorf("Expected %v \n but received \n %v", 0, idx) + } + + d = []*EventExporterJsonCfg{ + { + Id: nil, + }, + } + rcv, idx = getEventExporterJsonCfg(d, "EES_ID") + if rcv != nil { + t.Error("Received value should be null") + } else if idx != -1 { + t.Errorf("Expected %v \n but received \n %v", -1, idx) + } +} + +func TestGetEventExporterCfg(t *testing.T) { + d := []*EventExporterCfg{ + { + ID: "EES_ID", + }, + } + + expected := &EventExporterCfg{ + ID: "EES_ID", + } + + rcv := getEventExporterCfg(d, "EES_ID") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } + + d = []*EventExporterCfg{ + { + ID: "EES_ID2", + }, + } + + rcv = getEventExporterCfg(d, "EES_ID") + if !reflect.DeepEqual(rcv, new(EventExporterCfg)) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(new(EventExporterCfg)), utils.ToJSON(rcv)) + } +} + +func TestDiffEventExportersJsonCfg(t *testing.T) { + var d *[]*EventExporterJsonCfg + + v1 := []*EventExporterCfg{ + { + ID: "EES_ID", + Type: "xml", + ExportPath: "/tmp/ees", + Opts: map[string]interface{}{}, + Tenant: RSRParsers{ + { + Rules: "Rule1", + }, + }, + + Timezone: "UTC", + Filters: []string{"Filter1"}, + Flags: utils.FlagsWithParams{ + "FLAG_1": { + "PARAM_1": []string{"param1"}, + }, + }, + AttributeSIDs: []string{"ATTR_PRF"}, + AttributeSCtx: "*sessions", + Synchronous: false, + Attempts: 2, + FieldSep: "", + Fields: []*FCTemplate{ + { + Type: "*string", + }, + }, + headerFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + contentFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + trailerFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + }, + } + + v2 := []*EventExporterCfg{ + { + ID: "EES_ID2", + Type: "http", + ExportPath: "/var/tmp/ees", + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: RSRParsers{ + { + Rules: "cgrates.org", + }, + }, + + Timezone: "EEST", + Filters: []string{"Filter2"}, + Flags: utils.FlagsWithParams{ + "FLAG_2": { + "PARAM_2": []string{"param2"}, + }, + }, + AttributeSIDs: []string{"ATTR_PRF_2"}, + AttributeSCtx: "*actions", + Synchronous: true, + Attempts: 3, + FieldSep: ";", + Fields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + headerFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + contentFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + trailerFields: []*FCTemplate{ + { + Type: "*prefix", + }, + }, + }, + } + + expected := &[]*EventExporterJsonCfg{ + { + Id: utils.StringPointer("EES_ID2"), + Type: utils.StringPointer("http"), + Export_path: utils.StringPointer("/var/tmp/ees"), + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: utils.StringPointer("cgrates.org"), + Timezone: utils.StringPointer("EEST"), + Filters: &[]string{"Filter2"}, + Flags: &[]string{"FLAG_2:PARAM_2:param2"}, + Attribute_ids: &[]string{"ATTR_PRF_2"}, + Attribute_context: utils.StringPointer("*actions"), + Synchronous: utils.BoolPointer(true), + Attempts: utils.IntPointer(3), + Field_separator: utils.StringPointer(";"), + Fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*prefix"), + Layout: utils.StringPointer(""), + }, + }, + }, + } + + rcv := diffEventExportersJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} + +func TestDiffEEsJsonCfg(t *testing.T) { + var d *EEsJsonCfg + + v1 := &EEsCfg{ + Enabled: false, + AttributeSConns: []string{"*localhost"}, + Cache: map[string]*CacheParamCfg{}, + Exporters: []*EventExporterCfg{}, + } + + v2 := &EEsCfg{ + Enabled: true, + AttributeSConns: []string{"*birpc"}, + Cache: map[string]*CacheParamCfg{ + "CACHE_1": { + Limit: 1, + }, + }, + Exporters: []*EventExporterCfg{ + // { + // ID: "EES_ID", + // }, + }, + } + + expected := &EEsJsonCfg{ + Enabled: utils.BoolPointer(true), + Attributes_conns: &[]string{"*birpc"}, + Cache: map[string]*CacheParamJsonCfg{ + "CACHE_1": { + Limit: utils.IntPointer(1), + }, + }, + Exporters: &[]*EventExporterJsonCfg{ + // { + // Id: utils.StringPointer("EES_ID"), + // Opts: map[string]interface{}{}, + // }, + }, + } + + rcv := diffEEsJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} diff --git a/config/erscfg_test.go b/config/erscfg_test.go index 33ad0dca2..5e17f7304 100644 --- a/config/erscfg_test.go +++ b/config/erscfg_test.go @@ -1163,3 +1163,290 @@ func TestGetDefaultExporter(t *testing.T) { t.Fatalf("Unexpected default cfg returned: %s", utils.ToJSON(dft)) } } + +func TestDiffEventReaderJsonCfg(t *testing.T) { + var d *EventReaderJsonCfg + + v1 := &EventReaderCfg{ + ID: "ERS_ID", + Type: "xml", + RunDelay: 1 * time.Second, + ConcurrentReqs: 2, + SourcePath: "/tmp/ers/in", + ProcessedPath: "/tmp/ers/out", + Opts: map[string]interface{}{}, + Tenant: RSRParsers{ + { + Rules: "cgrates.org", + }, + }, + Timezone: "UTC", + Filters: []string{"Filter1"}, + Flags: utils.FlagsWithParams{}, + Fields: []*FCTemplate{}, + CacheDumpFields: []*FCTemplate{}, + } + + v2 := &EventReaderCfg{ + ID: "ERS_ID2", + Type: "json", + RunDelay: 3 * time.Second, + ConcurrentReqs: 1, + SourcePath: "/var/tmp/ers/in", + ProcessedPath: "/var/tmp/ers/out", + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: RSRParsers{ + { + Rules: "itsyscom.com", + }, + }, + Timezone: "EEST", + Filters: []string{"Filter2"}, + Flags: utils.FlagsWithParams{ + "FLAG1": { + "PARAM_1": []string{"param1"}, + }, + }, + Fields: []*FCTemplate{ + { + Type: "*string", + }, + }, + CacheDumpFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + } + + expected := &EventReaderJsonCfg{ + Id: utils.StringPointer("ERS_ID2"), + Type: utils.StringPointer("json"), + Run_delay: utils.StringPointer("3s"), + Concurrent_requests: utils.IntPointer(1), + Source_path: utils.StringPointer("/var/tmp/ers/in"), + Processed_path: utils.StringPointer("/var/tmp/ers/out"), + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: utils.StringPointer("itsyscom.com"), + Timezone: utils.StringPointer("EEST"), + Filters: &[]string{"Filter2"}, + Flags: &[]string{"FLAG1:PARAM_1:param1"}, + Fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*string"), + Layout: utils.StringPointer(""), + }, + }, + Cache_dump_fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*string"), + Layout: utils.StringPointer(""), + }, + }, + } + + rcv := diffEventReaderJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} + +func TestGetEventReaderJsonCfg(t *testing.T) { + d := []*EventReaderJsonCfg{ + { + Id: utils.StringPointer("ERS_ID"), + }, + } + + expected := &EventReaderJsonCfg{ + Id: utils.StringPointer("ERS_ID"), + } + + rcv, idx := getEventReaderJsonCfg(d, "ERS_ID") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } else if idx != 0 { + t.Errorf("Expected %v \n but received \n %v", 0, idx) + } + + d = []*EventReaderJsonCfg{ + { + Id: nil, + }, + } + rcv, idx = getEventReaderJsonCfg(d, "ERS_ID") + if rcv != nil { + t.Error("Received value should be null") + } else if idx != -1 { + t.Errorf("Expected %v \n but received \n %v", -1, idx) + } +} + +func TestGetEventReaderCfg(t *testing.T) { + d := []*EventReaderCfg{ + { + ID: "ERS_ID", + }, + } + + expected := &EventReaderCfg{ + ID: "ERS_ID", + } + + rcv := getEventReaderCfg(d, "ERS_ID") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } + + d = []*EventReaderCfg{ + { + ID: "ERS_ID2", + }, + } + + rcv = getEventReaderCfg(d, "ERS_ID") + if !reflect.DeepEqual(rcv, new(EventReaderCfg)) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(new(EventReaderCfg)), utils.ToJSON(rcv)) + } +} + +func TestDiffEventReadersJsonCfg(t *testing.T) { + var d *[]*EventReaderJsonCfg + + v1 := []*EventReaderCfg{ + { + ID: "ERS_ID", + Type: "xml", + RunDelay: 1 * time.Second, + ConcurrentReqs: 2, + SourcePath: "/tmp/ers/in", + ProcessedPath: "/tmp/ers/out", + Opts: map[string]interface{}{}, + Tenant: RSRParsers{ + { + Rules: "cgrates.org", + }, + }, + Timezone: "UTC", + Filters: []string{"Filter1"}, + Flags: utils.FlagsWithParams{}, + Fields: []*FCTemplate{}, + CacheDumpFields: []*FCTemplate{}, + }, + } + + v2 := []*EventReaderCfg{ + { + ID: "ERS_ID2", + Type: "json", + RunDelay: 3 * time.Second, + ConcurrentReqs: 1, + SourcePath: "/var/tmp/ers/in", + ProcessedPath: "/var/tmp/ers/out", + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: RSRParsers{ + { + Rules: "itsyscom.com", + }, + }, + Timezone: "EEST", + Filters: []string{"Filter2"}, + Flags: utils.FlagsWithParams{ + "FLAG1": { + "PARAM_1": []string{"param1"}, + }, + }, + Fields: []*FCTemplate{ + { + Type: "*string", + }, + }, + CacheDumpFields: []*FCTemplate{ + { + Type: "*string", + }, + }, + }, + } + + expected := &[]*EventReaderJsonCfg{ + { + Id: utils.StringPointer("ERS_ID2"), + Type: utils.StringPointer("json"), + Run_delay: utils.StringPointer("3s"), + Concurrent_requests: utils.IntPointer(1), + Source_path: utils.StringPointer("/var/tmp/ers/in"), + Processed_path: utils.StringPointer("/var/tmp/ers/out"), + Opts: map[string]interface{}{ + "OPT": "opt", + }, + Tenant: utils.StringPointer("itsyscom.com"), + Timezone: utils.StringPointer("EEST"), + Filters: &[]string{"Filter2"}, + Flags: &[]string{"FLAG1:PARAM_1:param1"}, + Fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*string"), + Layout: utils.StringPointer(""), + }, + }, + Cache_dump_fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer("*string"), + Layout: utils.StringPointer(""), + }, + }, + }, + } + + rcv := diffEventReadersJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} + +func TestDiffERsJsonCfg(t *testing.T) { + var d *ERsJsonCfg + + v1 := &ERsCfg{ + Enabled: false, + SessionSConns: []string{"*birpc"}, + Readers: []*EventReaderCfg{ + // { + // ID: "ERS_ID", + // }, + }, + } + + v2 := &ERsCfg{ + Enabled: true, + SessionSConns: []string{"*localhost"}, + Readers: []*EventReaderCfg{ + // { + // ID: "ERS_ID2", + // }, + }, + } + + expected := &ERsJsonCfg{ + Enabled: utils.BoolPointer(true), + Sessions_conns: &[]string{"*localhost"}, + Readers: &[]*EventReaderJsonCfg{ + // { + // Id: utils.StringPointer("ERS_ID2"), + // Opts: map[string]interface{}{}, + // }, + }, + } + + rcv := diffERsJsonCfg(d, v1, v2, ";") + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +}