From 09e43fe487f7cee40caa96ba554ac8b026ba5d64 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Thu, 11 Sep 2014 12:53:55 +0200 Subject: [PATCH 1/1] BUG-1521 Unit tests for netconf-util xml package. Change-Id: Iaafba131555b948130a9916127b808c00016f502 Signed-off-by: Maros Marsalek --- opendaylight/netconf/netconf-util/pom.xml | 4 + .../netconf/util/CloseableUtilTest.java | 44 ++++++ .../xml/HardcodedNamespaceResolverTest.java | 32 ++++ .../netconf/util/xml/XmlElementTest.java | 144 ++++++++++++++++++ .../netconf/util/xml/XmlUtilTest.java | 74 +++++++++ 5 files changed, 298 insertions(+) create mode 100644 opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java create mode 100644 opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java create mode 100644 opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java create mode 100644 opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java diff --git a/opendaylight/netconf/netconf-util/pom.xml b/opendaylight/netconf/netconf-util/pom.xml index df4d389705..bed58beb0f 100644 --- a/opendaylight/netconf/netconf-util/pom.xml +++ b/opendaylight/netconf/netconf-util/pom.xml @@ -46,6 +46,10 @@ xmlunit test + + org.opendaylight.yangtools + mockito-configuration + diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java new file mode 100644 index 0000000000..8d41ad7607 --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; + +import com.google.common.collect.Lists; +import org.junit.Test; + +public class CloseableUtilTest { + + @Test + public void testCloseAllFail() throws Exception { + final AutoCloseable failingCloseable = new AutoCloseable() { + @Override + public void close() throws Exception { + throw new RuntimeException("testing failing close"); + } + }; + + try { + CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable)); + fail("Exception with suppressed should be thrown"); + } catch (final RuntimeException e) { + assertEquals(1, e.getSuppressed().length); + } + } + + @Test + public void testCloseAll() throws Exception { + final AutoCloseable failingCloseable = mock(AutoCloseable.class); + doNothing().when(failingCloseable).close(); + CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable)); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java new file mode 100644 index 0000000000..f083cc1dbd --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.util.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import org.junit.Test; + +public class HardcodedNamespaceResolverTest { + + @Test + public void testResolver() throws Exception { + final HardcodedNamespaceResolver hardcodedNamespaceResolver = new HardcodedNamespaceResolver("prefix", "namespace"); + + assertEquals("namespace", hardcodedNamespaceResolver.getNamespaceURI("prefix")); + try{ + hardcodedNamespaceResolver.getNamespaceURI("unknown"); + fail("Unknown namespace lookup should fail"); + } catch(IllegalStateException e) {} + + assertNull(hardcodedNamespaceResolver.getPrefix("any")); + assertNull(hardcodedNamespaceResolver.getPrefixes("any")); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java new file mode 100644 index 0000000000..a88de956e2 --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.util.xml; + +import static org.hamcrest.CoreMatchers.both; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import com.google.common.base.Optional; + +public class XmlElementTest { + + private final String elementAsString = "" + + "" + + "deepValue" + + "" + + "innerNamespaceValue" + + "b:valueWithPrefix" + + ""; + private Document document; + private Element element; + private XmlElement xmlElement; + + @Before + public void setUp() throws Exception { + document = XmlUtil.readXmlToDocument(elementAsString); + element = document.getDocumentElement(); + xmlElement = XmlElement.fromDomElement(element); + } + + @Test + public void testConstruct() throws Exception { + final XmlElement fromString = XmlElement.fromString(elementAsString); + assertEquals(fromString, xmlElement); + XmlElement.fromDomDocument(document); + XmlElement.fromDomElement(element); + XmlElement.fromDomElementWithExpected(element, "top"); + XmlElement.fromDomElementWithExpected(element, "top", "namespace"); + + try { + XmlElement.fromString("notXml"); + fail(); + } catch (final NetconfDocumentedException e) {} + + try { + XmlElement.fromDomElementWithExpected(element, "notTop"); + fail(); + } catch (final NetconfDocumentedException e) {} + + try { + XmlElement.fromDomElementWithExpected(element, "top", "notNamespace"); + fail(); + } catch (final NetconfDocumentedException e) {} + } + + @Test + public void testGetters() throws Exception { + assertEquals(element, xmlElement.getDomElement()); + assertEquals(element.getElementsByTagName("inner").getLength(), xmlElement.getElementsByTagName("inner").getLength()); + + assertEquals("top", xmlElement.getName()); + assertTrue(xmlElement.hasNamespace()); + assertEquals("namespace", xmlElement.getNamespace()); + assertEquals("namespace", xmlElement.getNamespaceAttribute()); + assertEquals(Optional.of("namespace"), xmlElement.getNamespaceOptionally()); + + assertEquals("value1", xmlElement.getAttribute("attr1", "attrNamespace")); + assertEquals("value2", xmlElement.getAttribute("attr2")); + assertEquals(2 + 2/*Namespace definition*/, xmlElement.getAttributes().size()); + + assertEquals(3, xmlElement.getChildElements().size()); + assertEquals(1, xmlElement.getChildElements("inner").size()); + assertTrue(xmlElement.getOnlyChildElementOptionally("inner").isPresent()); + assertTrue(xmlElement.getOnlyChildElementWithSameNamespaceOptionally("inner").isPresent()); + assertEquals(0, xmlElement.getChildElements("unknown").size()); + assertFalse(xmlElement.getOnlyChildElementOptionally("unknown").isPresent()); + assertEquals(1, xmlElement.getChildElementsWithSameNamespace("inner").size()); + assertEquals(0, xmlElement.getChildElementsWithSameNamespace("innerNamespace").size()); + assertEquals(1, xmlElement.getChildElementsWithinNamespace("innerNamespace", "innerNamespace").size()); + assertTrue(xmlElement.getOnlyChildElementOptionally("innerNamespace", "innerNamespace").isPresent()); + assertFalse(xmlElement.getOnlyChildElementOptionally("innerNamespace", "unknownNamespace").isPresent()); + + final XmlElement noNamespaceElement = XmlElement.fromString(""); + assertFalse(noNamespaceElement.hasNamespace()); + try { + noNamespaceElement.getNamespace(); + fail(); + } catch (final MissingNameSpaceException e) {} + + final XmlElement inner = xmlElement.getOnlyChildElement("inner"); + final XmlElement deepInner = inner.getOnlyChildElementWithSameNamespaceOptionally().get(); + assertEquals(deepInner, inner.getOnlyChildElementWithSameNamespace()); + assertEquals(Optional.absent(), xmlElement.getOnlyChildElementOptionally("unknown")); + assertEquals("deepValue", deepInner.getTextContent()); + assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get()); + assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get()); + } + + @Test + public void testExtractNamespaces() throws Exception { + final XmlElement innerPrefixed = xmlElement.getOnlyChildElement("innerPrefixed"); + Map.Entry namespaceOfTextContent = innerPrefixed.findNamespaceOfTextContent(); + + assertNotNull(namespaceOfTextContent); + assertEquals("b", namespaceOfTextContent.getKey()); + assertEquals("prefixedValueNamespace", namespaceOfTextContent.getValue()); + final XmlElement innerNamespace = xmlElement.getOnlyChildElement("innerNamespace"); + namespaceOfTextContent = innerNamespace.findNamespaceOfTextContent(); + + assertEquals("", namespaceOfTextContent.getKey()); + assertEquals("innerNamespace", namespaceOfTextContent.getValue()); + } + + @Test + public void testUnrecognisedElements() throws Exception { + xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace")); + + try { + xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner")); + fail(); + } catch (final NetconfDocumentedException e) { + assertThat(e.getMessage(), both(containsString("innerNamespace")).and(containsString("innerNamespace"))); + } + } +} diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java new file mode 100644 index 0000000000..3796dd996a --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.util.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.common.base.Optional; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXParseException; + +public class XmlUtilTest { + + private final String xml = "\n" + + "value\n" + + "prefix:value\n" + + "prefix:value\n" + + ""; + + @Test + public void testCreateElement() throws Exception { + final Document document = XmlUtil.newDocument(); + final Element top = XmlUtil.createElement(document, "top", Optional.of("namespace")); + + top.appendChild(XmlUtil.createTextElement(document, "innerText", "value", Optional.of("namespace"))); + top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref", "prefixNamespace", "value", Optional.of("namespace"))); + top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref", "prefixNamespace", "value", Optional.of("randomNamespace"))); + + document.appendChild(top); + assertEquals("top", XmlUtil.createDocumentCopy(document).getDocumentElement().getTagName()); + + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreWhitespace(true); + + final Diff diff = XMLUnit.compareXML(XMLUnit.buildControlDocument(xml), document); + assertTrue(diff.toString(), diff.similar()); + } + + @Test + public void testLoadSchema() throws Exception { + XmlUtil.loadSchema(); + try { + XmlUtil.loadSchema(getClass().getResourceAsStream("/netconfMessages/commit.xml")); + fail("Input stream does not contain xsd"); + } catch (final IllegalStateException e) { + assertTrue(e.getCause() instanceof SAXParseException); + } + + } + + @Test + public void testXPath() throws Exception { + final XPathExpression correctXPath = XMLNetconfUtil.compileXPath("/top/innerText"); + try { + XMLNetconfUtil.compileXPath("!@(*&$!"); + fail("Incorrect xpath should fail"); + } catch (IllegalStateException e) {} + final Object value = XmlUtil.evaluateXPath(correctXPath, XmlUtil.readXmlToDocument("value"), XPathConstants.NODE); + assertEquals("value", ((Element) value).getTextContent()); + } +} \ No newline at end of file -- 2.36.6