Search in sources :

Example 6 with ErrorResponse

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");
}
Also used : CrnkExceptionMapper(io.crnk.core.engine.internal.exception.CrnkExceptionMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 7 with ErrorResponse

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);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 8 with ErrorResponse

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);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 9 with ErrorResponse

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);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 10 with ErrorResponse

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());
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ErrorData(io.crnk.core.engine.document.ErrorData) AccessDeniedExceptionMapper(io.crnk.spring.internal.AccessDeniedExceptionMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (io.crnk.core.engine.error.ErrorResponse)18 Test (org.junit.Test)15 ErrorData (io.crnk.core.engine.document.ErrorData)10 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)7 CrnkExceptionMapper (io.crnk.core.engine.internal.exception.CrnkExceptionMapper)6 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 Optional (io.crnk.core.utils.Optional)4 BadRequestException (io.crnk.core.exception.BadRequestException)3 Document (io.crnk.core.engine.document.Document)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)1 PreconditionUtil (io.crnk.core.engine.internal.utils.PreconditionUtil)1