Bug 977: Return RpcError result on neconf failure
[controller.git] / opendaylight / netconf / netconf-api / src / test / java / org / opendaylight / controller / netconf / api / NetconfDocumentedExceptionTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.api;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.Collections;
15 import java.util.Iterator;
16
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
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;
31
32 import com.google.common.collect.ImmutableMap;
33
34
35 /**
36  * Unit tests for NetconfDocumentedException.
37  *
38  * @author Thomas Pantelis
39  */
40 public class NetconfDocumentedExceptionTest {
41
42     private XPath xpath;
43
44     @Before
45     public void setUp() throws Exception {
46         XPathFactory xPathfactory = XPathFactory.newInstance();
47         xpath = xPathfactory.newXPath();
48         xpath.setNamespaceContext( new NamespaceContext() {
49             @Override
50             public Iterator<?> getPrefixes( String namespaceURI ) {
51                 return Collections.singletonList( "netconf" ).iterator();
52             }
53
54             @Override
55             public String getPrefix( String namespaceURI ) {
56                 return "netconf";
57             }
58
59             @Override
60             public String getNamespaceURI( String prefix ) {
61                 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
62             }
63         } );
64     }
65
66     @Test
67     public void testToAndFromXMLDocument() throws XPathExpressionException {
68         String errorMessage = "mock error message";
69         NetconfDocumentedException ex = new NetconfDocumentedException( errorMessage, null,
70                                                                         ErrorType.protocol,
71                                                                         ErrorTag.data_exists,
72                                                                         ErrorSeverity.warning,
73                                                                         ImmutableMap.of( "foo", "bar" ) );
74
75         Document doc = ex.toXMLDocument();
76         assertNotNull( "Document is null", doc );
77
78         Node rootNode = doc.getDocumentElement();
79
80         assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
81         assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
82
83         Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
84         assertNotNull( "rpc-error not found", rpcErrorNode );
85
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() );
90
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() );
95
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() );
100
101         Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
102         assertNotNull( "foo not found", errorInfoNode );
103         assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
104
105         Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
106         assertNotNull( "error-message not found", errorMsgNode );
107         assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
108
109         // Test fromXMLDocument
110
111         ex = NetconfDocumentedException.fromXMLDocument( doc );
112
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() );
119     }
120
121     @SuppressWarnings("unchecked")
122     <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
123         return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );
124     }
125 }
126