Merge "Rework the UI"
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.util;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import org.apache.batik.anim.dom.SVGDOMImplementation;
32 import org.apache.batik.util.SVGConstants;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.w3c.dom.Document;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38
39 public class XmlToolsTest {
40
41     @Test
42     public void exportXmlDocumentAsStringTest() throws IOException, ParserConfigurationException, SAXException {
43         String expected = ResourceFileUtil.getResourceAsString("clds/util/file.xml");
44         Document document = parseStringToXmlDocument(expected);
45         String actual = XmlTools.exportXmlDocumentAsString(document);
46         Assert.assertEquals(expected.trim(), actual.trim());
47     }
48
49     @Test
50     public void createEmptySvgDocumentTest() {
51         Document doc = XmlTools.createEmptySvgDocument();
52         Assert.assertEquals(SVGDOMImplementation.SVG_NAMESPACE_URI, doc.getDocumentElement().getNamespaceURI());
53         Assert.assertEquals(SVGConstants.SVG_SVG_TAG, doc.getDocumentElement().getNodeName());
54         Assert.assertNull(doc.getDoctype());
55     }
56
57
58     public static Document parseStringToXmlDocument(String res)
59         throws ParserConfigurationException, SAXException, IOException {
60         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
61         dbf.setValidating(false);
62         dbf.setNamespaceAware(true);
63         dbf.setFeature("http://xml.org/sax/features/namespaces", false);
64         dbf.setFeature("http://xml.org/sax/features/validation", false);
65         dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
66         dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
67         DocumentBuilder db = dbf.newDocumentBuilder();
68         InputSource is = new InputSource(new StringReader(res));
69         return db.parse(is);
70     }
71
72 }