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 org.hamcrest.BaseMatcher;
19 import org.hamcrest.Description;
20 import org.hamcrest.Matcher;
21 import org.junit.Test;
23 import static org.opendaylight.controller.sal.common.util.RpcErrors.getRpcError;
25 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
28 import org.opendaylight.yangtools.yang.common.RpcError;
31 * Unit tests for RestconfError.
34 * @author Thomas Pantelis
37 public class RestconfErrorTest {
39 static class Contains extends BaseMatcher<String> {
41 private final String text;
43 public Contains( String text ) {
48 public void describeTo( Description desc ) {
49 desc.appendText( "contains " ).appendValue( text );
53 public boolean matches( Object arg ) {
54 return arg != null && arg.toString().contains( text );
59 public void testErrorTagValueOf()
61 assertEquals( ErrorTag.IN_USE,
62 ErrorTag.valueOfCaseInsensitive( ErrorTag.IN_USE.getTagValue() ) );
66 public void testErrorTagValueOfIsLowercase()
68 assertEquals( "in-use",
69 ErrorTag.IN_USE.getTagValue() );
73 public void testErrorTypeGetErrorTypeTagIsLowerCase()
75 assertEquals( ErrorType.APPLICATION.name().toLowerCase(),
76 ErrorType.APPLICATION.getErrorTypeTag() );
80 public void testErrorTypeValueOf()
82 assertEquals( ErrorType.APPLICATION,
83 ErrorType.valueOfCaseInsensitive( ErrorType.APPLICATION.getErrorTypeTag() ) );
87 public void testErrorTagStatusCodes()
89 Map<String,Integer> lookUpMap = new HashMap<String,Integer>();
91 lookUpMap.put( "in-use", 409);
92 lookUpMap.put( "invalid-value", 400);
93 lookUpMap.put( "too-big", 413);
94 lookUpMap.put( "missing-attribute", 400);
95 lookUpMap.put( "bad-attribute", 400);
96 lookUpMap.put( "unknown-attribute", 400);
97 lookUpMap.put( "bad-element", 400);
98 lookUpMap.put( "unknown-element", 400);
99 lookUpMap.put( "unknown-namespace", 400);
100 lookUpMap.put( "access-denied", 403);
101 lookUpMap.put( "lock-denied", 409);
102 lookUpMap.put( "resource-denied", 409);
103 lookUpMap.put( "rollback-failed", 500);
104 lookUpMap.put( "data-exists", 409);
105 lookUpMap.put( "data-missing", 409);
106 lookUpMap.put( "operation-not-supported", 501);
107 lookUpMap.put( "operation-failed", 500);
108 lookUpMap.put( "partial-operation", 500);
109 lookUpMap.put( "malformed-message", 400);
111 for( ErrorTag tag : ErrorTag.values() )
113 Integer expectedStatusCode = lookUpMap.get( tag.getTagValue() );
114 assertNotNull( "Failed to find " + tag.getTagValue(), expectedStatusCode );
115 assertEquals( "Status Code does not match", expectedStatusCode,
116 Integer.valueOf( tag.getStatusCode() ) );
121 public void testRestConfDocumentedException_NoCause()
123 String expectedMessage = "Message";
124 ErrorType expectedErrorType = ErrorType.RPC;
125 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
127 new RestconfError( expectedErrorType,
128 expectedErrorTag, expectedMessage );
130 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
131 null, (String)null, e);
135 public void testRestConfDocumentedException_WithAppTag()
137 String expectedMessage = "Message";
138 ErrorType expectedErrorType = ErrorType.RPC;
139 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
140 String expectedErrorAppTag = "application.tag";
143 new RestconfError( expectedErrorType,
144 expectedErrorTag, expectedMessage, expectedErrorAppTag );
146 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
147 expectedErrorAppTag, (String)null, e);
151 public void testRestConfDocumentedException_WithAppTagErrorInfo()
153 String expectedMessage = "Message";
154 ErrorType expectedErrorType = ErrorType.RPC;
155 ErrorTag expectedErrorTag = ErrorTag.IN_USE;
156 String expectedErrorAppTag = "application.tag";
157 String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
159 RestconfError e = new RestconfError( expectedErrorType,
165 validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
166 expectedErrorAppTag, errorInfo, e);
170 public void testRestConfErrorWithRpcError() {
173 RpcError rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
174 "mock error-info", RpcError.ErrorSeverity.ERROR,
175 "mock error-message", RpcError.ErrorType.PROTOCOL,
176 new Exception( "mock cause" ) );
178 validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
179 "mock app-tag", "mock error-info",
180 new RestconfError( rpcError ) );
182 // All fields set except 'info' - expect error-info set to 'cause'
183 rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
184 null, RpcError.ErrorSeverity.ERROR,
185 "mock error-message", RpcError.ErrorType.PROTOCOL,
186 new Exception( "mock cause" ) );
188 validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
189 "mock app-tag", new Contains( "mock cause" ),
190 new RestconfError( rpcError ) );
192 // Some fields set - expect error-info set to ErrorSeverity
193 rpcError = getRpcError( null, ErrorTag.ACCESS_DENIED.getTagValue(),
194 null, RpcError.ErrorSeverity.ERROR,
195 null, RpcError.ErrorType.RPC, null );
197 validateRestConfError( null, ErrorType.RPC, ErrorTag.ACCESS_DENIED,
198 null, "<severity>error</severity>",
199 new RestconfError( rpcError ) );
201 // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
202 rpcError = getRpcError( null, "not mapped",
203 null, RpcError.ErrorSeverity.WARNING,
204 null, RpcError.ErrorType.TRANSPORT, null );
206 validateRestConfError( null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED,
207 null, "<severity>warning</severity>",
208 new RestconfError( rpcError ) );
210 // No fields set - edge case
211 rpcError = getRpcError( null, null, null, null, null, null, null );
213 validateRestConfError( null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
214 null, (String)null, new RestconfError( rpcError ) );
217 private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
218 ErrorTag expectedErrorTag, String expectedErrorAppTag, String errorInfo, RestconfError e) {
220 validateRestConfError( expectedMessage, expectedErrorType, expectedErrorTag,
221 expectedErrorAppTag, equalTo( errorInfo ), e );
224 private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
225 ErrorTag expectedErrorTag, String expectedErrorAppTag,
226 Matcher<String> errorInfoMatcher, RestconfError e) {
228 assertEquals( "getErrorMessage", expectedMessage, e.getErrorMessage() );
229 assertEquals( "getErrorType", expectedErrorType, e.getErrorType() );
230 assertEquals( "getErrorTag", expectedErrorTag, e.getErrorTag() );
231 assertEquals( "getErrorAppTag", expectedErrorAppTag, e.getErrorAppTag() );
232 assertThat( "getErrorInfo", e.getErrorInfo(), errorInfoMatcher );
233 e.toString(); // really just checking for NPE etc. Don't care about contents.