cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script Example - Testing for ascending sort

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



---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Tags (1)
1 REPLY 1
Highlighted
Community Hero

Re: Groovy Script Example - Testing for ascending sort

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




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors