use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class DocumentMapperTest method testRelationshipSingleValuedIncludeByDefault.
@Test
public void testRelationshipSingleValuedIncludeByDefault() {
Task task = createTask(2, "sample task");
Project project = createProject(3, "sample project");
task.setProject(project);
Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Assert.assertEquals("sample task", resource.getAttributes().get("name").asText());
Relationship relationship = resource.getRelationships().get("project");
Assert.assertNotNull(relationship);
Assert.assertEquals("https://service.local/tasks/2/relationships/project", getLinkText(relationship.getLinks().get("self")));
Assert.assertEquals("https://service.local/tasks/2/project", getLinkText(relationship.getLinks().get("related")));
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());
}
use of io.crnk.core.engine.document.Relationship 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.engine.document.Relationship in project crnk-framework by crnk-project.
the class DocumentMapperTest method testRelationshipSingleValuedEager.
@Test
public void testRelationshipSingleValuedEager() {
LazyTask task = createLazyTask(2);
Project project = createProject(3, "sample project");
task.setProject(project);
Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class));
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());
Assert.assertTrue(document.getIncluded().isEmpty());
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class DocumentMapperTest method testRelationshipLazyMultiValued.
@Test
public void testRelationshipLazyMultiValued() {
LazyTask task = createLazyTask(2);
Project project1 = createProject(3, "sample project");
Project project2 = createProject(4, "sample project");
task.setProjects(Arrays.asList(project1, project2));
Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Relationship relationship = resource.getRelationships().get("projects");
Assert.assertNotNull(relationship);
Nullable<List<ResourceIdentifier>> relationshipData = relationship.getCollectionData();
Assert.assertFalse(relationshipData.isPresent());
Assert.assertTrue(document.getIncluded().isEmpty());
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class PerRootPathIncludeBehaviorTest method includeParent.
@Test
public void includeParent() throws Exception {
QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
querySpec.includeRelation(Arrays.asList("parent"));
Document document = mapper.toDocument(toResponse(h11), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
Relationship parentRelationship = taskResource.getRelationships().get("parent");
assertNotNull(parentRelationship);
assertNotNull(parentRelationship.getSingleData());
ResourceIdentifier parentResource = parentRelationship.getSingleData().get();
assertNotNull(h1.getId().toString(), parentResource.getId());
List<Resource> included = document.getIncluded();
assertEquals(1, included.size());
assertNotNull(h1.getId().toString(), included.get(0).getId());
Relationship parentParentRelationship = included.get(0).getRelationships().get("parent");
assertFalse(parentParentRelationship.getData().isPresent());
}
Aggregations