cancel
Showing results for 
Search instead for 
Did you mean: 

Assert Repeating JSON Attribute Sorted Order

SOLVED
Highlighted
Community Hero

Assert Repeating JSON Attribute Sorted Order

Hi,

 

A while ago I asked for help on asserting a repeating attribute's value in a json response was sorted in ascending and descending and you guys gave me an answer - but it's problematic because the solution requires me to list the possible values in an array to assert the sort order is correct, however I have thousands of records for some of my responses and so this approach won't work.

 

I've been searching and found the following post that @msiadak and @nmrao provided answers to handle this  - unfortunately I can't quite read the code so can't tailor it to my problem - the original code is asserting that XML tag's id attribute values are sorted in ascending and descending order as follows:

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"

I was trying to edit it so that  it supports the asserting on the repeating 'Name' attribute within my json - the Name attributes JSONPATH is as follows: $['data'][x]['Name'] (I will attach an example response)

 

def response = context.expand( '${REST Request#Response}' ) 
def json = new groovy.json.JsonSlurper().parseText(response)

def idsFromResponse = json.'**'.findAll{ it.name() == 'Name'}.collect{it.@id.text()} //<<< this is the line I'm having trouble with - I believe I need to alter this to point to the 'Name' attribute in my response
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"

Can anyone provide any assistance how to alter the relevant line to point to the 'Name' attribute, JSONPATH=$['data'][x]['Name']??

 

Thanks to all,

 

richie

 

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Assert Repeating JSON Attribute Sorted Order

You can try replacing your idsFromResponse line with this one below. That being said, the sort() method appears to have issues with some of the symbols in the names you are sorting. It actually appears to put the list out of order and makes the script fail. Not sure if you're account for that or not. 

 

def idsFromResponse = json.data.Name;

 




---

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

Re: Assert Repeating JSON Attribute Sorted Order

You can try replacing your idsFromResponse line with this one below. That being said, the sort() method appears to have issues with some of the symbols in the names you are sorting. It actually appears to put the list out of order and makes the script fail. Not sure if you're account for that or not. 

 

def idsFromResponse = json.data.Name;

 




---

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

Re: Assert Repeating JSON Attribute Sorted Order

Thanks for your help, Matthew!

 

@richie, is this what you were looking for? 

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Hero

Re: Assert Repeating JSON Attribute Sorted Order

absolutely!

 

msiadak totally sorted me out!

 

Cheers fella,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Community Manager

Re: Assert Repeating JSON Attribute Sorted Order

That's great! I'll mark his reply as a solution in this case Smiley Happy

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video:
Top Kudoed Authors