From 6a73a7cf2c97d898c8af94fe0d21a583abcc92d9 Mon Sep 17 00:00:00 2001 From: TeoV Date: Thu, 2 Aug 2018 07:53:28 -0400 Subject: [PATCH] Add attribute test(Substitute take value from Header) --- apier/v1/attributes_it_test.go | 61 ++++++++++++++++++++++++++++ engine/attributes_test.go | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/apier/v1/attributes_it_test.go b/apier/v1/attributes_it_test.go index a7bcec469..c16d76457 100644 --- a/apier/v1/attributes_it_test.go +++ b/apier/v1/attributes_it_test.go @@ -57,6 +57,7 @@ var sTestsAlsPrf = []func(t *testing.T){ testAttributeSProcessEventWithNoneSubstitute, testAttributeSProcessEventWithNoneSubstitute2, testAttributeSProcessEventWithNoneSubstitute3, + testAttributeSProcessEventWithHeader, testAttributeSGetAlsPrfBeforeSet, testAttributeSSetAlsPrf, testAttributeSUpdateAlsPrf, @@ -553,6 +554,66 @@ func testAttributeSProcessEventWithNoneSubstitute3(t *testing.T) { } } +func testAttributeSProcessEventWithHeader(t *testing.T) { + attrPrf1 := &engine.AttributeProfile{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "ATTR_Header", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:Field1:Value1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*engine.Attribute{ + &engine.Attribute{ + FieldName: "Field2", + Initial: utils.META_ANY, + Substitute: utils.NewRSRParsersMustCompile("~Field1", true), + Append: true, + }, + }, + Blocker: true, + Weight: 10, + } + var result string + if err := attrSRPC.Call("ApierV1.SetAttributeProfile", attrPrf1, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + attrArgs := &engine.AttrArgsProcessEvent{ + ProcessRuns: utils.IntPointer(1), + CGREvent: utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "HeaderEventForAttribute", + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Field1": "Value1", + }, + }, + } + eRply := &engine.AttrSProcessEventReply{ + MatchedProfiles: []string{"ATTR_Header"}, + AlteredFields: []string{"Field2"}, + CGREvent: &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "HeaderEventForAttribute", + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Field1": "Value1", + "Field2": "Value1", + }, + }, + } + var rplyEv engine.AttrSProcessEventReply + if err := attrSRPC.Call(utils.AttributeSv1ProcessEvent, + attrArgs, &rplyEv); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eRply, &rplyEv) { + t.Errorf("Expecting: %s, received: %s", + utils.ToJSON(eRply), utils.ToJSON(rplyEv)) + } +} + func testAttributeSSetAlsPrf(t *testing.T) { alsPrf = &engine.AttributeProfile{ Tenant: "cgrates.org", diff --git a/engine/attributes_test.go b/engine/attributes_test.go index b7b27e9de..dabd7b289 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -1195,3 +1195,75 @@ func TestAttributeMultipleProcessWithBlocker2(t *testing.T) { t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event) } } + +func TestAttributeProcessSubstitute(t *testing.T) { + //refresh the DM + if err := dmAtr.DataDB().Flush(""); err != nil { + t.Error(err) + } + if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil { + t.Error(err) + } else if test != true { + t.Errorf("\nExpecting: true got :%+v", test) + } + attrPrf1 := &AttributeProfile{ + Tenant: config.CgrConfig().DefaultTenant, + ID: "ATTR_1", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:Field1:Value1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*Attribute{ + &Attribute{ + FieldName: "Field2", + Initial: utils.META_ANY, + Substitute: utils.NewRSRParsersMustCompile("~Field1", true), + Append: true, + }, + }, + Blocker: true, + Weight: 10, + } + // Add attribute in DM + if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil { + t.Error(err) + } + attrArgs := &AttrArgsProcessEvent{ + ProcessRuns: utils.IntPointer(1), + CGREvent: utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: utils.GenUUID(), + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Field1": "Value1", + }, + }, + } + eRply := &AttrSProcessEventReply{ + MatchedProfiles: []string{"ATTR_1"}, + AlteredFields: []string{"Field2"}, + CGREvent: &utils.CGREvent{ + Tenant: config.CgrConfig().DefaultTenant, + ID: utils.GenUUID(), + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Field1": "Value1", + "Field2": "Value1", + }, + }, + } + var reply AttrSProcessEventReply + if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) { + t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles) + } + if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) { + t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields) + } + if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) { + t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event) + } +}