diff --git a/engine/attributes_test.go b/engine/attributes_test.go index 7de3554fc..7b1961669 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -1498,106 +1498,3 @@ 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)) - } -} diff --git a/engine/z_attributes_test.go b/engine/z_attributes_test.go index 2f716f9b5..b64812623 100644 --- a/engine/z_attributes_test.go +++ b/engine/z_attributes_test.go @@ -3003,3 +3003,106 @@ func TestAttributeMetaTenant(t *testing.T) { t.Errorf("Expecting %s, received: %s", utils.ToJSON(eRply), utils.ToJSON(reply)) } } + +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.CompanyName", "*req.Password"}, + 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 sort.Strings(reply.AlteredFields); !reflect.DeepEqual(expReply, reply) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expReply), utils.ToJSON(reply)) + } +}