Search in sources :

Example 16 with Document

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

the class ResourcePatchTest method onGivenRequestResourcePatchShouldHandleMissingFields.

@Test
public void onGivenRequestResourcePatchShouldHandleMissingFields() throws Exception {
    JsonPath complexPojoPath = pathBuilder.build("/complexpojos/1");
    // WHEN
    ResourceGet resourceGet = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Response complexPojoResponse = resourceGet.handle(complexPojoPath, emptyComplexPojoQuery, null, null);
    assertThat(complexPojoResponse.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
    Long complexPojoId = Long.parseLong(complexPojoResponse.getDocument().getSingleData().get().getId());
    assertThat(complexPojoId).isNotNull();
    assertThat(complexPojoResponse.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("value from repository mock");
    // GIVEN
    Document complexPojoPatch = new Document();
    Resource data = new Resource();
    complexPojoPatch.setData(Nullable.of((Object) data));
    data.setType("complexpojos");
    String rawContainedPatchData = "  {" + "    'updateableProperty1':'updated value'" + "  }";
    rawContainedPatchData = rawContainedPatchData.replaceAll("'", "\"");
    data.setAttribute("containedPojo", objectMapper.readTree(rawContainedPatchData));
    data.setAttribute("updateableProperty", objectMapper.readTree("\"wasNullBefore\""));
    JsonPath jsonPath = pathBuilder.build("/complexpojos/" + complexPojoId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyComplexPojoQuery, null, complexPojoPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("updated value");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty2").asText()).isEqualTo("value from repository mock");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("updateableProperty").asText()).isEqualTo("wasNullBefore");
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 17 with Document

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

the class ResourcePatchTest method onGivenRequestResourceGetShouldHandleIt.

@Test
public void onGivenRequestResourceGetShouldHandleIt() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskId).isNotNull();
    // GIVEN
    Document taskPatch = new Document();
    data = createTask();
    taskPatch.setData(Nullable.of((Object) data));
    data.setAttribute("name", objectMapper.readTree("\"task updated\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("task updated");
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("name"), Mockito.eq("task updated"));
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 18 with Document

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

the class ResourcePatchTest method omittedFieldsShouldBeIgnored.

@Test
public void omittedFieldsShouldBeIgnored() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Resource data = new Resource();
    data.setType("tasks");
    data.setAttribute("category", objectMapper.readTree("\"TestCategory\""));
    Document taskPatch = new Document();
    taskPatch.setData(Nullable.of((Object) data));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Joe");
    assertThat(updatedTask.getAttributes().get("category").asText()).isEqualTo("TestCategory");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    Assert.assertEquals("Mary Joe", task.getName());
    Assert.assertEquals("TestCategory", task.getCategory());
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 19 with Document

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

the class ResourcePatchTest method omittedFieldsSettersAreNotCalled.

/*
	 * see github #122
	 */
@Test
public void omittedFieldsSettersAreNotCalled() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Document taskPatch = new Document();
    Resource data = new Resource();
    taskPatch.setData(Nullable.of((Object) data));
    data.setType("tasks");
    data.setAttribute("name", objectMapper.readTree("\"Mary Jane\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Jane");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    assertThat(updatedTask.getAttributes().get("category")).isNull();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 20 with Document

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

the class ResourceFilterTest method checkFilterGetOnResourceField.

@Test
public void checkFilterGetOnResourceField() {
    // setup test data
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
    Project project = new Project();
    project.setId(13L);
    project.setName("myProject");
    Task task = new Task();
    task.setId(12L);
    task.setName("myTask");
    task.setProject(project);
    resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
    // get information
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
    String path = "/tasks/";
    String method = HttpMethod.GET.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = null;
    // forbid field
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    Resource taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
    // allow resource
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) Resource(io.crnk.core.engine.document.Resource) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) 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