use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class DocumentMapperTest method testMultipleInclusions.
@Test
public void testMultipleInclusions() {
Task task1 = createTask(1, "other task");
Task task2 = createTask(2, "other task");
Task task3 = createTask(3, "sample task");
Project project = new Project();
project.setName("someProject");
project.setId(3L);
project.setTask(task1);
project.setTasks(Arrays.asList(task2, task3));
QuerySpec querySpec = new QuerySpec(Project.class);
querySpec.includeRelation(Arrays.asList("tasks"));
querySpec.includeRelation(Arrays.asList("task"));
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
Document document = mapper.toDocument(toResponse(project), queryAdapter);
Resource resource = document.getSingleData().get();
Assert.assertEquals("3", resource.getId());
Assert.assertEquals("projects", resource.getType());
Relationship taskRel = resource.getRelationships().get("task");
Assert.assertNotNull(taskRel.getLinks());
Assert.assertTrue(taskRel.getData().isPresent());
Assert.assertNotNull(taskRel.getData().get());
Relationship tasksRel = resource.getRelationships().get("tasks");
Assert.assertNotNull(tasksRel.getLinks());
Assert.assertTrue(tasksRel.getData().isPresent());
Assert.assertNotNull(tasksRel.getData().get());
Assert.assertEquals(2, tasksRel.getCollectionData().get().size());
Assert.assertEquals(3, document.getIncluded().size());
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class DocumentMapperTest method testRelationshipIncludeRelation.
@Test
public void testRelationshipIncludeRelation() {
LazyTask task = createLazyTask(2);
Project project = createProject(3, "sample project");
task.setProject(project);
QuerySpec querySpec = new QuerySpec(LazyTask.class);
querySpec.includeRelation(Arrays.asList("project"));
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());
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class DocumentMapperTest method testCompactMode.
@Test
public void testCompactMode() {
LinksInformation links = new TaskLinks();
Task task = createTask(2, "sample task");
task.setLinksInformation(links);
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(new QuerySpec(Task.class));
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 project = resource.getRelationships().get("project");
Assert.assertNull(project.getLinks());
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class DocumentMapperTest method testSerializeWithoutLinks.
@Test
public void testSerializeWithoutLinks() {
Task task = createTask(2, "sample task");
DocumentMappingConfig mappingConfig = new DocumentMappingConfig();
mappingConfig.getResourceMapping().setSerializeLinks(false);
Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class), mappingConfig);
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Assert.assertNull(resource.getLinks());
Relationship relationship = resource.getRelationships().get("project");
Assert.assertNull(relationship.getLinks());
}
use of io.crnk.core.engine.document.Resource 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());
}
Aggregations