use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.
the class RelationshipsResourceDeleteTest method onExistingToManyRelationshipShouldRemoveIt.
@Test
public void onExistingToManyRelationshipShouldRemoveIt() throws Exception {
// GIVEN
Document newUserDocument = new Document();
newUserDocument.setData(Nullable.of((Object) createUser()));
JsonPath taskPath = pathBuilder.build("/users");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a user
Response taskResponse = resourcePost.handle(taskPath, new QuerySpecAdapter(new QuerySpec(User.class), resourceRegistry), null, newUserDocument);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(userId).isNotNull();
/* ------- */
// GIVEN
Document newProjectDocument = new Document();
newProjectDocument.setData(Nullable.of((Object) createProject()));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectDocument);
// THEN
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
/* ------- */
// GIVEN
Document newProjectDocument2 = new Document();
newProjectDocument2.setData(Nullable.of((Object) createProject(projectId.toString())));
JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
RelationshipsResourcePost relationshipsResourcePost = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between user and project
Response projectRelationshipResponse = relationshipsResourcePost.handle(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
assertThat(project.getId()).isEqualTo(projectId);
/* ------- */
// GIVEN
RelationshipsResourceDelete sut = new RelationshipsResourceDelete(resourceRegistry, typeParser, modificationFilters);
// WHEN -- removing a relation between task and project
Response result = sut.handle(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
assertThat(result).isNotNull();
// THEN
Project nullProject = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
assertThat(nullProject).isNull();
}
Aggregations