2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.clds.util;
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;
41 public class XmlToolsTest {
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());
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());
60 * Method to parse String into XmlDocument.
66 * @throws ParserConfigurationException
67 * In case of issues with parse the document
68 * @throws SAXException
69 * In case of bad format of res
71 * In case of issues creating the document
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));