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(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock error-message",
96 "mock app-tag", "mock error-info", new Exception("mock cause"));
98 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
99 "mock error-info", new RestconfError(rpcError));
101 // All fields set except 'info' - expect error-info set to 'cause'
102 rpcError = RpcResultBuilder.newError(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock error-message",
103 "mock app-tag", null, new Exception("mock cause"));
105 validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
106 new Contains("mock cause"), new RestconfError(rpcError));
108 // Some fields set - expect error-info set to ErrorSeverity
109 rpcError = RpcResultBuilder.newError(ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, null, null, null);
111 validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
112 new RestconfError(rpcError));
114 // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
115 rpcError = RpcResultBuilder.newWarning(ErrorType.TRANSPORT, new ErrorTag("not mapped"), null, null, null, null);
117 validateRestConfError(null, ErrorType.TRANSPORT, new ErrorTag("not mapped"), null,
118 "<severity>warning</severity>", new RestconfError(rpcError));
120 // No fields set - edge case
121 rpcError = RpcResultBuilder.newError(ErrorType.APPLICATION, null, null, null, null, null);
123 validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
124 null, "<severity>error</severity>", new RestconfError(rpcError));
127 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
128 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
129 final RestconfError error) {
131 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
132 equalTo(errorInfo), error);
135 private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
136 final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
137 final RestconfError error) {
139 assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
140 assertEquals("getErrorType", expectedErrorType, error.getErrorType());
141 assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
142 assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
143 assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
144 error.toString(); // really just checking for NPE etc. Don't care about