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.controller.netconf.api.xml.XmlNetconfConstants;
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 XPathFactory xPathfactory = XPathFactory.newInstance();
42 xpath = xPathfactory.newXPath();
43 xpath.setNamespaceContext( new NamespaceContext() {
45 public Iterator<?> getPrefixes( String namespaceURI ) {
46 return Collections.singletonList( "netconf" ).iterator();
50 public String getPrefix( String namespaceURI ) {
55 public String getNamespaceURI( String prefix ) {
56 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
62 public void testToAndFromXMLDocument() throws XPathExpressionException {
63 String errorMessage = "mock error message";
64 DocumentedException ex = new NetconfDocumentedException( errorMessage, null,
65 DocumentedException.ErrorType.protocol,
66 DocumentedException.ErrorTag.data_exists,
67 DocumentedException.ErrorSeverity.warning,
68 ImmutableMap.of( "foo", "bar" ) );
70 Document doc = ex.toXMLDocument();
71 assertNotNull( "Document is null", doc );
73 Node rootNode = doc.getDocumentElement();
75 assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
76 assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
78 Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
79 assertNotNull( "rpc-error not found", rpcErrorNode );
81 Node errorTypeNode = getNode( "netconf:error-type", rpcErrorNode );
82 assertNotNull( "error-type not found", errorTypeNode );
83 assertEquals( "error-type", DocumentedException.ErrorType.protocol.getTagValue(),
84 errorTypeNode.getTextContent() );
86 Node errorTagNode = getNode( "netconf:error-tag", rpcErrorNode );
87 assertNotNull( "error-tag not found", errorTagNode );
88 assertEquals( "error-tag", DocumentedException.ErrorTag.data_exists.getTagValue(),
89 errorTagNode.getTextContent() );
91 Node errorSeverityNode = getNode( "netconf:error-severity", rpcErrorNode );
92 assertNotNull( "error-severity not found", errorSeverityNode );
93 assertEquals( "error-severity", DocumentedException.ErrorSeverity.warning.getTagValue(),
94 errorSeverityNode.getTextContent() );
96 Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
97 assertNotNull( "foo not found", errorInfoNode );
98 assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
100 Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
101 assertNotNull( "error-message not found", errorMsgNode );
102 assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
104 // Test fromXMLDocument
106 ex = DocumentedException.fromXMLDocument( doc );
108 assertNotNull( "NetconfDocumentedException is null", ex );
109 assertEquals( "getErrorSeverity", DocumentedException.ErrorSeverity.warning, ex.getErrorSeverity() );
110 assertEquals( "getErrorTag", DocumentedException.ErrorTag.data_exists, ex.getErrorTag() );
111 assertEquals( "getErrorType", DocumentedException.ErrorType.protocol, ex.getErrorType() );
112 assertEquals( "getLocalizedMessage", errorMessage, ex.getLocalizedMessage() );
113 assertEquals( "getErrorInfo", ImmutableMap.of( "foo", "bar" ), ex.getErrorInfo() );
116 @SuppressWarnings("unchecked")
117 <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
118 return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );