Correct cached data for httpXmlDP is index is present

This commit is contained in:
TeoV
2018-11-16 03:57:52 -05:00
committed by Dan Christian Bogos
parent 5ad34f0b01
commit 1b1966b46f
3 changed files with 89 additions and 5 deletions

View File

@@ -133,11 +133,6 @@ func (hU *httpXmlDP) FieldAsInterface(fldPath []string) (data interface{}, err e
if len(fldPath) == 0 {
return nil, fmt.Errorf("Empty path")
}
if data, err = hU.cache.FieldAsInterface(fldPath); err == nil ||
err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
var slctrStr string
for i := range fldPath {
if sIdx := strings.Index(fldPath[i], "["); sIdx != -1 {
@@ -156,6 +151,11 @@ func (hU *httpXmlDP) FieldAsInterface(fldPath []string) (data interface{}, err e
fldPath[i] = fldPath[i] + slctrStr
}
}
if data, err = hU.cache.FieldAsInterface(fldPath); err == nil ||
err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
//convert fldPath to HierarchyPath
path := utils.HierarchyPath(fldPath)
elmnt := xmlquery.FindOne(hU.xmlDoc, path.AsString("/", false))

View File

@@ -141,3 +141,53 @@ func TestHttpXmlDPFieldAsInterface(t *testing.T) {
t.Errorf("expecting: 222147, received: <%s>", data)
}
}
func TestHttpXmlDPFieldAsInterface2(t *testing.T) {
body := `<?xml version="1.0" encoding="UTF-8"?>
<sms-notification callid="145566709">
<createtime>2018-11-15T15:11:26</createtime>
<reference>SMS</reference>
<calltype calltypeid="8">smsrelay</calltype>
<userid>1636488</userid>
<username>447440935378</username>
<customerid>1632715</customerid>
<companyname>447440935378</companyname>
<totalcost amount="0.0000" currency="USD">0.0000</totalcost>
<agenttotalcost amount="0.0360" currency="USD">0.0360</agenttotalcost>
<agentid>2774</agentid>
<callleg calllegid="219816629" calllegtype="mo">
<number>447440935378</number>
<ratedforuseras><![CDATA[UK Mobile - O2 [GBRCN] [MSRN]]]></ratedforuseras>
<cost amount="0.0000" currency="USD">0.0000</cost>
<agentcost amount="0.0135" currency="USD">0.0135</agentcost>
</callleg>
<callleg calllegid="219816630" calllegtype="mt">
<number>447930323266</number>
<ratedforuseras><![CDATA[UK Mobile - T-Mobile [GBRME]]]></ratedforuseras>
<cost amount="0.0000" currency="USD">0.0000</cost>
<agentcost amount="0.0225" currency="USD">0.0225</agentcost>
</callleg>
</sms-notification>
`
req, err := http.NewRequest("POST", "http://localhost:8080/", bytes.NewBuffer([]byte(body)))
if err != nil {
t.Error(err)
}
dP, _ := newHTTPXmlDP(req)
if data, err := dP.FieldAsString([]string{"sms-notification", "callleg", "agentcost", "@amount"}); err != nil {
t.Error(err)
} else if data != "0.0135" {
t.Errorf("expecting: 0.0135, received: <%s>", data)
}
if data, err := dP.FieldAsString([]string{"sms-notification", "callleg[0]", "agentcost"}); err != nil {
t.Error(err)
} else if data != "0.0135" {
t.Errorf("expecting: 0.0135, received: <%s>", data)
}
if data, err := dP.FieldAsString([]string{"sms-notification", "callleg[1]", "agentcost"}); err != nil {
t.Error(err)
} else if data != "0.0225" {
t.Errorf("expecting: 0.0225, received: <%s>", data)
}
}

View File

@@ -507,3 +507,37 @@ func TestPassFilterMaxCost(t *testing.T) {
t.Errorf("Expecting: true, received: %+v", pass)
}
}
func TestPassFilterMissingField(t *testing.T) {
data, _ := NewMapStorage()
dmFilterPass := NewDataManager(data)
cfg, _ := config.NewDefaultCGRConfig()
filterS := FilterS{
cfg: cfg,
dm: dmFilterPass,
}
passEvent1 := map[string]interface{}{
"Category": "call",
}
if pass, err := filterS.Pass("cgrates.org",
[]string{"*rsr::~Category(^$)"}, config.NewNavigableMap(passEvent1)); err != nil {
t.Errorf(err.Error())
} else if pass {
t.Errorf("Expecting: true , received: %+v", pass)
}
if pass, err := filterS.Pass("cgrates.org",
[]string{"*rsr::~Category(!^$)"}, config.NewNavigableMap(passEvent1)); err != nil {
t.Errorf(err.Error())
} else if !pass {
t.Errorf("Expecting: true , received: %+v", pass)
}
passEvent2 := map[string]interface{}{
"test": "call",
}
if pass, err := filterS.Pass("cgrates.org",
[]string{"*rsr::~Category(^$)"}, config.NewNavigableMap(passEvent2)); err != nil {
t.Errorf(err.Error())
} else if pass {
t.Errorf("Expecting: true , received: %+v", pass)
}
}