3621f83312982d5570d29c37541a5f8a6b51349f
[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 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;
28
29
30 /**
31  * Unit tests for NetconfDocumentedException.
32  *
33  * @author Thomas Pantelis
34  */
35 public class NetconfDocumentedExceptionTest {
36
37     private XPath xpath;
38
39     @Before
40     public void setUp() throws Exception {
41         XPathFactory xPathfactory = XPathFactory.newInstance();
42         xpath = xPathfactory.newXPath();
43         xpath.setNamespaceContext( new NamespaceContext() {
44             @Override
45             public Iterator<?> getPrefixes( String namespaceURI ) {
46                 return Collections.singletonList( "netconf" ).iterator();
47             }
48
49             @Override
50             public String getPrefix( String namespaceURI ) {
51                 return "netconf";
52             }
53
54             @Override
55             public String getNamespaceURI( String prefix ) {
56                 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
57             }
58         } );
59     }
60
61     @Test
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" ) );
69
70         Document doc = ex.toXMLDocument();
71         assertNotNull( "Document is null", doc );
72
73         Node rootNode = doc.getDocumentElement();
74
75         assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
76         assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
77
78         Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
79         assertNotNull( "rpc-error not found", rpcErrorNode );
80
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() );
85
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() );
90
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() );
95
96         Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
97         assertNotNull( "foo not found", errorInfoNode );
98         assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
99
100         Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
101         assertNotNull( "error-message not found", errorMsgNode );
102         assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
103
104         // Test fromXMLDocument
105
106         ex = DocumentedException.fromXMLDocument( doc );
107
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() );
114     }
115
116     @SuppressWarnings("unchecked")
117     <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
118         return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );
119     }
120 }
121