Search in sources :

Example 46 with Document

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

the class DocumentMapperTest method testResourceInformation.

@Test
public void testResourceInformation() {
    TestLinksInformation links = new TestLinksInformation();
    links.value = "linksValue";
    TestMetaInformation meta = new TestMetaInformation();
    meta.value = "metaValue";
    Task task = createTask(2, "sample task");
    task.setMetaInformation(meta);
    task.setLinksInformation(links);
    Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("linksValue", getLinkText(resource.getLinks().get("value")));
    Assert.assertEquals("metaValue", resource.getMeta().get("value").asText());
}
Also used : LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 47 with Document

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

the class DocumentMapperTest method testRelationshipSingleValuedLazy.

@Test
public void testRelationshipSingleValuedLazy() {
    LazyTask task = createLazyTask(2);
    Project project = createProject(3, "sample project");
    task.setLazyProject(project);
    Document document = mapper.toDocument(toResponse(task), createAdapter(Task.class));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("lazy_tasks", resource.getType());
    Relationship relationship = resource.getRelationships().get("lazyProject");
    Assert.assertNotNull(relationship);
    Assert.assertEquals("https://service.local/lazy_tasks/2/relationships/lazyProject", getLinkText(relationship.getLinks().get("self")));
    Assert.assertEquals("https://service.local/lazy_tasks/2/lazyProject", getLinkText(relationship.getLinks().get("related")));
    Nullable<ResourceIdentifier> relationshipData = relationship.getSingleData();
    Assert.assertFalse(relationshipData.isPresent());
    Assert.assertTrue(document.getIncluded().isEmpty());
}
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) LazyTask(io.crnk.core.mock.models.LazyTask) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 48 with Document

use of io.crnk.core.engine.document.Document 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());
}
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 49 with Document

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

Example 50 with Document

use of io.crnk.core.engine.document.Document 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());
}
Also used : 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) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

Document (io.crnk.core.engine.document.Document)131 Test (org.junit.Test)95 Resource (io.crnk.core.engine.document.Resource)87 Response (io.crnk.core.engine.dispatcher.Response)56 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)47 QuerySpec (io.crnk.core.queryspec.QuerySpec)45 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)40 Relationship (io.crnk.core.engine.document.Relationship)39 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)35 Task (io.crnk.core.mock.models.Task)34 Project (io.crnk.core.mock.models.Project)27 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)25 LazyTask (io.crnk.core.mock.models.LazyTask)17 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)11 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)10