Create date-time UTC string in SOAPUI using groovy

When test web services in SOAPUI, the web service security policy might require current date-time stamp to be passed in security token. This is a bit annoy when test the web service you have to change the date-time string manually. You can use properties scripting provided by SOAPUI to resolve this issue.

When you pass an invalid timestamp in the request, the response will like this
soapui.invalidtimestamp

Here is the article to use properties in SOAPUI.
https://www.soapui.org/scripting—properties/property-expansion.html

There are two common cases of inserting dynamic date-time value in SOAPUI using groovy:
1. Insert formatted timestamp value. Use SimpleDateFormat in this case:
${=new java.text.SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss”).format(new Date())}
2. Format timestamp as xsd:dateTime value. Use DatatypeFactory to create instance of newXMLGregorianCalendar:
${=javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(GregorianCalendar.getInstance())}

Then you can convert the date-time value into UTC format:
1. Insert formatted timestamp value. Use SimpleDateFormat in this case:
${=new java.text.SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”).format(new Date(System.currentTimeMillis()+5*60*1000))}
2. Format timestamp as xsd:dateTime value. Use DatatypeFactory to create instance of newXMLGregorianCalendar:
${=javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(GregorianCalendar.getInstance(TimeZone.getTimeZone(“UTC”)))}

Below is how it should look like in SOAPUI.soapui.properties

The last step is to include these properties in your web service request.soapui.request

This is useful when creating automation test suite.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s