Functional Web Services Testing Made Easy with SoapUI - Part 2
Before we begin, a quick Groovy primer. We start with the fact that Groovy is Java and Java is Groovy. If you have written Java code, you have written Groovy code. Our tasks within this article will be to do 3 things: format a date, read and write to a properties file, and parse XML. Let’s look at the Groovy code for each of these in detail.
1. Format a date
The standard Java class for this task is SimpleDateFormat. This is a concrete subclass of DateFormat formats and parses dates and times using a string pattern, in our case, "yyyy-MM-dd". Let’s start by writing this in Java; later we will see how easily we can groovify this to use within SoapUI. Here is the Java code to get the date in the format “yyyy-MM-dd”.
java.util.Date today = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
String todayStr = sdf.format(today);
We could leave the Groovy code exactly as it is above, but let’s make it groovier, removing the bells and whistles necessary in Java. Our code finally looks like this:
today = new Date()
sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(today)
Not a lot of change, but considerably faster to type. The similarities to Java are just as we have said.
2. Reading and writing to a properties file
The java.util.Properties object does the work of reading and writing to a properties file in Java. This time we’ll go directly to the Groovy code:
sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(new Date())
props = new java.util.Properties ()
file = new File("C:/web-services-test/testprops.txt")
if(!file.exists())
{
file.createNewFile()
props.today = todayStr
props.zipCode = "20904"
fos = new java.io.FileOutputStream ( file )
props.store(fos, "Writing the zipcode and today's date")
}fis = new FileInputStream (file )
props.load (fis)
today = props.getProperty ( "today" )
zipCode = props.getProperty ( "zipCode" ) Simple, right? Similarly, if you need to read from a database, you can do so in a few simple lines of Groovy code.
3.Parse XML
If you have been writing enterprise Java applications you have certainly needed to use XML, and you can testify that XML and Java is not easy. Parsing XML in Java requires a lot of boilerplate code and is really tedious.
You may remember from part 1 that I said I was lazy. Fortunately, Groovy comes to the rescue. We can parse XML in just 5 lines of code. Yes, that’s what I said, just 5 lines. Let’s see how to parse the response from the LatLonListZipCode web service from Part 1. The XML looks like this:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:LatLonListZipCodeResponse xmlns:ns1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<listLatLonOut xsi:type="xsd:string"><?xml version='1.0' ?>
<dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/mdl/survey/pgb_survey/dev/DWMLgen/schema/DWML.xsd">
<latLonList>39.0138,-77.0242</latLonList>
</dwml></listLatLonOut>
</ns1:LatLonListZipCodeResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The code to parse this in Groovy is so easy:
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
holder = groovyUtils.getXmlHolder("LatLonListZipCode - Request 1#Response")
listLatLonOut = holder.getNodeValue( "//listLatLonOut" )
latlonNode = groovyUtils.getXmlHolder(listLatLonOut)
latlon = latlonNode.getNodeValue("//latLonList")
- Login or register to post comments
- 6545 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)







Comments
Guillaume Laforge replied on Tue, 2008/05/13 - 9:31am
Very minor comment, in idiomatic, instead of
You'd write:
Meera Subbarao replied on Tue, 2008/05/13 - 10:03am
Hi Guillaume,
Thanks for the comment; its even more groovified now. I will update the post with your changes.