Forum Discussion

groovyguy's avatar
groovyguy
Champion Level 1
6 years ago

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

1 Reply

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    Also, another small challenge: One issue I ran into with this script is that it assumes the service sorts the values as a string. If the element being sorted is an integer, this script would break. I'd like to see how other people would fix that part too. :)