Files
cgrates/engine/libattributes_test.go
2021-12-21 15:05:44 +01:00

283 lines
8.8 KiB
Go

/*
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 <http://www.gnu.org/licenses/>
*/
package engine
import (
"fmt"
"reflect"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
func TestConvertExternalToProfile(t *testing.T) {
external := &APIAttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_ID",
FilterIDs: []string{"FLTR_ACNT_dan", "FLTR_DST_DE", "*string:~*opts.*context:*sessions|*cdrs"},
Attributes: []*ExternalAttribute{
{
Path: utils.MetaReq + utils.NestingSep + "Account",
Value: "1001",
},
},
Weight: 20,
}
expAttr := &AttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_ID",
FilterIDs: []string{"FLTR_ACNT_dan", "FLTR_DST_DE", "*string:~*opts.*context:*sessions|*cdrs"},
Attributes: []*Attribute{
{
Path: utils.MetaReq + utils.NestingSep + "Account",
Value: config.NewRSRParsersMustCompile("1001", utils.InfieldSep),
},
},
Weight: 20,
}
rcv, err := external.AsAttributeProfile()
if err != nil {
t.Error(err)
}
rcv.Compile()
if !reflect.DeepEqual(expAttr, rcv) {
t.Errorf("Expecting : %+v, received: %+v", expAttr, rcv)
}
}
func TestConvertExternalToProfileMissing(t *testing.T) {
external := &APIAttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_ID",
FilterIDs: []string{"FLTR_ACNT_dan", "FLTR_DST_DE", "*ai:~*req.AnswerTime:2014-07-14T14:35:00Z|2014-07-14T14:36:00Z", "*string:~*opts.*context:*sessions|*cdrs"},
Attributes: []*ExternalAttribute{},
Weight: 20,
}
_, err := external.AsAttributeProfile()
if err == nil || err.Error() != "MANDATORY_IE_MISSING: [Attributes]" {
t.Error(err)
}
}
func TestConvertExternalToProfileMissing2(t *testing.T) {
external := &APIAttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_ID",
FilterIDs: []string{"FLTR_ACNT_dan", "FLTR_DST_DE", "*ai:~*req.AnswerTime:2014-07-14T14:35:00Z|2014-07-14T14:36:00Z", "*string:~*opts.*context:*sessions|*cdrs"},
Attributes: []*ExternalAttribute{
{
Path: utils.MetaReq + utils.NestingSep + "Account",
},
},
Weight: 20,
}
_, err := external.AsAttributeProfile()
if err == nil || err.Error() != "MANDATORY_IE_MISSING: [Value]" {
t.Error(err)
}
}
func TestNewAttributeFromInline(t *testing.T) {
attrID := "*sum:*req.Field2:10&~*req.NumField&20;*sum:*req.Field3:10&~*req.NumField4&20"
expAttrPrf1 := &AttributeProfile{
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
ID: attrID,
Attributes: []*Attribute{
{
Path: utils.MetaReq + utils.NestingSep + "Field2",
Type: utils.MetaSum,
Value: config.NewRSRParsersMustCompile("10;~*req.NumField;20", utils.InfieldSep),
},
{
Path: utils.MetaReq + utils.NestingSep + "Field3",
Type: utils.MetaSum,
Value: config.NewRSRParsersMustCompile("10;~*req.NumField4;20", utils.InfieldSep),
},
},
}
attr, err := NewAttributeFromInline(config.CgrConfig().GeneralCfg().DefaultTenant, attrID)
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expAttrPrf1, attr) {
t.Errorf("Expecting %+v, received: %+v", utils.ToJSON(expAttrPrf1), utils.ToJSON(attr))
}
}
func TestNewAttributeFromInlineWithMultipleRuns(t *testing.T) {
attrID := "*constant:*req.RequestType:*rated;*constant:*req.Category:call"
expAttrPrf1 := &AttributeProfile{
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
ID: attrID,
Attributes: []*Attribute{
{
Path: utils.MetaReq + utils.NestingSep + "RequestType",
Type: utils.MetaConstant,
Value: config.NewRSRParsersMustCompile("*rated", utils.InfieldSep),
},
{
Path: utils.MetaReq + utils.NestingSep + "Category",
Type: utils.MetaConstant,
Value: config.NewRSRParsersMustCompile("call", utils.InfieldSep),
},
},
}
attr, err := NewAttributeFromInline(config.CgrConfig().GeneralCfg().DefaultTenant, attrID)
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expAttrPrf1, attr) {
t.Errorf("Expecting %+v, received: %+v", utils.ToJSON(expAttrPrf1), utils.ToJSON(attr))
}
}
func TestNewAttributeFromInlineWithMultipleRuns2(t *testing.T) {
attrID := "*constant:*req.RequestType*rated;*constant:*req.Category:call"
expErr := fmt.Sprintf("inline parse error for string: <%s>", "*constant:*req.RequestType*rated")
if _, err := NewAttributeFromInline(config.CgrConfig().GeneralCfg().DefaultTenant, attrID); err == nil || err.Error() != expErr {
t.Errorf("Expected error: %s received %v", expErr, err)
}
attrID = "*constant:*req.RequestType:`*rated;*constant:*req.Category:call"
if _, err := NewAttributeFromInline(config.CgrConfig().GeneralCfg().DefaultTenant, attrID); err == nil {
t.Error(err)
}
}
func TestNewAttributeFromInlineWithMultipleVaslues(t *testing.T) {
attrID := "*variable:*req.Category:call_&*req.OriginID;*constant:*req.RequestType:*rated"
expAttrPrf1 := &AttributeProfile{
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
ID: attrID,
Attributes: []*Attribute{
{
Path: utils.MetaReq + utils.NestingSep + "Category",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("call_;*req.OriginID", utils.InfieldSep),
},
{
Path: utils.MetaReq + utils.NestingSep + "RequestType",
Type: utils.MetaConstant,
Value: config.NewRSRParsersMustCompile("*rated", utils.InfieldSep),
},
},
}
attr, err := NewAttributeFromInline(config.CgrConfig().GeneralCfg().DefaultTenant, attrID)
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expAttrPrf1, attr) {
t.Errorf("Expecting %+v, received: %+v", utils.ToJSON(expAttrPrf1), utils.ToJSON(attr))
}
}
func TestLibAttributesTenantIDInLine(t *testing.T) {
ap := &AttributeProfile{
Tenant: "cgrates.org",
ID: "AttrPrf",
Weight: 10,
}
exp := "cgrates.org:AttrPrf"
if rcv := ap.TenantIDInline(); rcv != exp {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
func TestLibAttributesTenantIDMetaPrefix(t *testing.T) {
ap := &AttributeProfile{
Tenant: "cgrates.org",
ID: "*default",
Weight: 10,
}
exp := "*default"
if rcv := ap.TenantIDInline(); rcv != exp {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
func TestAttributeProfileSet(t *testing.T) {
dp := AttributeProfile{}
exp := AttributeProfile{
Tenant: "cgrates.org",
ID: "ID",
FilterIDs: []string{"fltr1", "*string:~*req.Account:1001"},
Weight: 10,
Blocker: true,
Attributes: []*Attribute{{
Path: "*req.Account",
Type: utils.MetaConstant,
Value: config.NewRSRParsersMustCompile("10", utils.InfieldSep),
FilterIDs: []string{"fltr1"},
}},
}
if err := dp.Set([]string{}, "", false, utils.EmptyString); err != utils.ErrWrongPath {
t.Error(err)
}
if err := dp.Set([]string{"NotAField"}, "", false, utils.EmptyString); err != utils.ErrWrongPath {
t.Error(err)
}
if err := dp.Set([]string{"NotAField", "1"}, "", false, utils.EmptyString); err != utils.ErrWrongPath {
t.Error(err)
}
if err := dp.Set([]string{utils.Tenant}, "cgrates.org", false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.ID}, "ID", false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.FilterIDs}, "fltr1;*string:~*req.Account:1001", false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Weight}, 10, false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Blocker}, true, false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Attributes, utils.Path}, "*req.Account", false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Attributes, utils.Type}, utils.MetaConstant, false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Attributes, utils.Value}, "10", false, utils.InfieldSep); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Attributes, utils.FilterIDs}, "fltr1", false, utils.EmptyString); err != nil {
t.Error(err)
}
if err := dp.Set([]string{utils.Attributes, "Wrong"}, true, false, utils.EmptyString); err != utils.ErrWrongPath {
t.Error(err)
}
if !reflect.DeepEqual(exp, dp) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(dp))
}
}