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