Updates to config audit bundle
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.audit.node;
26
27
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34
35 import org.custommonkey.xmlunit.Diff;
36 import org.custommonkey.xmlunit.Difference;
37 import org.custommonkey.xmlunit.DifferenceConstants;
38 import org.custommonkey.xmlunit.DifferenceListener;
39 import org.custommonkey.xmlunit.ElementNameQualifier;
40 import org.custommonkey.xmlunit.XMLUnit;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48
49
50 public class CompareXmlData implements CompareDataInterface
51 {
52     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
53
54
55     String controlXml;
56     String testXml;
57
58     Document doc;
59
60     public CompareXmlData(String controlXml, String testXml) {
61         super();
62         this.controlXml = controlXml;
63         this.testXml = testXml;
64     }
65
66     @Override
67     public boolean compare() throws Exception
68     {
69
70         log.debug("controlXml : " + controlXml);
71         log.debug("testXml : " + testXml);
72         doSetup();
73
74         try
75         {
76              Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
77              diff.overrideElementQualifier(new ElementNameQualifier() {
78                     @Override
79                     protected boolean equalsNamespace(Node control, Node test) {
80                         return true;
81                     }
82                 });
83              diff.overrideDifferenceListener(new DifferenceListener() {
84                     @Override
85                     public int differenceFound(Difference diff) {
86                         if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
87                             return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
88                         }
89                         return RETURN_ACCEPT_DIFFERENCE;
90                     }
91                     @Override
92                     public void skippedComparison(Node arg0, Node arg1) { }
93                 });
94              if(diff.similar())
95                  return true;
96              else
97                  return false;
98         }
99         catch(SAXException se)
100         {
101             se.printStackTrace();
102             throw new Exception(se.getMessage());
103         }
104         catch(Exception e)
105         {
106             e.printStackTrace();
107             throw new Exception(e.getMessage());
108         }
109     }
110
111     private void doSetup() throws ParserConfigurationException, SAXException, IOException
112     {
113
114         XMLUnit.setIgnoreAttributeOrder(true);
115         XMLUnit.setIgnoreComments(true);
116         XMLUnit.setIgnoreWhitespace(true);
117     }
118
119
120     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
121     {
122         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
123         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
124         StringReader reader = new StringReader(inXml);
125         InputSource inputSource = new InputSource(reader);
126         Document doc = dBuilder.parse(inputSource);
127         doc.getDocumentElement().normalize();
128
129         return doc;
130     }
131 }