Search in sources :

Example 36 with QuerySpecAdapter

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));
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter)

Example 37 with QuerySpecAdapter

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();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) RelationshipsResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceDelete) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 38 with QuerySpecAdapter

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());
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 39 with QuerySpecAdapter

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"));
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) LinksInformation(io.crnk.core.resource.links.LinksInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 40 with QuerySpecAdapter

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);
}
Also used : Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) OffsetLimitPagingSpec(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Aggregations

QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)48 QuerySpec (io.crnk.core.queryspec.QuerySpec)32 Test (org.junit.Test)25 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)15 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)13 Task (io.crnk.core.mock.models.Task)11 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)11 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)11 Document (io.crnk.core.engine.document.Document)10 Resource (io.crnk.core.engine.document.Resource)8 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 Before (org.junit.Before)7 Relationship (io.crnk.core.engine.document.Relationship)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 Project (io.crnk.core.mock.models.Project)6 HasMoreResourcesMetaInformation (io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)4 Response (io.crnk.core.engine.dispatcher.Response)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4