Tested ProcessEvent on attributes by matching ProcessRuns

This commit is contained in:
porosnicuadrian
2021-08-19 17:59:37 +03:00
committed by Dan Christian Bogos
parent 20fe4bab43
commit c3f155216d

View File

@@ -1498,3 +1498,109 @@ func TestAttributesV1ProcessEventMultipleRuns2(t *testing.T) {
}
}
}
func TestAttributesPorcessEventMatchingProcessRuns(t *testing.T) {
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
cfg.AttributeSCfg().Enabled = true
cfg.AttributeSCfg().IndexedSelects = false
db := NewInternalDB(nil, nil, true)
dm := NewDataManager(db, cfg.CacheCfg(), nil)
fltrS := NewFilterS(cfg, nil, dm)
fltr := &Filter{
Tenant: "cgrates.org",
ID: "Process_Runs_Fltr",
Rules: []*FilterRule{
/*
{
Type: utils.MetaString,
Element: "~*req.Account",
Values: []string{"pc_test"},
},
*/
{
Type: utils.MetaGreaterThan,
Element: "~*vars.*processRuns",
Values: []string{"1"},
},
},
}
if err := dm.SetFilter(fltr, true); err != nil {
t.Error(err)
}
attrPfr := &AttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_ProcessRuns",
Contexts: []string{"*any"},
FilterIDs: []string{"Process_Runs_Fltr"},
Attributes: []*Attribute{
{
Path: "*req.CompanyName",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("ITSYS COMMUNICATIONS SRL", utils.InfieldSep),
},
},
Weight: 20,
}
// this I'll match first, no fltr and processRuns will be 1
attrPfr2 := &AttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_MatchSecond",
Contexts: []string{"*any"},
Attributes: []*Attribute{
{
Path: "*req.Password",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("CGRateS.org", utils.InfieldSep),
},
},
Weight: 10,
}
attrPfr.Compile()
fltr.Compile()
attrPfr2.Compile()
if err := dm.SetAttributeProfile(attrPfr, true); err != nil {
t.Error(err)
}
if err := dm.SetAttributeProfile(attrPfr2, true); err != nil {
t.Error(err)
}
attr := NewAttributeService(dm, fltrS, cfg)
args := &AttrArgsProcessEvent{
ProcessRuns: utils.IntPointer(2),
Context: utils.StringPointer(utils.MetaAny),
CGREvent: &utils.CGREvent{
Event: map[string]interface{}{
"Account": "pc_test",
"CompanyName": "MY_company_will_be_changed",
},
},
}
reply := &AttrSProcessEventReply{}
expReply := &AttrSProcessEventReply{
MatchedProfiles: []string{"cgrates.org:ATTR_MatchSecond", "cgrates.org:ATTR_ProcessRuns"},
AlteredFields: []string{"*req.Password", "*req.CompanyName"},
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
Event: map[string]interface{}{
"Account": "pc_test",
"CompanyName": "ITSYS COMMUNICATIONS SRL",
"Password": "CGRateS.org",
},
APIOpts: map[string]interface{}{},
},
}
if err := attr.V1ProcessEvent(args, reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expReply, reply){
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expReply), utils.ToJSON(reply))
}
}