mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Reactive unit tests in diamagent_test.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
9f0be53c26
commit
f0127e2a77
@@ -18,7 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package agents
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/rpcclient"
|
||||
|
||||
"github.com/cgrates/cgrates/sessions"
|
||||
)
|
||||
@@ -27,7 +34,6 @@ func TestDAsSessionSClientIface(t *testing.T) {
|
||||
_ = sessions.BiRPClient(new(DiameterAgent))
|
||||
}
|
||||
|
||||
/*
|
||||
type testMockSessionConn struct {
|
||||
calls map[string]func(arg interface{}, rply interface{}) error
|
||||
}
|
||||
@@ -40,10 +46,19 @@ func (s *testMockSessionConn) Call(method string, arg interface{}, rply interfac
|
||||
}
|
||||
}
|
||||
|
||||
func (s *testMockSessionConn) CallBiRPC(_ rpcclient.ClientConnector, method string, arg interface{}, rply interface{}) error {
|
||||
if call, has := s.calls[method]; !has {
|
||||
return rpcclient.ErrUnsupporteServiceMethod
|
||||
} else {
|
||||
return call(arg, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessRequest(t *testing.T) {
|
||||
data := engine.NewInternalDB(nil, nil)
|
||||
dfltCfg, _ := config.NewDefaultCGRConfig()
|
||||
data := engine.NewInternalDB(nil, nil, true, dfltCfg.DataDbCfg().Items)
|
||||
dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
filters := engine.NewFilterS(config.CgrConfig(), nil, nil, nil, dm) // no need for filterS but stiil try to configure the dm :D
|
||||
filters := engine.NewFilterS(config.CgrConfig(), nil, dm) // no need for filterS but still try to configure the dm :D
|
||||
|
||||
cgrRplyNM := config.NewNavigableMap(nil)
|
||||
rply := config.NewNavigableMap(nil)
|
||||
@@ -56,19 +71,37 @@ func TestProcessRequest(t *testing.T) {
|
||||
reqProcessor := &config.RequestProcessor{
|
||||
ID: "Default",
|
||||
Tenant: config.NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP),
|
||||
Filters: []string{}, // empty for momment
|
||||
Filters: []string{},
|
||||
RequestFields: []*config.FCTemplate{
|
||||
&config.FCTemplate{Tag: utils.ToR, Type: utils.META_CONSTANT, Path: utils.ToR, Value: config.NewRSRParsersMustCompile(utils.VOICE, true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: utils.OriginID, Type: utils.META_COMPOSED, Path: utils.OriginID, Value: config.NewRSRParsersMustCompile("~*req.SessionId", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.OriginHost, Type: utils.MetaRemoteHost, Path: utils.OriginHost, Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Category, Type: utils.META_CONSTANT, Path: utils.Category, Value: config.NewRSRParsersMustCompile(utils.CALL, true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: utils.Account, Type: utils.META_COMPOSED, Path: utils.Account, Value: config.NewRSRParsersMustCompile("~*req.Account", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Destination, Type: utils.META_COMPOSED, Path: utils.Destination, Value: config.NewRSRParsersMustCompile("~*req.Destination", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Usage, Type: utils.META_COMPOSED, Path: utils.Usage, Value: config.NewRSRParsersMustCompile("~*req.Usage", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.ToR,
|
||||
Type: utils.META_CONSTANT, Path: utils.MetaCgreq + utils.NestingSep + utils.ToR,
|
||||
Value: config.NewRSRParsersMustCompile(utils.VOICE, true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: utils.OriginID,
|
||||
Type: utils.META_COMPOSED, Path: utils.MetaCgreq + utils.NestingSep + utils.OriginID,
|
||||
Value: config.NewRSRParsersMustCompile("~*req.SessionId", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.OriginHost,
|
||||
Type: utils.MetaRemoteHost, Path: utils.MetaCgreq + utils.NestingSep + utils.OriginHost, Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Category,
|
||||
Type: utils.META_CONSTANT, Path: utils.MetaCgreq + utils.NestingSep + utils.Category,
|
||||
Value: config.NewRSRParsersMustCompile(utils.CALL, true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: utils.Account,
|
||||
Type: utils.META_COMPOSED, Path: utils.MetaCgreq + utils.NestingSep + utils.Account,
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Account", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Destination,
|
||||
Type: utils.META_COMPOSED, Path: utils.MetaCgreq + utils.NestingSep + utils.Destination,
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Destination", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: utils.Usage,
|
||||
Type: utils.META_COMPOSED, Path: utils.MetaCgreq + utils.NestingSep + utils.Usage,
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
},
|
||||
ReplyFields: []*config.FCTemplate{
|
||||
&config.FCTemplate{Tag: "ResultCode", Type: utils.META_CONSTANT, Path: "ResultCode", Value: config.NewRSRParsersMustCompile("2001", true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: "GrantedUnits", Type: utils.META_COMPOSED, Path: "GrantedUnits", Value: config.NewRSRParsersMustCompile("~*cgrep.MaxUsage{*duration_seconds}", true, utils.INFIELD_SEP), Mandatory: true},
|
||||
&config.FCTemplate{Tag: "ResultCode",
|
||||
Type: utils.META_CONSTANT, Path: utils.MetaRep + utils.NestingSep + "ResultCode",
|
||||
Value: config.NewRSRParsersMustCompile("2001", true, utils.INFIELD_SEP)},
|
||||
&config.FCTemplate{Tag: "GrantedUnits",
|
||||
Type: utils.MetaVariable, Path: utils.MetaRep + utils.NestingSep + "Granted-Service-Unit.CC-Time",
|
||||
Value: config.NewRSRParsersMustCompile("~*cgrep.MaxUsage{*duration_seconds}", true, utils.INFIELD_SEP),
|
||||
Mandatory: true},
|
||||
},
|
||||
}
|
||||
reqVars := map[string]interface{}{
|
||||
@@ -81,6 +114,9 @@ func TestProcessRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
sS := &testMockSessionConn{calls: map[string]func(arg interface{}, rply interface{}) error{
|
||||
utils.SessionSv1RegisterInternalBiJSONConn: func(arg interface{}, rply interface{}) error {
|
||||
return nil
|
||||
},
|
||||
utils.SessionSv1AuthorizeEvent: func(arg interface{}, rply interface{}) error {
|
||||
var tm *time.Time
|
||||
var id string
|
||||
@@ -118,30 +154,10 @@ func TestProcessRequest(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
*prply = sessions.V1AuthorizeReply{
|
||||
MaxUsage: time.Duration(-1),
|
||||
MaxUsage: utils.DurationPointer(time.Duration(-1)),
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaAuth, utils.MetaAccounts})
|
||||
agReq := NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters)
|
||||
da := &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
sS: sS,
|
||||
}
|
||||
pr, err := da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
sS = &testMockSessionConn{calls: map[string]func(arg interface{}, rply interface{}) error{
|
||||
utils.SessionSv1InitiateSession: func(arg interface{}, rply interface{}) error {
|
||||
var tm *time.Time
|
||||
var id string
|
||||
@@ -202,38 +218,10 @@ func TestProcessRequest(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
MaxUsage: 10 * time.Second,
|
||||
MaxUsage: utils.DurationPointer(10 * time.Second),
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaInitiate, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters)
|
||||
da = &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
sS: sS,
|
||||
}
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaUpdate, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
sS = &testMockSessionConn{calls: map[string]func(arg interface{}, rply interface{}) error{
|
||||
utils.SessionSv1UpdateSession: func(arg interface{}, rply interface{}) error {
|
||||
var tm *time.Time
|
||||
var id string
|
||||
@@ -294,35 +282,10 @@ func TestProcessRequest(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
MaxUsage: 10 * time.Second,
|
||||
MaxUsage: utils.DurationPointer(10 * time.Second),
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters)
|
||||
da = &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
sS: sS,
|
||||
}
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaTerminate, utils.MetaAccounts, utils.MetaAttributes, utils.MetaCDRs})
|
||||
reqProcessor.ReplyFields = []*config.FCTemplate{&config.FCTemplate{Tag: "ResultCode", Type: utils.META_CONSTANT, Path: "ResultCode", Value: config.NewRSRParsersMustCompile("2001", true, utils.INFIELD_SEP)}}
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
sS = &testMockSessionConn{calls: map[string]func(arg interface{}, rply interface{}) error{
|
||||
utils.SessionSv1ProcessCDR: func(arg interface{}, rply interface{}) error {
|
||||
var tm *time.Time
|
||||
var id string
|
||||
@@ -400,30 +363,6 @@ func TestProcessRequest(t *testing.T) {
|
||||
*prply = utils.OK
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters)
|
||||
da = &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
sS: sS,
|
||||
}
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 1 {
|
||||
t.Errorf("Expected the reply to have one value received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaMessage, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
sS = &testMockSessionConn{calls: map[string]func(arg interface{}, rply interface{}) error{
|
||||
utils.SessionSv1ProcessMessage: func(arg interface{}, rply interface{}) error {
|
||||
var tm *time.Time
|
||||
var id string
|
||||
@@ -484,20 +423,80 @@ func TestProcessRequest(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
MaxUsage: 10 * time.Second,
|
||||
MaxUsage: utils.DurationPointer(10 * time.Second),
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaAuth, utils.MetaAccounts})
|
||||
agReq := NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters, nil, nil)
|
||||
|
||||
internalSessionSChan := make(chan rpcclient.ClientConnector, 1)
|
||||
internalSessionSChan <- sS
|
||||
connMgr := engine.NewConnManager(config.CgrConfig(), map[string]chan rpcclient.ClientConnector{
|
||||
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaSessionS): internalSessionSChan,
|
||||
})
|
||||
da := &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
connMgr: connMgr,
|
||||
}
|
||||
pr, err := da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaInitiate, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters)
|
||||
da = &DiameterAgent{
|
||||
cgrCfg: config.CgrConfig(),
|
||||
filterS: filters,
|
||||
sS: sS,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters, nil, nil)
|
||||
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaUpdate, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters, nil, nil)
|
||||
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 2 {
|
||||
t.Errorf("Expected the reply to have 2 values received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaTerminate, utils.MetaAccounts, utils.MetaAttributes, utils.MetaCDRs})
|
||||
reqProcessor.ReplyFields = []*config.FCTemplate{&config.FCTemplate{Tag: "ResultCode",
|
||||
Type: utils.META_CONSTANT, Path: utils.MetaRep + utils.NestingSep + "ResultCode",
|
||||
Value: config.NewRSRParsersMustCompile("2001", true, utils.INFIELD_SEP)}}
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters, nil, nil)
|
||||
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -506,5 +505,22 @@ func TestProcessRequest(t *testing.T) {
|
||||
} else if len(rply.Values()) != 1 {
|
||||
t.Errorf("Expected the reply to have one value received: %s", rply.String())
|
||||
}
|
||||
|
||||
reqProcessor.Flags, _ = utils.FlagsWithParamsFromSlice([]string{utils.MetaMessage, utils.MetaAccounts, utils.MetaAttributes})
|
||||
cgrRplyNM = config.NewNavigableMap(nil)
|
||||
rply = config.NewNavigableMap(nil)
|
||||
|
||||
agReq = NewAgentRequest(diamDP, reqVars, cgrRplyNM, rply,
|
||||
reqProcessor.Tenant, config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
config.CgrConfig().GeneralCfg().DefaultTimezone, filters, nil, nil)
|
||||
|
||||
pr, err = da.processRequest(reqProcessor, agReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if !pr {
|
||||
t.Errorf("Expected the request to be processed")
|
||||
} else if len(rply.Values()) != 1 {
|
||||
t.Errorf("Expected the reply to have one value received: %s", rply.String())
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user