a34ef3f94df1b24a9c39c66eb192aa87cccda5c5
[clamp.git] / src / test / java / org / onap / clamp / clds / util / XmlToolsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.util;
27
28 import java.io.IOException;
29 import java.io.StringReader;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33 import org.apache.batik.anim.dom.SVGDOMImplementation;
34 import org.apache.batik.util.SVGConstants;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.w3c.dom.Document;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40
41 public class XmlToolsTest {
42
43     @Test
44     public void exportXmlDocumentAsStringTest() throws IOException, ParserConfigurationException, SAXException {
45         String expected = ResourceFileUtil.getResourceAsString("clds/util/file.xml");
46         Document document = parseStringToXmlDocument(expected);
47         String actual = XmlTools.exportXmlDocumentAsString(document);
48         Assert.assertEquals(expected.trim(), actual.trim());
49     }
50
51     @Test
52     public void createEmptySvgDocumentTest() {
53         Document doc = XmlTools.createEmptySvgDocument();
54         Assert.assertEquals(SVGDOMImplementation.SVG_NAMESPACE_URI, doc.getDocumentElement().getNamespaceURI());
55         Assert.assertEquals(SVGConstants.SVG_SVG_TAG, doc.getDocumentElement().getNodeName());
56         Assert.assertNull(doc.getDoctype());
57     }
58
59     /**
60      * Method to parse String into XmlDocument.
61      *
62      * @param res
63      *        String to parse
64      * @return
65      *        XmlDocument
66      * @throws ParserConfigurationException
67      *         In case of issues with parse the document
68      * @throws SAXException
69      *         In case of bad format of res
70      * @throws IOException
71      *         In case of issues creating the document
72      */
73     public static Document parseStringToXmlDocument(String res)
74         throws ParserConfigurationException, SAXException, IOException {
75         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
76         dbf.setValidating(false);
77         dbf.setNamespaceAware(true);
78         dbf.setFeature("http://xml.org/sax/features/namespaces", false);
79         dbf.setFeature("http://xml.org/sax/features/validation", false);
80         dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
81         dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
82         DocumentBuilder db = dbf.newDocumentBuilder();
83         InputSource is = new InputSource(new StringReader(res));
84         return db.parse(is);
85     }
86
87 }