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 java.util.Collections;
15 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;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
27 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
28 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Node;
32 import com.google.common.collect.ImmutableMap;
36 * Unit tests for NetconfDocumentedException.
38 * @author Thomas Pantelis
40 public class NetconfDocumentedExceptionTest {
45 public void setUp() throws Exception {
46 XPathFactory xPathfactory = XPathFactory.newInstance();
47 xpath = xPathfactory.newXPath();
48 xpath.setNamespaceContext( new NamespaceContext() {
50 public Iterator<?> getPrefixes( String namespaceURI ) {
51 return Collections.singletonList( "netconf" ).iterator();
55 public String getPrefix( String namespaceURI ) {
60 public String getNamespaceURI( String prefix ) {
61 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
67 public void testToAndFromXMLDocument() throws XPathExpressionException {
68 String errorMessage = "mock error message";
69 NetconfDocumentedException ex = new NetconfDocumentedException( errorMessage, null,
72 ErrorSeverity.warning,
73 ImmutableMap.of( "foo", "bar" ) );
75 Document doc = ex.toXMLDocument();
76 assertNotNull( "Document is null", doc );
78 Node rootNode = doc.getDocumentElement();
80 assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
81 assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
83 Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
84 assertNotNull( "rpc-error not found", rpcErrorNode );
86 Node errorTypeNode = getNode( "netconf:error-type", rpcErrorNode );
87 assertNotNull( "error-type not found", errorTypeNode );
88 assertEquals( "error-type", ErrorType.protocol.getTagValue(),
89 errorTypeNode.getTextContent() );
91 Node errorTagNode = getNode( "netconf:error-tag", rpcErrorNode );
92 assertNotNull( "error-tag not found", errorTagNode );
93 assertEquals( "error-tag", ErrorTag.data_exists.getTagValue(),
94 errorTagNode.getTextContent() );
96 Node errorSeverityNode = getNode( "netconf:error-severity", rpcErrorNode );
97 assertNotNull( "error-severity not found", errorSeverityNode );
98 assertEquals( "error-severity", ErrorSeverity.warning.getTagValue(),
99 errorSeverityNode.getTextContent() );
101 Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
102 assertNotNull( "foo not found", errorInfoNode );
103 assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
105 Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
106 assertNotNull( "error-message not found", errorMsgNode );
107 assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
109 // Test fromXMLDocument
111 ex = NetconfDocumentedException.fromXMLDocument( doc );
113 assertNotNull( "NetconfDocumentedException is null", ex );
114 assertEquals( "getErrorSeverity", ErrorSeverity.warning, ex.getErrorSeverity() );
115 assertEquals( "getErrorTag", ErrorTag.data_exists, ex.getErrorTag() );
116 assertEquals( "getErrorType", ErrorType.protocol, ex.getErrorType() );
117 assertEquals( "getLocalizedMessage", errorMessage, ex.getLocalizedMessage() );
118 assertEquals( "getErrorInfo", ImmutableMap.of( "foo", "bar" ), ex.getErrorInfo() );
121 @SuppressWarnings("unchecked")
122 <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
123 return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );