From 13704118edb4de96c86225f0ee9abb4e8dad586c Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 6 Oct 2020 11:36:01 +0300 Subject: [PATCH] Covered httpagntcfg to 100% --- config/httpagntcfg.go | 4 +- config/httpagntcfg_test.go | 137 ++++++++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/config/httpagntcfg.go b/config/httpagntcfg.go index 667116a25..419d704a5 100644 --- a/config/httpagntcfg.go +++ b/config/httpagntcfg.go @@ -18,7 +18,9 @@ along with this program. If not, see package config -import "github.com/cgrates/cgrates/utils" +import ( + "github.com/cgrates/cgrates/utils" +) type HttpAgentCfgs []*HttpAgentCfg diff --git a/config/httpagntcfg_test.go b/config/httpagntcfg_test.go index 9b9087a3b..9bb447f4a 100644 --- a/config/httpagntcfg_test.go +++ b/config/httpagntcfg_test.go @@ -25,7 +25,7 @@ import ( "github.com/cgrates/cgrates/utils" ) -func TestHttpAgentCfgsloadFromJsonCfg(t *testing.T) { +func TestHttpAgentCfgsloadFromJsonCfgCase1(t *testing.T) { cfgJSON := &[]*HttpAgentJsonCfg{ { Id: utils.StringPointer("RandomID"), @@ -88,7 +88,7 @@ func TestHttpAgentCfgsloadFromJsonCfg(t *testing.T) { } } -func TestHttpAgentCfgsloadFromJsonCfg1(t *testing.T) { +func TestHttpAgentCfgsloadFromJsonCfgCase2(t *testing.T) { cfgJSON := &[]*HttpAgentJsonCfg{ { Id: utils.StringPointer("conecto1"), @@ -229,7 +229,7 @@ func TestHttpAgentCfgsloadFromJsonCfg1(t *testing.T) { } } -func TestHttpAgentCfgloadFromJsonCfg(t *testing.T) { +func TestHttpAgentCfgloadFromJsonCfgCase3(t *testing.T) { jsnhttpCfg := &HttpAgentJsonCfg{ Id: utils.StringPointer("conecto1"), Url: utils.StringPointer("/conecto"), @@ -270,6 +270,137 @@ func TestHttpAgentCfgloadFromJsonCfg(t *testing.T) { } } +func TestHttpAgentCfgloadFromJsonCfgCase4(t *testing.T) { + cfgJSON := &[]*HttpAgentJsonCfg{ + { + Id: utils.StringPointer("conecto1"), + Url: utils.StringPointer("/conecto"), + Sessions_conns: &[]string{utils.MetaLocalHost}, + Request_payload: utils.StringPointer(utils.MetaUrl), + Reply_payload: utils.StringPointer(utils.MetaXml), + Request_processors: &[]*ReqProcessorJsnCfg{ + { + ID: utils.StringPointer("OutboundAUTHDryRun"), + Filters: &[]string{"*string:*req.request_type:OutboundAUTH", "*string:*req.Msisdn:497700056231"}, + Tenant: utils.StringPointer("cgrates.org"), + Flags: &[]string{utils.MetaDryRun}, + Request_fields: &[]*FcTemplateJsonCfg{}, + Reply_fields: &[]*FcTemplateJsonCfg{ + { + Tag: utils.StringPointer("Allow"), + Path: utils.StringPointer("response.Allow"), + Type: utils.StringPointer(utils.META_CONSTANT), + Value: utils.StringPointer("1"), + Mandatory: utils.BoolPointer(true), + Layout: utils.StringPointer("2006-01-02T15:04:05Z07:00"), + }, + }, + }, + { + ID: utils.StringPointer("mtcall_cdr"), + Filters: &[]string{"*string:*req.request_type:MTCALL_CDR"}, + Tenant: utils.StringPointer("cgrates.org"), + Flags: &[]string{utils.MetaCDRs}, + Request_fields: &[]*FcTemplateJsonCfg{ + { + Tag: utils.StringPointer("RequestType"), + Path: utils.StringPointer("RequestType"), + Type: utils.StringPointer(utils.META_CONSTANT), + Value: utils.StringPointer("*pseudoprepaid"), + Mandatory: utils.BoolPointer(true), + Layout: utils.StringPointer("2006-01-02T15:04:05Z07:00"), + }, + }, + Reply_fields: &[]*FcTemplateJsonCfg{ + { + Tag: utils.StringPointer("CDR_ID"), + Path: utils.StringPointer("CDR_RESPONSE.CDR_ID"), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~*req.CDR_ID"), + Mandatory: utils.BoolPointer(true), + Layout: utils.StringPointer("2006-01-02T15:04:05Z07:00"), + }, + }, + }}, + }, + { + Id: utils.StringPointer("conecto_xml"), + Url: utils.StringPointer("/conecto_xml"), + Sessions_conns: &[]string{utils.MetaLocalHost}, + Request_payload: utils.StringPointer("*xml"), + Reply_payload: utils.StringPointer("*xml"), + Request_processors: &[]*ReqProcessorJsnCfg{ + { + ID: utils.StringPointer("cdr_from_xml"), + Tenant: utils.StringPointer("a{*"), + Flags: &[]string{utils.MetaCDRs}, + Request_fields: &[]*FcTemplateJsonCfg{}, + Reply_fields: &[]*FcTemplateJsonCfg{}, + }, + }, + }, + } + expected := "invalid converter terminator in rule: " + if jsonCfg, err := NewDefaultCGRConfig(); err != nil { + t.Error(err) + } else if err = jsonCfg.httpAgentCfg.loadFromJsonCfg(cfgJSON, jsonCfg.generalCfg.RSRSep); err == nil || err.Error() != expected { + t.Errorf("Expected %+v, received %+v", expected, err) + } +} + +func TestHttpAgentCfgloadFromJsonCfgCase5(t *testing.T) { + cfgJSON := &[]*HttpAgentJsonCfg{ + { + Request_processors: nil, + }, + } + jsonCfg, err := NewDefaultCGRConfig() + if err != nil { + t.Error(err) + } + if err := jsonCfg.httpAgentCfg.loadFromJsonCfg(cfgJSON, jsonCfg.generalCfg.RSRSep); err != nil { + t.Error(err) + } +} + +func TestHttpAgentCfgloadFromJsonCfgCase6(t *testing.T) { + jsonCfg, err := NewDefaultCGRConfig() + if err != nil { + t.Error(err) + } + httpAgentCfg := new(HttpAgentCfg) + if err := httpAgentCfg.loadFromJsonCfg(nil, jsonCfg.generalCfg.RSRSep); err != nil { + t.Error(err) + } +} + +func TestHttpAgentCfgloadFromJsonCfgCase7(t *testing.T) { + cfgJSONStr := `{ +"http_agent": [ + { + "id": "RandomID", + }, + ], +}` + cfgJSON := &[]*HttpAgentJsonCfg{ + { + Id: utils.StringPointer("RandomID"), + }, + } + expected := HttpAgentCfgs{ + { + ID: "RandomID", + }, + } + if jsnCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { + t.Error(err) + } else if err = jsnCfg.httpAgentCfg.loadFromJsonCfg(cfgJSON, jsnCfg.generalCfg.RSRSep); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(&expected, &jsnCfg.httpAgentCfg) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(jsnCfg.httpAgentCfg)) + } +} + func TestHttpAgentCfgappendHttpAgntProcCfgs(t *testing.T) { initial := &HttpAgentCfg{ ID: "conecto1",