How to match only selected node values of an xml?

962 views Asked by At

I have an xml string and I want to compare only selected node values and assert my test case on the result. For example: I have the following xml string

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:requestTransferResponse xmlns:ns2="http://external.interfaces.my.company.com/">
         <return>
            <ersReference>2017051017472278401000061</ersReference>
            <resultCode>0</resultCode>
            <resultDescription>SUCCESS</resultDescription>
            <receivedAmount>
               <currency>BSD</currency>
               <value>5500.00</value>
            </receivedAmount>
            <receiverPrincipal>
               <principalId>
                  <id>SUBDIST1-1</id>
                  <type>RESELLERID</type>
               </principalId>
               <principalName>Sub Distributor 1</principalName>
               <accounts>
                  <account>
                     <accountSpecifier>
                        <accountId>SUBDIST1-1</accountId>
                        <accountTypeId>RESELLER</accountTypeId>
                     </accountSpecifier>
                     <balance>
                        <currency>BSD</currency>
                        <value>985500.00</value>
                     </balance>
                     <creditLimit>
                        <currency>BSD</currency>
                        <value>0.00000</value>
                     </creditLimit>
                  </account>
               </accounts>
               <status>Active</status>
               <msisdn>12420101000</msisdn>
            </receiverPrincipal>
            <requestedTransferAmount>
               <currency>BSD</currency>
               <value>5500</value>
            </requestedTransferAmount>
            <senderPrincipal>
               <principalId>
                  <id>DIST1</id>
                  <type>RESELLERID</type>
               </principalId>
               <principalName>Distributor 1</principalName>
               <accounts>
                  <account>
                     <accountSpecifier>
                        <accountId>DIST1</accountId>
                        <accountTypeId>RESELLER</accountTypeId>
                     </accountSpecifier>
                     <balance>
                        <currency>BSD</currency>
                        <value>1994429.24</value>
                     </balance>
                     <creditLimit>
                        <currency>BSD</currency>
                        <value>0.00000</value>
                     </creditLimit>
                  </account>
               </accounts>
               <status>Active</status>
               <msisdn>12420100000</msisdn>
            </senderPrincipal>
         </return>
      </ns2:requestTransferResponse>
   </soap:Body>
</soap:Envelope>

Now I want validate the values of the following nodes having XPATHs

1. //receiverPrincipal//balance//value i.e 985500.00
2. //senderPrincipal//balance//value i.e. 1994429.24

Rest of the nodes are not required. So I searched internet and found XmlUnit library. Can anyone give a hint on how I can use this to validate xpath node values?

1

There are 1 answers

0
Stefan Bodewig On

If you really only need a few selected texts that you can identify by XPath then XMLUnit's XPath support should work.

Translating the example from XMLUnit's github page (see https://github.com/xmlunit/xmlunit/#asserting-an-xpath-value ) to your first requirement

XPathEngine xpath = new JAXPXPathEngine();
String content = xpath.evaluate("//receiverPrincipal//balance/value/text()", source);
assertEquals(985500.00, Double.valueOf(content), 1e-3);

if you are really sure there'll be only one match for the XPath.

Using the Hamcrest matcher it would be something like

assertThat(source, EvaluateXPathMatcher.hasXPath("//receiverPrincipal//balance/value/text(),
    equalTo("985500.00"))