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();
}
Aggregations