0

I am trying to compare PUT Request XML and GET Response XML. The following code works so good but according to the requirements some elements are only readable and therefore appears in GET Response XML file. How can I eliminate such a following result?

Result:

[different] Expected presence of child node 'null' but was 'bs:key_id' - comparing  at null to <bs:key_id...> at /container[1]/int_user_object[1]/attributes[1]/key_id[1]

What I tried:

import java.io.IOException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
    public class XMLComparator {

        static Logger logger = LogManager.getLogger(XMLComparator.class);

        public boolean compare(String xml1, String xml2) throws SAXException, IOException {

            XMLUnit.setIgnoreWhitespace(true);
            XMLUnit.setIgnoreComments(true);
            XMLUnit.setIgnoreAttributeOrder(true);

            DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); //  wrap the Diff inside Detailed Diff.
            List<?> allDiff = diff.getAllDifferences();
            boolean result = diff.similar();
            if (result == false) {

                logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());

            } else {
                logger.info("Sending XML and received XML are same: " + result);
            }
            return result;

        }

    }

I planned to add such a logic in my code after reading this document doc but I could not.

if(NODE_TYPE_ID == null){
difference.ignore();} 

According to the Stefan help you can see the last version of my code

EDIT

public class XMLComparator {

    static Logger logger = LogManager.getLogger(XMLComparator.class);

    public boolean compare(String xml1, String xml2) throws SAXException, IOException {

        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreAttributeOrder(true);

        DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); 
        diff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            /* CHILD_NODE_NOT_FOUND_ID is used: A child node in one piece of XML could not be match against any other node of the other piece. 
             * 
             */
            public int differenceFound(Difference difference) {
                return difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID 
                        ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : RETURN_ACCEPT_DIFFERENCE;
            }


            @Override
            public void skippedComparison(Node arg0, Node arg1) {

            }
        });
        List<?> allDiff = diff.getAllDifferences();
        boolean result = diff.identical();
        if (result == false) {

            logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());

        } else {
            logger.info("Sending XML and received XML are same: " + result);
        }
        return result;

    }

}
limonik
  • 499
  • 1
  • 6
  • 28

1 Answers1

1

One approach is to implement a DifferenceListener and suppress the differences that arise from missing nodes, something like

public int differenceFound(Difference difference) {
    return difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
        ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL
        : RETURN_ACCEPT_DIFFERENCE;
}

If you've got the choice of switching to XMLUnit 2.x, though, you could use a NodeFilter to completely hide the elements you know are missing (rather than ignoring all missing elements).

Stefan Bodewig
  • 3,260
  • 15
  • 22
  • Than you but I still saw all the result inclusive [different] Expected presence of child node 'null' but was 'bs:key_id' - comparing at null to at /container[1]/int_user_object[1]/attributes[1]/key_id[1] – limonik Dec 21 '16 at 11:24
  • I used CHILD_NODE_NOT_FOUND_ID instead of CHILD_NODELIST_SEQUENCE_ID. Since my problem is not about the order. I managed eliminate some of them but still I could not satisfy my results. – limonik Dec 21 '16 at 11:44
  • One question: I have to use NAMESPACE_PREFIX_ID it is document level constant. On the other hand CHILD_NODE_NOT_FOUND_ID is element level. Can I have a chance to use bot of them. If yes how? Programmatically I tried && but it did not work. – limonik Dec 21 '16 at 12:02
  • I solved this by changing my xml detailsrather than using NAMESPACE_PREFIX_ID – limonik Dec 21 '16 at 12:44