From e39b452e676de6437cd02f397ede29c747c37f48 Mon Sep 17 00:00:00 2001 From: adragusin Date: Thu, 16 Apr 2020 17:51:47 +0300 Subject: [PATCH] Added some tests for AsMapInterface methods --- config/cdrecfg.go | 6 ++-- config/cdrecfg_test.go | 66 +++++++++++++++++++++++++++++++++++++++++ config/config_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++ config/fctemplate.go | 4 ++- config/generalcfg.go | 27 +++++++++++++---- config/rpcconn.go | 14 ++++----- config/rpcconn_test.go | 39 ++++++++++++++++++++++++ utils/consts.go | 10 ++++--- 8 files changed, 214 insertions(+), 19 deletions(-) diff --git a/config/cdrecfg.go b/config/cdrecfg.go index 9e92db639..65f2a2110 100644 --- a/config/cdrecfg.go +++ b/config/cdrecfg.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" +) // One instance of CdrExporter type CdreCfg struct { @@ -107,7 +109,7 @@ func (cdre *CdreCfg) AsMapInterface() map[string]interface{} { utils.AttributeSContextCfg: cdre.AttributeSContext, utils.SynchronousCfg: cdre.Synchronous, utils.AttemptsCfg: cdre.Attempts, - utils.FieldSeparatorCfg: cdre.FieldSeparator, + utils.FieldSeparatorCfg: string(cdre.FieldSeparator), utils.FieldsCfg: fields, } } diff --git a/config/cdrecfg_test.go b/config/cdrecfg_test.go index 47c1b3520..392859709 100644 --- a/config/cdrecfg_test.go +++ b/config/cdrecfg_test.go @@ -137,3 +137,69 @@ func TestCdreCfgloadFromJsonCfg(t *testing.T) { t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(lstcfg)) } } + +func testCdre(t *testing.T) { + var cdre CdreCfg + cfgJSONStr := `{ + "cdre": { + "*default": { + "export_format": "*file_csv", + "export_path": "/var/spool/cgrates/cdre", + "filters" :[], + "tenant": "", + "synchronous": false, + "attempts": 1, + "field_separator": ",", + "attributes_context": "", + "fields": [ + {"path": "*exp.CGRID", "type": "*variable", "value": "~*req.CGRID"}, + ], + }, + }, +}` + + eMap := map[string]interface{}{ + "export_format": "*file_csv", + "export_path": "/var/spool/cgrates/cdre", + "filters": []string{}, + "tenant": "", + "synchronous": false, + "attempts": 1, + "field_separator": ",", + "attributes_context": "", + "fields": []map[string]interface{}{ + { + "attribute_id": "", + "blocker": false, + "break_on_success": false, + "cost_shift_digits": 0, + "filters": nil, + "layout": "2006-01-02T15:04:05Z07:00", + "mandatory": false, + "mask_destinationd_id": "", + "mask_length": 0, + "new_branch": false, + "padding": "", + "path": "*exp.CGRID", + "rounding_decimals": nil, + "strip": "", + "tag": "*exp.CGRID", + "timezone": "", + "type": "*variable", + "value": "~*req.CGRID", + "width": 0, + }, + }, + } + + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if cdreCfg, err := jsnCfg.CdreJsonCfgs(); err != nil { + 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) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } + +} diff --git a/config/config_test.go b/config/config_test.go index fa2849905..63f441956 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -1990,3 +1990,70 @@ func TestCheckConfigSanity(t *testing.T) { t.Errorf("Expecting: %+q received: %+q", expected, err) } } + +func TestGeneralCfg(t *testing.T) { + var gencfg GeneralCfg + cfgJSONStr := `{ + "general": { + "node_id": "", + "logger":"*syslog", + "log_level": 6, + "http_skip_tls_verify": false, + "rounding_decimals": 5, + "dbdata_encoding": "*msgpack", + "tpexport_dir": "/var/spool/cgrates/tpe", + "poster_attempts": 3, + "failed_posts_dir": "/var/spool/cgrates/failed_posts", + "failed_posts_ttl": "5s", + "default_request_type": "*rated", + "default_category": "call", + "default_tenant": "cgrates.org", + "default_timezone": "Local", + "default_caching":"*reload", + "connect_attempts": 5, + "reconnects": -1, + "connect_timeout": "1s", + "reply_timeout": "2s", + "locking_timeout": "0", + "digest_separator": ",", + "digest_equal": ":", + "rsr_separator": ";", + "max_parralel_conns": 100, + }, +}` + eMap := map[string]interface{}{ + "node_id": "", + "logger": "*syslog", + "log_level": 6, + "http_skip_tls_verify": false, + "rounding_decimals": 5, + "dbdata_encoding": "*msgpack", + "tpexport_dir": "/var/spool/cgrates/tpe", + "poster_attempts": 3, + "failed_posts_dir": "/var/spool/cgrates/failed_posts", + "failed_posts_ttl": "5s", + "default_request_type": "*rated", + "default_category": "call", + "default_tenant": "cgrates.org", + "default_timezone": "Local", + "default_caching": "*reload", + "connect_attempts": 5, + "reconnects": -1, + "connect_timeout": "1s", + "reply_timeout": "2s", + "locking_timeout": "0", + "digest_separator": ",", + "digest_equal": ":", + "rsr_separator": ";", + "max_parralel_conns": 100, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnGenCfg, err := jsnCfg.GeneralJsonCfg(); err != nil { + t.Error(err) + } else if err = gencfg.loadFromJsonCfg(jsnGenCfg); err != nil { + t.Error(err) + } else if rcv := gencfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("Expected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } +} diff --git a/config/fctemplate.go b/config/fctemplate.go index f7175df58..3085a1787 100755 --- a/config/fctemplate.go +++ b/config/fctemplate.go @@ -20,6 +20,7 @@ package config import ( "fmt" + "strings" "time" "github.com/cgrates/cgrates/utils" @@ -199,13 +200,14 @@ func (fc *FCTemplate) AsMapInterface() map[string]interface{} { for i, item := range fc.Value { values[i] = item.Rules } + asInitial := strings.Join(values, "") return map[string]interface{}{ utils.TagCfg: fc.Tag, utils.TypeCf: fc.Type, utils.PathCfg: fc.Path, utils.FiltersCfg: fc.Filters, - utils.ValueCfg: values, + utils.ValueCfg: asInitial, utils.WidthCfg: fc.Width, utils.StripCfg: fc.Strip, utils.PaddingCfg: fc.Padding, diff --git a/config/generalcfg.go b/config/generalcfg.go index 9a5ea9674..80b49a38d 100644 --- a/config/generalcfg.go +++ b/config/generalcfg.go @@ -144,17 +144,34 @@ func (gencfg *GeneralCfg) loadFromJsonCfg(jsnGeneralCfg *GeneralJsonCfg) (err er } func (gencfg *GeneralCfg) AsMapInterface() map[string]interface{} { + var lockingTimeout string = "0" + var failedPostsTTL string = "0" + var connectTimeout string = "0" + var replyTimeout string = "0" + if gencfg.LockingTimeout != 0 { + lockingTimeout = gencfg.LockingTimeout.String() + } + if gencfg.FailedPostsTTL != 0 { + failedPostsTTL = gencfg.FailedPostsTTL.String() + } + if gencfg.ConnectTimeout != 0 { + connectTimeout = gencfg.ConnectTimeout.String() + } + if gencfg.ReplyTimeout != 0 { + replyTimeout = gencfg.ReplyTimeout.String() + } + return map[string]interface{}{ utils.NodeIDCfg: gencfg.NodeID, utils.LoggerCfg: gencfg.Logger, utils.LogLevelCfg: gencfg.LogLevel, utils.HttpSkipTlsVerifyCfg: gencfg.HttpSkipTlsVerify, utils.RoundingDecimalsCfg: gencfg.RoundingDecimals, - utils.DBDataEncodingCfg: gencfg.DBDataEncoding, + utils.DBDataEncodingCfg: utils.Meta + gencfg.DBDataEncoding, utils.TpExportPathCfg: gencfg.TpExportPath, utils.PosterAttemptsCfg: gencfg.PosterAttempts, utils.FailedPostsDirCfg: gencfg.FailedPostsDir, - utils.FailedPostsTTLCfg: gencfg.FailedPostsTTL, + utils.FailedPostsTTLCfg: failedPostsTTL, utils.DefaultReqTypeCfg: gencfg.DefaultReqType, utils.DefaultCategoryCfg: gencfg.DefaultCategory, utils.DefaultTenantCfg: gencfg.DefaultTenant, @@ -162,9 +179,9 @@ func (gencfg *GeneralCfg) AsMapInterface() map[string]interface{} { utils.DefaultCachingCfg: gencfg.DefaultCaching, utils.ConnectAttemptsCfg: gencfg.ConnectAttempts, utils.ReconnectsCfg: gencfg.Reconnects, - utils.ConnectTimeoutCfg: gencfg.ConnectTimeout, - utils.ReplyTimeoutCfg: gencfg.ReplyTimeout, - utils.LockingTimeoutCfg: gencfg.LockingTimeout, + utils.ConnectTimeoutCfg: connectTimeout, + utils.ReplyTimeoutCfg: replyTimeout, + utils.LockingTimeoutCfg: lockingTimeout, utils.DigestSeparatorCfg: gencfg.DigestSeparator, utils.DigestEqualCfg: gencfg.DigestEqual, utils.RSRSepCfg: gencfg.RSRSep, diff --git a/config/rpcconn.go b/config/rpcconn.go index 1f13506e0..acf36d4e6 100644 --- a/config/rpcconn.go +++ b/config/rpcconn.go @@ -69,9 +69,9 @@ func (rC *RPCConn) AsMapInterface() map[string]interface{} { } return map[string]interface{}{ - utils.Strategy: rC.Strategy, - utils.PoolSize: rC.PoolSize, - utils.Conns: conns, + utils.StrategyCfg: rC.Strategy, + utils.PoolSize: rC.PoolSize, + utils.Conns: conns, } } @@ -104,9 +104,9 @@ func (self *RemoteHost) loadFromJsonCfg(jsnCfg *RemoteHostJson) error { func (rh *RemoteHost) AsMapInterface() map[string]interface{} { return map[string]interface{}{ - utils.Address: rh.Address, - utils.Transport: rh.Transport, - utils.Synchronous: rh.Synchronous, - utils.TLS: rh.TLS, + utils.AddressCfg: rh.Address, + utils.TransportCfg: rh.Transport, + utils.SynchronousCfg: rh.Synchronous, + utils.TLS: rh.TLS, } } diff --git a/config/rpcconn_test.go b/config/rpcconn_test.go index 6d55b49ee..3ca9386c3 100644 --- a/config/rpcconn_test.go +++ b/config/rpcconn_test.go @@ -17,3 +17,42 @@ along with this program. If not, see */ package config + +import ( + "reflect" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestRPCConns(t *testing.T) { + var cfg RPCConn + cfgJSONStr := `{ + "rpc_conns": { + "*localhost": { + "conns": [{"address": "127.0.0.1:2012", "transport":"*json"}], + }, + }, +}` + eMap := map[string]interface{}{ + "poolSize": 0, + "strategy": "", + "conns": []map[string]interface{}{ + { + "address": "127.0.0.1:2012", + "transport": "*json", + "synchronous": false, + "TLS": false, + }, + }, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnRPCCfg, err := jsnCfg.RPCConnJsonCfg(); err != nil { + t.Error(err) + } else if err = cfg.loadFromJsonCfg(jsnRPCCfg["*localhost"]); err != nil { + t.Error(err) + } else if rcv := cfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } +} diff --git a/utils/consts.go b/utils/consts.go index 8544d37bd..3ee3be45b 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1880,11 +1880,13 @@ const ( DefaultRatioCfg = "default_ratio" ReadersCfg = "readers" - PoolSize = "PoolSize" - Conns = "Conns" + PoolSize = "poolSize" + Conns = "conns" FilenameCfg = "file_name" - RequestPayloadCfg = "Request_payload" - ReplyPayloadCfg = "Reply_payload" + RequestPayloadCfg = "request_payload" + ReplyPayloadCfg = "reply_payload" + TransportCfg = "transport" + StrategyCfg = "strategy" ) // FC Template