use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class IncludeLookupUtil method isInclusionRequestedForQueryspec.
private boolean isInclusionRequestedForQueryspec(QueryAdapter queryAdapter, List<ResourceField> fieldPath) {
QuerySpec querySpec = ((QuerySpecAdapter) queryAdapter).getQuerySpec();
if (includeBehavior == IncludeBehavior.PER_ROOT_PATH) {
return contains(querySpec, toPathList(fieldPath, 0));
} else {
for (int i = fieldPath.size() - 1; i >= 0; i--) {
List<String> path = toPathList(fieldPath, i);
// TODO subtyping not properly supported
ResourceInformation rootInformation = fieldPath.get(i).getParentResourceInformation();
QuerySpec rootQuerySpec = querySpec.getQuerySpec(rootInformation);
if (rootQuerySpec != null && contains(rootQuerySpec, path)) {
return true;
}
}
return contains(querySpec, toPathList(fieldPath, 0));
}
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class RelationshipsResourceDeleteTest method onExistingToManyRelationshipShouldRemoveIt.
@Test
public void onExistingToManyRelationshipShouldRemoveIt() throws Exception {
// GIVEN
Document newUserDocument = new Document();
newUserDocument.setData(Nullable.of((Object) createUser()));
JsonPath taskPath = pathBuilder.build("/users");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a user
Response taskResponse = resourcePost.handle(taskPath, new QuerySpecAdapter(new QuerySpec(User.class), resourceRegistry), null, newUserDocument);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(userId).isNotNull();
/* ------- */
// GIVEN
Document newProjectDocument = new Document();
newProjectDocument.setData(Nullable.of((Object) createProject()));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectDocument);
// 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 newProjectDocument2 = new Document();
newProjectDocument2.setData(Nullable.of((Object) createProject(projectId.toString())));
JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
RelationshipsResourcePost relationshipsResourcePost = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between user and project
Response projectRelationshipResponse = relationshipsResourcePost.handle(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
assertThat(project.getId()).isEqualTo(projectId);
/* ------- */
// GIVEN
RelationshipsResourceDelete sut = new RelationshipsResourceDelete(resourceRegistry, typeParser, modificationFilters);
// WHEN -- removing a relation between task and project
Response result = sut.handle(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
assertThat(result).isNotNull();
// THEN
Project nullProject = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
assertThat(nullProject).isNull();
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class DocumentMapperTest method testConvergingInclusionPaths.
@Test
public void testConvergingInclusionPaths() {
Task task1 = createTask(1, "other task");
Task task2 = createTask(2, "other task");
Project project1 = new Project();
project1.setName("someProject");
project1.setId(3L);
project1.setTasks(Arrays.asList(task1, task2));
Project project2 = new Project();
project2.setName("someProject");
project2.setId(2L);
task1.setProject(project1);
task1.setProjectsInit(Arrays.asList(project2));
// come back/converge to same task
project1.setTask(task2);
project2.setTask(task2);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("project", "task"));
querySpec.includeRelation(Arrays.asList("projectsInit", "task"));
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
Document document = mapper.toDocument(toResponse(task1), queryAdapter);
Resource resource = document.getSingleData().get();
Assert.assertEquals("1", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Relationship projectRel = resource.getRelationships().get("project");
Assert.assertNotNull(projectRel.getLinks());
Assert.assertTrue(projectRel.getData().isPresent());
Assert.assertNotNull(projectRel.getData().get());
Relationship projectsRel = resource.getRelationships().get("projectsInit");
Assert.assertNotNull(projectsRel.getLinks());
Assert.assertTrue(projectsRel.getData().isPresent());
Assert.assertNotNull(projectsRel.getData().get());
Assert.assertEquals(1, projectsRel.getCollectionData().get().size());
Assert.assertEquals(3, document.getIncluded().size());
List<Resource> included = document.getIncluded();
Resource projectResource2 = included.get(0);
Resource projectResource3 = included.get(1);
Assert.assertTrue(projectResource2.getRelationships().get("task").getData().isPresent());
Assert.assertTrue(projectResource3.getRelationships().get("task").getData().isPresent());
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class DocumentMapperTest method testCompactModeWithInclusion.
@Test
public void testCompactModeWithInclusion() {
Project project = new Project();
project.setName("someProject");
project.setId(3L);
LinksInformation links = new TaskLinks();
Task task = createTask(2, "sample task");
task.setLinksInformation(links);
task.setProject(project);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("project"));
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
queryAdapter.setCompactMode(true);
Document document = mapper.toDocument(toResponse(task), queryAdapter);
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Assert.assertNull(resource.getLinks().get("self"));
Assert.assertNotNull(resource.getLinks().get("someLink"));
Relationship projectRel = resource.getRelationships().get("project");
Assert.assertNull(projectRel.getLinks());
Assert.assertEquals(1, document.getIncluded().size());
Resource projectResource = document.getIncluded().get(0);
Assert.assertNull(projectResource.getRelationships().get("tasks"));
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class RepositoryFilterTest method findAllWithResourceListResult.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void findAllWithResourceListResult() throws Exception {
RegistryEntry scheduleRegistry = resourceRegistry.getEntry(Schedule.class);
ResourceRepositoryAdapter<Schedule, Serializable> scheduleResourceAdapter = scheduleRegistry.getResourceRepository(null);
querySpec = new QuerySpec(Schedule.class);
querySpec.setPagingSpec(new OffsetLimitPagingSpec());
queryAdapter = new QuerySpecAdapter(querySpec, resourceRegistry);
scheduleResourceAdapter.findAll(queryAdapter);
ArgumentCaptor<Iterable> linksResources = ArgumentCaptor.forClass(Iterable.class);
ArgumentCaptor<Iterable> metaResources = ArgumentCaptor.forClass(Iterable.class);
ArgumentCaptor<RepositoryFilterContext> contexts = ArgumentCaptor.forClass(RepositoryFilterContext.class);
Mockito.verify(filter, Mockito.times(1)).filterRequest(contexts.capture(), Mockito.any(RepositoryRequestFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterResult(Mockito.any(RepositoryFilterContext.class), Mockito.any(RepositoryResultFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterLinks(Mockito.any(RepositoryFilterContext.class), linksResources.capture(), Mockito.any(RepositoryLinksFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterMeta(Mockito.any(RepositoryFilterContext.class), metaResources.capture(), Mockito.any(RepositoryMetaFilterChain.class));
Assert.assertEquals(1, linksResources.getAllValues().size());
Assert.assertEquals(1, metaResources.getAllValues().size());
Assert.assertEquals(1, contexts.getAllValues().size());
RepositoryFilterContext context = contexts.getAllValues().iterator().next();
RepositoryRequestSpec requestSpec = context.getRequest();
Assert.assertEquals(queryAdapter, requestSpec.getQueryAdapter());
Assert.assertNull(requestSpec.getId());
Assert.assertNull(requestSpec.getIds());
QuerySpec actualQuerySpec = requestSpec.getQuerySpec(scheduleInfo);
Assert.assertSame(querySpec, actualQuerySpec);
}
Aggregations