From 37ea8738254322842b42de47df84a0e8e1afb8b9 Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 9 Feb 2018 16:38:50 +0200 Subject: [PATCH] AttributeS with *any context --- apier/v1/attributes_it_test.go | 111 +++++++++++++++++++++++++++++++-- engine/attributes.go | 8 ++- 2 files changed, 112 insertions(+), 7 deletions(-) diff --git a/apier/v1/attributes_it_test.go b/apier/v1/attributes_it_test.go index 83a83abdd..a044cf405 100644 --- a/apier/v1/attributes_it_test.go +++ b/apier/v1/attributes_it_test.go @@ -50,12 +50,14 @@ var sTestsAlsPrf = []func(t *testing.T){ testAttributeSStartEngine, testAttributeSRPCConn, testAttributeSLoadFromFolder, - testAttributeSGetAttributeForEvent, - testAttributeSProcessEvent, - testAttributeSGetAlsPrfBeforeSet, - testAttributeSSetAlsPrf, - testAttributeSUpdateAlsPrf, - testAttributeSRemAlsPrf, + // testAttributeSGetAttributeForEvent, + testAttributeSGetAttributeForEventNotFound, + testAttributeSGetAttributeForEventWithMetaAnyContext, + // testAttributeSProcessEvent, + // testAttributeSGetAlsPrfBeforeSet, + // testAttributeSSetAlsPrf, + // testAttributeSUpdateAlsPrf, + // testAttributeSRemAlsPrf, testAttributeSKillEngine, } @@ -181,6 +183,103 @@ func testAttributeSGetAttributeForEvent(t *testing.T) { } } +func testAttributeSGetAttributeForEventNotFound(t *testing.T) { + ev := &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testAttributeSGetAttributeForEventWihMetaAnyContext", + Context: utils.StringPointer(utils.MetaCDRs), + Event: map[string]interface{}{ + utils.Account: "dan", + utils.Destination: "+491511231234", + }, + } + eAttrPrf2 := &engine.AttributeProfile{ + Tenant: ev.Tenant, + ID: "ATTR_3", + FilterIDs: []string{"*string:Account:dan"}, + Contexts: []string{utils.MetaRating}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 1, 14, 0, 0, 0, 0, time.UTC)}, + Attributes: []*engine.Attribute{ + &engine.Attribute{ + FieldName: utils.Account, + Initial: utils.ANY, + Substitute: "1001", + Append: false, + }, + }, + Weight: 10.0, + } + var result string + if err := attrSRPC.Call("ApierV1.SetAttributeProfile", eAttrPrf2, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + var reply *engine.AttributeProfile + if err := attrSRPC.Call("ApierV1.GetAttributeProfile", + &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_3"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eAttrPrf2, reply) { + t.Errorf("Expecting : %+v, received: %+v", eAttrPrf2, reply) + } + var attrReply *engine.AttributeProfile + if err := attrSRPC.Call(utils.AttributeSv1GetAttributeForEvent, + ev, &attrReply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } + +} + +func testAttributeSGetAttributeForEventWithMetaAnyContext(t *testing.T) { + ev := &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testAttributeSGetAttributeForEventWihMetaAnyContext", + Context: utils.StringPointer(utils.MetaCDRs), + Event: map[string]interface{}{ + utils.Account: "dan", + utils.Destination: "+491511231234", + }, + } + eAttrPrf2 := &engine.AttributeProfile{ + Tenant: ev.Tenant, + ID: "ATTR_2", + FilterIDs: []string{"*string:Account:dan"}, + Contexts: []string{utils.META_ANY}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 1, 14, 0, 0, 0, 0, time.UTC)}, + Attributes: []*engine.Attribute{ + &engine.Attribute{ + FieldName: utils.Account, + Initial: utils.ANY, + Substitute: "1001", + Append: false, + }, + }, + Weight: 10.0, + } + var result string + if err := attrSRPC.Call("ApierV1.SetAttributeProfile", eAttrPrf2, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + var reply *engine.AttributeProfile + if err := attrSRPC.Call("ApierV1.GetAttributeProfile", + &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_2"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eAttrPrf2, reply) { + t.Errorf("Expecting : %+v, received: %+v", eAttrPrf2, reply) + } + var attrReply *engine.AttributeProfile + if err := attrSRPC.Call(utils.AttributeSv1GetAttributeForEvent, + ev, &attrReply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eAttrPrf2, attrReply) { + t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eAttrPrf2), utils.ToJSON(attrReply)) + } + +} func testAttributeSProcessEvent(t *testing.T) { ev := &utils.CGREvent{ Tenant: "cgrates.org", diff --git a/engine/attributes.go b/engine/attributes.go index 4182b1b0b..ee809349e 100644 --- a/engine/attributes.go +++ b/engine/attributes.go @@ -67,7 +67,13 @@ func (alS *AttributeService) matchingAttributeProfilesForEvent(ev *utils.CGREven aPrflIDs, err := matchingItemIDsForEvent(ev.Event, alS.stringIndexedFields, alS.prefixIndexedFields, alS.dm, utils.AttributeFilterIndexes+attrIdxKey) if err != nil { - return nil, err + if err != utils.ErrNotFound { + return nil, err + } + if aPrflIDs, err = matchingItemIDsForEvent(ev.Event, alS.stringIndexedFields, alS.prefixIndexedFields, + alS.dm, utils.AttributeFilterIndexes+utils.ConcatenatedKey(ev.Tenant, utils.META_ANY)); err != nil { + return nil, err + } } lockIDs := utils.PrefixSliceItems(aPrflIDs.Slice(), utils.AttributeFilterIndexes) guardian.Guardian.GuardIDs(config.CgrConfig().LockingTimeout, lockIDs...)