Convert modules and relations to SVG
[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.Matchers.any;
27 import static org.mockito.Mockito.when;
28
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class DocumentBuilderTest {
47     @Mock
48     private SVGGraphics2D mockG2d;
49
50     @Mock
51     private Document mockDomImpl;
52
53     @Test
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));
62
63         when(mockG2d.getRoot(any(Element.class))).then(a -> a.getArgumentAt(0, Element.class).appendChild(newNode));
64
65         DocumentBuilder db = new DocumentBuilder(document, document);
66         db.pushChangestoDocument(mockG2d, dataElementId);
67         Document actualDocument = db.getGroupingDocument();
68
69         Node addedActualNode = actualDocument.getDocumentElement().getLastChild();
70         String actualDataElementId =
71             addedActualNode.getAttributes().getNamedItem(DocumentBuilder.DATA_ELEMENT_ID_ATTRIBUTE).getTextContent();
72
73         Assert.assertEquals(dataElementId, actualDataElementId);
74         Assert.assertEquals(SVGConstants.SVG_G_TAG, addedActualNode.getNodeName());
75
76         Node addedActualNodeChild = addedActualNode.getLastChild();
77         Assert.assertEquals(newNodeTag, addedActualNodeChild.getNodeName());
78         Assert.assertEquals(newNodeText, addedActualNodeChild.getTextContent());
79     }
80 }