Files
cgrates/migrator/attributes_test.go
2025-10-29 19:42:24 +01:00

262 lines
8.3 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 Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package migrator
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func TestV1AttributeProfileAsAttributeProfile(t *testing.T) {
cloneExpTime := time.Now().Add(time.Duration(20 * time.Minute))
mapSubstitutes := make(map[string]map[string]*v1Attribute)
mapSubstitutes["FL1"] = make(map[string]*v1Attribute)
mapSubstitutes["FL1"]["In1"] = &v1Attribute{
FieldName: "FL1",
Initial: "In1",
Substitute: "Al1",
Append: true,
}
v1Attribute := &v1AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: mapSubstitutes,
Weight: 20,
}
attrPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*engine.Attribute{
{
FilterIDs: []string{"*string:FL1:In1"},
Path: utils.MetaReq + utils.NestingSep + "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
if ap, err := v1Attribute.AsAttributeProfile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(attrPrf, ap) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(ap))
}
}
func TestV2AttributeProfileAsAttributeProfile(t *testing.T) {
cloneExpTime := time.Now().Add(time.Duration(20 * time.Minute))
v2Attribute := v2AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*v2Attribute{
{
FieldName: "FL1",
Initial: "In1",
Substitute: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
Append: true,
},
},
Weight: 20,
}
attrPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*engine.Attribute{
{
FilterIDs: []string{"*string:FL1:In1"},
Path: utils.MetaReq + utils.NestingSep + "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
if ap, err := v2Attribute.AsAttributeProfile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(attrPrf, ap) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(ap))
}
}
func TestV2AttributeProfileAsAttributeProfile2(t *testing.T) {
cloneExpTime := time.Now().Add(time.Duration(20 * time.Minute))
v2Attribute := v2AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*v2Attribute{
{
FieldName: "FL1",
Initial: nil,
Substitute: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
Append: true,
},
},
Weight: 20,
}
attrPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*engine.Attribute{
{
FilterIDs: []string{},
Path: utils.MetaReq + utils.NestingSep + "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
if ap, err := v2Attribute.AsAttributeProfile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(attrPrf, ap) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(ap))
}
}
func TestV3AttributeProfileAsAttributeProfile(t *testing.T) {
cloneExpTime := time.Now().Add(time.Duration(20 * time.Minute))
v3Attribute := v3AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*v3Attribute{
{
FilterIDs: []string{"*string:FL1:In1"},
FieldName: "FL1",
Substitute: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
attrPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*engine.Attribute{
{
FilterIDs: []string{"*string:FL1:In1"},
Path: utils.MetaReq + utils.NestingSep + "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
if ap, err := v3Attribute.AsAttributeProfile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(attrPrf, ap) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(ap))
}
}
func TestV4AttributeProfileAsAttributeProfile(t *testing.T) {
cloneExpTime := time.Now().Add(time.Duration(20 * time.Minute))
v4Attribute := v4AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*v4Attribute{
{
FieldName: "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("~Category:s/(.*)/${1}_UK_Mobile_Vodafone_GBRVF/", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
attrPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "attributeprofile1",
Contexts: []string{utils.MetaSessionS},
FilterIDs: []string{"filter1"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
ExpiryTime: cloneExpTime,
},
Attributes: []*engine.Attribute{
{
Path: utils.MetaReq + utils.NestingSep + "FL1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("~*req.Category:s/(.*)/${1}_UK_Mobile_Vodafone_GBRVF/", true, utils.INFIELD_SEP),
},
},
Weight: 20,
}
if ap, err := v4Attribute.AsAttributeProfile(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(attrPrf, ap) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(ap))
}
}