use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.
the class CrnkExceptionMapperTest method notAuthorized.
@Test
public void notAuthorized() {
CrnkExceptionMapper mapper = new CrnkExceptionMapper();
ErrorResponse response = mapper.toErrorResponse(new UnauthorizedException("testMessage"));
assertThat(response.getHttpStatus()).isEqualTo(HttpStatus.UNAUTHORIZED_401);
assertThat(mapper.accepts(response)).isTrue();
CrnkMappableException exception = mapper.fromErrorResponse(response);
assertThat(exception).isInstanceOf(UnauthorizedException.class);
assertThat(exception.getMessage()).isEqualTo("testMessage");
}
use of io.crnk.core.engine.error.ErrorResponse 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.error.ErrorResponse 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.error.ErrorResponse in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldFindDirectExceptionMapperFromError.
@Test
public void shouldFindDirectExceptionMapperFromError() throws Exception {
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
Throwable throwable = mapper.get().fromErrorResponse(response);
assertThat(throwable).isExactlyInstanceOf(IllegalStateException.class);
}
use of io.crnk.core.engine.error.ErrorResponse 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());
}
Aggregations