Merge "BUG-1521 Unit tests for netconf-util xml package."
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / xml / XmlElementTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.util.xml;
10
11 import static org.hamcrest.CoreMatchers.both;
12 import static org.hamcrest.CoreMatchers.containsString;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThat;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.util.Map;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 import com.google.common.base.Optional;
29
30 public class XmlElementTest {
31
32     private final String elementAsString = "<top xmlns=\"namespace\" xmlns:a=\"attrNamespace\" a:attr1=\"value1\" attr2=\"value2\">" +
33             "<inner>" +
34             "<deepInner>deepValue</deepInner>" +
35             "</inner>" +
36             "<innerNamespace xmlns=\"innerNamespace\">innerNamespaceValue</innerNamespace>" +
37             "<innerPrefixed xmlns:b=\"prefixedValueNamespace\">b:valueWithPrefix</innerPrefixed>" +
38             "</top>";
39     private Document document;
40     private Element element;
41     private XmlElement xmlElement;
42
43     @Before
44     public void setUp() throws Exception {
45         document = XmlUtil.readXmlToDocument(elementAsString);
46         element = document.getDocumentElement();
47         xmlElement = XmlElement.fromDomElement(element);
48     }
49
50     @Test
51     public void testConstruct() throws Exception {
52         final XmlElement fromString = XmlElement.fromString(elementAsString);
53         assertEquals(fromString, xmlElement);
54         XmlElement.fromDomDocument(document);
55         XmlElement.fromDomElement(element);
56         XmlElement.fromDomElementWithExpected(element, "top");
57         XmlElement.fromDomElementWithExpected(element, "top", "namespace");
58
59         try {
60             XmlElement.fromString("notXml");
61             fail();
62         } catch (final NetconfDocumentedException e) {}
63
64         try {
65             XmlElement.fromDomElementWithExpected(element, "notTop");
66             fail();
67         } catch (final NetconfDocumentedException e) {}
68
69         try {
70             XmlElement.fromDomElementWithExpected(element, "top", "notNamespace");
71             fail();
72         } catch (final NetconfDocumentedException e) {}
73     }
74
75     @Test
76     public void testGetters() throws Exception {
77         assertEquals(element, xmlElement.getDomElement());
78         assertEquals(element.getElementsByTagName("inner").getLength(), xmlElement.getElementsByTagName("inner").getLength());
79
80         assertEquals("top", xmlElement.getName());
81         assertTrue(xmlElement.hasNamespace());
82         assertEquals("namespace", xmlElement.getNamespace());
83         assertEquals("namespace", xmlElement.getNamespaceAttribute());
84         assertEquals(Optional.of("namespace"), xmlElement.getNamespaceOptionally());
85
86         assertEquals("value1", xmlElement.getAttribute("attr1", "attrNamespace"));
87         assertEquals("value2", xmlElement.getAttribute("attr2"));
88         assertEquals(2 + 2/*Namespace definition*/, xmlElement.getAttributes().size());
89
90         assertEquals(3, xmlElement.getChildElements().size());
91         assertEquals(1, xmlElement.getChildElements("inner").size());
92         assertTrue(xmlElement.getOnlyChildElementOptionally("inner").isPresent());
93         assertTrue(xmlElement.getOnlyChildElementWithSameNamespaceOptionally("inner").isPresent());
94         assertEquals(0, xmlElement.getChildElements("unknown").size());
95         assertFalse(xmlElement.getOnlyChildElementOptionally("unknown").isPresent());
96         assertEquals(1, xmlElement.getChildElementsWithSameNamespace("inner").size());
97         assertEquals(0, xmlElement.getChildElementsWithSameNamespace("innerNamespace").size());
98         assertEquals(1, xmlElement.getChildElementsWithinNamespace("innerNamespace", "innerNamespace").size());
99         assertTrue(xmlElement.getOnlyChildElementOptionally("innerNamespace", "innerNamespace").isPresent());
100         assertFalse(xmlElement.getOnlyChildElementOptionally("innerNamespace", "unknownNamespace").isPresent());
101
102         final XmlElement noNamespaceElement = XmlElement.fromString("<noNamespace/>");
103         assertFalse(noNamespaceElement.hasNamespace());
104         try {
105             noNamespaceElement.getNamespace();
106             fail();
107         } catch (final MissingNameSpaceException e) {}
108
109         final XmlElement inner = xmlElement.getOnlyChildElement("inner");
110         final XmlElement deepInner = inner.getOnlyChildElementWithSameNamespaceOptionally().get();
111         assertEquals(deepInner, inner.getOnlyChildElementWithSameNamespace());
112         assertEquals(Optional.<XmlElement>absent(), xmlElement.getOnlyChildElementOptionally("unknown"));
113         assertEquals("deepValue", deepInner.getTextContent());
114         assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get());
115         assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get());
116     }
117
118     @Test
119     public void testExtractNamespaces() throws Exception {
120         final XmlElement innerPrefixed = xmlElement.getOnlyChildElement("innerPrefixed");
121         Map.Entry<String, String> namespaceOfTextContent = innerPrefixed.findNamespaceOfTextContent();
122
123         assertNotNull(namespaceOfTextContent);
124         assertEquals("b", namespaceOfTextContent.getKey());
125         assertEquals("prefixedValueNamespace", namespaceOfTextContent.getValue());
126         final XmlElement innerNamespace = xmlElement.getOnlyChildElement("innerNamespace");
127         namespaceOfTextContent = innerNamespace.findNamespaceOfTextContent();
128
129         assertEquals("", namespaceOfTextContent.getKey());
130         assertEquals("innerNamespace", namespaceOfTextContent.getValue());
131     }
132
133     @Test
134     public void testUnrecognisedElements() throws Exception {
135         xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace"));
136
137         try {
138             xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"));
139             fail();
140         } catch (final NetconfDocumentedException e) {
141             assertThat(e.getMessage(), both(containsString("innerNamespace")).and(containsString("innerNamespace")));
142         }
143     }
144 }