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