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