|
| 1 | +/** |
| 2 | + * Copyright 2017 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.ibm.watson.developer_cloud.service; |
| 14 | + |
| 15 | +import com.ibm.watson.developer_cloud.WatsonServiceUnitTest; |
| 16 | +import com.ibm.watson.developer_cloud.http.HttpMediaType; |
| 17 | +import com.ibm.watson.developer_cloud.http.RequestBuilder; |
| 18 | +import com.ibm.watson.developer_cloud.http.ServiceCall; |
| 19 | +import com.ibm.watson.developer_cloud.service.exception.BadRequestException; |
| 20 | +import com.ibm.watson.developer_cloud.service.exception.ConflictException; |
| 21 | +import com.ibm.watson.developer_cloud.service.exception.ForbiddenException; |
| 22 | +import com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException; |
| 23 | +import com.ibm.watson.developer_cloud.service.exception.NotFoundException; |
| 24 | +import com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException; |
| 25 | +import com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException; |
| 26 | +import com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException; |
| 27 | +import com.ibm.watson.developer_cloud.service.exception.UnauthorizedException; |
| 28 | +import com.ibm.watson.developer_cloud.service.exception.UnsupportedException; |
| 29 | +import com.ibm.watson.developer_cloud.service.model.GenericModel; |
| 30 | +import com.ibm.watson.developer_cloud.util.ResponseConverterUtils; |
| 31 | +import okhttp3.mockwebserver.MockResponse; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import static org.junit.Assert.assertTrue; |
| 37 | + |
| 38 | +public class ErrorResponseTest extends WatsonServiceUnitTest { |
| 39 | + |
| 40 | + public class TestService extends WatsonService { |
| 41 | + |
| 42 | + private static final String SERVICE_NAME = "test"; |
| 43 | + |
| 44 | + public TestService() { |
| 45 | + super(SERVICE_NAME); |
| 46 | + } |
| 47 | + |
| 48 | + public ServiceCall<GenericModel> testMethod() { |
| 49 | + RequestBuilder builder = RequestBuilder.get("/v1/test"); |
| 50 | + return createServiceCall(builder.build(), ResponseConverterUtils.getObject(GenericModel.class)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + TestService service; |
| 55 | + |
| 56 | + /* |
| 57 | + * (non-Javadoc) |
| 58 | + * |
| 59 | + * @see com.ibm.watson.developer_cloud.WatsonServiceTest#setUp() |
| 60 | + */ |
| 61 | + @Override |
| 62 | + @Before |
| 63 | + public void setUp() throws Exception { |
| 64 | + super.setUp(); |
| 65 | + service = new TestService(); |
| 66 | + service.setApiKey(""); |
| 67 | + service.setEndPoint(getMockWebServerUrl()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test HTTP status code 400 (Bad Request) error response. |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void testBadRequest() { |
| 75 | + |
| 76 | + String message = "The request failed because the moon is full."; |
| 77 | + server.enqueue(new MockResponse() |
| 78 | + .setResponseCode(400) |
| 79 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 80 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 81 | + |
| 82 | + try { |
| 83 | + GenericModel response = service.testMethod().execute(); |
| 84 | + } catch (Exception e) { |
| 85 | + assertTrue(e instanceof BadRequestException); |
| 86 | + BadRequestException ex = (BadRequestException) e; |
| 87 | + assertEquals(400, ex.getStatusCode()); |
| 88 | + assertEquals(message, ex.getMessage()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test HTTP status code 401 (Unauthorized) error response. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void testUnauthorized() { |
| 97 | + |
| 98 | + String message = "The request failed because the moon is full."; |
| 99 | + server.enqueue(new MockResponse() |
| 100 | + .setResponseCode(401) |
| 101 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 102 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 103 | + |
| 104 | + try { |
| 105 | + GenericModel response = service.testMethod().execute(); |
| 106 | + } catch (Exception e) { |
| 107 | + assertTrue(e instanceof UnauthorizedException); |
| 108 | + UnauthorizedException ex = (UnauthorizedException) e; |
| 109 | + assertEquals(401, ex.getStatusCode()); |
| 110 | + assertTrue(ex.getMessage().startsWith("Unauthorized: Access is denied due to invalid credentials.")); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test HTTP status code 403 (Forbidden) error response. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testForbidden() { |
| 119 | + |
| 120 | + String message = "The request failed because the moon is full."; |
| 121 | + server.enqueue(new MockResponse() |
| 122 | + .setResponseCode(403) |
| 123 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 124 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 125 | + |
| 126 | + try { |
| 127 | + GenericModel response = service.testMethod().execute(); |
| 128 | + } catch (Exception e) { |
| 129 | + assertTrue(e instanceof ForbiddenException); |
| 130 | + ForbiddenException ex = (ForbiddenException) e; |
| 131 | + assertEquals(403, ex.getStatusCode()); |
| 132 | + assertEquals(message, ex.getMessage()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test HTTP status code 404 (NotFound) error response. |
| 138 | + */ |
| 139 | + @Test |
| 140 | + public void testNotFound() { |
| 141 | + |
| 142 | + String message = "The request failed because the moon is full."; |
| 143 | + server.enqueue(new MockResponse() |
| 144 | + .setResponseCode(404) |
| 145 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 146 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 147 | + |
| 148 | + try { |
| 149 | + GenericModel response = service.testMethod().execute(); |
| 150 | + } catch (Exception e) { |
| 151 | + assertTrue(e instanceof NotFoundException); |
| 152 | + NotFoundException ex = (NotFoundException) e; |
| 153 | + assertEquals(404, ex.getStatusCode()); |
| 154 | + assertEquals(message, ex.getMessage()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Test HTTP status code 409 (Conflict) error response. |
| 160 | + */ |
| 161 | + @Test |
| 162 | + public void testConflict() { |
| 163 | + |
| 164 | + String message = "The request failed because the moon is full."; |
| 165 | + server.enqueue(new MockResponse() |
| 166 | + .setResponseCode(409) |
| 167 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 168 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 169 | + |
| 170 | + try { |
| 171 | + GenericModel response = service.testMethod().execute(); |
| 172 | + } catch (Exception e) { |
| 173 | + assertTrue(e instanceof ConflictException); |
| 174 | + ConflictException ex = (ConflictException) e; |
| 175 | + assertEquals(409, ex.getStatusCode()); |
| 176 | + assertEquals(message, ex.getMessage()); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Test HTTP status code 413 (RequestTooLarge) error response. |
| 182 | + */ |
| 183 | + @Test |
| 184 | + public void testRequestTooLarge() { |
| 185 | + |
| 186 | + String message = "The request failed because the moon is full."; |
| 187 | + server.enqueue(new MockResponse() |
| 188 | + .setResponseCode(413) |
| 189 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 190 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 191 | + |
| 192 | + try { |
| 193 | + GenericModel response = service.testMethod().execute(); |
| 194 | + } catch (Exception e) { |
| 195 | + assertTrue(e instanceof RequestTooLargeException); |
| 196 | + RequestTooLargeException ex = (RequestTooLargeException) e; |
| 197 | + assertEquals(413, ex.getStatusCode()); |
| 198 | + assertEquals(message, ex.getMessage()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Test HTTP status code 415 (Unsupported Media Type) error response. |
| 204 | + */ |
| 205 | + @Test |
| 206 | + public void testUnsupported() { |
| 207 | + |
| 208 | + String message = "The request failed because the moon is full."; |
| 209 | + server.enqueue(new MockResponse() |
| 210 | + .setResponseCode(415) |
| 211 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 212 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 213 | + |
| 214 | + try { |
| 215 | + GenericModel response = service.testMethod().execute(); |
| 216 | + } catch (Exception e) { |
| 217 | + assertTrue(e instanceof UnsupportedException); |
| 218 | + UnsupportedException ex = (UnsupportedException) e; |
| 219 | + assertEquals(415, ex.getStatusCode()); |
| 220 | + assertEquals(message, ex.getMessage()); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Test HTTP status code 429 (TooManyRequests) error response. |
| 226 | + */ |
| 227 | + @Test |
| 228 | + public void testTooManyRequests() { |
| 229 | + |
| 230 | + String message = "The request failed because the moon is full."; |
| 231 | + server.enqueue(new MockResponse() |
| 232 | + .setResponseCode(429) |
| 233 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 234 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 235 | + |
| 236 | + try { |
| 237 | + GenericModel response = service.testMethod().execute(); |
| 238 | + } catch (Exception e) { |
| 239 | + assertTrue(e instanceof TooManyRequestsException); |
| 240 | + TooManyRequestsException ex = (TooManyRequestsException) e; |
| 241 | + assertEquals(429, ex.getStatusCode()); |
| 242 | + assertEquals(message, ex.getMessage()); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Test HTTP status code 500 (InternalServerError) error response. |
| 248 | + */ |
| 249 | + @Test |
| 250 | + public void testInternalServerError() { |
| 251 | + |
| 252 | + String message = "The request failed because the moon is full."; |
| 253 | + server.enqueue(new MockResponse() |
| 254 | + .setResponseCode(500) |
| 255 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 256 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 257 | + |
| 258 | + try { |
| 259 | + GenericModel response = service.testMethod().execute(); |
| 260 | + } catch (Exception e) { |
| 261 | + assertTrue(e instanceof InternalServerErrorException); |
| 262 | + InternalServerErrorException ex = (InternalServerErrorException) e; |
| 263 | + assertEquals(500, ex.getStatusCode()); |
| 264 | + assertEquals(message, ex.getMessage()); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Test HTTP status code 503 (ServiceUnavailable) error response. |
| 270 | + */ |
| 271 | + @Test |
| 272 | + public void testServiceUnavailable() { |
| 273 | + |
| 274 | + String message = "The request failed because the moon is full."; |
| 275 | + server.enqueue(new MockResponse() |
| 276 | + .setResponseCode(503) |
| 277 | + .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) |
| 278 | + .setBody("{\"error\": \"" + message + "\"}")); |
| 279 | + |
| 280 | + try { |
| 281 | + GenericModel response = service.testMethod().execute(); |
| 282 | + } catch (Exception e) { |
| 283 | + assertTrue(e instanceof ServiceUnavailableException); |
| 284 | + ServiceUnavailableException ex = (ServiceUnavailableException) e; |
| 285 | + assertEquals(503, ex.getStatusCode()); |
| 286 | + assertEquals(message, ex.getMessage()); |
| 287 | + } |
| 288 | + } |
| 289 | +} |
0 commit comments