use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.
the class ResourcePatchTest method onGivenRequestResourceShouldThrowException.
@Test
public void onGivenRequestResourceShouldThrowException() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
data.setType("tasks");
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 = new Resource();
taskPatch.setData(Nullable.of((Object) data));
data.setType("WRONG_AND_MISSING_TYPE");
data.setAttribute("name", objectMapper.readTree("\"task updated\""));
JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = null;
try {
response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
Assert.fail("Should have recieved exception.");
} catch (CrnkException rbe) {
// Got correct exception
} catch (Error ex) {
Assert.fail("Got bad exception: " + ex);
}
}
use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.
the class ResourcePatchTest method onGivenRequestResourcePatchShouldHandleMissingFields.
@Test
public void onGivenRequestResourcePatchShouldHandleMissingFields() throws Exception {
JsonPath complexPojoPath = pathBuilder.build("/complexpojos/1");
// WHEN
ResourceGet resourceGet = new ResourceGet(resourceRegistry, typeParser, documentMapper);
Response complexPojoResponse = resourceGet.handle(complexPojoPath, emptyComplexPojoQuery, null, null);
assertThat(complexPojoResponse.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
Long complexPojoId = Long.parseLong(complexPojoResponse.getDocument().getSingleData().get().getId());
assertThat(complexPojoId).isNotNull();
assertThat(complexPojoResponse.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("value from repository mock");
// GIVEN
Document complexPojoPatch = new Document();
Resource data = new Resource();
complexPojoPatch.setData(Nullable.of((Object) data));
data.setType("complexpojos");
String rawContainedPatchData = " {" + " 'updateableProperty1':'updated value'" + " }";
rawContainedPatchData = rawContainedPatchData.replaceAll("'", "\"");
data.setAttribute("containedPojo", objectMapper.readTree(rawContainedPatchData));
data.setAttribute("updateableProperty", objectMapper.readTree("\"wasNullBefore\""));
JsonPath jsonPath = pathBuilder.build("/complexpojos/" + complexPojoId);
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = sut.handle(jsonPath, emptyComplexPojoQuery, null, complexPojoPatch);
// THEN
Assert.assertNotNull(response);
assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("updated value");
assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty2").asText()).isEqualTo("value from repository mock");
assertThat(response.getDocument().getSingleData().get().getAttributes().get("updateableProperty").asText()).isEqualTo("wasNullBefore");
}
use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.
the class ResourcePatchTest method onGivenRequestResourceGetShouldHandleIt.
@Test
public void onGivenRequestResourceGetShouldHandleIt() 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();
taskPatch.setData(Nullable.of((Object) data));
data.setAttribute("name", objectMapper.readTree("\"task updated\""));
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");
Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("name"), Mockito.eq("task updated"));
}
use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.
the class ResourcePatchTest method omittedFieldsShouldBeIgnored.
@Test
public void omittedFieldsShouldBeIgnored() throws Exception {
// GIVEN
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
Task task = new Task();
task.setName("Mary Joe");
JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
task = (Task) (jsonApiResponse.getEntity());
// GIVEN
Resource data = new Resource();
data.setType("tasks");
data.setAttribute("category", objectMapper.readTree("\"TestCategory\""));
Document taskPatch = new Document();
taskPatch.setData(Nullable.of((Object) data));
JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
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");
Resource updatedTask = response.getDocument().getSingleData().get();
assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Joe");
assertThat(updatedTask.getAttributes().get("category").asText()).isEqualTo("TestCategory");
assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
Assert.assertEquals("Mary Joe", task.getName());
Assert.assertEquals("TestCategory", task.getCategory());
}
use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.
the class ResourcePatchTest method omittedFieldsSettersAreNotCalled.
/*
* see github #122
*/
@Test
public void omittedFieldsSettersAreNotCalled() throws Exception {
// GIVEN
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
Task task = new Task();
task.setName("Mary Joe");
JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
task = (Task) (jsonApiResponse.getEntity());
// GIVEN
Document taskPatch = new Document();
Resource data = new Resource();
taskPatch.setData(Nullable.of((Object) data));
data.setType("tasks");
data.setAttribute("name", objectMapper.readTree("\"Mary Jane\""));
JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
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");
Resource updatedTask = response.getDocument().getSingleData().get();
assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Jane");
assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
assertThat(updatedTask.getAttributes().get("category")).isNull();
}
Aggregations