2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.util.xml;
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;
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;
28 import com.google.common.base.Optional;
30 public class XmlElementTest {
32 private final String elementAsString = "<top xmlns=\"namespace\" xmlns:a=\"attrNamespace\" a:attr1=\"value1\" attr2=\"value2\">" +
34 "<deepInner>deepValue</deepInner>" +
36 "<innerNamespace xmlns=\"innerNamespace\">innerNamespaceValue</innerNamespace>" +
37 "<innerPrefixed xmlns:b=\"prefixedValueNamespace\">b:valueWithPrefix</innerPrefixed>" +
39 private Document document;
40 private Element element;
41 private XmlElement xmlElement;
44 public void setUp() throws Exception {
45 document = XmlUtil.readXmlToDocument(elementAsString);
46 element = document.getDocumentElement();
47 xmlElement = XmlElement.fromDomElement(element);
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");
60 XmlElement.fromString("notXml");
62 } catch (final NetconfDocumentedException e) {}
65 XmlElement.fromDomElementWithExpected(element, "notTop");
67 } catch (final NetconfDocumentedException e) {}
70 XmlElement.fromDomElementWithExpected(element, "top", "notNamespace");
72 } catch (final NetconfDocumentedException e) {}
76 public void testGetters() throws Exception {
77 assertEquals(element, xmlElement.getDomElement());
78 assertEquals(element.getElementsByTagName("inner").getLength(), xmlElement.getElementsByTagName("inner").getLength());
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());
86 assertEquals("value1", xmlElement.getAttribute("attr1", "attrNamespace"));
87 assertEquals("value2", xmlElement.getAttribute("attr2"));
88 assertEquals(2 + 2/*Namespace definition*/, xmlElement.getAttributes().size());
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());
102 final XmlElement noNamespaceElement = XmlElement.fromString("<noNamespace/>");
103 assertFalse(noNamespaceElement.hasNamespace());
105 noNamespaceElement.getNamespace();
107 } catch (final MissingNameSpaceException e) {}
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());
119 public void testExtractNamespaces() throws Exception {
120 final XmlElement innerPrefixed = xmlElement.getOnlyChildElement("innerPrefixed");
121 Map.Entry<String, String> namespaceOfTextContent = innerPrefixed.findNamespaceOfTextContent();
123 assertNotNull(namespaceOfTextContent);
124 assertEquals("b", namespaceOfTextContent.getKey());
125 assertEquals("prefixedValueNamespace", namespaceOfTextContent.getValue());
126 final XmlElement innerNamespace = xmlElement.getOnlyChildElement("innerNamespace");
127 namespaceOfTextContent = innerNamespace.findNamespaceOfTextContent();
129 assertEquals("", namespaceOfTextContent.getKey());
130 assertEquals("innerNamespace", namespaceOfTextContent.getValue());
134 public void testUnrecognisedElements() throws Exception {
135 xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace"));
138 xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"));
140 } catch (final NetconfDocumentedException e) {
141 assertThat(e.getMessage(), both(containsString("innerNamespace")).and(containsString("innerNamespace")));