use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter 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.repository.ResourceRepositoryAdapter 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.internal.repository.ResourceRepositoryAdapter 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.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.
the class FieldResourceGetTest method onGivenIncludeRequestFieldResourcesGetShouldHandleIt.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {
// get repositories
ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class).getRelationshipRepository("assignedProjects", null);
RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class).getRelationshipRepository("tasks", null);
ResourceInformation userInfo = resourceRegistry.getEntry(User.class).getResourceInformation();
ResourceInformation projectInfo = resourceRegistry.getEntry(Project.class).getResourceInformation();
ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
ResourceField assignedProjectsField = userInfo.findRelationshipFieldByName("assignedProjects");
// setup test data
User user = new User();
user.setId(1L);
userRepo.create(user, emptyUserQuery);
Project project = new Project();
project.setId(2L);
projectRepo.create(project, emptyProjectQuery);
Task task = new Task();
task.setId(3L);
taskRepo.create(task, emptyTaskQuery);
relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), assignedProjectsField, emptyProjectQuery);
relRepositoryProjectToTask.setRelation(project, task.getId(), includedTaskField, emptyTaskQuery);
Map<String, Set<String>> params = new HashMap<String, Set<String>>();
addParams(params, "include[projects]", "includedTask");
QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
QueryAdapter queryAdapter = new QueryParamsAdapter(projectInfo, queryParams, moduleRegistry);
JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
Response response = sut.handle(jsonPath, queryAdapter, null, null);
// THEN
Assert.assertNotNull(response);
Assert.assertNotNull(response.getDocument().getData());
List<Resource> entityList = ((List<Resource>) response.getDocument().getData().get());
Assert.assertEquals(true, entityList.size() > 0);
Assert.assertEquals("projects", entityList.get(0).getType());
Resource returnedProject = entityList.get(0);
Assert.assertEquals(project.getId().toString(), returnedProject.getId());
Relationship relationship = returnedProject.getRelationships().get("includedTask");
Assert.assertNotNull(relationship);
Assert.assertEquals(task.getId().toString(), relationship.getSingleData().get().getId());
}
use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.
the class AbstractIncludeBehaviorTest method setup.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
super.setup();
ResourceRepositoryAdapter hierarchicalTaskRepository = resourceRegistry.findEntry(HierarchicalTask.class).getResourceRepository(null);
h = new HierarchicalTask();
h.setId(1L);
h.setName("");
h0 = new HierarchicalTask();
h0.setId(2L);
h0.setName("0");
h0.setParent(h);
h1 = new HierarchicalTask();
h1.setId(3L);
h1.setName("1");
h1.setParent(h);
h11 = new HierarchicalTask();
h11.setId(4L);
h11.setName("11");
h11.setParent(h1);
h.setChildren(Arrays.asList(h0, h1));
h0.setChildren(new ArrayList<HierarchicalTask>());
h1.setChildren(Arrays.asList(h11));
h11.setChildren(new ArrayList<HierarchicalTask>());
QueryAdapter emptyQueryAdapter = new QuerySpecAdapter(new QuerySpec(HierarchicalTask.class), resourceRegistry);
hierarchicalTaskRepository.create(h, emptyQueryAdapter);
hierarchicalTaskRepository.create(h0, emptyQueryAdapter);
hierarchicalTaskRepository.create(h1, emptyQueryAdapter);
hierarchicalTaskRepository.create(h11, emptyQueryAdapter);
}
Aggregations