Add FCTemplate in config + test for it

This commit is contained in:
TeoV
2018-08-22 10:43:23 -04:00
committed by Dan Christian Bogos
parent 595866cf75
commit dcd18fef98
3 changed files with 155 additions and 0 deletions

62
config/fctemplate.go Executable file
View File

@@ -0,0 +1,62 @@
/*
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 config
import (
"github.com/cgrates/cgrates/utils"
)
func NewFCTemplateFromFCTemplateJsonCfg(jsnCfg *FcTemplateJsonCfg) *FCTemplate {
fcTmp := new(FCTemplate)
if jsnCfg.Id != nil {
fcTmp.ID = *jsnCfg.Id
}
if jsnCfg.Type != nil {
fcTmp.Type = *jsnCfg.Type
}
if jsnCfg.Field_id != nil {
fcTmp.FieldId = *jsnCfg.Field_id
}
if jsnCfg.Filters != nil {
fcTmp.Filters = make([]string, len(*jsnCfg.Filters))
for i, fltr := range *jsnCfg.Filters {
fcTmp.Filters[i] = fltr
}
}
if jsnCfg.Value != nil {
fcTmp.Value = utils.NewRSRParsersMustCompile(*jsnCfg.Value, true)
}
return fcTmp
}
type FCTemplate struct {
ID string
Type string // Type of field
FieldId string // Field identifier
Filters []string // list of filter profiles
Value utils.RSRParsers
}
func FCTemplatesFromFCTemapltesJsonCfg(jsnCfgFlds []*FcTemplateJsonCfg) []*FCTemplate {
retFields := make([]*FCTemplate, len(jsnCfgFlds))
for i, jsnFld := range jsnCfgFlds {
retFields[i] = NewFCTemplateFromFCTemplateJsonCfg(jsnFld)
}
return retFields
}

85
config/fctemplate_test.go Executable file
View File

@@ -0,0 +1,85 @@
/*
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 config
import (
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestNewFCTemplateFromFCTemplateJsonCfg(t *testing.T) {
jsonCfg := &FcTemplateJsonCfg{
Id: utils.StringPointer("Tenant"),
Type: utils.StringPointer("*composed"),
Field_id: utils.StringPointer("Tenant"),
Filters: &[]string{"Filter1", "Filter2"},
Value: utils.StringPointer("cgrates.org"),
}
expected := &FCTemplate{
ID: "Tenant",
Type: "*composed",
FieldId: "Tenant",
Filters: []string{"Filter1", "Filter2"},
Value: utils.NewRSRParsersMustCompile("cgrates.org", true),
}
rcv := NewFCTemplateFromFCTemplateJsonCfg(jsonCfg)
if !reflect.DeepEqual(expected, rcv) {
t.Errorf("expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(rcv))
}
}
func TestFCTemplatesFromFCTemapltesJsonCfg(t *testing.T) {
jsnCfgs := []*FcTemplateJsonCfg{
&FcTemplateJsonCfg{
Id: utils.StringPointer("Tenant"),
Type: utils.StringPointer("*composed"),
Field_id: utils.StringPointer("Tenant"),
Filters: &[]string{"Filter1", "Filter2"},
Value: utils.StringPointer("cgrates.org"),
},
&FcTemplateJsonCfg{
Id: utils.StringPointer("RunID"),
Type: utils.StringPointer("*composed"),
Field_id: utils.StringPointer("RunID"),
Filters: &[]string{"Filter1_1", "Filter2_2"},
Value: utils.StringPointer("SampleValue"),
},
}
expected := []*FCTemplate{
&FCTemplate{
ID: "Tenant",
Type: "*composed",
FieldId: "Tenant",
Filters: []string{"Filter1", "Filter2"},
Value: utils.NewRSRParsersMustCompile("cgrates.org", true),
},
&FCTemplate{
ID: "RunID",
Type: "*composed",
FieldId: "RunID",
Filters: []string{"Filter1_1", "Filter2_2"},
Value: utils.NewRSRParsersMustCompile("SampleValue", true),
},
}
rcv := FCTemplatesFromFCTemapltesJsonCfg(jsnCfgs)
if !reflect.DeepEqual(expected, rcv) {
t.Errorf("expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(rcv))
}
}

View File

@@ -572,3 +572,11 @@ type MigratorCfgJson struct {
Out_storDB_user *string
Out_storDB_password *string
}
type FcTemplateJsonCfg struct {
Id *string
Type *string
Field_id *string
Filters *[]string
Value *string
}