From c86fdd168686711a309628467c39f5762851cebb Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 6 May 2016 16:40:34 +0200 Subject: [PATCH] CDRC XML ElementText and more testing --- cdrc/xml.go | 37 +++++++++++++++++++++++++++------- cdrc/xml_test.go | 52 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/cdrc/xml.go b/cdrc/xml.go index 08d904ef5..b7b9d9d32 100644 --- a/cdrc/xml.go +++ b/cdrc/xml.go @@ -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 } diff --git a/cdrc/xml_test.go b/cdrc/xml_test.go index e6791afde..b386ea731 100644 --- a/cdrc/xml_test.go +++ b/cdrc/xml_test.go @@ -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 = ` @@ -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: , 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) } }