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.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
14 import org.hamcrest.BaseMatcher;
15 import org.hamcrest.Description;
16 import org.hamcrest.Matcher;
17 import org.junit.Test;
18 import org.opendaylight.restconf.common.errors.RestconfError;
19 import org.opendaylight.yangtools.yang.common.ErrorTag;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 * Unit tests for RestconfError.
28 * @author Thomas Pantelis
31 public class RestconfErrorTest {
33 static class Contains extends BaseMatcher<String> {
35 private final String text;
37 Contains(final String text) {
42 public void describeTo(final Description desc) {
43 desc.appendText("contains ").appendValue(text);
47 public boolean matches(final Object arg) {
48 return arg != null && arg.toString().contains(text);
53 public void testRestConfDocumentedException_NoCause() {
54 String expectedMessage = "Message";
55 ErrorType expectedErrorType = ErrorType.RPC;
56 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
57 RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
59 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
63 public void testRestConfDocumentedException_WithAppTag() {
64 String expectedMessage = "Message";
65 ErrorType expectedErrorType = ErrorType.RPC;
66 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
67 String expectedErrorAppTag = "application.tag";
70 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
72 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
77 public void testRestConfDocumentedException_WithAppTagErrorInfo() {
78 String expectedMessage = "Message";
79 ErrorType expectedErrorType = ErrorType.RPC;
80 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
81 String expectedErrorAppTag = "application.tag";
82 String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
85 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);
87 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo,
92 public void testRestConfErrorWithRpcError() {
95 RpcError rpcError = RpcResultBuilder.newError(
96 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.elementBody(), "mock error-message",
97 "mock app-tag", "mock error-info", new Exception("mock cause"));
99 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
100 "mock error-info", new RestconfError(rpcError));
102 // All fields set except 'info' - expect error-info set to 'cause'
103 rpcError = RpcResultBuilder.newError(
104 RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.elementBody(), "mock error-message",
105 "mock app-tag", null, new Exception("mock cause"));
107 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
108 new Contains("mock cause"), new RestconfError(rpcError));
110 // Some fields set - expect error-info set to ErrorSeverity
111 rpcError = RpcResultBuilder.newError(
112 RpcError.ErrorType.RPC, ErrorTag.ACCESS_DENIED.elementBody(), null, null, null, null);
114 validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
115 new RestconfError(rpcError));
117 // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
118 rpcError = RpcResultBuilder.newWarning(RpcError.ErrorType.TRANSPORT, "not mapped", null, null, null, null);
120 validateRestConfError(null, ErrorType.TRANSPORT, new ErrorTag("not mapped"), null,
121 "<severity>warning</severity>", new RestconfError(rpcError));
123 // No fields set - edge case
124 rpcError = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, null, null, null, null, null);
126 validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
127 null, "<severity>error</severity>", new RestconfError(rpcError));
130 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
131 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
132 final RestconfError error) {
134 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
135 equalTo(errorInfo), error);
138 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
139 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
140 final RestconfError error) {
142 assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
143 assertEquals("getErrorType", expectedErrorType, error.getErrorType());
144 assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
145 assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
146 assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
147 error.toString(); // really just checking for NPE etc. Don't care about