2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.hamcrest.CoreMatchers.equalTo;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
15 import java.util.HashMap;
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.restconf.common.errors.RestconfError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 * Unit tests for RestconfError.
31 * @author Thomas Pantelis
34 public class RestconfErrorTest {
36 static class Contains extends BaseMatcher<String> {
38 private final String text;
40 Contains(final String text) {
45 public void describeTo(final Description desc) {
46 desc.appendText("contains ").appendValue(text);
50 public boolean matches(final Object arg) {
51 return arg != null && arg.toString().contains(text);
56 public void testErrorTagValueOf() {
57 assertEquals(ErrorTag.IN_USE, ErrorTag.valueOfCaseInsensitive(ErrorTag.IN_USE.getTagValue()));
61 public void testErrorTagValueOfIsLowercase() {
62 assertEquals("in-use", ErrorTag.IN_USE.getTagValue());
66 public void testErrorTypeGetErrorTypeTagIsLowerCase() {
67 assertEquals(ErrorType.APPLICATION.name().toLowerCase(), ErrorType.APPLICATION.getErrorTypeTag());
71 public void testErrorTypeValueOf() {
72 assertEquals(ErrorType.APPLICATION, ErrorType.valueOfCaseInsensitive(ErrorType.APPLICATION.getErrorTypeTag()));
76 public void testErrorTagStatusCodes() {
77 Map<String, Integer> lookUpMap = new HashMap<>();
79 lookUpMap.put("in-use", 409);
80 lookUpMap.put("invalid-value", 400);
81 lookUpMap.put("too-big", 413);
82 lookUpMap.put("missing-attribute", 400);
83 lookUpMap.put("bad-attribute", 400);
84 lookUpMap.put("unknown-attribute", 400);
85 lookUpMap.put("missing-element", 400);
86 lookUpMap.put("bad-element", 400);
87 lookUpMap.put("unknown-element", 400);
88 lookUpMap.put("unknown-namespace", 400);
89 lookUpMap.put("access-denied", 403);
90 lookUpMap.put("lock-denied", 409);
91 lookUpMap.put("resource-denied", 409);
92 lookUpMap.put("rollback-failed", 500);
93 lookUpMap.put("data-exists", 409);
94 lookUpMap.put("data-missing", 404);
95 lookUpMap.put("operation-not-supported", 501);
96 lookUpMap.put("operation-failed", 500);
97 lookUpMap.put("partial-operation", 500);
98 lookUpMap.put("malformed-message", 400);
99 lookUpMap.put("resource-denied-transport", 503);
101 for (ErrorTag tag : ErrorTag.values()) {
102 Integer expectedStatusCode = lookUpMap.get(tag.getTagValue());
103 assertNotNull("Failed to find " + tag.getTagValue(), expectedStatusCode);
104 assertEquals("Status Code does not match", expectedStatusCode, Integer.valueOf(tag.getStatusCode()));
109 public void testRestConfDocumentedException_NoCause() {
110 String expectedMessage = "Message";
111 ErrorType expectedErrorType = ErrorType.RPC;
112 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
113 RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
115 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
119 public void testRestConfDocumentedException_WithAppTag() {
120 String expectedMessage = "Message";
121 ErrorType expectedErrorType = ErrorType.RPC;
122 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
123 String expectedErrorAppTag = "application.tag";
125 RestconfError error =
126 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
128 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
133 public void testRestConfDocumentedException_WithAppTagErrorInfo() {
134 String expectedMessage = "Message";
135 ErrorType expectedErrorType = ErrorType.RPC;
136 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
137 String expectedErrorAppTag = "application.tag";
138 String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
140 RestconfError error =
141 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);
143 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo,
148 public void testRestConfErrorWithRpcError() {
151 RpcError rpcError = RpcResultBuilder.newError(
152 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
153 "mock app-tag", "mock error-info", new Exception("mock cause"));
155 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
156 "mock error-info", new RestconfError(rpcError));
158 // All fields set except 'info' - expect error-info set to 'cause'
159 rpcError = RpcResultBuilder.newError(
160 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
161 "mock app-tag", null, new Exception("mock cause"));
163 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
164 new Contains("mock cause"), new RestconfError(rpcError));
166 // Some fields set - expect error-info set to ErrorSeverity
167 rpcError = RpcResultBuilder.newError(
168 RpcError.ErrorType.RPC, ErrorTag.ACCESS_DENIED.getTagValue(), null, null, null, null);
170 validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
171 new RestconfError(rpcError));
173 // 'tag' field not mapped to ErrorTag - expect error-tag set to
175 rpcError = RpcResultBuilder.newWarning(
176 RpcError.ErrorType.TRANSPORT, "not mapped", null, null, null, null);
178 validateRestConfError(null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, null,
179 "<severity>warning</severity>", new RestconfError(rpcError));
181 // No fields set - edge case
182 rpcError = RpcResultBuilder.newError(null, null, null, null, null, null);
184 validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
185 null, "<severity>error</severity>", new RestconfError(rpcError));
188 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
189 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
190 final RestconfError error) {
192 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
193 equalTo(errorInfo), error);
196 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
197 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
198 final RestconfError error) {
200 assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
201 assertEquals("getErrorType", expectedErrorType, error.getErrorType());
202 assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
203 assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
204 assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
205 error.toString(); // really just checking for NPE etc. Don't care about