0

I am performing simple RESTFUL service API verification in Java.

To handle response in JSON format is very convenient. Using org.json library, it's easy to convert JSON string from RESTFUL response into JSON object, and compare it with that of the expected JSON string.

JSONObject response = new JSONObject(json_response_str);
JSONObject expected = new JSONObject(json_expected_str);
JSONAssert.assertEquals(expected, response, JSONCompareMode.LENIENT);

If it is some element of the JSON response that need to compare, it is also easy because it is easy to extract sub element from JSONObject using APIs like:

JSONObject element_in_response = response.get("..."); or
JSONObject element_in_response = response.getJSONObject("...");

However, to handle response in XML format, things are more difficult. To compare the whole XML response with expected XML is not bad, I can use XMLUnit to do it:

String xml_response_str = ...
String xml_expected_str = ...
assertXMLEquals(xml_response_str, xml_expected_str);

However, there's no such things like xmlOject as there is in JSON.

So what do I do if want to compare some element of the XML response with expected?

I've search forums and JAXB is sometimes mentioned. I checked and it is about parsing XML to Java object. So am I supposed to parse both response XML string and expected XML string, then extract the element as Java object, then compare them? It seems complicated, not to mention I need the XML schema to start with.

What is the effective way to do this, is there anything that is as convenient as in the case of JSON?

Thanks,

user1559625
  • 2,583
  • 5
  • 37
  • 75

3 Answers3

1

You can try to use XPATH.

There is a short example. Here is XML string:

<?xml version="1.0" encoding="UTF-8"?>
    <resp>
    <status>good</status>
    <msg>hi</msg>
</resp>

The folowing code will get status and message:

String xml = "<resp><status>good</status><msg>hi</msg></resp>";
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(xml));
Document doc = (Document) xpath.evaluate("/", source, XPathConstants.NODE);
String status = xpath.evaluate("/resp/status", doc);
String msg = xpath.evaluate("/resp/msg", doc);
System.out.println("status=" + status);
System.out.println("Message=" + msg);

Here is more examples about how to use XPATH: http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/

Andrey E
  • 856
  • 8
  • 18
0

There are a number of ways for testing XML. Converting XML to JSON not being one of them, but can be done.

Testing XML is usually performed using XPath style comparisons which focus on elements, attributes and content and not so much on comparing chunks.

From looking at your code you're already familiar with XML assertions from http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/XMLAssert.htm but you might also want to look at http://www.w3schools.com/xsl/xpath_intro.asp.

XML validation is not that easy and does require a lot of effort to begin with. Once you've got your tesing tools in order it gets a whole lot easier.

Mike Murphy
  • 1,006
  • 8
  • 16
0

verify (or extract) XML is independent from protocol, RESTful service, etc. , but it is normally used in SOAP services.

Comparison with JSON is interesting. JSON is more easy to use with php, javascript, ...

If you want to connect two java servers, XML is sufficient, or plain java Objects (not portable solution with other languages).

Better point to use XML: it is more, more powerfull, well standardized, and you have lot of tools to process it.

What you are asking: equivalent of JSONobject exists in XML for a while: it is a DOM document, or a Node.

1 read your XML

String xml="<root>content</root>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new StringReader(xml)));

2 Best way to get some particular data: XPath: you give some path to your datas (root/group/class1/other_group/...), you can put wildcards (*), select about parameters, values, etc. see this: How to read XML using XPath in Java

XPath xpath = XPathFactory.newInstance().newXPath();
String expression="/root";

3 you can get direct values

expression="/root/text()";
String value = xpath.evaluate(expression, document);

4 or you get all data (if several)

XPathExpression expr = xpath.compile(expression) ; 
NodeList nodes  = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

  for (int i = 0; i < nodes.getLength(); i++) 
      {
      Node nodeSegment = nodes.item(i);
     if (nodeSegment.getNodeType() == Node.ELEMENT_NODE)
           {
           Element eElement = (Element) nodeSegment;
           System.out.println("TAG="+eElement.getTagName());
           System.out.println("VALUE="+eElement.getNodeValue());
Community
  • 1
  • 1