CDRC XML ElementText and more testing

This commit is contained in:
DanB
2016-05-06 16:40:34 +02:00
parent 4e7a012e0a
commit c86fdd1686
2 changed files with 67 additions and 22 deletions

View File

@@ -28,8 +28,34 @@ import (
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
// getElementText will process the node to extract the elementName's text out of it (only first one found)
// returns utils.ErrNotFound if the element is not found in the node
func elementText(xmlRes tree.Res, elmntPath string) (string, error) {
xp, err := goxpath.Parse(elmntPath)
if err != nil {
return "", err
}
elmntBuf := bytes.NewBufferString(xml.Header)
if err := goxpath.Marshal(xmlRes.(tree.Node), elmntBuf); err != nil {
return "", err
}
elmntNode, err := xmltree.ParseXML(elmntBuf)
if err != nil {
return "", err
}
elmnts, err := goxpath.Exec(xp, elmntNode, nil)
if err != nil {
return "", err
}
if len(elmnts) == 0 {
return "", utils.ErrNotFound
}
return elmnts[0].String(), nil
}
func NewXMLRecordsProcessor(recordsReader io.Reader) (*XMLRecordsProcessor, error) {
xp, err := goxpath.Parse(path.Join("/broadWorksCDR/cdrData/"))
if err != nil {
@@ -44,7 +70,7 @@ func NewXMLRecordsProcessor(recordsReader io.Reader) (*XMLRecordsProcessor, erro
}
xmlProc := new(XMLRecordsProcessor)
xmlProc.cdrXmlElmts = goxpath.MustExec(xp, xmlNode, nil)
return nil, nil
return xmlProc, nil
}
type XMLRecordsProcessor struct {
@@ -60,11 +86,8 @@ func (xmlProc *XMLRecordsProcessor) ProcessNextRecord() (cdrs []*engine.CDR, err
if len(xmlProc.cdrXmlElmts) <= xmlProc.procItems {
return nil, io.EOF // have processed all items
}
cdrXml := xmlProc.cdrXmlElmts[xmlProc.procItems]
cdrs = make([]*engine.CDR, 0)
//cdrXml := xmlProc.cdrXmlElmts[xmlProc.procItems]
xmlProc.procItems += 1
cdrBuf := bytes.NewBufferString(xml.Header)
if err := goxpath.Marshal(cdrXml.(tree.Node), cdrBuf); err != nil {
return nil, err
}
return nil, nil
return cdrs, nil
}

View File

@@ -20,14 +20,14 @@ package cdrc
import (
"bytes"
"encoding/xml"
"fmt"
"path"
"reflect"
"testing"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var cdrXmlBroadsoft = `<?xml version="1.0" encoding="ISO-8859-1"?>
@@ -169,20 +169,42 @@ func optsNotStrict(s *xmltree.ParseOptions) {
s.Strict = false
}
func TestXmlPathCDRs(t *testing.T) {
func TestElementText(t *testing.T) {
xp := goxpath.MustParse(path.Join("/broadWorksCDR/cdrData/"))
xmlTree := xmltree.MustParseXML(bytes.NewBufferString(cdrXmlBroadsoft), optsNotStrict)
cdrs := goxpath.MustExec(xp, xmlTree, nil)
for _, cdr := range cdrs {
cdrBuf := bytes.NewBufferString(xml.Header)
if err := goxpath.Marshal(cdr.(tree.Node), cdrBuf); err != nil {
t.Error(err)
}
xp := goxpath.MustParse(path.Join("/cdrData/basicModule/userNumber"))
userNumberNode := xmltree.MustParseXML(cdrBuf, optsNotStrict)
userNumber := goxpath.MustExec(xp, userNumberNode, nil)
if len(userNumber) != 0 {
fmt.Printf("UserNumber: %s\n", userNumber[0].String())
}
cdrWithoutUserNr := cdrs[0]
if _, err := elementText(cdrWithoutUserNr, "cdrData/basicModule/userNumber"); err != utils.ErrNotFound {
t.Error(err)
}
cdrWithUser := cdrs[1]
if val, err := elementText(cdrWithUser, "cdrData/basicModule/userNumber"); err != nil {
t.Error(err)
} else if val != "1001" {
t.Errorf("Expecting: 1001, received: %s", val)
}
if val, err := elementText(cdrWithUser, "/cdrData/centrexModule/locationList/locationInformation/locationType"); err != nil {
t.Error(err)
} else if val != "Primary Device" {
t.Errorf("Expecting: <Primary Device>, received: <%s>", val)
}
}
func TestXMLRPProcessNextRecord(t *testing.T) {
xmlRP, err := NewXMLRecordsProcessor(bytes.NewBufferString(cdrXmlBroadsoft))
if err != nil {
t.Error(err)
}
var cdrs []*engine.CDR
for {
cdrs, err = xmlRP.ProcessNextRecord()
break
}
if err != nil {
t.Error(err)
}
expectedCDRs := []*engine.CDR{}
if !reflect.DeepEqual(expectedCDRs, cdrs) {
t.Errorf("Expecting: %+v, received: %+v", expectedCDRs, cdrs)
}
}