groovyguy
7 years agoCommunity Hero
Groovy Script Example - Testing for ascending sort
One of the tests I often have to deal with is verifying that a service can sort (ascending or descending) on a particular element (or a number of them.) I'm sharing a groovy script I've written to help assert that the service is sorting as expected. It's a small step to change it to test for descending sort, and I wanted to see how people would do that. I know how I would, and can post my version later. Here's my snippet:
import com.eviware.soapui.support.XmlHolder def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ); def holder = groovyUtils.getXmlHolder( messageExchange.responseContentAsXml ) def nodeCount = holder["count(//*:Objects/*:Object)"] nodeCount = nodeCount.toInteger() bool ascending = true; if (nodeCount == 1) { // If only one object is returned, it is sorted. ascending = true; } else { // If there are more than one object in the response, use XPATH to get the values of the element that was sorted by. Using ObjectIdentifier for this example. currentValue = holder["//*:Objects/*:Object/*:ObjectIdentifier"]; // Test to see if the collection is equal to the sorted collection. Using false keeps the original collection the same and creates a temporary sorted copy. ascending = currentValue == currentValue.sort(false); } assert ascending