Updated AsMapInterface method for CacheCfg/Added test

This commit is contained in:
adragusin
2020-04-29 17:53:30 +03:00
committed by Dan Christian Bogos
parent e1274ec16e
commit 4a0277ba53
2 changed files with 74 additions and 5 deletions

View File

@@ -61,11 +61,16 @@ func (cParam *CacheParamCfg) loadFromJsonCfg(jsnCfg *CacheParamJsonCfg) error {
}
func (cParam *CacheParamCfg) AsMapInterface() map[string]interface{} {
var TTL string = ""
if cParam.TTL != 0 {
TTL = cParam.TTL.String()
}
return map[string]interface{}{
utils.Limit: cParam.Limit,
utils.TTL: cParam.TTL,
utils.StaticTTL: cParam.StaticTTL,
utils.Precache: cParam.Precache,
utils.LimitCfg: cParam.Limit,
utils.TTLCfg: TTL,
utils.StaticTTLCfg: cParam.StaticTTL,
utils.PrecacheCfg: cParam.Precache,
}
}
@@ -127,10 +132,14 @@ func (cCfg *CacheCfg) AsMapInterface() map[string]interface{} {
for key, value := range cCfg.Partitions {
partitions[key] = value.AsMapInterface()
}
replicationConns := make([]string, len(cCfg.ReplicationConns))
for i, item := range cCfg.ReplicationConns {
replicationConns[i] = item
}
return map[string]interface{}{
utils.PartitionsCfg: partitions,
utils.RplConnsCfg: cCfg.ReplicationConns,
utils.RplConnsCfg: replicationConns,
}
}

View File

@@ -123,3 +123,63 @@ func TestCacheParamCfgloadFromJsonCfg(t *testing.T) {
t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(fscocfg))
}
}
func TestCacheCfgAsMapInterface(t *testing.T) {
var cachecfg *CacheCfg
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},
},
},
}`
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},
},
"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))
}
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))
}
}