From 10833ec31241efea8a7fb27e0e838bcb0eef76c7 Mon Sep 17 00:00:00 2001 From: adragusin Date: Wed, 22 Apr 2020 18:06:07 +0300 Subject: [PATCH] Added tests for some AsMapInterface methods --- config/cdrecfg.go | 4 +- config/cdrecfg_test.go | 2 +- config/config.go | 16 ++++---- config/diametercfg.go | 6 +-- config/dnsagntcfg.go | 35 +++++++++++++----- config/erscfg.go | 10 ++--- config/fctemplate.go | 14 ++++--- config/httpagntcfg.go | 14 +++++-- config/httpagntcfg_test.go | 75 ++++++++++++++++++++++++++++++++++++++ config/loaderscfg.go | 18 ++++++--- config/loaderscfg_test.go | 75 ++++++++++++++++++++++++++++++++++++++ config/radiuscfg.go | 4 +- utils/consts.go | 5 ++- 13 files changed, 231 insertions(+), 47 deletions(-) diff --git a/config/cdrecfg.go b/config/cdrecfg.go index 65f2a2110..f4b5a556f 100644 --- a/config/cdrecfg.go +++ b/config/cdrecfg.go @@ -95,10 +95,10 @@ func (self *CdreCfg) Clone() *CdreCfg { return clnCdre } -func (cdre *CdreCfg) AsMapInterface() map[string]interface{} { +func (cdre *CdreCfg) AsMapInterface(separator string) map[string]interface{} { fields := make([]map[string]interface{}, len(cdre.Fields)) for i, item := range cdre.Fields { - fields[i] = item.AsMapInterface() + fields[i] = item.AsMapInterface(separator) } return map[string]interface{}{ diff --git a/config/cdrecfg_test.go b/config/cdrecfg_test.go index 30674bce0..3af7881e5 100644 --- a/config/cdrecfg_test.go +++ b/config/cdrecfg_test.go @@ -182,7 +182,7 @@ func TestCdreAsMapInterface(t *testing.T) { t.Error(err) } else if err = cdre.loadFromJsonCfg(cdreCfg["*default"], utils.EmptyString); err != nil { t.Error(err) - } else if rcv := cdre.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + } else if rcv := cdre.AsMapInterface(""); !reflect.DeepEqual(eMap, rcv) { t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } diff --git a/config/config.go b/config/config.go index 77134cfb7..a5f97c3eb 100755 --- a/config/config.go +++ b/config/config.go @@ -1431,7 +1431,7 @@ func (cfg *CGRConfig) reloadSections(sections ...string) (err error) { return } -func (cfg *CGRConfig) AsMapInterface() map[string]interface{} { +func (cfg *CGRConfig) AsMapInterface(separator string) map[string]interface{} { rpcConns := make(map[string]map[string]interface{}, len(cfg.rpcConns)) for key, val := range cfg.rpcConns { rpcConns[key] = val.AsMapInterface() @@ -1439,17 +1439,17 @@ func (cfg *CGRConfig) AsMapInterface() map[string]interface{} { cdreProfiles := make(map[string]map[string]interface{}) for key, val := range cfg.CdreProfiles { - cdreProfiles[key] = val.AsMapInterface() + cdreProfiles[key] = val.AsMapInterface(separator) } loaderCfg := make([]map[string]interface{}, len(cfg.loaderCfg)) for i, item := range cfg.loaderCfg { - loaderCfg[i] = item.AsMapInterface() + loaderCfg[i] = item.AsMapInterface(separator) } httpAgentCfg := make([]map[string]interface{}, len(cfg.httpAgentCfg)) for i, item := range cfg.httpAgentCfg { - httpAgentCfg[i] = item.AsMapInterface() + httpAgentCfg[i] = item.AsMapInterface(separator) } return map[string]interface{}{ @@ -1473,9 +1473,9 @@ func (cfg *CGRConfig) AsMapInterface() map[string]interface{} { utils.FsAgentCfg: cfg.fsAgentCfg.AsMapInterface(), utils.KamAgentCfg: cfg.kamAgentCfg.AsMapInterface(), utils.AsteriskAgentCfg: cfg.asteriskAgentCfg.AsMapInterface(), - utils.DiameterAgentCfg: cfg.diameterAgentCfg.AsMapInterface(), - utils.RadiusAgentCfg: cfg.radiusAgentCfg.AsMapInterface(), - utils.DnsAgentCfg: cfg.dnsAgentCfg.AsMapInterface(), + utils.DiameterAgentCfg: cfg.diameterAgentCfg.AsMapInterface(separator), + utils.RadiusAgentCfg: cfg.radiusAgentCfg.AsMapInterface(separator), + utils.DnsAgentCfg: cfg.dnsAgentCfg.AsMapInterface(separator), utils.AttributeSCfg: cfg.attributeSCfg.AsMapInterface(), utils.ChargerSCfg: cfg.chargerSCfg.AsMapInterface(), utils.ResourceSCfg: cfg.resourceSCfg.AsMapInterface(), @@ -1489,6 +1489,6 @@ func (cfg *CGRConfig) AsMapInterface() map[string]interface{} { utils.MailerCfg: cfg.mailerCfg.AsMapInterface(), utils.AnalyzerSCfg: cfg.analyzerSCfg.AsMapInterface(), utils.Apier: cfg.apier.AsMapInterface(), - utils.ErsCfg: cfg.ersCfg.AsMapInterface(), + utils.ErsCfg: cfg.ersCfg.AsMapInterface(separator), } } diff --git a/config/diametercfg.go b/config/diametercfg.go index f2a7ccb45..d58725047 100644 --- a/config/diametercfg.go +++ b/config/diametercfg.go @@ -125,12 +125,12 @@ func (da *DiameterAgentCfg) loadFromJsonCfg(jsnCfg *DiameterAgentJsonCfg, separa return nil } -func (ds *DiameterAgentCfg) AsMapInterface() map[string]interface{} { +func (ds *DiameterAgentCfg) AsMapInterface(separator string) map[string]interface{} { templates := make(map[string][]map[string]interface{}) for key, value := range ds.Templates { fcTemplate := make([]map[string]interface{}, len(value)) for i, val := range value { - fcTemplate[i] = val.AsMapInterface() + fcTemplate[i] = val.AsMapInterface(separator) } templates[key] = fcTemplate @@ -138,7 +138,7 @@ func (ds *DiameterAgentCfg) AsMapInterface() map[string]interface{} { requestProcessors := make([]map[string]interface{}, len(ds.RequestProcessors)) for i, item := range ds.RequestProcessors { - requestProcessors[i] = item.AsMapInterface() + requestProcessors[i] = item.AsMapInterface(separator) } return map[string]interface{}{ diff --git a/config/dnsagntcfg.go b/config/dnsagntcfg.go index 912c15b6b..e1a61b94d 100644 --- a/config/dnsagntcfg.go +++ b/config/dnsagntcfg.go @@ -19,6 +19,8 @@ along with this program. If not, see package config import ( + "strings" + "github.com/cgrates/cgrates/utils" ) @@ -80,10 +82,10 @@ func (da *DNSAgentCfg) loadFromJsonCfg(jsnCfg *DNSAgentJsonCfg, sep string) (err return nil } -func (da *DNSAgentCfg) AsMapInterface() map[string]interface{} { +func (da *DNSAgentCfg) AsMapInterface(separator string) map[string]interface{} { requestProcessors := make([]map[string]interface{}, len(da.RequestProcessors)) for i, item := range da.RequestProcessors { - requestProcessors[i] = item.AsMapInterface() + requestProcessors[i] = item.AsMapInterface(separator) } return map[string]interface{}{ @@ -147,23 +149,36 @@ func (rp *RequestProcessor) loadFromJsonCfg(jsnCfg *ReqProcessorJsnCfg, sep stri return nil } -func (rp *RequestProcessor) AsMapInterface() map[string]interface{} { +func (rp *RequestProcessor) AsMapInterface(separator string) map[string]interface{} { replyFields := make([]map[string]interface{}, len(rp.ReplyFields)) for i, item := range rp.ReplyFields { - replyFields[i] = item.AsMapInterface() + replyFields[i] = item.AsMapInterface(separator) } requestFields := make([]map[string]interface{}, len(rp.RequestFields)) for i, item := range rp.RequestFields { - requestFields[i] = item.AsMapInterface() + requestFields[i] = item.AsMapInterface(separator) + } + var tenant string + if rp.Tenant != nil { + values := make([]string, len(rp.Tenant)) + for i, item := range rp.Tenant { + values[i] = item.Rules + } + tenant = strings.Join(values, utils.EmptyString) + } + + flags := make(map[string][]string, len(rp.Flags)) + for key, item := range rp.Flags { + flags[key] = item } return map[string]interface{}{ - utils.ID: rp.ID, - utils.Tenant: rp.Tenant, - utils.Filters: rp.Filters, - utils.Flags: rp.Flags, - utils.TimezoneCfg: rp.Timezone, + utils.IDCfg: rp.ID, + utils.TenantCfg: tenant, + utils.FiltersCfg: rp.Filters, + utils.FlagsCfg: flags, + utils.TimezoneCfgC: rp.Timezone, utils.RequestFieldsCfg: requestFields, utils.ReplyFieldsCfg: replyFields, } diff --git a/config/erscfg.go b/config/erscfg.go index 2f4616f20..f55f7be34 100644 --- a/config/erscfg.go +++ b/config/erscfg.go @@ -98,11 +98,11 @@ func (erS *ERsCfg) Clone() (cln *ERsCfg) { return } -func (erS *ERsCfg) AsMapInterface() map[string]interface{} { +func (erS *ERsCfg) AsMapInterface(separator string) map[string]interface{} { readers := make([]map[string]interface{}, len(erS.Readers)) for i, item := range erS.Readers { - readers[i] = item.AsMapInterface() + readers[i] = item.AsMapInterface(separator) } return map[string]interface{}{ @@ -247,7 +247,7 @@ func (er *EventReaderCfg) Clone() (cln *EventReaderCfg) { return } -func (er *EventReaderCfg) AsMapInterface() map[string]interface{} { +func (er *EventReaderCfg) AsMapInterface(separator string) map[string]interface{} { xmlRootPath := make([]string, len(er.XmlRootPath)) for i, item := range er.XmlRootPath { xmlRootPath[i] = item @@ -262,11 +262,11 @@ func (er *EventReaderCfg) AsMapInterface() map[string]interface{} { } fields := make([]map[string]interface{}, len(er.Fields)) for i, item := range er.Fields { - fields[i] = item.AsMapInterface() + fields[i] = item.AsMapInterface(separator) } cacheDumpFields := make([]map[string]interface{}, len(er.CacheDumpFields)) for i, item := range er.CacheDumpFields { - cacheDumpFields[i] = item.AsMapInterface() + cacheDumpFields[i] = item.AsMapInterface(separator) } return map[string]interface{}{ diff --git a/config/fctemplate.go b/config/fctemplate.go index 44ee84348..2a9e9058d 100755 --- a/config/fctemplate.go +++ b/config/fctemplate.go @@ -20,7 +20,6 @@ package config import ( "fmt" - "strings" "time" "github.com/cgrates/cgrates/utils" @@ -195,7 +194,7 @@ func (self *FCTemplate) Clone() *FCTemplate { return cln } -func (fc *FCTemplate) AsMapInterface() (mp map[string]interface{}) { +func (fc *FCTemplate) AsMapInterface(separator string) (mp map[string]interface{}) { mp = make(map[string]interface{}) if fc.Tag != utils.EmptyString { mp[utils.TagCfg] = fc.Tag @@ -210,11 +209,16 @@ func (fc *FCTemplate) AsMapInterface() (mp map[string]interface{}) { mp[utils.FiltersCfg] = fc.Filters } if fc.Value != nil { - values := make([]string, len(fc.Value)) for i, item := range fc.Value { - values[i] = item.Rules + if i != 0 { + mp[utils.ValueCfg] = mp[utils.ValueCfg].(string) + separator + } + if mp[utils.ValueCfg] == nil { + mp[utils.ValueCfg] = item.Rules + } else { + mp[utils.ValueCfg] = mp[utils.ValueCfg].(string) + item.Rules + } } - mp[utils.ValueCfg] = strings.Join(values, utils.EmptyString) } if fc.Width != 0 { mp[utils.WidthCfg] = fc.Width diff --git a/config/httpagntcfg.go b/config/httpagntcfg.go index ce8caad0e..2a704827c 100644 --- a/config/httpagntcfg.go +++ b/config/httpagntcfg.go @@ -50,6 +50,14 @@ func (hcfgs *HttpAgentCfgs) loadFromJsonCfg(jsnHttpAgntCfg *[]*HttpAgentJsonCfg, return nil } +func (hcfgs *HttpAgentCfgs) AsMapInterface(separator string) []map[string]interface{} { + mp := make([]map[string]interface{}, len(*hcfgs)) + for i, item := range *hcfgs { + mp[i] = item.AsMapInterface(separator) + } + return mp +} + type HttpAgentCfg struct { ID string // identifier for the agent, so we can update it's processors Url string @@ -118,10 +126,10 @@ func (ca *HttpAgentCfg) loadFromJsonCfg(jsnCfg *HttpAgentJsonCfg, separator stri return nil } -func (ca *HttpAgentCfg) AsMapInterface() map[string]interface{} { +func (ca *HttpAgentCfg) AsMapInterface(separator string) map[string]interface{} { requestProcessors := make([]map[string]interface{}, len(ca.RequestProcessors)) for i, item := range ca.RequestProcessors { - requestProcessors[i] = item.AsMapInterface() + requestProcessors[i] = item.AsMapInterface(separator) } return map[string]interface{}{ @@ -130,6 +138,6 @@ func (ca *HttpAgentCfg) AsMapInterface() map[string]interface{} { utils.SessionSConnsCfg: ca.SessionSConns, utils.RequestPayloadCfg: ca.RequestPayload, utils.ReplyPayloadCfg: ca.ReplyPayload, - utils.RequestProcessorsCfg: ca.RequestProcessors, + utils.RequestProcessorsCfg: requestProcessors, } } diff --git a/config/httpagntcfg_test.go b/config/httpagntcfg_test.go index ddf57cf2a..625fc1e55 100644 --- a/config/httpagntcfg_test.go +++ b/config/httpagntcfg_test.go @@ -351,3 +351,78 @@ func TestHttpAgentCfgappendHttpAgntProcCfgs(t *testing.T) { t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(initial)) } } + +func testHttpAgentCfgAsMapInterface(t *testing.T) { + var httpcfg HttpAgentCfgs + cfgJSONStr := `{ + +"http_agent": [ + { + "id": "conecto1", + "url": "/conecto", + "sessions_conns": ["*localhost"], + "request_payload": "*url", + "reply_payload": "*xml", + "request_processors": [ + { + "id": "OutboundAUTHDryRun", + "filters": ["*string:~*req.request_type:OutboundAUTH","*string:~*req.Msisdn:497700056231"], + "tenant": "cgrates.org", + "flags": ["*dryrun"], + "request_fields":[ + ], + "reply_fields":[ + {"tag": "Allow", "path": "*rep.response.Allow", "type": "*constant", + "value": "1", "mandatory": true}, + {"tag": "Concatenated1", "path": "*rep.response.Concatenated", "type": "*composed", + "value": "~*req.MCC;/", "mandatory": true}, + {"tag": "Concatenated2", "path": "*rep.response.Concatenated", "type": "*composed", + "value": "Val1"}, + {"tag": "MaxDuration", "path": "*rep.response.MaxDuration", "type": "*constant", + "value": "1200", "blocker": true}, + {"tag": "Unused", "path": "*rep.response.Unused", "type": "*constant", + "value": "0"}, + ], + }, + ], + }, + ], +}` + request_fields := []string{} + eMap := []map[string]interface{}{ + { + "id": "conecto1", + "url": "/conecto", + "sessions_conns": []string{"*localhost"}, + "request_payload": "*url", + "reply_payload": "*xml", + "request_processors": []map[string]interface{}{ + { + "id": "OutboundAUTHDryRun", + "filters": []string{"*string:~*req.request_type:OutboundAUTH", "*string:~*req.Msisdn:497700056231"}, + "tenant": "cgrates.org", + "flags": map[string][]string{"*dryrun": request_fields}, + "Timezone": "", + "request_fields": request_fields, + "reply_fields": []map[string]interface{}{ + {"tag": "Allow", "path": "*rep.response.Allow", "type": "*constant", "value": "1", "mandatory": true}, + {"tag": "Concatenated1", "path": "*rep.response.Concatenated", "type": "*composed", "value": "~*req.MCC;/", "mandatory": true}, + {"tag": "Concatenated2", "path": "*rep.response.Concatenated", "type": "*composed", "value": "Val1"}, + {"tag": "MaxDuration", "path": "*rep.response.MaxDuration", "type": "*constant", "value": "1200", "blocker": true}, + {"tag": "Unused", "path": "*rep.response.Unused", "type": "*constant", "value": "0"}, + }, + }, + }, + }, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnhttpCfg, err := jsnCfg.HttpAgentJsonCfg(); err != nil { + t.Error(err) + } else if err = httpcfg.loadFromJsonCfg(jsnhttpCfg, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if rcv := httpcfg.AsMapInterface(";"); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("Expected: %+v,\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } + +} diff --git a/config/loaderscfg.go b/config/loaderscfg.go index 412c01121..c40a0d769 100644 --- a/config/loaderscfg.go +++ b/config/loaderscfg.go @@ -19,6 +19,7 @@ along with this program. If not, see package config import ( + "strings" "time" "github.com/cgrates/cgrates/utils" @@ -182,10 +183,10 @@ func (self *LoaderSCfg) Clone() *LoaderSCfg { return clnLoader } -func (lData *LoaderDataType) AsMapInterface() map[string]interface{} { +func (lData *LoaderDataType) AsMapInterface(separator string) map[string]interface{} { fields := make([]map[string]interface{}, len(lData.Fields)) for i, item := range lData.Fields { - fields[i] = item.AsMapInterface() + fields[i] = item.AsMapInterface(separator) } return map[string]interface{}{ @@ -195,23 +196,28 @@ func (lData *LoaderDataType) AsMapInterface() map[string]interface{} { } } -func (l *LoaderSCfg) AsMapInterface() map[string]interface{} { +func (l *LoaderSCfg) AsMapInterface(separator string) map[string]interface{} { tenant := make([]string, len(l.Tenant)) for i, item := range l.Tenant { tenant[i] = item.Rules } + strings.Join(tenant, utils.EmptyString) data := make([]map[string]interface{}, len(l.Data)) for i, item := range l.Data { - data[i] = item.AsMapInterface() + data[i] = item.AsMapInterface(separator) + } + var runDelay string = "0" + if l.RunDelay != 0 { + runDelay = l.RunDelay.String() } return map[string]interface{}{ utils.IdCfg: l.Id, utils.EnabledCfg: l.Enabled, - utils.TenantCfg: tenant, + utils.TenantCfg: strings.Join(tenant, utils.EmptyString), utils.DryRunCfg: l.DryRun, - utils.RunDelayCfg: l.RunDelay, + utils.RunDelayCfg: runDelay, utils.LockFileNameCfg: l.LockFileName, utils.CacheSConnsCfg: l.CacheSConns, utils.FieldSeparatorCfg: l.FieldSeparator, diff --git a/config/loaderscfg_test.go b/config/loaderscfg_test.go index de26ab714..99015fff6 100644 --- a/config/loaderscfg_test.go +++ b/config/loaderscfg_test.go @@ -104,3 +104,78 @@ func TestLoaderSCfgloadFromJsonCfg(t *testing.T) { t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(loadscfg)) } } + +func TestLoaderCfgAsMapInterface(t *testing.T) { + var loadscfg LoaderSCfg + cfgJSONStr := `{ + "loaders": [ + { + "id": "*default", + "enabled": false, + "tenant": "", + "dry_run": false, + "run_delay": 0, + "lock_filename": ".cgr.lck", + "caches_conns": ["*internal"], + "field_separator": ",", + "tp_in_dir": "/var/spool/cgrates/loader/in", + "tp_out_dir": "/var/spool/cgrates/loader/out", + "data":[ + { + "type": "*attributes", + "file_name": "Attributes.csv", + "fields": [ + {"tag": "TenantID", "path": "Tenant", "type": "*variable", "value": "~0", "mandatory": true}, + {"tag": "ProfileID", "path": "ID", "type": "*variable", "value": "~1", "mandatory": true}, + ], + }, + ], + }, + ], + +}` + eMap := map[string]interface{}{ + "id": "*default", + "enabled": false, + "tenant": "", + "dry_run": false, + "run_delay": "0", + "lock_filename": ".cgr.lck", + "caches_conns": []string{"*internal:*caches"}, + "field_separator": ",", + "tp_in_dir": "/var/spool/cgrates/loader/in", + "tp_out_dir": "/var/spool/cgrates/loader/out", + "data": []map[string]interface{}{ + { + "type": "*attributes", + "file_name": "Attributes.csv", + "fields": []map[string]interface{}{ + { + "tag": "TenantID", + "path": "Tenant", + "type": "*variable", + "value": "~0", + "mandatory": true, + }, { + "tag": "ProfileID", + "path": "ID", + "type": "*variable", + "value": "~1", + "mandatory": true, + }, + }, + }, + }, + } + + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnLoadersCfg, err := jsnCfg.LoaderJsonCfg(); err != nil { + t.Error(err) + } else if err = loadscfg.loadFromJsonCfg(jsnLoadersCfg[0], utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if rcv := loadscfg.AsMapInterface(""); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } + +} diff --git a/config/radiuscfg.go b/config/radiuscfg.go index b2ce1aaa7..7c1ca7db7 100644 --- a/config/radiuscfg.go +++ b/config/radiuscfg.go @@ -96,7 +96,7 @@ func (self *RadiusAgentCfg) loadFromJsonCfg(jsnCfg *RadiusAgentJsonCfg, separato return nil } -func (ra *RadiusAgentCfg) AsMapInterface() map[string]interface{} { +func (ra *RadiusAgentCfg) AsMapInterface(separator string) map[string]interface{} { clientSecrets := make(map[string]interface{}, len(ra.ClientSecrets)) for key, val := range ra.ClientSecrets { clientSecrets[key] = val @@ -109,7 +109,7 @@ func (ra *RadiusAgentCfg) AsMapInterface() map[string]interface{} { requestProcessors := make([]map[string]interface{}, len(ra.RequestProcessors)) for i, item := range ra.RequestProcessors { - requestProcessors[i] = item.AsMapInterface() + requestProcessors[i] = item.AsMapInterface(separator) } return map[string]interface{}{ diff --git a/utils/consts.go b/utils/consts.go index d4d3c226e..502b66fa9 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1824,6 +1824,7 @@ const ( // KamAgentCfg EvapiConnsCfg = "evapi_conns" TimezoneCfg = "timezone" + TimezoneCfgC = "Timezone" // AsteriskConnCfg UserCf = "user" @@ -1849,8 +1850,8 @@ const ( RequestProcessorsCfg = "request_processors" // RequestProcessor - RequestFieldsCfg = "Request_fields" - ReplyFieldsCfg = "Reply_fields" + RequestFieldsCfg = "request_fields" + ReplyFieldsCfg = "reply_fields" // RadiusAgentCfg ListenAuthCfg = "listen_auth"