Ask a Question

How to assert ascending/desciding order in response

SOLVED
vpachpute1
Frequent Contributor

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 details.

 

Regards

Vishal

7 REPLIES 7
nmrao
Community Hero

The attachment has same values. Please provide data supporting your question.


Regards,
Rao.
vpachpute1
Frequent Contributor

Hi

 

Sorry to say, but because of security policies, I cannot provide full response.

 

But I need to check weather <id>16</id> is in descending order or not.

 

Can you please provide the solution. So I will make necessary changes according to my actual response.

 

Regards

Vishal

No one will ask you to provide your data. The request is about dummy data resembling the problem.


Regards,
Rao.
vpachpute1
Frequent Contributor

Hi nmrao

 

Following is my response.

 

I have to validate weather subscription-id is in descending order or not.

 

<?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>

Your best bet is going to be to use a groovy script assertion. 

 

You can do something like the following:

 

 

def nodeCount = holder["count(//*:MobilityRemarkSet)"].toInteger();

def currValue = holder["//*:er-response/*:subscriptions-v2/*:subscription/@id"];

if (nodeCount == 1)
{
	// If only one object is returned, it is sorted.
	assert true;
}
else if (nodeCount > 1)
{
	// If more than one object is returned, test for descending sort
	// Create a copy of the list, and sort it. Reverse the list, and verify the two match. 
	assert = (currValue.sort(false) == currValue.reverse());
}

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

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"


Regards,
Rao.
vpachpute1
Frequent Contributor

Hi nmrao

 

Thanks very much for your solution. It perfectly worked out.

 

Regards

Vishal

cancel
Showing results for 
Search instead for 
Did you mean: