e5e2b757f8fb0ae42bc3ce84a0e7a8303e63200a
[appc.git] / appc-config / appc-config-audit / provider / src / main / java / org / onap / sdnc / config / audit / node / CompareXmlData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.audit.node;
27
28
29 import java.io.IOException;
30 import java.io.StringReader;
31
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35
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;
46
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFManager;
49
50
51 public class CompareXmlData implements CompareDataInterface
52 {
53     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
54
55
56     String controlXml;
57     String testXml;
58
59     Document doc;
60
61     public CompareXmlData(String controlXml, String testXml) {
62         super();
63         this.controlXml = controlXml;
64         this.testXml = testXml;
65     }
66
67     @Override
68     public boolean compare() throws Exception
69     {
70
71         log.debug("controlXml : " + controlXml);
72         log.debug("testXml : " + testXml);
73         doSetup();
74
75         try
76         {
77              Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
78              diff.overrideElementQualifier(new ElementNameQualifier() {
79                     @Override
80                     protected boolean equalsNamespace(Node control, Node test) {
81                         return true;
82                     }
83                 });
84              diff.overrideDifferenceListener(new DifferenceListener() {
85                     @Override
86                     public int differenceFound(Difference diff) {
87                         if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
88                             return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
89                         }
90                         return RETURN_ACCEPT_DIFFERENCE;
91                     }
92                     @Override
93                     public void skippedComparison(Node arg0, Node arg1) { }
94                 });
95              if(diff.similar())
96                  return true;
97              else
98                  return false;
99         }
100         catch(SAXException se)
101         {
102             log.error("Exception caught", se);
103             throw new Exception(se.getMessage());
104         }
105         catch(Exception e)
106         {
107             log.error("Exception caught", e);
108             throw new Exception(e.getMessage());
109         }
110     }
111
112     private void doSetup() throws ParserConfigurationException, SAXException, IOException
113     {
114
115         XMLUnit.setIgnoreAttributeOrder(true);
116         XMLUnit.setIgnoreComments(true);
117         XMLUnit.setIgnoreWhitespace(true);
118     }
119
120
121     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
122     {
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();
129
130         return doc;
131     }
132 }