From f4ccfbe47cfbb2e064d6aca0fb71a03c587ceb30 Mon Sep 17 00:00:00 2001 From: TeoV Date: Mon, 4 Mar 2019 18:57:09 +0200 Subject: [PATCH] Add test for AttributeS AttributeFilterIDs --- apier/v2/attributes.go | 51 +++++++++++++++++++++++++ engine/attributes_test.go | 78 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 apier/v2/attributes.go diff --git a/apier/v2/attributes.go b/apier/v2/attributes.go new file mode 100644 index 000000000..c1d66635f --- /dev/null +++ b/apier/v2/attributes.go @@ -0,0 +1,51 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package v2 + +/* +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +//SetAttributeProfile add/update a new Attribute Profile +func (apierV1 *ApierV2) SetAttributeProfile(alsPrf *engine.AttributeProfile, reply *string) error { + if missing := utils.MissingStructFields(alsPrf, []string{"Tenant", "ID"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + if len(alsPrf.Attributes) != 0 { + for _, attr := range alsPrf.Attributes { + for _, sub := range attr.Substitute { + if sub.Rules == "" { + return utils.NewErrMandatoryIeMissing("Rules") + } + if err := sub.Compile(); err != nil { + return utils.NewErrServerError(err) + } + } + } + } + + if err := apierV1.DataManager.SetAttributeProfile(alsPrf, true); err != nil { + return utils.APIErrorHandler(err) + } + *reply = utils.OK + return nil +} +*/ diff --git a/engine/attributes_test.go b/engine/attributes_test.go index 49cb02ba3..0eb031521 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -1149,3 +1149,81 @@ func TestAttributeProcessSubstitute(t *testing.T) { t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event) } } + +func TestAttributeAttributeFilterIDs(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().GeneralCfg().DefaultTenant, + ID: "ATTR_1", + Contexts: []string{utils.META_ANY}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*Attribute{ + { + FilterIDs: []string{"*string:PassField:Test"}, + FieldName: "PassField", + Substitute: config.NewRSRParsersMustCompile("Pass", true, utils.INFIELD_SEP), + }, + { + FilterIDs: []string{"*string:PassField:RandomValue"}, + FieldName: "NotPassField", + Substitute: config.NewRSRParsersMustCompile("NotPass", true, utils.INFIELD_SEP), + }, + { + FilterIDs: []string{"*notexists:RandomField:"}, + FieldName: "RandomField", + Substitute: config.NewRSRParsersMustCompile("RandomValue", true, utils.INFIELD_SEP), + }, + }, + Weight: 10, + } + // Add attribute in DM + if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil { + t.Error(err) + } + attrArgs := &AttrArgsProcessEvent{ + Context: utils.StringPointer(utils.MetaSessionS), + ProcessRuns: utils.IntPointer(1), + CGREvent: utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: utils.GenUUID(), + Event: map[string]interface{}{ + "PassField": "Test", + }, + }, + } + eRply := &AttrSProcessEventReply{ + MatchedProfiles: []string{"ATTR_1"}, + AlteredFields: []string{"PassField", "RandomField"}, + CGREvent: &utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: utils.GenUUID(), + Event: map[string]interface{}{ + "PassField": "Pass", + "RandomField": "RandomValue", + }, + }, + } + 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) + } +}