Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
8 years ago

How to assert ascending/desciding order in response

Hi   I have one API which returns the transactions id's in descending order.   I have to assert the order.   Can you please suggest the way how to do it.   Please find the attached file for d...
  • nmrao's avatar
    nmrao
    8 years ago

    Here you go:

     

    def xml = """<?xml version="1.0" encoding="UTF-8"?> <er-response>    <subscriptions-v2>          <subscription id="909" status="1" />          <subscription id="908" status="1" />          <subscription id="904" status="2" />          <subscription id="898" status="1" />          <subscription id="897" status="1" />          <subscription id="896" status="2" />          <subscription id="895" status="1" />          <subscription id="7" status="1" />          <subscription id="6" status="1" />          <subscription id="5" status="11" />       </subscriptions-v2>   </er-response>"""
    def parsedXml = new XmlSlurper().parseText(xml)
    //Get the ids from the response def idsFromResponse = parsedXml.'**'.findAll{ it.name() == 'subscription'}.collect{it.@id.text()} println "Original list from response : $idsFromResponse" //Sort the numbers in descending order def descIds = idsFromResponse.sort(false).reverse() assert idsFromResponse == descIds, "Response does not have the ids in descending order"