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;
18 import javax.ws.rs.core.Response.Status;
20 import org.hamcrest.BaseMatcher;
21 import org.hamcrest.Description;
22 import org.hamcrest.Matcher;
23 import org.junit.Test;
25 import static org.opendaylight.controller.sal.common.util.RpcErrors.getRpcError;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
30 import org.opendaylight.yangtools.yang.common.RpcError;
33 * Unit tests for RestconfError.
36 * @author Thomas Pantelis
39 public class RestconfErrorTest {
41 static class Contains extends BaseMatcher<String> {
43 private final String text;
45 public Contains( String text ) {
50 public void describeTo( Description desc ) {
51 desc.appendText( "contains " ).appendValue( text );
55 public boolean matches( Object arg ) {
56 return arg != null && arg.toString().contains( text );
61 public void testErrorTagValueOf()
63 assertEquals( ErrorTag.IN_USE,
64 ErrorTag.valueOfCaseInsensitive( ErrorTag.IN_USE.getTagValue() ) );
68 public void testErrorTagValueOfIsLowercase()
70 assertEquals( "in-use",
71 ErrorTag.IN_USE.getTagValue() );
75 public void testErrorTypeGetErrorTypeTagIsLowerCase()
77 assertEquals( ErrorType.APPLICATION.name().toLowerCase(),
78 ErrorType.APPLICATION.getErrorTypeTag() );
82 public void testErrorTypeValueOf()
84 assertEquals( ErrorType.APPLICATION,
85 ErrorType.valueOfCaseInsensitive( ErrorType.APPLICATION.getErrorTypeTag() ) );
89 public void testErrorTagStatusCodes()
91 Map<String,Status> lookUpMap = new HashMap<String,Status>();
93 lookUpMap.put( "in-use", Status.fromStatusCode(409));
94 lookUpMap.put( "invalid-value", Status.fromStatusCode(400));
95 lookUpMap.put( "too-big", Status.fromStatusCode(413));
96 lookUpMap.put( "missing-attribute", Status.fromStatusCode(400));
97 lookUpMap.put( "bad-attribute", Status.fromStatusCode(400));
98 lookUpMap.put( "unknown-attribute", Status.fromStatusCode(400));
99 lookUpMap.put( "bad-element", Status.fromStatusCode(400));
100 lookUpMap.put( "unknown-element", Status.fromStatusCode(400));
101 lookUpMap.put( "unknown-namespace", Status.fromStatusCode(400));
102 lookUpMap.put( "access-denied", Status.fromStatusCode(403));
103 lookUpMap.put( "lock-denied", Status.fromStatusCode(409));
104 lookUpMap.put( "resource-denied", Status.fromStatusCode(409));
105 lookUpMap.put( "rollback-failed", Status.fromStatusCode(500));
106 lookUpMap.put( "data-exists", Status.fromStatusCode(409));
107 lookUpMap.put( "data-missing", Status.fromStatusCode(409));
108 lookUpMap.put( "operation-not-supported", Status.fromStatusCode(501));
109 lookUpMap.put( "operation-failed", Status.fromStatusCode(500));
110 lookUpMap.put( "partial-operation", Status.fromStatusCode(500));
111 lookUpMap.put( "malformed-message", Status.fromStatusCode(400));
113 for( ErrorTag tag : ErrorTag.values() )
115 Status expectedStatusCode = lookUpMap.get( tag.getTagValue() );
116 assertNotNull( "Failed to find " + tag.getTagValue(), expectedStatusCode );
117 assertEquals( "Status Code does not match", expectedStatusCode, tag.getStatusCode() );
122 public void testRestConfDocumentedException_NoCause()
124 String expectedMessage = "Message";
125 ErrorType expectedErrorType = ErrorType.RPC;
126 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
128 new RestconfError( expectedErrorType,
129 expectedErrorTag, expectedMessage );
131 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
132 null, (String)null, e);
136 public void testRestConfDocumentedException_WithAppTag()
138 String expectedMessage = "Message";
139 ErrorType expectedErrorType = ErrorType.RPC;
140 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
141 String expectedErrorAppTag = "application.tag";
144 new RestconfError( expectedErrorType,
145 expectedErrorTag, expectedMessage, expectedErrorAppTag );
147 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
148 expectedErrorAppTag, (String)null, e);
152 public void testRestConfDocumentedException_WithAppTagErrorInfo()
154 String expectedMessage = "Message";
155 ErrorType expectedErrorType = ErrorType.RPC;
156 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
157 String expectedErrorAppTag = "application.tag";
158 String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
160 RestconfError e = new RestconfError( expectedErrorType,
166 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
167 expectedErrorAppTag, errorInfo, e);
171 public void testRestConfErrorWithRpcError() {
174 RpcError rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
175 "mock error-info", RpcError.ErrorSeverity.ERROR,
176 "mock error-message", RpcError.ErrorType.PROTOCOL,
177 new Exception( "mock cause" ) );
179 validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
180 "mock app-tag", "mock error-info",
181 new RestconfError( rpcError ) );
183 // All fields set except 'info' - expect error-info set to 'cause'
184 rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
185 null, RpcError.ErrorSeverity.ERROR,
186 "mock error-message", RpcError.ErrorType.PROTOCOL,
187 new Exception( "mock cause" ) );
189 validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
190 "mock app-tag", new Contains( "mock cause" ),
191 new RestconfError( rpcError ) );
193 // Some fields set - expect error-info set to ErrorSeverity
194 rpcError = getRpcError( null, ErrorTag.ACCESS_DENIED.getTagValue(),
195 null, RpcError.ErrorSeverity.ERROR,
196 null, RpcError.ErrorType.RPC, null );
198 validateRestConfError( null, ErrorType.RPC, ErrorTag.ACCESS_DENIED,
199 null, "<severity>error</severity>",
200 new RestconfError( rpcError ) );
202 // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
203 rpcError = getRpcError( null, "not mapped",
204 null, RpcError.ErrorSeverity.WARNING,
205 null, RpcError.ErrorType.TRANSPORT, null );
207 validateRestConfError( null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED,
208 null, "<severity>warning</severity>",
209 new RestconfError( rpcError ) );
211 // No fields set - edge case
212 rpcError = getRpcError( null, null, null, null, null, null, null );
214 validateRestConfError( null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
215 null, (String)null, new RestconfError( rpcError ) );
218 private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
219 ErrorTag expectedErrorTag, String expectedErrorAppTag, String errorInfo, RestconfError e) {
221 validateRestConfError( expectedMessage, expectedErrorType, expectedErrorTag,
222 expectedErrorAppTag, equalTo( errorInfo ), e );
225 private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
226 ErrorTag expectedErrorTag, String expectedErrorAppTag,
227 Matcher<String> errorInfoMatcher, RestconfError e) {
229 assertEquals( "getErrorMessage", expectedMessage, e.getErrorMessage() );
230 assertEquals( "getErrorType", expectedErrorType, e.getErrorType() );
231 assertEquals( "getErrorTag", expectedErrorTag, e.getErrorTag() );
232 assertEquals( "getErrorAppTag", expectedErrorAppTag, e.getErrorAppTag() );
233 assertThat( "getErrorInfo", e.getErrorInfo(), errorInfoMatcher );
234 e.toString(); // really just checking for NPE etc. Don't care about contents.