2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.audit.node;
28 import java.io.IOException;
29 import java.io.StringReader;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
35 import org.xmlunit.diff.ComparisonResult;
36 import org.xmlunit.diff.ComparisonType;
37 import org.xmlunit.diff.DefaultNodeMatcher;
38 import org.xmlunit.diff.ElementSelectors;
39 import org.xmlunit.util.Nodes;
40 import org.xmlunit.builder.DiffBuilder;
41 import org.w3c.dom.Attr;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
50 public class CompareXmlData implements CompareDataInterface
52 private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
60 public CompareXmlData(String controlXml, String testXml) {
62 this.controlXml = controlXml;
63 this.testXml = testXml;
67 public boolean compare() throws Exception
70 log.debug("controlXml : " + controlXml);
71 log.debug("testXml : " + testXml);
72 controlXml = controlXml.replace("junos:", "");
73 testXml = testXml.replace("junos:", "");
78 //Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
79 final org.xmlunit.diff.Diff documentDiff = DiffBuilder
82 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
84 .withDifferenceEvaluator((comparison, outcome) -> {
85 if (outcome != ComparisonResult.EQUAL && comparison.getType() == ComparisonType.ATTR_VALUE) {
86 Attr a = (Attr) comparison.getControlDetails().getTarget();
87 if ("commit-seconds".equals(Nodes.getQName(a).getLocalPart()) || "commit-localtime".equals(Nodes.getQName(a).getLocalPart())
88 && "configuration".equals(Nodes.getQName(a.getOwnerElement()).getLocalPart())) {
89 return ComparisonResult.EQUAL;
93 return ComparisonResult.SIMILAR;
100 if(documentDiff.hasDifferences())
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();