2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (c) 2019 IBM
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.sdnc.config.audit.node;
29 import java.io.IOException;
30 import java.io.StringReader;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
36 import org.custommonkey.xmlunit.Diff;
37 import org.custommonkey.xmlunit.Difference;
38 import org.custommonkey.xmlunit.DifferenceConstants;
39 import org.custommonkey.xmlunit.DifferenceListener;
40 import org.custommonkey.xmlunit.ElementNameQualifier;
41 import org.custommonkey.xmlunit.XMLUnit;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Node;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFManager;
51 public class CompareXmlData implements CompareDataInterface
53 private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
61 public CompareXmlData(String controlXml, String testXml) {
63 this.controlXml = controlXml;
64 this.testXml = testXml;
68 public boolean compare() throws Exception
71 log.debug("controlXml : " + controlXml);
72 log.debug("testXml : " + testXml);
77 Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
78 diff.overrideElementQualifier(new ElementNameQualifier() {
80 protected boolean equalsNamespace(Node control, Node test) {
84 diff.overrideDifferenceListener(new DifferenceListener() {
86 public int differenceFound(Difference diff) {
87 if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
88 return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
90 return RETURN_ACCEPT_DIFFERENCE;
93 public void skippedComparison(Node arg0, Node arg1) { }
100 catch(SAXException se)
102 log.error("Exception caught", se);
103 throw new Exception(se.getMessage());
107 log.error("Exception caught", e);
108 throw new Exception(e.getMessage());
112 private void doSetup() throws ParserConfigurationException, SAXException, IOException
115 XMLUnit.setIgnoreAttributeOrder(true);
116 XMLUnit.setIgnoreComments(true);
117 XMLUnit.setIgnoreWhitespace(true);
121 public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
123 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
124 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
125 StringReader reader = new StringReader(inXml);
126 InputSource inputSource = new InputSource(reader);
127 Document doc = dBuilder.parse(inputSource);
128 doc.getDocumentElement().normalize();