From 08118224105fb09e316a782493d3b6446b4d60dc Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 12 Apr 2019 14:04:52 +0300 Subject: [PATCH] Add test for config.DNSAgent --- apier/v1/dispatcher_it_test.go | 2 +- config/dnsagentcfg_test.go | 101 +++++++++++++++++++++++++++++++++ config/dnsagntcfg.go | 3 + 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 config/dnsagentcfg_test.go diff --git a/apier/v1/dispatcher_it_test.go b/apier/v1/dispatcher_it_test.go index 4e2e24f83..2397b2655 100644 --- a/apier/v1/dispatcher_it_test.go +++ b/apier/v1/dispatcher_it_test.go @@ -128,7 +128,7 @@ func testDispatcherSSetDispatcherProfile(t *testing.T) { DispatcherProfile: &engine.DispatcherProfile{ Tenant: "cgrates.org", ID: "Dsp1", - FilterIDs: []string{"*string:Account:1001"}, + FilterIDs: []string{"*string:~Account:1001"}, Strategy: utils.MetaFirst, Weight: 20, }, diff --git a/config/dnsagentcfg_test.go b/config/dnsagentcfg_test.go new file mode 100644 index 000000000..ca0b374f2 --- /dev/null +++ b/config/dnsagentcfg_test.go @@ -0,0 +1,101 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ +package config + +import ( + "reflect" + "strings" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestDNSAgentCfgloadFromJsonCfg(t *testing.T) { + var dnsCfg, expected DNSAgentCfg + if err := dnsCfg.loadFromJsonCfg(nil, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(dnsCfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, dnsCfg) + } + if err := dnsCfg.loadFromJsonCfg(new(DNSAgentJsonCfg), utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(dnsCfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, dnsCfg) + } + cfgJSONStr := `{ +"dns_agent": { + "enabled": false, // enables the DNS agent: + "listen": "127.0.0.1:2053", // address where to listen for DNS requests + "listen_net": "udp", // network to listen on + "sessions_conns": [ // connections to SessionS for session management and CDR posting + {"address": "*internal"} + ], + "timezone": "UTC", // timezone of the events if not specified + "request_processors": [ // request processors to be applied to DNS messages + ], +}, +}` + expected = DNSAgentCfg{ + Listen: "127.0.0.1:2053", + ListenNet: "udp", + SessionSConns: []*RemoteHost{{Address: "*internal"}}, + Timezone: "UTC", + } + if jsnCfg, err := NewCgrJsonCfgFromReader(strings.NewReader(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 !reflect.DeepEqual(expected, dnsCfg) { + t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(dnsCfg)) + } +} + +func TestRequestProcessorloadFromJsonCfg(t *testing.T) { + var dareq, expected RequestProcessor + if err := dareq.loadFromJsonCfg(nil, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(dareq, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, dareq) + } + if err := dareq.loadFromJsonCfg(new(ReqProcessorJsnCfg), utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(dareq, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, dareq) + } + json := &ReqProcessorJsnCfg{ + ID: utils.StringPointer("cgrates"), + Tenant: utils.StringPointer("tenant"), + Filters: &[]string{"filter1", "filter2"}, + Flags: &[]string{"flag1", "flag2"}, + Continue_on_success: utils.BoolPointer(true), + } + expected = RequestProcessor{ + ID: "cgrates", + Tenant: NewRSRParsersMustCompile("tenant", true, utils.INFIELD_SEP), + Filters: []string{"filter1", "filter2"}, + Flags: utils.StringMap{"flag1": true, "flag2": true}, + ContinueOnSuccess: true, + } + if err = dareq.loadFromJsonCfg(json, utils.INFIELD_SEP); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, dareq) { + t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(dareq)) + } +} diff --git a/config/dnsagntcfg.go b/config/dnsagntcfg.go index 923d65eaf..6abdc904f 100644 --- a/config/dnsagntcfg.go +++ b/config/dnsagntcfg.go @@ -44,6 +44,9 @@ func (da *DNSAgentCfg) loadFromJsonCfg(jsnCfg *DNSAgentJsonCfg, sep string) (err if jsnCfg.Listen != nil { da.Listen = *jsnCfg.Listen } + if jsnCfg.Timezone != nil { + da.Timezone = *jsnCfg.Timezone + } if jsnCfg.Sessions_conns != nil { da.SessionSConns = make([]*RemoteHost, len(*jsnCfg.Sessions_conns)) for idx, jsnHaCfg := range *jsnCfg.Sessions_conns {