Ask a Question

Verify a repeating attribute in .json response appears in ascending and descending order

SOLVED
richie
Community Hero

Verify a repeating attribute in .json response appears in ascending and descending order

Hi,

 

I have a GET request that interacts with a database retrieving records based on the URI/Template and Query Parameters in the URI string submitted and the response returned is in .json type.

 

I have a requirement that I can sort the response based on a specific field in the response in both ascending and descending order.

 

I found the following post with yet more @nmrao code and I've been trying to tailor it

 

MY GET request format is as follows:

https://${#Project#TestEndpoint}/api/1/geographical-indication/product-group?_sort=Name asc  //sorts the response using the Name attribute value in ascending order

https://${#Project#TestEndpoint}/api/1/{geographical-indication/product-group?_sort=Name desc  //sorts the response using the Name attribute value in descending order

I've attached the 2 .json responses for both ascending and descending sort on the Name attribute

 

I've tried updating @Rao's original code as per my requirement - but whether the results are returned using descending or ascending - the groovy script passes fine - so I must be doing something wrong

 

My update to @Rao's script is as follows:

 

/**All Rao's Work
* Refer:https://community.smartbear.com/t5/SoapUI-NG/Assertions-to-verify-items-are-being-returned-in-alphabetical/m-p/145218#M32959
* this is to handle multiple properties
**/
def response = context.expand( '${REST Request#Response#$[\'data\']}' )  //data is the section of response that has the repeating groups in it

//ATTENTION: add the list of property names
def userList = ['Name']  //this is the attribute I am sorting on in the previous REST request

def json = new groovy.json.JsonSlurper().parseText(response)
def msg = new StringBuffer()

def getPropertyValues = { prop ->
	json.results."$prop"
}

def areValuesAscending = { list ->
	(list == list.sort(false)) ? true : false
}

def buildAssertionErrors = { propList ->
	propList.each { prop ->
		def getData = getPropertyValues(prop)
		println "list of ${prop} : ${getData}"
		if (!areValuesAscending(getData)) {
			if (!msg) msg.append('There are assertion errors\n')
			msg.append("${prop} : ${getData}").append('\n')
		}
	}
}

buildAssertionErrors(userList)

if (msg) { 
	println 'Test is a fail'
	throw new Error(msg.toString())
} else {
	println 'Test is a pass'
}

and I think I know what it is - unfortunately - I cant read all of @Rao's code - but I looking at the script - I need to specify the repeating attribute as a property somehow?  ("def getPropertyValues"

 

currently my test object hierarchy in the test is as follows:

 

REST Request step

Groovy Script (with the above code)

 

I'm guessing I need to pass the 'Name' attribute to a Property - but it can't be that easy - if anyone can translate, I'd be very grateful!

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
6 REPLIES 6
nmrao
Community Hero

@richie,

Not sure what you are expecting here.

The script is sorting to ascending (irrespective of the order in the response).

Any ways, please do below changes:

def response = context.expand( '${REST Request#Response#$[\'data\']}' )

to

def response = context.expand( '${REST Request#Response}' )

Another change is

def getPropertyValues = { prop ->
json.results."$prop"
}

To

def getPropertyValues = { prop ->
json.data."$prop"
}


Regards,
Rao.
richie
Community Hero

@nmrao 

 

OH!

 

Sorry fella - I thought the script asserted that the response was returned in a certain order - as you can see - there's plenty of the script I can't actually read - I didnt realise the script was actually doing the sorting!

 

Apologies - that's not what I'm after.

 

I have a requirement where I'm requesting the data back in either ascending or descending order relative to the queryparm I'm specifying (e.g. /api/1/{namespace}/{table}?_sort=tableAttribute desc)

 

I thought your script actually ASSERTED the response was in ascending order as this is what I need to verify,

 

my bad!

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nmrao
Community Hero

More over, it was not even reading your data at all, forget about sort order. Because that was tailored for original question.



Regards,
Rao.
richie
Community Hero

Hahaha!

What a dunce.....what can i say? I'm definitely making progress with my groovy, but it's very slow going!

But actually it's a lot more elegant than using the otb functionality....totally changed my opinion....im trying to do everything with groovy now! 😉

Nice one!

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nmrao
Community Hero

@richie ,

 

Does the below helps?

 

//Define the ordered list which is in the same order of the values as expected
def expectedList = []

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

assert expectedList == json.data.Name


Regards,
Rao.
richie
Community Hero

hahahaha!

 

Perfect!

 

thanks man!

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
cancel
Showing results for 
Search instead for 
Did you mean: