XMLUnit-2.0 xpath doesn't ignoring the XML node order

420 views Asked by At

I've a XML as below

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
<ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
   <Attributes>
     <Map>
       <entry key="accountFlags">
         <value>
           <List>
             <String>Normal User Account</String>
             <String>Password Cannot Expire</String>
           </List>
         </value>
       </entry>
       <entry key="homePhone" value="6555"/>
       <entry key="l" value="Pune"/>
       <entry key="memberOf">
         <value>
           <List>
             <String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
           </List>
         </value>
       </entry>
       <entry key="objectClass">
         <value>
           <List>
             <String>top</String>
             <String>person</String>
             <String>organizationalPerson</String>
             <String>user</String>
           </List>
         </value>
       </entry>
       <entry key="sn" value="test"/>
       <entry key="st" value="MH"/>
       <entry key="streetAddress" value="SB ROAD"/>
       <entry key="title" value="QA"/>
       <entry key="userPrincipalName" value="AggUserFSP [email protected]"/>
     </Map>
   </Attributes>
 </ResourceObject>

And, following XPATH I tried to ignore the order of the elements but still it is not working, can someone help me out here? I referred the discussion here https://github.com/xmlunit/xmlunit/issues/123


diff = DiffBuilder
                    .compare(control)
                    .withTest(test)
                    .checkForSimilar().checkForIdentical() //ignore the order of 'elements` but check they are identical
                    .normalizeWhitespace()
                    .ignoreComments()
                    .ignoreWhitespace()
                    //.ignoreElementContentWhitespace()
                    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
                            .whenElementIsNamed("Attributes").thenUse(ElementSelectors.byXPath("./Map/entry[@key]", ElementSelectors.byNameAndText))
                            .elseUse(ElementSelectors.byName)
                            .build()))
                    .build();
1

There are 1 answers

0
Rafał On

I assume you wish to match entry elements inside of Map element, and you wish to match them them by their key attribute, thus ignoring the order?

If so, I suggest you to write your NodeMatcher like this:

    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("entry").thenUse(
                        (e1, e2) -> StringUtils.equals(e1.getAttribute("key"), e2.getAttribute("key")))
                        .elseUse(byName)
                        .build()