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