faddeca5993393899745d6d6bbe1a5ab959c2804
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfErrorTest.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.controller.sal.restconf.impl.test;
9
10 import static org.hamcrest.CoreMatchers.equalTo;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.hamcrest.BaseMatcher;
18 import org.hamcrest.Description;
19 import org.hamcrest.Matcher;
20 import org.junit.Test;
21 import org.opendaylight.restconf.common.errors.RestconfError;
22 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26
27 /**
28  * Unit tests for RestconfError.
29  *
30  * @author Devin Avery
31  * @author Thomas Pantelis
32  *
33  */
34 public class RestconfErrorTest {
35
36     static class Contains extends BaseMatcher<String> {
37
38         private final String text;
39
40         Contains(final String text) {
41             this.text = text;
42         }
43
44         @Override
45         public void describeTo(final Description desc) {
46             desc.appendText("contains ").appendValue(text);
47         }
48
49         @Override
50         public boolean matches(final Object arg) {
51             return arg != null && arg.toString().contains(text);
52         }
53     }
54
55     @Test
56     public void testErrorTagValueOf() {
57         assertEquals(ErrorTag.IN_USE, ErrorTag.valueOfCaseInsensitive(ErrorTag.IN_USE.getTagValue()));
58     }
59
60     @Test
61     public void testErrorTagValueOfIsLowercase() {
62         assertEquals("in-use", ErrorTag.IN_USE.getTagValue());
63     }
64
65     @Test
66     public void testErrorTagStatusCodes() {
67         Map<String, Integer> lookUpMap = new HashMap<>();
68
69         lookUpMap.put("in-use", 409);
70         lookUpMap.put("invalid-value", 400);
71         lookUpMap.put("too-big", 413);
72         lookUpMap.put("missing-attribute", 400);
73         lookUpMap.put("bad-attribute", 400);
74         lookUpMap.put("unknown-attribute", 400);
75         lookUpMap.put("missing-element", 400);
76         lookUpMap.put("bad-element", 400);
77         lookUpMap.put("unknown-element", 400);
78         lookUpMap.put("unknown-namespace", 400);
79         lookUpMap.put("access-denied", 403);
80         lookUpMap.put("lock-denied", 409);
81         lookUpMap.put("resource-denied", 409);
82         lookUpMap.put("rollback-failed", 500);
83         lookUpMap.put("data-exists", 409);
84         lookUpMap.put("data-missing", 409);
85         lookUpMap.put("operation-not-supported", 501);
86         lookUpMap.put("operation-failed", 500);
87         lookUpMap.put("partial-operation", 500);
88         lookUpMap.put("malformed-message", 400);
89         lookUpMap.put("resource-denied-transport", 503);
90
91         for (ErrorTag tag : ErrorTag.values()) {
92             Integer expectedStatusCode = lookUpMap.get(tag.getTagValue());
93             assertNotNull("Failed to find " + tag.getTagValue(), expectedStatusCode);
94             assertEquals("Status Code does not match", expectedStatusCode, Integer.valueOf(tag.getStatusCode()));
95         }
96     }
97
98     @Test
99     public void testRestConfDocumentedException_NoCause() {
100         String expectedMessage = "Message";
101         ErrorType expectedErrorType = ErrorType.RPC;
102         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
103         RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
104
105         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
106     }
107
108     @Test
109     public void testRestConfDocumentedException_WithAppTag() {
110         String expectedMessage = "Message";
111         ErrorType expectedErrorType = ErrorType.RPC;
112         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
113         String expectedErrorAppTag = "application.tag";
114
115         RestconfError error =
116                 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
117
118         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
119                 error);
120     }
121
122     @Test
123     public void testRestConfDocumentedException_WithAppTagErrorInfo() {
124         String expectedMessage = "Message";
125         ErrorType expectedErrorType = ErrorType.RPC;
126         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
127         String expectedErrorAppTag = "application.tag";
128         String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
129
130         RestconfError error =
131                 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);
132
133         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo,
134                 error);
135     }
136
137     @Test
138     public void testRestConfErrorWithRpcError() {
139
140         // All fields set
141         RpcError rpcError = RpcResultBuilder.newError(
142                 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
143                 "mock app-tag", "mock error-info", new Exception("mock cause"));
144
145         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
146                 "mock error-info", new RestconfError(rpcError));
147
148         // All fields set except 'info' - expect error-info set to 'cause'
149         rpcError = RpcResultBuilder.newError(
150                 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
151                 "mock app-tag", null, new Exception("mock cause"));
152
153         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
154                 new Contains("mock cause"), new RestconfError(rpcError));
155
156         // Some fields set - expect error-info set to ErrorSeverity
157         rpcError = RpcResultBuilder.newError(
158                 RpcError.ErrorType.RPC, ErrorTag.ACCESS_DENIED.getTagValue(), null, null, null, null);
159
160         validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
161                 new RestconfError(rpcError));
162
163         // 'tag' field not mapped to ErrorTag - expect error-tag set to
164         // OPERATION_FAILED
165         rpcError = RpcResultBuilder.newWarning(
166                 RpcError.ErrorType.TRANSPORT, "not mapped", null, null, null, null);
167
168         validateRestConfError(null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, null,
169                 "<severity>warning</severity>", new RestconfError(rpcError));
170
171         // No fields set - edge case
172         rpcError = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, null, null, null, null, null);
173
174         validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
175                 null, "<severity>error</severity>", new RestconfError(rpcError));
176     }
177
178     private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
179             final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
180             final RestconfError error) {
181
182         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
183                 equalTo(errorInfo), error);
184     }
185
186     private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
187             final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
188             final RestconfError error) {
189
190         assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
191         assertEquals("getErrorType", expectedErrorType, error.getErrorType());
192         assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
193         assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
194         assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
195         error.toString(); // really just checking for NPE etc. Don't care about
196                       // contents.
197     }
198 }