use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class ResourcePatchTest method onInheritedResourceShouldUpdateInheritedResource.
@Test
// TODO inhertiance/resourceregistry
@org.junit.Ignore
public void onInheritedResourceShouldUpdateInheritedResource() throws Exception {
// GIVEN
Document memorandumBody = new Document();
Resource data = new Resource();
memorandumBody.setData(Nullable.of((Object) data));
data.setType("memoranda");
data.setAttribute("title", objectMapper.readTree("\"some title\""));
data.setAttribute("body", objectMapper.readTree("\"sample body\""));
JsonPath documentsPath = pathBuilder.build("/documents");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response taskResponse = resourcePost.handle(documentsPath, emptyMemorandumQuery, null, memorandumBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("memoranda");
Long memorandumId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(memorandumId).isNotNull();
// --------------------------
// GIVEN
memorandumBody = new Document();
data = new Resource();
memorandumBody.setData(Nullable.of((Object) data));
data.setType("memoranda");
data.setAttribute("title", objectMapper.readTree("\"new title\""));
data.setAttribute("body", objectMapper.readTree("\"new body\""));
JsonPath documentPath = pathBuilder.build("/documents/" + memorandumId);
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response memorandumResponse = sut.handle(documentPath, emptyMemorandumQuery, null, memorandumBody);
// THEN
assertThat(memorandumResponse.getDocument().getSingleData().get().getType()).isEqualTo("memoranda");
Resource persistedMemorandum = memorandumResponse.getDocument().getSingleData().get();
assertThat(persistedMemorandum.getId()).isNotNull();
assertThat(persistedMemorandum.getAttributes().get("title").asText()).isEqualTo("new title");
assertThat(persistedMemorandum.getAttributes().get("body").asText()).isEqualTo("new body");
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class ResourcePatchTest method onResourceRelationshipNullifiedShouldSaveIt.
@Test
public void onResourceRelationshipNullifiedShouldSaveIt() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/tasks");
// WHEN
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
// GIVEN
Document taskPatch = new Document();
data = createTask();
data.setAttribute("name", objectMapper.readTree("\"task updated\""));
data.getRelationships().put("project", null);
taskPatch.setData(Nullable.of((Object) data));
JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
// THEN
Assert.assertNotNull(response);
assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
assertThat(response.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("task updated");
assertThat(response.getDocument().getSingleData().get().getRelationships().get("project").getData().get()).isNull();
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class ResourcePatchTest method onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior.
@Test
public void onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
// GIVEN
Document requestDocument = new Document();
Resource data = createTask();
requestDocument.setData(Nullable.of((Object) data));
JsonPath postPath = pathBuilder.build("/tasks");
ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
post.handle(postPath, emptyTaskQuery, null, requestDocument);
PropertiesProvider propertiesProvider = new PropertiesProvider() {
@Override
public String getProperty(String key) {
if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
return ResourceFieldImmutableWriteBehavior.FAIL.toString();
}
return null;
}
};
ResourcePatch sut = new ResourcePatch(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
// WHEN
try {
JsonPath patchPath = pathBuilder.build("/tasks/" + data.getId());
sut.handle(patchPath, emptyTaskQuery, null, requestDocument);
Assert.fail("should not be allowed to update read-only field");
} catch (ForbiddenException e) {
Assert.assertEquals("field 'readOnlyValue' cannot be modified", e.getMessage());
}
}
use of io.crnk.core.engine.document.Resource 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.Resource 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();
}
Aggregations