X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FXmlUtilTest.java;fp=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FXmlUtilTest.java;h=3796dd996ae47179265837c488a58aa52c60870d;hb=09e43fe487f7cee40caa96ba554ac8b026ba5d64;hp=0000000000000000000000000000000000000000;hpb=ce9ca282f8e0eb5a0cd1b97cab882f9f80fa598a;p=controller.git 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