How to assert ascending/desciding order in response
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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>
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
