use of io.crnk.core.engine.dispatcher.Response 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();
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class RelationshipsResourcePatchTest method onDeletingToOneRelationshipShouldSetTheValue.
@Test
public void onDeletingToOneRelationshipShouldSetTheValue() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
/* ------- */
// GIVEN
Document newTaskToProjectBody = new Document();
newTaskToProjectBody.setData(Nullable.nullValue());
JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between user and project
Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
assertThat(projectRelationshipResponse.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
Project project = localUserToProjectRepository.findOneTarget(1L, "project", new QuerySpec(Project.class));
assertThat(project).isNull();
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class RelationshipsResourcePatchTest method onExistingResourcesShouldAddToOneRelationship.
@Test
public void onExistingResourcesShouldAddToOneRelationship() 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 resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
/* ------- */
// GIVEN
Document newProjectBody = new Document();
data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
// 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 newTaskToProjectBody = new Document();
data = new Resource();
newTaskToProjectBody.setData(Nullable.of((Object) data));
data.setType("projects");
data.setId(projectId.toString());
JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between task and project
Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
Project project = taskToProjectRepository.findOneTarget(taskId, "project", REQUEST_PARAMS);
assertThat(project.getId()).isEqualTo(projectId);
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class ResourceIdControllerTest method checkPost.
private Resource checkPost() throws IOException {
// GIVEN
Resource resource = createResource();
resource.getRelationships().put("testLookupAlways", new Relationship(schedule3Id));
Document newDocument = createDocument(resource);
JsonPath postPath = pathBuilder.build("/relationIdTest");
// WHEN POST
ResourcePost sut = new ResourcePost(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
Response taskResponse = sut.handle(postPath, emptyTaskQuery, null, newDocument);
// THEN POST
assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
Resource createdResource = taskResponse.getDocument().getSingleData().get();
Assert.assertEquals(resource.getType(), createdResource.getType());
Assert.assertEquals(resource.getId(), createdResource.getId());
Assert.assertEquals(resource.getAttributes().get("name"), createdResource.getAttributes().get("name"));
Relationship relationship = createdResource.getRelationships().get("testLookupAlways");
Assert.assertTrue(relationship.getData().isPresent());
Assert.assertEquals(schedule3Id, relationship.getData().get());
// validate relationship not accessed, only id set => performance
RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
Assert.assertEquals(schedule3.getId(), entity.getTestLookupAlwaysId());
Assert.assertNull(entity.getTestLookupAlways());
return createdResource;
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class ResourceIdControllerTest method checkPatchRelationship.
private void checkPatchRelationship(Resource resource, ResourceIdentifier scheduleId) {
JsonPath path = pathBuilder.build("/relationIdTest/" + resource.getId() + "/relationships/testLookupAlways");
Document newDocument = createDocument(scheduleId);
// WHEN PATCH
RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
Response taskResponse = sut.handle(path, emptyTaskQuery, null, newDocument);
// THEN PATCH
assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
// validate relationship not accessed, only id set => performance
RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
Assert.assertEquals(scheduleId != null ? Long.parseLong(scheduleId.getId()) : null, entity.getTestLookupAlwaysId());
Assert.assertNull(entity.getTestLookupAlways());
}
Aggregations