Create SVG in UI
[clamp.git] / src / test / java / org / onap / clamp / clds / util / drawing / DocumentBuilderTest.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.drawing;
25
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.when;
28
29 import java.io.IOException;
30
31 import javax.xml.parsers.ParserConfigurationException;
32
33 import org.apache.batik.svggen.SVGGraphics2D;
34 import org.apache.batik.util.SVGConstants;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.clamp.clds.util.ResourceFileUtil;
41 import org.onap.clamp.clds.util.XmlToolsTest;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Element;
44 import org.w3c.dom.Node;
45 import org.xml.sax.SAXException;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class DocumentBuilderTest {
49     @Mock
50     private SVGGraphics2D mockG2d;
51
52     @Test
53     public void pushChangestoDocumentTest() throws IOException, ParserConfigurationException, SAXException {
54         String dataElementId = "someId";
55         String newNodeTag = "tagged";
56         String newNodeText = "Sample text";
57         String xml = ResourceFileUtil.getResourceAsString("clds/util/file.xml");
58         Document document = XmlToolsTest.parseStringToXmlDocument(xml);
59         Node newNode = document.createElement(newNodeTag);
60         newNode.appendChild(document.createTextNode(newNodeText));
61
62         when(mockG2d.getRoot(any(Element.class))).then(a -> a.getArgument(0, Element.class).appendChild(newNode));
63
64         DocumentBuilder db = new DocumentBuilder(document, document);
65         db.pushChangestoDocument(mockG2d, dataElementId);
66         Document actualDocument = db.getGroupingDocument();
67
68         Node addedActualNode = actualDocument.getDocumentElement().getLastChild();
69         String actualDataElementId = addedActualNode.getAttributes()
70                 .getNamedItem(DocumentBuilder.DATA_ELEMENT_ID_ATTRIBUTE).getTextContent();
71
72         Assert.assertEquals(dataElementId, actualDataElementId);
73         Assert.assertEquals(SVGConstants.SVG_G_TAG, addedActualNode.getNodeName());
74
75         Node addedActualNodeChild = addedActualNode.getLastChild();
76         Assert.assertEquals(newNodeTag, addedActualNodeChild.getNodeName());
77         Assert.assertEquals(newNodeText, addedActualNodeChild.getTextContent());
78     }
79 }