Search in sources :

Example 16 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.

the class ResourcePatchTest method onUnchagedLazyRelationshipDataShouldNotReturnThatData.

@Test
public void onUnchagedLazyRelationshipDataShouldNotReturnThatData() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    data.setType("tasks");
    JsonPath taskPath = pathBuilder.build("/tasks");
    ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = post.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    Document newProjectBody = new Document();
    data = createProject();
    data.setType("projects");
    data.getRelationships().put("tasks", new Relationship(Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectsPath = pathBuilder.build("/projects");
    Response projectsResponse = post.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
    // update relationship and availability in response
    ResourcePatch patch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getRelationships().remove("tasks");
    data.getAttributes().put("name", objectMapper.readTree("\"updated project\""));
    projectsResponse = patch.handle(pathBuilder.build("/projects/2"), emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("updated project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().isPresent()).isFalse();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) 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)

Aggregations

ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)16 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)15 Test (org.junit.Test)15 Document (io.crnk.core.engine.document.Document)14 Resource (io.crnk.core.engine.document.Resource)14 Response (io.crnk.core.engine.dispatcher.Response)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)9 Relationship (io.crnk.core.engine.document.Relationship)4 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)3 Task (io.crnk.core.mock.models.Task)3 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)2 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceGet (io.crnk.core.engine.internal.dispatcher.controller.ResourceGet)1 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)1 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)1 CrnkException (io.crnk.core.exception.CrnkException)1