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