use of io.crnk.core.engine.dispatcher.Response 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.dispatcher.Response 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.dispatcher.Response 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();
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class ResourceFilterTest method checkFilterGetOnResourceField.
@Test
public void checkFilterGetOnResourceField() {
// setup test data
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
Project project = new Project();
project.setId(13L);
project.setName("myProject");
Task task = new Task();
task.setId(12L);
task.setName("myTask");
task.setProject(project);
resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
// get information
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
String path = "/tasks/";
String method = HttpMethod.GET.toString();
Map<String, Set<String>> parameters = Collections.emptyMap();
Document requestBody = null;
// forbid field
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
Resource taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
// allow resource
Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class FieldResourceGetTest method onGivenRequestFieldResourceGetShouldHandleIt.
@Test
public void onGivenRequestFieldResourceGetShouldHandleIt() throws Exception {
// GIVEN
JsonPath jsonPath = pathBuilder.build("/tasks/1/project");
FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
// WHEN
Response response = sut.handle(jsonPath, emptyProjectQuery, null, null);
// THEN
Assert.assertNotNull(response);
}
Aggregations