Search in sources :

Example 1 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method requestWithInvalidJson.

@Test
public void requestWithInvalidJson() throws IOException {
    Mockito.when(requestContextBase.getMethod()).thenReturn("POST");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(requestContext.getRequestBody()).thenReturn("{ INVALID }".getBytes());
    Assert.assertTrue(JsonApiRequestProcessor.isJsonApiRequest(requestContext, false));
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(HttpStatus.BAD_REQUEST_400), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertFalse(document.getData().isPresent());
    Assert.assertEquals(1, document.getErrors().size());
    ErrorData errorData = document.getErrors().get(0);
    Assert.assertEquals("400", errorData.getStatus());
    Assert.assertEquals("Json Parsing failed", errorData.getTitle());
    Assert.assertNotNull(errorData.getDetail());
}
Also used : Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 2 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method postTasksWithBadRequestException.

@Test
public void postTasksWithBadRequestException() throws IOException {
    // badName triggers an error in repository
    String requestBody = createRequestBody("badName");
    Mockito.when(requestContextBase.getMethod()).thenReturn("POST");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestBody()).thenReturn(requestBody.getBytes());
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(HttpStatus.BAD_REQUEST_400), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertFalse(document.getData().isPresent());
    Assert.assertEquals(1, document.getErrors().size());
    ErrorData errorData = document.getErrors().get(0);
    Assert.assertEquals("400", errorData.getStatus());
}
Also used : Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 3 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class ExceptionMapperHelperTest method test.

@Test
public void test() {
    IllegalStateException exception = new IllegalStateException("test");
    ErrorResponse response = ExceptionMapperHelper.toErrorResponse(exception, 499, "illegal");
    Assert.assertEquals(1, response.getErrors().size());
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals("test", errorData.getCode());
    Assert.assertEquals("test", errorData.getTitle());
    Assert.assertEquals("499", errorData.getStatus());
    Assert.assertEquals("illegal", errorData.getMeta().get("type"));
    Assert.assertEquals(499, response.getHttpStatus());
    Assert.assertEquals("test", ExceptionMapperHelper.createErrorMessage(response));
    Assert.assertTrue(ExceptionMapperHelper.accepts(response, 499, "illegal"));
    Assert.assertFalse(ExceptionMapperHelper.accepts(response, 1, "illegal"));
    Assert.assertFalse(ExceptionMapperHelper.accepts(response, 499, "test"));
    Assert.assertFalse(ExceptionMapperHelper.accepts(new ErrorResponseBuilder().setStatus(499).build(), 499, "illegal"));
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 4 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class ErrorDataBuilderTest method shouldSetAboutLink.

@Test
public void shouldSetAboutLink() throws Exception {
    ErrorData error = ErrorData.builder().setAboutLink(ErrorDataMother.ABOUT_LINK).build();
    assertThat(error.getAboutLink()).isEqualTo(ErrorDataMother.ABOUT_LINK);
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 5 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class ErrorDataBuilderTest method shouldAddMeta.

@Test
public void shouldAddMeta() throws Exception {
    ErrorData error = ErrorData.builder().addMetaField("a", "b").addMetaField("c", "d").build();
    assertThat(error.getMeta()).contains(MapEntry.entry("a", "b"));
    assertThat(error.getMeta()).contains(MapEntry.entry("c", "d"));
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

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