Added tests for SessionSCfg.loadFromJsonCfg

This commit is contained in:
Trial97
2018-10-25 16:10:29 +03:00
committed by Dan Christian Bogos
parent f9f4f8df26
commit 97c6924c91
3 changed files with 83 additions and 16 deletions

View File

@@ -145,8 +145,8 @@ func NewDefaultCGRConfig() (*CGRConfig, error) {
cfg.CdreProfiles = make(map[string]*CdreCfg)
cfg.CdrcProfiles = make(map[string][]*CdrcCfg)
cfg.analyzerSCfg = new(AnalyzerSCfg)
cfg.sessionSCfg = new(SessionSCfg)
cfg.fsAgentCfg = new(FsAgentConfig)
cfg.kamAgentCfg = new(KamAgentCfg)
cfg.SmOsipsConfig = new(SmOsipsConfig)
@@ -260,7 +260,6 @@ type CGRConfig struct {
CdrcProfiles map[string][]*CdrcCfg // Number of CDRC instances running imports, format map[dirPath][]{Configs}
loaderCfg []*LoaderSCfg // LoaderS configurations
sessionSCfg *SessionSCfg
fsAgentCfg *FsAgentConfig // FreeSWITCHAgent configuration
kamAgentCfg *KamAgentCfg // KamailioAgent Configuration
SmOsipsConfig *SmOsipsConfig // SMOpenSIPS Configuration
@@ -305,6 +304,7 @@ type CGRConfig struct {
schedulerCfg *SchedulerCfg // Scheduler config
cdrsCfg *CdrsCfg // Cdrs config
cdrStatsCfg *CdrStatsCfg // CdrStats config - deprecated
sessionSCfg *SessionSCfg // SessionS config
analyzerSCfg *AnalyzerSCfg
}
@@ -819,6 +819,9 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) {
if err != nil {
return err
}
if err := self.sessionSCfg.loadFromJsonCfg(jsnsessionSCfg); err != nil {
return err
}
jsnSmFsCfg, err := jsnCfg.FreeswitchAgentJsonCfg()
if err != nil {
@@ -1005,11 +1008,6 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) {
}
}
if jsnsessionSCfg != nil {
if err := self.sessionSCfg.loadFromJsonCfg(jsnsessionSCfg); err != nil {
return err
}
}
if jsnSmFsCfg != nil {
if err := self.fsAgentCfg.loadFromJsonCfg(jsnSmFsCfg); err != nil {
return err

View File

@@ -122,11 +122,10 @@ type SessionSCfg struct {
ChannelSyncInterval time.Duration
}
func (self *SessionSCfg) loadFromJsonCfg(jsnCfg *SessionSJsonCfg) error {
func (self *SessionSCfg) loadFromJsonCfg(jsnCfg *SessionSJsonCfg) (err error) {
if jsnCfg == nil {
return nil
}
var err error
if jsnCfg.Enabled != nil {
self.Enabled = *jsnCfg.Enabled
}

View File

@@ -18,23 +18,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"github.com/cgrates/cgrates/utils"
"reflect"
"strings"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
func TesFsAgentConfigLoadFromJsonCfg(t *testing.T) {
func TestFsAgentConfigLoadFromJsonCfg(t *testing.T) {
fsAgentJsnCfg := &FreeswitchAgentJsonCfg{
Enabled: utils.BoolPointer(true),
Create_cdr: utils.BoolPointer(true),
Subscribe_park: utils.BoolPointer(true),
Event_socket_conns: &[]*FsConnJsonCfg{
&FsConnJsonCfg{
{
Address: utils.StringPointer("1.2.3.4:8021"),
Password: utils.StringPointer("ClueCon"),
Reconnects: utils.IntPointer(5),
},
&FsConnJsonCfg{
{
Address: utils.StringPointer("2.3.4.5:8021"),
Password: utils.StringPointer("ClueCon"),
Reconnects: utils.IntPointer(5),
@@ -46,14 +49,81 @@ func TesFsAgentConfigLoadFromJsonCfg(t *testing.T) {
CreateCdr: true,
SubscribePark: true,
EventSocketConns: []*FsConnConfig{
&FsConnConfig{Address: "1.2.3.4:8021", Password: "ClueCon", Reconnects: 5},
&FsConnConfig{Address: "1.2.3.4:8021", Password: "ClueCon", Reconnects: 5},
{Address: "1.2.3.4:8021", Password: "ClueCon", Reconnects: 5, Alias: "1.2.3.4:8021"},
{Address: "2.3.4.5:8021", Password: "ClueCon", Reconnects: 5, Alias: "2.3.4.5:8021"},
},
}
fsAgentCfg := new(FsAgentConfig)
if err := fsAgentCfg.loadFromJsonCfg(fsAgentJsnCfg); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eFsAgentConfig, fsAgentCfg) {
t.Error("Received: ", fsAgentCfg)
t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(eFsAgentConfig), utils.ToJSON(fsAgentCfg))
}
}
func TestSessionSCfgloadFromJsonCfg(t *testing.T) {
var sescfg, expected SessionSCfg
if err := sescfg.loadFromJsonCfg(nil); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(sescfg, expected) {
t.Errorf("Expected: %+v ,recived: %+v", expected, sescfg)
}
if err := sescfg.loadFromJsonCfg(new(SessionSJsonCfg)); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(sescfg, expected) {
t.Errorf("Expected: %+v ,recived: %+v", expected, sescfg)
}
cfgJSONStr := `{
"sessions": {
"enabled": false, // starts session manager service: <true|false>
"listen_bijson": "127.0.0.1:2014", // address where to listen for bidirectional JSON-RPC requests
"chargers_conns": [], // address where to reach the charger service, empty to disable charger functionality: <""|*internal|x.y.z.y:1234>
"rals_conns": [
{"address": "*internal"} // address where to reach the RALs <""|*internal|127.0.0.1:2013>
],
"cdrs_conns": [
{"address": "*internal"} // address where to reach CDR Server, empty to disable CDR capturing <*internal|x.y.z.y:1234>
],
"resources_conns": [], // address where to reach the ResourceS <""|*internal|127.0.0.1:2013>
"thresholds_conns": [], // address where to reach the ThresholdS <""|*internal|127.0.0.1:2013>
"stats_conns": [], // address where to reach the StatS <""|*internal|127.0.0.1:2013>
"suppliers_conns": [], // address where to reach the SupplierS <""|*internal|127.0.0.1:2013>
"attributes_conns": [], // address where to reach the AttributeS <""|*internal|127.0.0.1:2013>
"session_replication_conns": [], // replicate sessions towards these session services
"debit_interval": "0s", // interval to perform debits on.
"min_call_duration": "0s", // only authorize calls with allowed duration higher than this
"max_call_duration": "3h", // maximum call duration a prepaid call can last
"session_ttl": "0s", // time after a session with no updates is terminated, not defined by default
//"session_ttl_max_delay": "", // activates session_ttl randomization and limits the maximum possible delay
//"session_ttl_last_used": "", // tweak LastUsed for sessions timing-out, not defined by default
//"session_ttl_usage": "", // tweak Usage for sessions timing-out, not defined by default
"session_indexes": [], // index sessions based on these fields for GetActiveSessions API
"client_protocol": 1.0, // version of protocol to use when acting as JSON-PRC client <"0","1.0">
"channel_sync_interval": "0", // sync channels regularly (0 to disable sync session)
},
}`
expected = SessionSCfg{
ListenBijson: "127.0.0.1:2014",
ChargerSConns: []*HaPoolConfig{},
RALsConns: []*HaPoolConfig{{Address: "*internal"}},
ResSConns: []*HaPoolConfig{},
ThreshSConns: []*HaPoolConfig{},
StatSConns: []*HaPoolConfig{},
SupplSConns: []*HaPoolConfig{},
AttrSConns: []*HaPoolConfig{},
CDRsConns: []*HaPoolConfig{{Address: "*internal"}},
SessionReplicationConns: []*HaPoolConfig{},
MaxCallDuration: time.Duration(3 * time.Hour),
SessionIndexes: map[string]bool{},
ClientProtocol: 1,
}
if jsnCfg, err := NewCgrJsonCfgFromReader(strings.NewReader(cfgJSONStr)); err != nil {
t.Error(err)
} else if jsnSchCfg, err := jsnCfg.SessionSJsonCfg(); err != nil {
t.Error(err)
} else if err = sescfg.loadFromJsonCfg(jsnSchCfg); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, sescfg) {
t.Errorf("Expected: %+v , recived: %+v", expected, sescfg)
}
}