2 * Copyright (c) 2014 Brocade Communications 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
8 package org.opendaylight.netconf.api;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import com.google.common.collect.Iterators;
14 import java.util.Iterator;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.xpath.XPath;
18 import javax.xml.xpath.XPathConstants;
19 import javax.xml.xpath.XPathExpressionException;
20 import javax.xml.xpath.XPathFactory;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Node;
31 * Unit tests for NetconfDocumentedException.
33 * @author Thomas Pantelis
35 public class NetconfDocumentedExceptionTest {
40 public void setUp() throws Exception {
41 final XPathFactory xPathfactory = XPathFactory.newInstance();
42 xpath = xPathfactory.newXPath();
43 xpath.setNamespaceContext(new NamespaceContext() {
45 public Iterator<String> getPrefixes(final String namespaceURI) {
46 return Iterators.singletonIterator("netconf");
50 public String getPrefix(final String namespaceURI) {
55 public String getNamespaceURI(final String prefix) {
56 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
62 public void testToAndFromXMLDocument() throws XPathExpressionException {
63 final String errorMessage = "mock error message";
64 DocumentedException ex = new NetconfDocumentedException(errorMessage, null, ErrorType.PROTOCOL,
65 DocumentedException.ErrorTag.DATA_EXISTS, ErrorSeverity.WARNING, Map.of("foo", "bar"));
67 final Document doc = ex.toXMLDocument();
68 assertNotNull("Document is null", doc);
70 final Node rootNode = doc.getDocumentElement();
72 assertEquals("getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI());
73 assertEquals("getLocalName", "rpc-reply", rootNode.getLocalName());
75 final Node rpcErrorNode = getNode("/netconf:rpc-reply/netconf:rpc-error", rootNode);
76 assertNotNull("rpc-error not found", rpcErrorNode);
78 final Node errorTypeNode = getNode("netconf:error-type", rpcErrorNode);
79 assertNotNull("error-type not found", errorTypeNode);
80 assertEquals("error-type", ErrorType.PROTOCOL.elementBody(), errorTypeNode.getTextContent());
82 final Node errorTagNode = getNode("netconf:error-tag", rpcErrorNode);
83 assertNotNull("error-tag not found", errorTagNode);
84 assertEquals("error-tag", DocumentedException.ErrorTag.DATA_EXISTS.getTagValue(),
85 errorTagNode.getTextContent());
87 final Node errorSeverityNode = getNode("netconf:error-severity", rpcErrorNode);
88 assertNotNull("error-severity not found", errorSeverityNode);
89 assertEquals("error-severity", ErrorSeverity.WARNING.elementBody(), errorSeverityNode.getTextContent());
91 final Node errorInfoNode = getNode("netconf:error-info/netconf:foo", rpcErrorNode);
92 assertNotNull("foo not found", errorInfoNode);
93 assertEquals("foo", "bar", errorInfoNode.getTextContent());
95 final Node errorMsgNode = getNode("netconf:error-message", rpcErrorNode);
96 assertNotNull("error-message not found", errorMsgNode);
97 assertEquals("error-message", errorMessage, errorMsgNode.getTextContent());
99 // Test fromXMLDocument
101 ex = DocumentedException.fromXMLDocument(doc);
103 assertNotNull("NetconfDocumentedException is null", ex);
104 assertEquals("getErrorSeverity", ErrorSeverity.WARNING, ex.getErrorSeverity());
105 assertEquals("getErrorTag", DocumentedException.ErrorTag.DATA_EXISTS, ex.getErrorTag());
106 assertEquals("getErrorType", ErrorType.PROTOCOL, ex.getErrorType());
107 assertEquals("getLocalizedMessage", errorMessage, ex.getLocalizedMessage());
108 assertEquals("getErrorInfo", Map.of("foo", "bar"), ex.getErrorInfo());
111 @SuppressWarnings("unchecked")
112 <T> T getNode(final String xpathExp, final Node node) throws XPathExpressionException {
113 return (T) xpath.compile(xpathExp).evaluate(node, XPathConstants.NODE);