b96f9476475ee3216552d9a71627eba4a3cf3107
[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.XMLUnit;
37 import org.w3c.dom.Document;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43
44
45 public class CompareXmlData implements CompareDataInterface
46 {
47     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
48
49
50     String controlXml;
51     String testXml;
52
53     Document doc;
54
55     public CompareXmlData(String controlXml, String testXml) {
56         super();
57         this.controlXml = controlXml;
58         this.testXml = testXml;
59     }
60
61     @Override
62     public boolean compare() throws Exception
63     {
64
65         log.debug("controlXml : " + controlXml);
66         log.debug("testXml : " + testXml);
67
68         doSetup();
69
70         try
71         {
72             Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
73             if(diff.similar())
74                 return true;
75             else
76                 return false;
77         }
78         catch(SAXException se)
79         {
80             se.printStackTrace();
81             throw new Exception(se.getMessage());
82         }
83         catch(Exception e)
84         {
85             e.printStackTrace();
86             throw new Exception(e.getMessage());
87         }
88     }
89
90     private void doSetup() throws ParserConfigurationException, SAXException, IOException
91     {
92
93         XMLUnit.setIgnoreAttributeOrder(true);
94         XMLUnit.setIgnoreComments(true);
95         XMLUnit.setIgnoreWhitespace(true);
96     }
97
98     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
99     {
100         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
101         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
102         StringReader reader = new StringReader(inXml);
103         InputSource inputSource = new InputSource(reader);
104         Document doc = dBuilder.parse(inputSource);
105         doc.getDocumentElement().normalize();
106
107         return doc;
108     }
109 }