use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method createRequestBody.
private String createRequestBody(String name) throws JsonProcessingException {
Task task = new Task();
task.setId(1L);
task.setName(name);
task.setCategory("testCategory");
JsonApiResponse request = new JsonApiResponse();
request.setEntity(task);
Document requestDocument = boot.getDocumentMapper().toDocument(request, new QuerySpecAdapter(new QuerySpec(Task.class), boot.getResourceRegistry()));
return boot.getObjectMapper().writeValueAsString(requestDocument);
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method getTasks.
@Test
public void getTasks() throws IOException {
Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
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.assertEquals("http://localhost:8080/tasks/1", resource.getLinks().get("self").asText());
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 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.Document 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.Document in project crnk-framework by crnk-project.
the class ResponseTest method testHashCodeEquals.
@Test
public void testHashCodeEquals() {
Document r1 = new Document();
Document r2 = new Document();
r2.setData(Nullable.of((Object) new Resource()));
Response c1 = new Response(r1, 201);
Response c1copy = new Response(r1, 201);
Response c2 = new Response(r2, 202);
Response c3 = new Response(r1, 202);
Assert.assertEquals(c1.hashCode(), c1copy.hashCode());
Assert.assertTrue(c1.equals(c1));
Assert.assertTrue(c1.equals(c1copy));
Assert.assertFalse(c1.equals(c2));
Assert.assertFalse(c1.equals(c3));
Assert.assertFalse(c2.equals(c3));
Assert.assertFalse(c2.equals("otherType"));
}
Aggregations