2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.audit.node;
27 import java.io.IOException;
28 import java.io.StringReader;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
34 import org.custommonkey.xmlunit.Diff;
35 import org.custommonkey.xmlunit.Difference;
36 import org.custommonkey.xmlunit.DifferenceConstants;
37 import org.custommonkey.xmlunit.DifferenceListener;
38 import org.custommonkey.xmlunit.ElementNameQualifier;
39 import org.custommonkey.xmlunit.XMLUnit;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Node;
42 import org.xml.sax.InputSource;
43 import org.xml.sax.SAXException;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
49 public class CompareXmlData implements CompareDataInterface
51 private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
59 public CompareXmlData(String controlXml, String testXml) {
61 this.controlXml = controlXml;
62 this.testXml = testXml;
66 public boolean compare() throws Exception
69 log.debug("controlXml : " + controlXml);
70 log.debug("testXml : " + testXml);
75 Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
76 diff.overrideElementQualifier(new ElementNameQualifier() {
78 protected boolean equalsNamespace(Node control, Node test) {
82 diff.overrideDifferenceListener(new DifferenceListener() {
84 public int differenceFound(Difference diff) {
85 if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
86 return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
88 return RETURN_ACCEPT_DIFFERENCE;
91 public void skippedComparison(Node arg0, Node arg1) { }
98 catch(SAXException se)
100 se.printStackTrace();
101 throw new Exception(se.getMessage());
106 throw new Exception(e.getMessage());
110 private void doSetup() throws ParserConfigurationException, SAXException, IOException
113 XMLUnit.setIgnoreAttributeOrder(true);
114 XMLUnit.setIgnoreComments(true);
115 XMLUnit.setIgnoreWhitespace(true);
119 public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
121 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
122 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
123 StringReader reader = new StringReader(inXml);
124 InputSource inputSource = new InputSource(reader);
125 Document doc = dBuilder.parse(inputSource);
126 doc.getDocumentElement().normalize();