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.w3c.dom.Document;
26 import org.w3c.dom.Node;
30 * Unit tests for NetconfDocumentedException.
32 * @author Thomas Pantelis
34 public class NetconfDocumentedExceptionTest {
39 public void setUp() throws Exception {
40 final XPathFactory xPathfactory = XPathFactory.newInstance();
41 xpath = xPathfactory.newXPath();
42 xpath.setNamespaceContext(new NamespaceContext() {
44 public Iterator<String> getPrefixes(final String namespaceURI) {
45 return Iterators.singletonIterator("netconf");
49 public String getPrefix(final String namespaceURI) {
54 public String getNamespaceURI(final String prefix) {
55 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
61 public void testToAndFromXMLDocument() throws XPathExpressionException {
62 final String errorMessage = "mock error message";
63 DocumentedException ex = new NetconfDocumentedException(errorMessage, null,
64 DocumentedException.ErrorType.PROTOCOL,
65 DocumentedException.ErrorTag.DATA_EXISTS,
66 ErrorSeverity.WARNING,
67 Map.of("foo", "bar"));
69 final Document doc = ex.toXMLDocument();
70 assertNotNull("Document is null", doc);
72 final Node rootNode = doc.getDocumentElement();
74 assertEquals("getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI());
75 assertEquals("getLocalName", "rpc-reply", rootNode.getLocalName());
77 final Node rpcErrorNode = getNode("/netconf:rpc-reply/netconf:rpc-error", rootNode);
78 assertNotNull("rpc-error not found", rpcErrorNode);
80 final Node errorTypeNode = getNode("netconf:error-type", rpcErrorNode);
81 assertNotNull("error-type not found", errorTypeNode);
82 assertEquals("error-type", DocumentedException.ErrorType.PROTOCOL.getTypeValue(),
83 errorTypeNode.getTextContent());
85 final Node errorTagNode = getNode("netconf:error-tag", rpcErrorNode);
86 assertNotNull("error-tag not found", errorTagNode);
87 assertEquals("error-tag", DocumentedException.ErrorTag.DATA_EXISTS.getTagValue(),
88 errorTagNode.getTextContent());
90 final Node errorSeverityNode = getNode("netconf:error-severity", rpcErrorNode);
91 assertNotNull("error-severity not found", errorSeverityNode);
92 assertEquals("error-severity", ErrorSeverity.WARNING.elementBody(), errorSeverityNode.getTextContent());
94 final Node errorInfoNode = getNode("netconf:error-info/netconf:foo", rpcErrorNode);
95 assertNotNull("foo not found", errorInfoNode);
96 assertEquals("foo", "bar", errorInfoNode.getTextContent());
98 final Node errorMsgNode = getNode("netconf:error-message", rpcErrorNode);
99 assertNotNull("error-message not found", errorMsgNode);
100 assertEquals("error-message", errorMessage, errorMsgNode.getTextContent());
102 // Test fromXMLDocument
104 ex = DocumentedException.fromXMLDocument(doc);
106 assertNotNull("NetconfDocumentedException is null", ex);
107 assertEquals("getErrorSeverity", ErrorSeverity.WARNING, ex.getErrorSeverity());
108 assertEquals("getErrorTag", DocumentedException.ErrorTag.DATA_EXISTS, ex.getErrorTag());
109 assertEquals("getErrorType", DocumentedException.ErrorType.PROTOCOL, ex.getErrorType());
110 assertEquals("getLocalizedMessage", errorMessage, ex.getLocalizedMessage());
111 assertEquals("getErrorInfo", Map.of("foo", "bar"), ex.getErrorInfo());
114 @SuppressWarnings("unchecked")
115 <T> T getNode(final String xpathExp, final Node node) throws XPathExpressionException {
116 return (T) xpath.compile(xpathExp).evaluate(node, XPathConstants.NODE);