From c93365599cc8f1a6dde8686d3e9f88520a0fb5e7 Mon Sep 17 00:00:00 2001 From: adragusin Date: Tue, 28 Apr 2020 17:59:54 +0300 Subject: [PATCH] Updated AsMapInterface method for DNSAgent/Added test --- config/dnsagentcfg_test.go | 98 ++++++++++++++++++++++++++++++++++++++ config/dnsagntcfg.go | 11 ++++- 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/config/dnsagentcfg_test.go b/config/dnsagentcfg_test.go index fab5cf868..9a52d683d 100644 --- a/config/dnsagentcfg_test.go +++ b/config/dnsagentcfg_test.go @@ -94,3 +94,101 @@ func TestRequestProcessorloadFromJsonCfg(t *testing.T) { t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(dareq)) } } + +func TestDNSAgentCfgAsMapInterface(t *testing.T) { + var dnsCfg DNSAgentCfg + cfgJSONStr := `{ + "dns_agent": { + "enabled": false, + "listen": "127.0.0.1:2053", + "listen_net": "udp", + "sessions_conns": ["*internal"], + "timezone": "", + "request_processors": [ + ], + }, +}` + eMap := map[string]interface{}{ + "enabled": false, + "listen": "127.0.0.1:2053", + "listen_net": "udp", + "sessions_conns": []string{"*internal"}, + "timezone": "", + "request_processors": []map[string]interface{}{}, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnDaCfg, err := jsnCfg.DNSAgentJsonCfg(); err != nil { + t.Error(err) + } else if err = dnsCfg.loadFromJsonCfg(jsnDaCfg, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if rcv := dnsCfg.AsMapInterface(utils.EmptyString); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } + + cfgJSONStr = `{ + "dns_agent": { + "enabled": false, + "listen": "127.0.0.1:2053", + "listen_net": "udp", + "sessions_conns": ["*internal"], + "timezone": "UTC", + "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"}, + ], + }, + ], + }, + }` + eMap = map[string]interface{}{ + "enabled": false, + "listen": "127.0.0.1:2053", + "listen_net": "udp", + "sessions_conns": []string{"*internal"}, + "timezone": "UTC", + "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": {}}, + "Timezone": "", + "request_fields": []map[string]interface{}{}, + "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 jsnDaCfg, err := jsnCfg.DNSAgentJsonCfg(); err != nil { + t.Error(err) + } else if err = dnsCfg.loadFromJsonCfg(jsnDaCfg, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if rcv := dnsCfg.AsMapInterface(";"); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } + +} diff --git a/config/dnsagntcfg.go b/config/dnsagntcfg.go index 4f09f5fa9..52daffaf1 100644 --- a/config/dnsagntcfg.go +++ b/config/dnsagntcfg.go @@ -87,12 +87,21 @@ func (da *DNSAgentCfg) AsMapInterface(separator string) map[string]interface{} { for i, item := range da.RequestProcessors { requestProcessors[i] = item.AsMapInterface(separator) } + sessionSConns := make([]string, len(da.SessionSConns)) + for i, item := range da.SessionSConns { + buf := utils.ConcatenatedKey(utils.MetaInternal, utils.MetaSessionS) + if item == buf { + sessionSConns[i] = strings.ReplaceAll(item, utils.CONCATENATED_KEY_SEP+utils.MetaSessionS, utils.EmptyString) + } else { + sessionSConns[i] = item + } + } return map[string]interface{}{ utils.EnabledCfg: da.Enabled, utils.ListenCfg: da.Listen, utils.ListenNetCfg: da.ListenNet, - utils.SessionSConnsCfg: da.SessionSConns, + utils.SessionSConnsCfg: sessionSConns, utils.TimezoneCfg: da.Timezone, utils.RequestProcessorsCfg: requestProcessors, }