Search in sources :

Example 1 with Document

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);
}
Also used : Task(io.crnk.core.mock.models.Task) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 2 with Document

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"));
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 3 with Document

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());
}
Also used : Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 4 with Document

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());
}
Also used : Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 5 with Document

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"));
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

Document (io.crnk.core.engine.document.Document)131 Test (org.junit.Test)95 Resource (io.crnk.core.engine.document.Resource)87 Response (io.crnk.core.engine.dispatcher.Response)56 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)47 QuerySpec (io.crnk.core.queryspec.QuerySpec)45 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)40 Relationship (io.crnk.core.engine.document.Relationship)39 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)35 Task (io.crnk.core.mock.models.Task)34 Project (io.crnk.core.mock.models.Project)27 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)25 LazyTask (io.crnk.core.mock.models.LazyTask)17 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)11 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)10