Search in sources :

Example 16 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testPaging.

@Test
public void testPaging() throws InstantiationException, IllegalAccessException {
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(2L, 2L), resourceRegistry);
    JsonApiResponse results = adapter.findAll(querySpec);
    HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
    Assert.assertTrue(metaInformation.getHasMoreResources());
    PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=2", linksInformation.getFirst());
    Assert.assertNull(linksInformation.getLast());
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=2", linksInformation.getPrev());
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=2&page[offset]=4", linksInformation.getNext());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 17 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testPagingNoContents.

@Test
public void testPagingNoContents() throws InstantiationException, IllegalAccessException {
    HasNextPageTestRepository.clear();
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(0L, 2L), resourceRegistry);
    JsonApiResponse results = adapter.findAll(querySpec);
    HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
    Assert.assertFalse(metaInformation.getHasMoreResources());
    PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
    Assert.assertNull(linksInformation.getFirst());
    Assert.assertNull(linksInformation.getLast());
    Assert.assertNull(linksInformation.getPrev());
    Assert.assertNull(linksInformation.getNext());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 18 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testInvalidPaging.

@Test(expected = BadRequestException.class)
public void testInvalidPaging() throws InstantiationException, IllegalAccessException {
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(1L, 3L), resourceRegistry);
    adapter.findAll(querySpec).getLinksInformation();
}
Also used : QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 19 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testNoPaging.

@Test
public void testNoPaging() throws InstantiationException, IllegalAccessException {
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(), resourceRegistry);
    JsonApiResponse results = adapter.findAll(querySpec);
    HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
    Assert.assertNull(metaInformation.getHasMoreResources());
    PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
    Assert.assertNull(linksInformation);
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 20 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method supportPolymorphicRelationshipTypes.

@Test
public void supportPolymorphicRelationshipTypes() {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = new Resource();
    data.setType(ClassUtils.getAnnotation(Task.class, JsonApiResource.class).get().type());
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    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 taskIdOne = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdTwo = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdThree = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    newTaskBody = new Document();
    // Create ProjectPolymorphic object
    Document newProjectBody = new Document();
    data = new Resource();
    String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
    data.setType(type);
    data.getRelationships().put("task", new Relationship(new ResourceIdentifier(taskIdOne.toString(), "tasks")));
    data.getRelationships().put("tasks", new Relationship(Arrays.asList(new ResourceIdentifier(taskIdTwo.toString(), "tasks"), new ResourceIdentifier(taskIdThree.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPolymorphicTypePath = pathBuilder.build("/" + type);
    Response projectResponse = resourcePost.handle(projectPolymorphicTypePath, emptyProjectQuery, null, newProjectBody);
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    Resource projectPolymorphic = projectResponse.getDocument().getSingleData().get();
    assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
    assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
    ProjectPolymorphicRepository resourceRepository = (ProjectPolymorphicRepository) resourceRegistry.getEntry(ProjectPolymorphic.class).getResourceRepository(null).getResourceRepository();
    ProjectPolymorphic projectPolymorphicObj = resourceRepository.findOne(projectId, null);
    assertEquals(2, projectPolymorphicObj.getTasks().size());
    projectPolymorphicTypePath = pathBuilder.build("/" + type + "/" + projectPolymorphic.getId());
    ResourcePatch resourcePatch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    data = newProjectBody.getSingleData().get();
    data.setId(projectId.toString());
    projectPolymorphic.setId(Long.toString(projectId));
    data.getRelationships().get("tasks").setData(Nullable.of((Object) new ArrayList<ResourceIdentifier>()));
    // WHEN
    Response baseResponseContext = resourcePatch.handle(projectPolymorphicTypePath, new QuerySpecAdapter(new QuerySpec(ProjectPolymorphic.class), resourceRegistry), null, newProjectBody);
    assertThat(baseResponseContext.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
    projectId = Long.parseLong(baseResponseContext.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    projectPolymorphic = baseResponseContext.getDocument().getSingleData().get();
    assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
    assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
    projectPolymorphicObj = resourceRepository.findOne(projectId, null);
    assertEquals(0, projectPolymorphicObj.getTasks().size());
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ProjectPolymorphicRepository(io.crnk.core.mock.repository.ProjectPolymorphicRepository) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) Relationship(io.crnk.core.engine.document.Relationship) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) 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