Search in sources :

Example 26 with ErrorData

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);
}
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 27 with ErrorData

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);
}
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 28 with ErrorData

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());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RestTemplate(org.springframework.web.client.RestTemplate) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 29 with ErrorData

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());
}
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)

Example 30 with ErrorData

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());
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) List(java.util.List) ErrorData(io.crnk.core.engine.document.ErrorData)

Aggregations

ErrorData (io.crnk.core.engine.document.ErrorData)50 Test (org.junit.Test)30 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)10 Document (io.crnk.core.engine.document.Document)9 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)8 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)6 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)6 ArrayList (java.util.ArrayList)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)3 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)3 BadRequestException (io.crnk.core.exception.BadRequestException)3 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)2 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)2 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)2 Optional (io.crnk.core.utils.Optional)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1