First part of onap rename
[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 : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.config.audit.node;
22
23
24 import java.io.IOException;
25 import java.io.StringReader;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.custommonkey.xmlunit.Diff;
32 import org.custommonkey.xmlunit.XMLUnit;
33 import org.w3c.dom.Document;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 public class CompareXmlData implements CompareDataInterface
42 {
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
44
45
46     String controlXml;
47     String testXml;
48
49     Document doc;
50
51     public CompareXmlData(String controlXml, String testXml) {
52         super();
53         this.controlXml = controlXml;
54         this.testXml = testXml;
55     }
56
57     @Override
58     public boolean compare() throws Exception
59     {
60
61         log.debug("controlXml : " + controlXml);
62         log.debug("testXml : " + testXml);
63
64         doSetup();
65
66         try
67         {
68             Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
69             if(diff.similar())
70                 return true;
71             else
72                 return false;
73         }
74         catch(SAXException se)
75         {
76             se.printStackTrace();
77             throw new Exception(se.getMessage());
78         }
79         catch(Exception e)
80         {
81             e.printStackTrace();
82             throw new Exception(e.getMessage());
83         }
84     }
85
86     private void doSetup() throws ParserConfigurationException, SAXException, IOException
87     {
88
89         XMLUnit.setIgnoreAttributeOrder(true);
90         XMLUnit.setIgnoreComments(true);
91         XMLUnit.setIgnoreWhitespace(true);
92     }
93
94     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
95     {
96         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
97         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
98         StringReader reader = new StringReader(inXml);
99         InputSource inputSource = new InputSource(reader);
100         Document doc = dBuilder.parse(inputSource);
101         doc.getDocumentElement().normalize();
102
103         return doc;
104     }
105 }