Roman  Shabanov
Roman Shabanov

Reputation: 152

How to validate specific node from SOAP XML response with several namespaces using Rest-assured?

I'm using SOAP and testing it by Rest-Assured. I want to validate response body XML by Rest-Assured that it has expected node value. But I can't get node I need.

Here is my response XML. It has several namespaces. I want to get the value of this node ns3:site

<soap-env:envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap-env:body>
      <ns4:findsiteconfigurationbysmth xmlns:ns3="http://www.testsite.com/common" xmlns:ns2="http://www.testsite.com/plant" xmlns:ns4="someapi:com:plant" xmlns:ns5="someapi:com:reasoncode">
         <ns4:response>
            <ns2:ref>SiteWD:QWERTY</ns2:ref>
            <ns3:site>QWERTY</ns3:site>
            <ns3:description>test description</ns3:description>
            <ns3:timezone>Africa/Abidjan</ns3:timezone>
         </ns4:response>
      </ns4:findsiteconfigurationbysmth>
   </soap-env:body>
</soap-env:envelope>

Previously I saved my response to a String variable and produce my validation with this code.

   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setNamespaceAware(true);
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document myXml = builder.parse(new InputSource(new StringReader(myStringXml)));

   NodeList node = myXml.getDocumentElement()
  .getElementsByTagNameNS("http://www.testsite.com/common", "site");
   node.item(0).getTextContent();

This code works! Response is QWERTY

Now I'm trying to validate it by Rest-Assured.

.spec(defaultRequestSpecification(mySpec))
.config(RestAssuredConfig.config()
.xmlConfig(XmlConfig.xmlConfig()
.with().namespaceAware(true)
.declareNamespace("site", "http://www.testsite.com/common")))
.post()
.then()
.statusCode(200)
.body("site", equalTo("QWERTY"));

And my response is

1 expectation failed. 
XML path site doesn't match. 
Expected: QWERTY 
Actual: SiteWD:QWERTYQWERTYtest descriptionAfrica/Abidjan

I tried to change declared namespace to "ns3", "ns3:site". And the same story with xPath in a body method - "ns3", "ns3:site", "site" and so on. The result is the same... A single string of text from ns3 nodes.

What I'm doing wrong? Please help me to figure out where is the problem. How to get only one node? What should I change?

Upvotes: 1

Views: 2944

Answers (2)

Alex Karamfilov
Alex Karamfilov

Reputation: 674

Not directly related to your question but I really think its overkill to use Rest Assured for testing SOAP service. Its much easier if you use something like Apache Axis and all of your headaches with XML building/parsing will go away. Especially if you need to test whole service, not just one/two endpoints.

Upvotes: 0

Not a JD
Not a JD

Reputation: 1902

According to the REST Assured docs, the following should work for you:

.config(RestAssuredConfig.config()
.xmlConfig(XmlConfig.xmlConfig()
.with()
.namespaceAware(true)
.declareNamespace("soap-env", "http://schemas.xmlsoap.org/soap/envelope/")
.declareNamespace("ns4", "someapi:com:plant")
.declareNamespace("ns3", "http://www.testsite.com/common")))
.post()
.then()
.statusCode(200)
.body("soap-env:envelope.soap-env:body.ns4:findsiteconfigurationbysmth.ns4:response.ns3:site.text()", equalTo("QWERTY"));

Upvotes: 3

Related Questions