use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithNoErrorsAnd500Status.
@Test
public void checkBodyWithNoErrorsAnd500Status() throws IOException {
Document document = new Document();
document.setErrors(new ArrayList<ErrorData>());
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.code()).thenReturn(500);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof InternalServerErrorException);
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class ErrorResponse method toResponse.
public Response toResponse() {
Document responseDocument = new Document();
List<ErrorData> errors = new ArrayList<>();
for (ErrorData error : getErrors()) {
errors.add(error);
}
responseDocument.setErrors(errors);
return new Response(responseDocument, getHttpStatus());
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method testHttpRequestProcessorRegistration.
@Test
public void testHttpRequestProcessorRegistration() throws IOException {
Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
boot.getRequestDispatcher().process(requestContext);
ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(200), contentCaptor.capture());
String json = new String(contentCaptor.getValue());
Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
Assert.assertTrue(document.getData().isPresent());
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method getTasksWithCompactHeader.
@Test
public void getTasksWithCompactHeader() throws IOException {
Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_HEADER_CRNK_COMPACT)).thenReturn("true");
processor.process(requestContext);
ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(200), contentCaptor.capture());
String json = new String(contentCaptor.getValue());
Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
Assert.assertTrue(document.getData().isPresent());
List<Resource> resources = document.getCollectionData().get();
Assert.assertEquals(1, resources.size());
Resource resource = resources.get(0);
Assert.assertNull(resource.getLinks().get("self"));
Assert.assertNotNull(resource.getLinks().get("value"));
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method postTasks.
@Test
public void postTasks() throws IOException {
String requestBody = createRequestBody("test");
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.CREATED_201), contentCaptor.capture());
String json = new String(contentCaptor.getValue());
Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
Assert.assertTrue(document.getData().isPresent());
Resource updatedTask = (Resource) document.getData().get();
Assert.assertEquals("1", updatedTask.getId());
Assert.assertEquals("tasks", updatedTask.getType());
}
Aggregations