Search in sources :

Example 96 with Resource

use of io.crnk.core.engine.document.Resource 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());
}
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) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 97 with Resource

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

the class DocumentMapperTest method testAttributesSelection.

@Test
public void testAttributesSelection() {
    Task task = createTask(2, "sample task");
    task.setCategory("sample category");
    task.setProject(new Project());
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(task);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeField(Arrays.asList("category"));
    Document document = mapper.toDocument(response, toAdapter(querySpec));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertNull(resource.getAttributes().get("name"));
    Assert.assertNull(resource.getRelationships().get("project"));
    Assert.assertEquals("sample category", resource.getAttributes().get("category").asText());
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 98 with Resource

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

the class DocumentMapperTest method testAttributesBasic.

@Test
public void testAttributesBasic() {
    Task task = createTask(2, "sample task");
    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());
    Assert.assertThat(resource.getAttributes().get("writeOnlyValue"), CoreMatchers.nullValue());
}
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 99 with Resource

use of io.crnk.core.engine.document.Resource 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"));
}
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) LinksInformation(io.crnk.core.resource.links.LinksInformation) 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 100 with Resource

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

Aggregations

Resource (io.crnk.core.engine.document.Resource)130 Document (io.crnk.core.engine.document.Document)86 Test (org.junit.Test)85 Relationship (io.crnk.core.engine.document.Relationship)48 Response (io.crnk.core.engine.dispatcher.Response)43 QuerySpec (io.crnk.core.queryspec.QuerySpec)43 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)42 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)41 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)37 Task (io.crnk.core.mock.models.Task)32 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)31 Project (io.crnk.core.mock.models.Project)24 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)17 LazyTask (io.crnk.core.mock.models.LazyTask)15 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)14 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)13 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10