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.config.util.xml.DocumentedException;
25 import org.opendaylight.netconf.api.NetconfDocumentedException;
26 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Node;
32 * Unit tests for NetconfDocumentedException.
34 * @author Thomas Pantelis
36 public class NetconfDocumentedExceptionTest {
41 public void setUp() throws Exception {
42 XPathFactory xPathfactory = XPathFactory.newInstance();
43 xpath = xPathfactory.newXPath();
44 xpath.setNamespaceContext( new NamespaceContext() {
46 public Iterator<?> getPrefixes( String namespaceURI ) {
47 return Collections.singletonList( "netconf" ).iterator();
51 public String getPrefix( String namespaceURI ) {
56 public String getNamespaceURI( String prefix ) {
57 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
63 public void testToAndFromXMLDocument() throws XPathExpressionException {
64 String errorMessage = "mock error message";
65 DocumentedException ex = new NetconfDocumentedException( errorMessage, null,
66 DocumentedException.ErrorType.protocol,
67 DocumentedException.ErrorTag.data_exists,
68 DocumentedException.ErrorSeverity.warning,
69 ImmutableMap.of( "foo", "bar" ) );
71 Document doc = ex.toXMLDocument();
72 assertNotNull( "Document is null", doc );
74 Node rootNode = doc.getDocumentElement();
76 assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
77 assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
79 Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
80 assertNotNull( "rpc-error not found", rpcErrorNode );
82 Node errorTypeNode = getNode( "netconf:error-type", rpcErrorNode );
83 assertNotNull( "error-type not found", errorTypeNode );
84 assertEquals( "error-type", DocumentedException.ErrorType.protocol.getTagValue(),
85 errorTypeNode.getTextContent() );
87 Node errorTagNode = getNode( "netconf:error-tag", rpcErrorNode );
88 assertNotNull( "error-tag not found", errorTagNode );
89 assertEquals( "error-tag", DocumentedException.ErrorTag.data_exists.getTagValue(),
90 errorTagNode.getTextContent() );
92 Node errorSeverityNode = getNode( "netconf:error-severity", rpcErrorNode );
93 assertNotNull( "error-severity not found", errorSeverityNode );
94 assertEquals( "error-severity", DocumentedException.ErrorSeverity.warning.getTagValue(),
95 errorSeverityNode.getTextContent() );
97 Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
98 assertNotNull( "foo not found", errorInfoNode );
99 assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
101 Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
102 assertNotNull( "error-message not found", errorMsgNode );
103 assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
105 // Test fromXMLDocument
107 ex = DocumentedException.fromXMLDocument( doc );
109 assertNotNull( "NetconfDocumentedException is null", ex );
110 assertEquals( "getErrorSeverity", DocumentedException.ErrorSeverity.warning, ex.getErrorSeverity() );
111 assertEquals( "getErrorTag", DocumentedException.ErrorTag.data_exists, ex.getErrorTag() );
112 assertEquals( "getErrorType", DocumentedException.ErrorType.protocol, ex.getErrorType() );
113 assertEquals( "getLocalizedMessage", errorMessage, ex.getLocalizedMessage() );
114 assertEquals( "getErrorInfo", ImmutableMap.of( "foo", "bar" ), ex.getErrorInfo() );
117 @SuppressWarnings("unchecked")
118 <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
119 return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );