|
32 | 32 | import org.skyscreamer.jsonassert.JSONAssert; |
33 | 33 | import org.springframework.mock.web.MockHttpServletResponse; |
34 | 34 |
|
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
35 | 40 | /** |
36 | 41 | * Tests for {@link RestResponseResultWriter}. |
37 | 42 | */ |
@@ -269,4 +274,79 @@ public void writeError_extraFields() throws Exception { |
269 | 274 | writer.writeError(serviceException); |
270 | 275 | JSONAssert.assertEquals(expectedError, response.getContentAsString(), true); |
271 | 276 | } |
| 277 | + |
| 278 | + @Test |
| 279 | + public void writeError_extraFieldsUnsafe() throws Exception { |
| 280 | + |
| 281 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 282 | + RestResponseResultWriter writer = new RestResponseResultWriter(response, null, true /* prettyPrint */, |
| 283 | + true /* addContentLength */, true /* enableExceptionCompatibility */); |
| 284 | + |
| 285 | + TestServiceExceptionExtraFieldUnsafe serviceException = new TestServiceExceptionExtraFieldUnsafe(400, "customMessage", "customReason", "customDomain"); |
| 286 | + |
| 287 | + // Extra field array |
| 288 | + Boolean[] booleans = new Boolean[] { TRUE, FALSE, TRUE }; |
| 289 | + |
| 290 | + // Extra field List |
| 291 | + List<String> stringList = Arrays.asList("First", "Second", "Last"); |
| 292 | + |
| 293 | + // Extra field Map |
| 294 | + Map<Integer, TestValue> map = new HashMap<>(); |
| 295 | + map.put(1, new TestValue("Alice", 7, TestEnum.VALUE1)); |
| 296 | + map.put(2, new TestValue("Bob", 12, TestEnum.VALUE2)); |
| 297 | + map.put(3, new TestValue("Clark", 31, TestEnum.VALUE3)); |
| 298 | + |
| 299 | + serviceException.putExtraFieldUnsafe("someExtraNull", null) |
| 300 | + .putExtraFieldUnsafe("someExtraArray", booleans) |
| 301 | + .putExtraFieldUnsafe("someExtraList", stringList) |
| 302 | + .putExtraFieldUnsafe("someExtraMap", map); |
| 303 | + |
| 304 | + String expectedError = "{\"error\": {\"errors\": [{" + |
| 305 | + " \"domain\": \"customDomain\"," + |
| 306 | + " \"reason\": \"customReason\"," + |
| 307 | + " \"message\": \"customMessage\"," + |
| 308 | + " \"someExtraNull\": null," + |
| 309 | + " \"someExtraArray\": [true, false, true]," + |
| 310 | + " \"someExtraList\": [\"First\", \"Second\", \"Last\"]," + |
| 311 | + " \"someExtraMap\": {" + |
| 312 | + " \"1\": {\"name\": \"Alice\", \"age\": 7, \"testEnum\": \"VALUE1\"}," + |
| 313 | + " \"2\": {\"name\": \"Bob\", \"age\": 12, \"testEnum\": \"VALUE2\"}," + |
| 314 | + " \"3\": {\"name\": \"Clark\", \"age\": 31, \"testEnum\": \"VALUE3\"}" + |
| 315 | + " }" + |
| 316 | + " }]," + |
| 317 | + " \"code\": 400," + |
| 318 | + " \"message\": \"customMessage\"" + |
| 319 | + "}}"; |
| 320 | + |
| 321 | + writer.writeError(serviceException); |
| 322 | + JSONAssert.assertEquals(expectedError, response.getContentAsString(), true); |
| 323 | + } |
| 324 | + |
| 325 | + enum TestEnum { |
| 326 | + VALUE1, VALUE2, VALUE3; |
| 327 | + } |
| 328 | + |
| 329 | + class TestValue { |
| 330 | + public String name; |
| 331 | + public int age; |
| 332 | + public TestEnum testEnum; |
| 333 | + |
| 334 | + TestValue(String name, int age, TestEnum testEnum) { |
| 335 | + this.name = name; |
| 336 | + this.age = age; |
| 337 | + this.testEnum = testEnum; |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + class TestServiceExceptionExtraFieldUnsafe extends ServiceException { |
| 342 | + |
| 343 | + TestServiceExceptionExtraFieldUnsafe(int statusCode, String statusMessage, String reason, String domain) { |
| 344 | + super(statusCode, statusMessage, reason, domain); |
| 345 | + } |
| 346 | + |
| 347 | + @Override |
| 348 | + public TestServiceExceptionExtraFieldUnsafe putExtraFieldUnsafe(String fieldName, Object value) { |
| 349 | + return (TestServiceExceptionExtraFieldUnsafe) super.putExtraFieldUnsafe(fieldName, value); |
| 350 | + } |
| 351 | + } |
272 | 352 | } |
0 commit comments