Monday, February 13, 2012

SoapUI / REST / JSON / variable namespaces

As a developer of large Web applications, I'm used to relying on a few proven test strategies:
When I joined my current development team to work on the project TradeInsight, I was introduced to SoapUI for the functional tests. I like it's ability to convert JSON to XML, enabling then the writing of XPath match assertions.

There's one little caveat related to the conversion and the XPath assertions:
  • When the server response contains a single JSON object, the conversion introduce a namespace into the generated XML.
  • And this namespace depends on the server address.
The following figures show the original JSON response and its conversion to XML with the inferred namespace.
JSON response produced by a REST service.
Transformation into a XML payload with an inferred namespace.

This automatic mapping to namespace server dependent does not allow to write server-agnostic code if you follow the suggested solution!

The following figures show a XPath match expression as documented in the SoapUI training materials. Sadly, running it against an XML with a namespace, this error is reported:

XPathContains assertion failed for path [//startDate/text()] : Exception missing content for xpath [//startDate/text()] in Response.

Simple XPath expression with the corresponding error message.
Corrected XPath expression as suggested, now server dependent :(

 A simple solution consists in replacing the specified namespace with the meta-character '*', which match any namespace. As all elements of the XML document are under the scope of the inferred namespace, it's important to prefix all nodes of the XPath expression with '*:', as illustrated by the following figure.

Use of the '*' prefix to produce assertion server independent.
I hope this helps.

A+, Dom

12 comments:

  1. Awesome! I needed this for my SoapUI tests.
    Thanks
    Andrew

    ReplyDelete
  2. Very helpful, thanks!

    ReplyDelete
  3. Hi

    I have to send JSON request to my REST service. If i set parameter/Value pairs and set the accept and media type to be application/json and tried to invoke the resource, with failure message.Looks like the input request is not showing up in raw tab of the soap ui request.(not in json format either).How can i resolve this issues?

    ReplyDelete
    Replies
    1. I guess you want to issue a GET request, right? Just once after having created a test step, with the header { 'Accept': 'application/json' } and some request parameters, the Raw view is effectively empty. If you've run it, you'll see it populated with on the first line:
      - the verb GET,
      - your service URL,
      - all parameters serialized in a form: ?name=value&name=value&...
      - and the HTTP protocol version

      Here is the Raw view content after a successful run:

      GET http://127.0.0.1:8080/API/Nova/promotion?account=Costco&status=PENDING&toDate=2012-03-31 HTTP/1.1
      Accept-Encoding: gzip,deflate
      Accept: application/json
      Authorization: Basic ZXBvbGluc2tpQG5vdmEuY29tOk9uM09uMw==
      User-Agent: Jakarta Commons-HttpClient/3.1
      Host: 127.0.0.1:8080
      Cookie: $Version=0; JSESSIONID=B7DF915AE128E3E148EF20169027C5D9; $Path=/API/

      I hope it helps.
      A+, DOm

      Delete
  4. Thanks, this saved my day!

    ReplyDelete
  5. It helped me a lot, thanks!

    ReplyDelete
  6. Thank you, this saved my day too!

    ReplyDelete
    Replies
    1. You're welcome. I'm just sad to see that the issue has not been fixed :(

      Delete
  7. Thank you so much for this post it helped me a lot

    ReplyDelete