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
9 package org.opendaylight.controller.netconf.api;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import javax.xml.namespace.NamespaceContext;
18 import javax.xml.xpath.XPath;
19 import javax.xml.xpath.XPathConstants;
20 import javax.xml.xpath.XPathExpressionException;
21 import javax.xml.xpath.XPathFactory;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
25 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
27 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Node;
33 * Unit tests for NetconfDocumentedException.
35 * @author Thomas Pantelis
37 public class NetconfDocumentedExceptionTest {
42 public void setUp() throws Exception {
43 XPathFactory xPathfactory = XPathFactory.newInstance();
44 xpath = xPathfactory.newXPath();
45 xpath.setNamespaceContext( new NamespaceContext() {
47 public Iterator<?> getPrefixes( String namespaceURI ) {
48 return Collections.singletonList( "netconf" ).iterator();
52 public String getPrefix( String namespaceURI ) {
57 public String getNamespaceURI( String prefix ) {
58 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
64 public void testToAndFromXMLDocument() throws XPathExpressionException {
65 String errorMessage = "mock error message";
66 NetconfDocumentedException ex = new NetconfDocumentedException( errorMessage, null,
69 ErrorSeverity.warning,
70 ImmutableMap.of( "foo", "bar" ) );
72 Document doc = ex.toXMLDocument();
73 assertNotNull( "Document is null", doc );
75 Node rootNode = doc.getDocumentElement();
77 assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
78 assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
80 Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
81 assertNotNull( "rpc-error not found", rpcErrorNode );
83 Node errorTypeNode = getNode( "netconf:error-type", rpcErrorNode );
84 assertNotNull( "error-type not found", errorTypeNode );
85 assertEquals( "error-type", ErrorType.protocol.getTagValue(),
86 errorTypeNode.getTextContent() );
88 Node errorTagNode = getNode( "netconf:error-tag", rpcErrorNode );
89 assertNotNull( "error-tag not found", errorTagNode );
90 assertEquals( "error-tag", ErrorTag.data_exists.getTagValue(),
91 errorTagNode.getTextContent() );
93 Node errorSeverityNode = getNode( "netconf:error-severity", rpcErrorNode );
94 assertNotNull( "error-severity not found", errorSeverityNode );
95 assertEquals( "error-severity", ErrorSeverity.warning.getTagValue(),
96 errorSeverityNode.getTextContent() );
98 Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
99 assertNotNull( "foo not found", errorInfoNode );
100 assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
102 Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
103 assertNotNull( "error-message not found", errorMsgNode );
104 assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
106 // Test fromXMLDocument
108 ex = NetconfDocumentedException.fromXMLDocument( doc );
110 assertNotNull( "NetconfDocumentedException is null", ex );
111 assertEquals( "getErrorSeverity", ErrorSeverity.warning, ex.getErrorSeverity() );
112 assertEquals( "getErrorTag", ErrorTag.data_exists, ex.getErrorTag() );
113 assertEquals( "getErrorType", ErrorType.protocol, ex.getErrorType() );
114 assertEquals( "getLocalizedMessage", errorMessage, ex.getLocalizedMessage() );
115 assertEquals( "getErrorInfo", ImmutableMap.of( "foo", "bar" ), ex.getErrorInfo() );
118 @SuppressWarnings("unchecked")
119 <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
120 return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );