use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class ResourcePatchTest method onUpdatedLazyRelationshipDataShouldReturnThatData.
@Test
public void onUpdatedLazyRelationshipDataShouldReturnThatData() 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);
Nullable<Object> emptyRelation = Nullable.of((Object) new ArrayList<ResourceIdentifier>());
data.getRelationships().get("tasks").setData(emptyRelation);
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("sample project");
assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(0);
}
use of io.crnk.core.engine.document.ResourceIdentifier 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();
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class ResourcePostTest method onUpdatedLazyRelationshipDataShouldReturnThatData.
@Test
public void onUpdatedLazyRelationshipDataShouldReturnThatData() 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 sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
assertThat(taskResponse.getDocument().getSingleData().get().getId()).isNotNull();
assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
/* ------- */
// GIVEN
Document newProjectBody = new Document();
data = createProject();
data.setType("projects");
List<ResourceIdentifier> taskIds = Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"));
data.getRelationships().put("tasks", new Relationship(taskIds));
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectsPath = pathBuilder.build("/projects");
// WHEN
Response projectsResponse = sut.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
// THEN
assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
Long userId = Long.parseLong(projectsResponse.getDocument().getSingleData().get().getId());
assertThat(userId).isNotNull();
assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get().get(0).getId()).isEqualTo(taskId.toString());
Mockito.verify(modificationFilter, Mockito.times(1)).modifyManyRelationship(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq(ResourceRelationshipModificationType.SET), Mockito.eq(taskIds));
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class ResourcePostTest method onNewResourcesAndRelationshipShouldPersistThoseData.
@Test
public void onNewResourcesAndRelationshipShouldPersistThoseData() throws Exception {
// GIVEN
Document newProjectBody = new Document();
Resource data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response projectResponse = sut.handle(projectPath, emptyProjectQuery, null, newProjectBody);
// THEN
assertThat(projectResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
assertThat(projectResponse.getDocument().getData().get()).isExactlyInstanceOf(Resource.class);
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
Resource persistedProject = projectResponse.getDocument().getSingleData().get();
assertThat(persistedProject.getId()).isNotNull();
assertThat(persistedProject.getAttributes().get("name").asText()).isEqualTo("sample project");
assertThat(persistedProject.getAttributes().get("data").get("data").asText()).isEqualTo("asd");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("data"), Mockito.any());
/* ------- */
// GIVEN
Document newTasksBody = new Document();
newTasksBody.setData(Nullable.of((Object) createTask()));
newTasksBody.getSingleData().get().getRelationships().put("project", new Relationship(new ResourceIdentifier(projectId.toString(), "projects")));
JsonPath taskPath = pathBuilder.build("/tasks");
// WHEN
Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTasksBody);
// THEN
assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
String taskId = taskResponse.getDocument().getSingleData().get().getId();
assertThat(taskId).isNotNull();
assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
TaskRepository taskRepository = new TaskRepository();
Task persistedTask = taskRepository.findOne(Long.parseLong(taskId), null);
assertThat(persistedTask.getProject().getId()).isEqualTo(projectId);
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class DocumentMapperTest method testRelationshipIncludeMultiValued.
@Test
public void testRelationshipIncludeMultiValued() {
LazyTask task = createLazyTask(2);
Project project1 = createProject(3, "sample project3");
Project project2 = createProject(4, "sample project4");
task.setProjects(Arrays.asList(project1, project2));
QuerySpec querySpec = new QuerySpec(LazyTask.class);
querySpec.includeRelation(Arrays.asList("projects"));
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Relationship relationship = resource.getRelationships().get("projects");
Assert.assertNotNull(relationship);
List<ResourceIdentifier> relationshipData = relationship.getCollectionData().get();
Assert.assertNotNull(relationshipData);
Assert.assertEquals(2, relationshipData.size());
Assert.assertEquals("3", relationshipData.get(0).getId());
Assert.assertEquals("projects", relationshipData.get(0).getType());
Assert.assertEquals("4", relationshipData.get(1).getId());
Assert.assertEquals("projects", relationshipData.get(1).getType());
Assert.assertFalse(document.getIncluded().isEmpty());
List<Resource> included = document.getIncluded();
Assert.assertEquals(2, included.size());
Assert.assertEquals("3", included.get(0).getId());
Assert.assertEquals("projects", included.get(0).getType());
Assert.assertEquals("sample project3", included.get(0).getAttributes().get("name").asText());
Assert.assertEquals("4", included.get(1).getId());
Assert.assertEquals("projects", included.get(1).getType());
Assert.assertEquals("sample project4", included.get(1).getAttributes().get("name").asText());
}
Aggregations