From fa0c6b6956c8ee3a86cf83da25f9e6ac4c117523 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Fri, 11 Sep 2020 17:31:05 +0300 Subject: [PATCH] Updated cachecfg AsMapInterface method --- config/attributescfg_test.go | 12 ++--- config/cachecfg.go | 30 ++++++----- config/cachecfg_test.go | 102 +++++++++++++++++------------------ 3 files changed, 71 insertions(+), 73 deletions(-) diff --git a/config/attributescfg_test.go b/config/attributescfg_test.go index 51c4e6ed3..44132e901 100644 --- a/config/attributescfg_test.go +++ b/config/attributescfg_test.go @@ -29,12 +29,12 @@ func TestAttributeSCfgloadFromJsonCfg(t *testing.T) { if err := attscfg.loadFromJsonCfg(nil); err != nil { t.Error(err) } else if !reflect.DeepEqual(attscfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, attscfg) + t.Errorf("Expected: %+v ,received: %+v", expected, attscfg) } if err := attscfg.loadFromJsonCfg(new(AttributeSJsonCfg)); err != nil { t.Error(err) } else if !reflect.DeepEqual(attscfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, attscfg) + t.Errorf("Expected: %+v ,received: %+v", expected, attscfg) } cfgJSONStr := `{ "attributes": { // Attribute service @@ -56,7 +56,7 @@ func TestAttributeSCfgloadFromJsonCfg(t *testing.T) { } else if err = attscfg.loadFromJsonCfg(jsnAttSCfg); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, attscfg) { - t.Errorf("Expected: %+v , recived: %+v", expected, attscfg) + t.Errorf("Expected: %+v , received: %+v", expected, attscfg) } } @@ -79,7 +79,7 @@ func TestAttributeSCfgAsMapInterface(t *testing.T) { if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { t.Error(err) } else if rcv := cgrCfg.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { - t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + t.Errorf("\nExpected: %+v\n Received: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } @@ -103,7 +103,7 @@ func TestAttributeSCfgAsMapInterface2(t *testing.T) { if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { t.Error(err) } else if newMap := cgrCfg.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(expectedMap, newMap) { - t.Errorf("Expected %+v \n, recieved %+v", utils.ToJSON(expectedMap), utils.ToJSON(newMap)) + t.Errorf("Expected %+v \n, receieved %+v", utils.ToJSON(expectedMap), utils.ToJSON(newMap)) } } @@ -124,6 +124,6 @@ func TestAttributeSCfgAsMapInterface3(t *testing.T) { if conv, err := NewCGRConfigFromJsonStringWithDefaults(myJSONStr); err != nil { t.Error(err) } else if newMap := conv.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(expectedMap, newMap) { - t.Errorf("Expected %+v, recieved %+v", expectedMap, newMap) + t.Errorf("Expected %+v, receieved %+v", expectedMap, newMap) } } diff --git a/config/cachecfg.go b/config/cachecfg.go index c3a7a6699..e1cd72a43 100755 --- a/config/cachecfg.go +++ b/config/cachecfg.go @@ -65,12 +65,12 @@ func (cParam *CacheParamCfg) AsMapInterface() map[string]interface{} { if cParam.TTL != 0 { TTL = cParam.TTL.String() } - return map[string]interface{}{ utils.LimitCfg: cParam.Limit, utils.TTLCfg: TTL, utils.StaticTTLCfg: cParam.StaticTTL, utils.PrecacheCfg: cParam.Precache, + utils.ReplicateCfg: cParam.Replicate, } } @@ -127,19 +127,21 @@ func (cCfg *CacheCfg) AddTmpCaches() { } } -func (cCfg *CacheCfg) AsMapInterface() map[string]interface{} { - partitions := make(map[string]interface{}, len(cCfg.Partitions)) - for key, value := range cCfg.Partitions { - partitions[key] = value.AsMapInterface() +func (cCfg *CacheCfg) AsMapInterface() (initialMP map[string]interface{}) { + initialMP = make(map[string]interface{}) + if cCfg.Partitions != nil { + partitions := make(map[string]interface{}, len(cCfg.Partitions)) + for key, value := range cCfg.Partitions { + partitions[key] = value.AsMapInterface() + } + initialMP[utils.PartitionsCfg] = partitions } - replicationConns := make([]string, len(cCfg.ReplicationConns)) - for i, item := range cCfg.ReplicationConns { - replicationConns[i] = item + if cCfg.ReplicationConns != nil { + replicationConns := make([]string, len(cCfg.ReplicationConns)) + for i, item := range cCfg.ReplicationConns { + replicationConns[i] = item + } + initialMP[utils.RplConnsCfg] = replicationConns } - - return map[string]interface{}{ - utils.PartitionsCfg: partitions, - utils.RplConnsCfg: replicationConns, - } - + return } diff --git a/config/cachecfg_test.go b/config/cachecfg_test.go index 6d8dd7ba6..f03128c9e 100644 --- a/config/cachecfg_test.go +++ b/config/cachecfg_test.go @@ -57,12 +57,12 @@ func TestCacheCfgloadFromJsonCfg(t *testing.T) { if err := cachecfg.loadFromJsonCfg(nil); err != nil { t.Error(err) } else if !reflect.DeepEqual(cachecfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, cachecfg) + t.Errorf("Expected: %+v ,received: %+v", expected, cachecfg) } if err := cachecfg.loadFromJsonCfg(new(CacheJsonCfg)); err != nil { t.Error(err) } else if !reflect.DeepEqual(cachecfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, cachecfg) + t.Errorf("Expected: %+v ,received: %+v", expected, cachecfg) } cfgJSONStr := `{ "caches":{ @@ -89,7 +89,7 @@ func TestCacheCfgloadFromJsonCfg(t *testing.T) { } else if err = cachecfg.loadFromJsonCfg(jsnCacheCfg); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, cachecfg) { - t.Errorf("Expected: %+v , recived: %+v", expected, cachecfg) + t.Errorf("Expected: %+v , received: %+v", expected, cachecfg) } } @@ -98,12 +98,12 @@ func TestCacheParamCfgloadFromJsonCfg(t *testing.T) { if err := fscocfg.loadFromJsonCfg(nil); err != nil { t.Error(err) } else if !reflect.DeepEqual(fscocfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, fscocfg) + t.Errorf("Expected: %+v ,received: %+v", expected, fscocfg) } if err := fscocfg.loadFromJsonCfg(new(CacheParamJsonCfg)); err != nil { t.Error(err) } else if !reflect.DeepEqual(fscocfg, expected) { - t.Errorf("Expected: %+v ,recived: %+v", expected, fscocfg) + t.Errorf("Expected: %+v ,received: %+v", expected, fscocfg) } json := &CacheParamJsonCfg{ Limit: utils.IntPointer(5), @@ -120,66 +120,62 @@ func TestCacheParamCfgloadFromJsonCfg(t *testing.T) { if err = fscocfg.loadFromJsonCfg(json); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, fscocfg) { - t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(fscocfg)) + t.Errorf("Expected: %+v , received: %+v", utils.ToJSON(expected), utils.ToJSON(fscocfg)) } } -func TestCacheCfgAsMapInterface(t *testing.T) { - var cachecfg *CacheCfg +func TestCachesCfgAsMapInterface1(t *testing.T) { cfgJSONStr := `{ "caches":{ "partitions": { - "*destinations": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, - "*reverse_destinations": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, - "*rating_plans": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, + "*destinations": {"limit": 10000, "ttl": "", "static_ttl": false, "precache": true, "replicate": true}, }, }, }` eMap := map[string]interface{}{ - "partitions": map[string]interface{}{ - "*destinations": map[string]interface{}{"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, - "*reverse_destinations": map[string]interface{}{"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, - "*rating_plans": map[string]interface{}{"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, + utils.PartitionsCfg: map[string]interface{}{ + utils.MetaDestinations: map[string]interface{}{"limit": 10000, "ttl": "", "static_ttl": false, "precache": true, "replicate": true}, }, - "replication_conns": []string{}, + utils.ReplicationConnsCfg: []string{}, } - cachecfg = new(CacheCfg) - cachecfg.Partitions = make(map[string]*CacheParamCfg) - if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { t.Error(err) - } else if jsnCacheCfg, err := jsnCfg.CacheJsonCfg(); err != nil { - t.Error(err) - } else if err = cachecfg.loadFromJsonCfg(jsnCacheCfg); err != nil { - t.Error(err) - } else if rcv := cachecfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { - t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) - } - cfgJSONStr = `{ -"caches":{ - "partitions": { - "*destinations": {"limit": -1, "ttl": "8m", "static_ttl": false, "precache": false}, - "*reverse_destinations": {"limit": -1, "ttl": "1m", "static_ttl": false, "precache": false}, - "*rating_plans": {"limit": 10, "ttl": "", "static_ttl": true, "precache": true}, - }, - }, -}` - eMap = map[string]interface{}{ - "partitions": map[string]interface{}{ - "*destinations": map[string]interface{}{"limit": -1, "ttl": "8m0s", "static_ttl": false, "precache": false}, - "*reverse_destinations": map[string]interface{}{"limit": -1, "ttl": "1m0s", "static_ttl": false, "precache": false}, - "*rating_plans": map[string]interface{}{"limit": 10, "ttl": "", "static_ttl": true, "precache": true}, - }, - "replication_conns": []string{}, - } - cachecfg = new(CacheCfg) - cachecfg.Partitions = make(map[string]*CacheParamCfg) - if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { - t.Error(err) - } else if jsnCacheCfg, err := jsnCfg.CacheJsonCfg(); err != nil { - t.Error(err) - } else if err = cachecfg.loadFromJsonCfg(jsnCacheCfg); err != nil { - t.Error(err) - } else if rcv := cachecfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { - t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } else { + newMap := cgrCfg.cacheCfg.AsMapInterface() + if !reflect.DeepEqual(newMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaDestinations], + eMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaDestinations]) { + t.Errorf("Expected %+v, received %+v", eMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaDestinations], + newMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaDestinations]) + } + } +} + +func TestCachesCfgAsMapInterface2(t *testing.T) { + cfgJSONStr := `{ +"caches":{ + "partitions": { + "*rating_plans": {"limit": 10, "ttl": "", "static_ttl": true, "precache": true, "replicate": false}, + }, + "replication_conns": ["conn1", "conn2"], + }, +}` + eMap := map[string]interface{}{ + utils.PartitionsCfg: map[string]interface{}{ + utils.MetaRatingPlans: map[string]interface{}{"limit": 10, "ttl": "", "static_ttl": true, "precache": true}, + }, + utils.ReplicationConnsCfg: []string{"conn1", "conn2"}, + } + if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { + t.Error(err) + } else { + newMap := cgrCfg.cacheCfg.AsMapInterface() + if !reflect.DeepEqual(newMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaRatingPlans], + newMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaRatingPlans]) { + t.Errorf("Expected %+v, received %+v", eMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaRatingPlans], + eMap[utils.PartitionsCfg].(map[string]interface{})[utils.MetaRatingPlans]) + } + if !reflect.DeepEqual(newMap[utils.ReplicationConnsCfg], eMap[utils.ReplicationConnsCfg]) { + t.Errorf("Expected %+v, received %+v", eMap[utils.ReplicationConnsCfg], newMap[utils.ReplicationConnsCfg]) + } } }