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