PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

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

 November 22, 2022     java, rest-assured, soap, xml, xpath     No comments   

Issue

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?


Solution

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"));


Answered By - Not a JD
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing