Remove DocumentedException.ErrorType
[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 package org.opendaylight.netconf.api;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import com.google.common.collect.Iterators;
14 import java.util.Iterator;
15 import java.util.Map;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.xpath.XPath;
18 import javax.xml.xpath.XPathConstants;
19 import javax.xml.xpath.XPathExpressionException;
20 import javax.xml.xpath.XPathFactory;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
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         final XPathFactory xPathfactory = XPathFactory.newInstance();
42         xpath = xPathfactory.newXPath();
43         xpath.setNamespaceContext(new NamespaceContext() {
44             @Override
45             public Iterator<String> getPrefixes(final String namespaceURI) {
46                 return Iterators.singletonIterator("netconf");
47             }
48
49             @Override
50             public String getPrefix(final String namespaceURI) {
51                 return "netconf";
52             }
53
54             @Override
55             public String getNamespaceURI(final 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         final String errorMessage = "mock error message";
64         DocumentedException ex = new NetconfDocumentedException(errorMessage, null, ErrorType.PROTOCOL,
65                 DocumentedException.ErrorTag.DATA_EXISTS, ErrorSeverity.WARNING, Map.of("foo", "bar"));
66
67         final Document doc = ex.toXMLDocument();
68         assertNotNull("Document is null", doc);
69
70         final Node rootNode = doc.getDocumentElement();
71
72         assertEquals("getNamespaceURI", "urn:ietf:params:xml:ns:netconf:base:1.0", rootNode.getNamespaceURI());
73         assertEquals("getLocalName", "rpc-reply", rootNode.getLocalName());
74
75         final Node rpcErrorNode = getNode("/netconf:rpc-reply/netconf:rpc-error", rootNode);
76         assertNotNull("rpc-error not found", rpcErrorNode);
77
78         final Node errorTypeNode = getNode("netconf:error-type", rpcErrorNode);
79         assertNotNull("error-type not found", errorTypeNode);
80         assertEquals("error-type", ErrorType.PROTOCOL.elementBody(), errorTypeNode.getTextContent());
81
82         final Node errorTagNode = getNode("netconf:error-tag", rpcErrorNode);
83         assertNotNull("error-tag not found", errorTagNode);
84         assertEquals("error-tag", DocumentedException.ErrorTag.DATA_EXISTS.getTagValue(),
85                 errorTagNode.getTextContent());
86
87         final Node errorSeverityNode = getNode("netconf:error-severity", rpcErrorNode);
88         assertNotNull("error-severity not found", errorSeverityNode);
89         assertEquals("error-severity", ErrorSeverity.WARNING.elementBody(), errorSeverityNode.getTextContent());
90
91         final Node errorInfoNode = getNode("netconf:error-info/netconf:foo", rpcErrorNode);
92         assertNotNull("foo not found", errorInfoNode);
93         assertEquals("foo", "bar", errorInfoNode.getTextContent());
94
95         final Node errorMsgNode = getNode("netconf:error-message", rpcErrorNode);
96         assertNotNull("error-message not found", errorMsgNode);
97         assertEquals("error-message", errorMessage, errorMsgNode.getTextContent());
98
99         // Test fromXMLDocument
100
101         ex = DocumentedException.fromXMLDocument(doc);
102
103         assertNotNull("NetconfDocumentedException is null", ex);
104         assertEquals("getErrorSeverity", ErrorSeverity.WARNING, ex.getErrorSeverity());
105         assertEquals("getErrorTag", DocumentedException.ErrorTag.DATA_EXISTS, ex.getErrorTag());
106         assertEquals("getErrorType", ErrorType.PROTOCOL, ex.getErrorType());
107         assertEquals("getLocalizedMessage", errorMessage, ex.getLocalizedMessage());
108         assertEquals("getErrorInfo", Map.of("foo", "bar"), ex.getErrorInfo());
109     }
110
111     @SuppressWarnings("unchecked")
112     <T> T getNode(final String xpathExp, final Node node) throws XPathExpressionException {
113         return (T) xpath.compile(xpathExp).evaluate(node, XPathConstants.NODE);
114     }
115 }
116