Implemented *datetime type in AgentRequest and EventRequest and added tests for them

This commit is contained in:
andronache
2021-04-29 17:10:58 +03:00
committed by Dan Christian Bogos
parent c8433aacd2
commit 9adacc8a54
4 changed files with 139 additions and 0 deletions

View File

@@ -461,6 +461,17 @@ func (ar *AgentRequest) ParseField(
return
}
out = strconv.Itoa(int(t1.Unix()))
case utils.MetaDateTime: // Convert the requested field value into datetime with layout
var val string
if val, err = cfgFld.Value.ParseDataProvider(ar); err != nil {
return
}
var dtFld time.Time
dtFld, err = utils.ParseTimeDetectLayout(val, utils.FirstNonEmpty(cfgFld.Timezone, config.CgrConfig().GeneralCfg().DefaultTimezone))
if err != nil {
return
}
out = dtFld.Format(cfgFld.Layout)
}
if err != nil &&

View File

@@ -2190,3 +2190,62 @@ func TestAgReqSetFieldsFromCfg(t *testing.T) {
}
}
func TestFieldAsInterface(t *testing.T) {
fldPath := []string{utils.MetaOpts, utils.AccountField}
ar := &AgentRequest{
Request: nil,
Vars: &utils.DataNode{},
CGRRequest: &utils.OrderedNavigableMap{},
diamreq: nil,
tmp: &utils.DataNode{},
Opts: utils.MapStorage{
utils.AccountField: "Field1",
},
}
rcv, err := ar.FieldAsInterface(fldPath)
rcvExpect := ar.Opts[utils.AccountField]
if err != nil {
t.Error(err)
} else if rcv != rcvExpect {
t.Errorf("Expected %v but received %v", rcvExpect, rcv)
}
//default case
fldPath = []string{utils.MetaNone}
_, err = ar.FieldAsInterface(fldPath)
errExpect := "unsupported field prefix: <*none>"
if err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
}
func TestAgentRequestParseFieldDateTime(t *testing.T) {
tntTpl := config.NewRSRParsersMustCompile("*daily", utils.InfieldSep)
AgentReq := NewAgentRequest(utils.MapStorage{}, nil, nil, nil, nil, tntTpl, "", "", nil, nil, nil)
fctTemp := &config.FCTemplate{
Type: utils.MetaDateTime,
Value: config.NewRSRParsersMustCompile("*daily", utils.InfieldSep),
Layout: "“Mon Jan _2 15:04:05 2006”",
Timezone: "",
}
result, err := AgentReq.ParseField(fctTemp)
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
expected, err := utils.ParseTimeDetectLayout("*daily", utils.FirstNonEmpty(fctTemp.Timezone, config.CgrConfig().GeneralCfg().DefaultTimezone))
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
strRes := fmt.Sprintf("%v", result)
finRes, err := time.Parse("“Mon Jan _2 15:04:05 2006”", strRes)
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
if !reflect.DeepEqual(finRes.Day(), expected.Day()) {
t.Errorf("Expected %v but received %v", finRes.Day(), expected.Day())
}
}

View File

@@ -332,6 +332,17 @@ func (eeR *EventRequest) ParseField(
return
}
out = strconv.Itoa(int(t1.Unix()))
case utils.MetaDateTime: // Convert the requested field value into datetime with layout
var val string
if val, err = cfgFld.Value.ParseDataProvider(eeR); err != nil {
return
}
var dtFld time.Time
dtFld, err = utils.ParseTimeDetectLayout(val, utils.FirstNonEmpty(cfgFld.Timezone, config.CgrConfig().GeneralCfg().DefaultTimezone))
if err != nil {
return
}
out = dtFld.Format(cfgFld.Layout)
case utils.MetaMaskedDestination:
//check if we have destination in the event
var dst string

View File

@@ -0,0 +1,58 @@
/*
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"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
func TestAgentRequestParseFieldDateTime(t *testing.T) {
tntTpl := config.NewRSRParsersMustCompile("*daily", utils.InfieldSep)
AgentReq := NewEventRequest(utils.MapStorage{}, nil, nil, tntTpl, "", "", nil, nil)
fctTemp := &config.FCTemplate{
Type: utils.MetaDateTime,
Value: config.NewRSRParsersMustCompile("*daily", utils.InfieldSep),
Layout: "“Mon Jan _2 15:04:05 2006”",
Timezone: "",
}
result, err := AgentReq.ParseField(fctTemp)
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
expected, err := utils.ParseTimeDetectLayout("*daily", utils.FirstNonEmpty(fctTemp.Timezone, config.CgrConfig().GeneralCfg().DefaultTimezone))
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
strRes := fmt.Sprintf("%v", result)
finRes, err := time.Parse("“Mon Jan _2 15:04:05 2006”", strRes)
if err != nil {
t.Errorf("Expected %v but received %v", nil, err)
}
if !reflect.DeepEqual(finRes.Day(), expected.Day()) {
t.Errorf("Expected %v but received %v", expected, result)
}
}