use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldFindDescendantExceptionMapperFromError.
@Test
public void shouldFindDescendantExceptionMapperFromError() throws Exception {
// two exception mapper will match (IllegalStateException and SomeIllegalStateException)
// subtype should be choosen.
ErrorData errorData = ErrorData.builder().setId("someId").build();
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(SomeIllegalStateExceptionMapper.class);
Throwable throwable = mapper.get().fromErrorResponse(response);
assertThat(throwable).isExactlyInstanceOf(SomeIllegalStateException.class);
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldNotFindDescendantExceptionMapperFromError.
@Test
public void shouldNotFindDescendantExceptionMapperFromError() throws Exception {
ErrorData errorData = ErrorData.builder().setId("someOtherId").build();
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class BasicSpringBootTest method testErrorsSerializedAsJsonApi.
@Test
public void testErrorsSerializedAsJsonApi() throws IOException {
RestTemplate testRestTemplate = new RestTemplate();
try {
testRestTemplate.getForEntity("http://localhost:" + this.port + "/doesNotExist", String.class);
Assert.fail();
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
String body = e.getResponseBodyAsString();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(JacksonModule.createJacksonModule());
Document document = mapper.readerFor(Document.class).readValue(body);
Assert.assertEquals(1, document.getErrors().size());
ErrorData errorData = document.getErrors().get(0);
Assert.assertEquals("404", errorData.getStatus());
Assert.assertEquals("Not Found", errorData.getTitle());
Assert.assertEquals("No message available", errorData.getDetail());
}
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class SpringSecurityExceptionMapperTest method testAccessDenied.
@Test
public void testAccessDenied() {
AccessDeniedExceptionMapper mapper = new AccessDeniedExceptionMapper();
AccessDeniedException exception = new AccessDeniedException("hi");
ErrorResponse response = mapper.toErrorResponse(exception);
Iterable<ErrorData> errors = response.getErrors();
Iterator<ErrorData> iterator = errors.iterator();
ErrorData data = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals("403", data.getStatus());
Assert.assertEquals("hi", data.getCode());
Assert.assertTrue(mapper.accepts(response));
AccessDeniedException fromErrorResponse = mapper.fromErrorResponse(response);
Assert.assertEquals("hi", fromErrorResponse.getMessage());
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class TestExceptionMapper method fromErrorResponse.
@Override
public TestException fromErrorResponse(ErrorResponse errorResponse) {
JsonApiResponse response = errorResponse.getResponse();
List<ErrorData> errors = (List<ErrorData>) response.getEntity();
StringBuilder message = new StringBuilder();
for (ErrorData error : errors) {
String title = error.getDetail();
message.append(title);
}
return new TestException(message.toString());
}
Aggregations