2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.util.drawing;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import javax.xml.parsers.ParserConfigurationException;
31 import org.apache.batik.svggen.SVGGraphics2D;
32 import org.apache.batik.util.SVGConstants;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.clamp.clds.util.ResourceFileUtil;
39 import org.onap.clamp.clds.util.XmlToolsTest;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43 import org.xml.sax.SAXException;
45 @RunWith(MockitoJUnitRunner.class)
46 public class DocumentBuilderTest {
48 private SVGGraphics2D mockG2d;
51 private Document mockDomImpl;
54 public void pushChangestoDocumentTest() throws IOException, ParserConfigurationException, SAXException {
55 String dataElementId = "someId";
56 String newNodeTag = "tagged";
57 String newNodeText = "Sample text";
58 String xml = ResourceFileUtil.getResourceAsString("clds/util/file.xml");
59 Document document = XmlToolsTest.parseStringToXmlDocument(xml);
60 Node newNode = document.createElement(newNodeTag);
61 newNode.appendChild(document.createTextNode(newNodeText));
63 when(mockG2d.getRoot(any(Element.class))).then(a -> a.getArgumentAt(0, Element.class).appendChild(newNode));
65 DocumentBuilder db = new DocumentBuilder(document, document);
66 db.pushChangestoDocument(mockG2d, dataElementId);
67 Document actualDocument = db.getGroupingDocument();
69 Node addedActualNode = actualDocument.getDocumentElement().getLastChild();
70 String actualDataElementId =
71 addedActualNode.getAttributes().getNamedItem(DocumentBuilder.DATA_ELEMENT_ID_ATTRIBUTE).getTextContent();
73 Assert.assertEquals(dataElementId, actualDataElementId);
74 Assert.assertEquals(SVGConstants.SVG_G_TAG, addedActualNode.getNodeName());
76 Node addedActualNodeChild = addedActualNode.getLastChild();
77 Assert.assertEquals(newNodeTag, addedActualNodeChild.getNodeName());
78 Assert.assertEquals(newNodeText, addedActualNodeChild.getTextContent());