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.yangtools.yang.common.ErrorSeverity;
24 import org.opendaylight.yangtools.yang.common.ErrorTag;
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 NamespaceURN.BASE;
62 public void testToAndFromXMLDocument() throws XPathExpressionException {
63 final String errorMessage = "mock error message";
64 DocumentedException ex = new NetconfDocumentedException(errorMessage, null, ErrorType.PROTOCOL,
65 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", ErrorTag.DATA_EXISTS.elementBody(), errorTagNode.getTextContent());
86 final Node errorSeverityNode = getNode("netconf:error-severity", rpcErrorNode);
87 assertNotNull("error-severity not found", errorSeverityNode);
88 assertEquals("error-severity", ErrorSeverity.WARNING.elementBody(), errorSeverityNode.getTextContent());
90 final Node errorInfoNode = getNode("netconf:error-info/netconf:foo", rpcErrorNode);
91 assertNotNull("foo not found", errorInfoNode);
92 assertEquals("foo", "bar", errorInfoNode.getTextContent());
94 final Node errorMsgNode = getNode("netconf:error-message", rpcErrorNode);
95 assertNotNull("error-message not found", errorMsgNode);
96 assertEquals("error-message", errorMessage, errorMsgNode.getTextContent());
98 // Test fromXMLDocument
100 ex = DocumentedException.fromXMLDocument(doc);
102 assertNotNull("NetconfDocumentedException is null", ex);
103 assertEquals("getErrorSeverity", ErrorSeverity.WARNING, ex.getErrorSeverity());
104 assertEquals("getErrorTag", ErrorTag.DATA_EXISTS, ex.getErrorTag());
105 assertEquals("getErrorType", ErrorType.PROTOCOL, ex.getErrorType());
106 assertEquals("getLocalizedMessage", errorMessage, ex.getLocalizedMessage());
107 assertEquals("getErrorInfo", Map.of("foo", "bar"), ex.getErrorInfo());
110 @SuppressWarnings("unchecked")
111 <T> T getNode(final String xpathExp, final Node node) throws XPathExpressionException {
112 return (T) xpath.compile(xpathExp).evaluate(node, XPathConstants.NODE);