use of io.crnk.core.engine.document.Resource 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.document.Resource in project crnk-framework by crnk-project.
the class ResourceIdControllerTest method createResource.
public Resource createResource() throws IOException {
Resource data = new Resource();
data.setType("relationIdTest");
data.setId("1");
data.setAttribute("name", objectMapper.readTree("\"test\""));
return data;
}
use of io.crnk.core.engine.document.Resource 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.document.Resource 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.Resource in project crnk-framework by crnk-project.
the class ResourcePostTest method onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior.
@Test
public void onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior() throws Exception {
// GIVEN
Document requestDocument = new Document();
Resource data = createTask();
data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
requestDocument.setData(Nullable.of((Object) data));
JsonPath path = pathBuilder.build("/tasks");
PropertiesProvider propertiesProvider = new PropertiesProvider() {
@Override
public String getProperty(String key) {
if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
return ResourceFieldImmutableWriteBehavior.IGNORE.toString();
}
return null;
}
};
ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = sut.handle(path, emptyTaskQuery, null, requestDocument);
String persistedValue = response.getDocument().getSingleData().get().getAttributes().get("readOnlyValue").asText();
Assert.assertEquals("someReadOnlyValue", persistedValue);
}
Aggregations