Search in sources :

Example 21 with Relationship

use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.

the class DocumentMapperTest method testRelationshipCyclicInclusion.

@Test
public void testRelationshipCyclicInclusion() {
    Task task = createTask(2, "sample task");
    Project project = createProject(3, "sample project");
    task.setProject(project);
    project.setTask(task);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    querySpec.getOrCreateQuerySpec(Project.class).includeRelation(Arrays.asList("task"));
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Relationship relationship = resource.getRelationships().get("project");
    Assert.assertNotNull(relationship);
    ResourceIdentifier relationshipData = relationship.getSingleData().get();
    Assert.assertNotNull(relationshipData);
    Assert.assertEquals("3", relationshipData.getId());
    Assert.assertEquals("projects", relationshipData.getType());
    List<Resource> included = document.getIncluded();
    Assert.assertEquals(1, included.size());
    Assert.assertEquals("3", included.get(0).getId());
    Assert.assertEquals("projects", included.get(0).getType());
    Assert.assertEquals("sample project", included.get(0).getAttributes().get("name").asText());
    Assert.assertEquals("2", included.get(0).getRelationships().get("task").getSingleData().get().getId());
}
Also used : Project(io.crnk.core.mock.models.Project) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 22 with Relationship

use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method paginationMustHappenRootAndInclusions.

@Test
public void paginationMustHappenRootAndInclusions() {
    Mockito.when(propertiesProvider.getProperty(Mockito.eq(CrnkProperties.INCLUDE_PAGING_ENABLED))).thenReturn("true");
    setup();
    HierarchicalTask hDetached = new HierarchicalTask();
    hDetached.setId(1L);
    hDetached.setName("");
    QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
    querySpec.setOffset(0L);
    querySpec.setLimit(1L);
    querySpec.includeRelation(Arrays.asList("children"));
    Document document = mapper.toDocument(toResponse(hDetached), toAdapter(querySpec));
    Relationship childrenRelationship = document.getSingleData().get().getRelationships().get("children");
    List<ResourceIdentifier> childIds = childrenRelationship.getCollectionData().get();
    Assert.assertEquals(1, childIds.size());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Relationship(io.crnk.core.engine.document.Relationship) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 23 with Relationship

use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includeOneDeepNestedRelationLookup.

@Test
public void includeOneDeepNestedRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProject", "includedTask", "includedProject"));
    Task task = new Task();
    task.setId(3L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship oneRelationship = taskResource.getRelationships().get("includedProject");
    assertNotNull(oneRelationship);
    assertNotNull(oneRelationship.getSingleData());
    List<Resource> includes = document.getIncluded();
    assertEquals(1, includes.size());
    Resource includedResource = includes.get(0);
    assertEquals("projects", includedResource.getType());
    assertNotNull(includedResource.getRelationships().get("includedTask"));
    assertNotNull(includedResource.getRelationships().get("includedTask").getData());
}
Also used : HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) 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) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 24 with Relationship

use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includeByDefaultSerializeNLevels.

@Test
public void includeByDefaultSerializeNLevels() throws Exception {
    Project project = new Project();
    project.setId(1L);
    Task task = new Task().setId(2L);
    project.setTask(task);
    Project projectDefault = new Project().setId(3L);
    task.setProject(projectDefault);
    mapper = new DocumentMapper(resourceRegistry, objectMapper, new EmptyPropertiesProvider(), resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Project.class);
    querySpec.includeRelation(Collections.singletonList("task"));
    Document document = mapper.toDocument(toResponse(project), toAdapter(querySpec));
    Resource projectResource = document.getSingleData().get();
    Relationship relationship = projectResource.getRelationships().get("task");
    assertNotNull(relationship);
    assertEquals("2", relationship.getSingleData().get().getId());
    assertNotNull(document.getIncluded());
    assertEquals(2, document.getIncluded().size());
    List<Resource> resources = document.getIncluded();
    assertEquals("projects", resources.get(0).getType());
    assertEquals("3", resources.get(0).getId());
    assertEquals("tasks", resources.get(1).getType());
    assertEquals("2", resources.get(1).getId());
}
Also used : Project(io.crnk.core.mock.models.Project) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 25 with Relationship

use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includeManyRelationLookup.

@Test
public void includeManyRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProjects"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("includedProjects");
    assertNotNull(relationship);
    assertEquals(1, relationship.getCollectionData().get().size());
}
Also used : HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) 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) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Aggregations

Relationship (io.crnk.core.engine.document.Relationship)54 Resource (io.crnk.core.engine.document.Resource)48 Document (io.crnk.core.engine.document.Document)39 Test (org.junit.Test)39 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)26 Task (io.crnk.core.mock.models.Task)22 Response (io.crnk.core.engine.dispatcher.Response)14 Project (io.crnk.core.mock.models.Project)14 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)13 LazyTask (io.crnk.core.mock.models.LazyTask)12 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)9 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)4