Merge "Fix raw references to Iterator"
[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.netconf.api.NetconfDocumentedException.ErrorSeverity;
25 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
27 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Node;
30
31
32 /**
33  * Unit tests for NetconfDocumentedException.
34  *
35  * @author Thomas Pantelis
36  */
37 public class NetconfDocumentedExceptionTest {
38
39     private XPath xpath;
40
41     @Before
42     public void setUp() throws Exception {
43         XPathFactory xPathfactory = XPathFactory.newInstance();
44         xpath = xPathfactory.newXPath();
45         xpath.setNamespaceContext( new NamespaceContext() {
46             @Override
47             public Iterator<?> getPrefixes( String namespaceURI ) {
48                 return Collections.singletonList( "netconf" ).iterator();
49             }
50
51             @Override
52             public String getPrefix( String namespaceURI ) {
53                 return "netconf";
54             }
55
56             @Override
57             public String getNamespaceURI( String prefix ) {
58                 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
59             }
60         } );
61     }
62
63     @Test
64     public void testToAndFromXMLDocument() throws XPathExpressionException {
65         String errorMessage = "mock error message";
66         NetconfDocumentedException ex = new NetconfDocumentedException( errorMessage, null,
67                                                                         ErrorType.protocol,
68                                                                         ErrorTag.data_exists,
69                                                                         ErrorSeverity.warning,
70                                                                         ImmutableMap.of( "foo", "bar" ) );
71
72         Document doc = ex.toXMLDocument();
73         assertNotNull( "Document is null", doc );
74
75         Node rootNode = doc.getDocumentElement();
76
77         assertEquals( "getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI() );
78         assertEquals( "getLocalName", "rpc-reply", rootNode.getLocalName() );
79
80         Node rpcErrorNode = getNode( "/netconf:rpc-reply/netconf:rpc-error", rootNode );
81         assertNotNull( "rpc-error not found", rpcErrorNode );
82
83         Node errorTypeNode = getNode( "netconf:error-type", rpcErrorNode );
84         assertNotNull( "error-type not found", errorTypeNode );
85         assertEquals( "error-type", ErrorType.protocol.getTagValue(),
86                       errorTypeNode.getTextContent() );
87
88         Node errorTagNode = getNode( "netconf:error-tag", rpcErrorNode );
89         assertNotNull( "error-tag not found", errorTagNode );
90         assertEquals( "error-tag", ErrorTag.data_exists.getTagValue(),
91                       errorTagNode.getTextContent() );
92
93         Node errorSeverityNode = getNode( "netconf:error-severity", rpcErrorNode );
94         assertNotNull( "error-severity not found", errorSeverityNode );
95         assertEquals( "error-severity", ErrorSeverity.warning.getTagValue(),
96                       errorSeverityNode.getTextContent() );
97
98         Node errorInfoNode = getNode( "netconf:error-info/netconf:foo", rpcErrorNode );
99         assertNotNull( "foo not found", errorInfoNode );
100         assertEquals( "foo", "bar", errorInfoNode.getTextContent() );
101
102         Node errorMsgNode = getNode( "netconf:error-message", rpcErrorNode );
103         assertNotNull( "error-message not found", errorMsgNode );
104         assertEquals( "error-message", errorMessage, errorMsgNode.getTextContent() );
105
106         // Test fromXMLDocument
107
108         ex = NetconfDocumentedException.fromXMLDocument( doc );
109
110         assertNotNull( "NetconfDocumentedException is null", ex );
111         assertEquals( "getErrorSeverity", ErrorSeverity.warning, ex.getErrorSeverity() );
112         assertEquals( "getErrorTag", ErrorTag.data_exists, ex.getErrorTag() );
113         assertEquals( "getErrorType", ErrorType.protocol, ex.getErrorType() );
114         assertEquals( "getLocalizedMessage", errorMessage, ex.getLocalizedMessage() );
115         assertEquals( "getErrorInfo", ImmutableMap.of( "foo", "bar" ), ex.getErrorInfo() );
116     }
117
118     @SuppressWarnings("unchecked")
119     <T> T getNode( String xpathExp, Node node ) throws XPathExpressionException {
120         return (T)xpath.compile( xpathExp ).evaluate( node, XPathConstants.NODE );
121     }
122 }
123