0

I have a SOAP Response for which need to use Groovy script to assert if the date parameter in the row xml child are coming in ascending order.

xml response

<tree>
 <row>
  <id>123<id>
  <date>20220501</date>
 </row>
 <row>
  <id>242<id>
  <date>20220502</date>
 </row>
 <row>
  <id>125<id>
  <date>20220502</date>
 </row>
</tree>

Please help me with the groovy scripting approach/ script for the same, I am a beginner here.

  • This should be doable with an [XQuery assertion](https://www.soapui.org/docs/functional-testing/validating-messages/validating-xml-messages/#2-The-XQuery-Match-Assertion). – SiKing Aug 09 '22 at 21:44

2 Answers2

0

simple sax parsing solution with date (as int) comparison:

import javax.xml.parsers.SAXParserFactory
import javax.xml.streams.*
import org.xml.sax.*
import org.xml.sax.helpers.DefaultHandler

def xml = '''
<tree>
 <row>
  <id>123</id>
  <date>20220501</date>
 </row>
 <row>
  <id>242</id>
  <date>20220502</date>
 </row>
 <row>
  <id>125</id>
  <date>20220502</date>
 </row>
</tree>
'''

 
 class MyHandler extends DefaultHandler {
     
    int lastDate = -1
    def parsingDate = false
    def ordered = true
 
 
    void startElement(String namespace, String localname, String qname, Attributes atts){
        // enters into <date> tag
        parsingDate = (qname == 'date')
    }
    
    void characters(char[] ch, int start, int length){
        if (parsingDate ){
            // get date as int
            def date = new String(ch).substring(start,start+length) as int
            // comparison
            ordered &= (date >= lastDate )
            lastDate = date
            // else may be throw an exception
        }
    }

   
    void endElement(String uri, String localName, String qName) {
        // exit from date tag
        parsingDate = false
    }

}
 
def handler = new MyHandler()
def factory  = SAXParserFactory.newInstance()
def reader = factory.newSAXParser().XMLReader
reader.contentHandler = handler
 
reader.parse(new InputSource(new ByteArrayInputStream(xml.bytes)))

println handler.ordered
Cthurier
  • 28
  • 1
  • 1
  • 4
0

You should be able to leverage the XmlSlurper to read in and access your XML.

def xmlStr = '''
  <tree>
    <row>
      <id>123</id>
      <date>20220501</date>
    </row>
    <row>
      <id>242</id>
      <date>20220502</date>
    </row>
    <row>
      <id>125</id>
      <date>20220502</date>
    </row>
  </tree>
'''

// parse the XML string so we can manipulate it

def xml = new groovy.xml.XmlSlurper().parseText(xmlStr)

// starting at the root element, get every row tag and then 
// every date tag within that row and then get the text contents 

def dates = xml.row.date*.text()

// lets parse every date string we found into a date object; the 
// collect {} method is similar to the js map method

dates = dates.collect { new SimpleDateFormat("yyyymmdd").parse(it) }

// now lets check that the scraped values are the same as a listed of 
// sorted values - we add the 'false' parameter to sort so that we do 
// not mutate the original list 

assert dates == dates.sort(false)

For some helpful information, check out the groovy docs for working with XML

aspok
  • 285
  • 3
  • 10