Search in sources :

Example 41 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier 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 42 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier 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)

Example 43 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier 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());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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 44 with ResourceIdentifier

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

the class PerRootPathIncludeBehaviorTest method includeParentParent.

@Test
public void includeParentParent() throws Exception {
    QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
    querySpec.includeRelation(Arrays.asList("parent", "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(2, included.size());
    assertNotNull(h1.getId().toString(), included.get(0).getId());
    assertNotNull(h.getId().toString(), included.get(1).getId());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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 45 with ResourceIdentifier

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

the class IncludeLookupSetterBaseTest method includeManyDeepNestedRelationLookup.

@Test
public void includeManyDeepNestedRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProjects", "includedTask", "includedProject"));
    Task task = new Task();
    task.setId(3L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship manyRelationship = taskResource.getRelationships().get("includedProjects");
    assertNotNull(manyRelationship);
    List<ResourceIdentifier> relationshipData = manyRelationship.getCollectionData().get();
    assertNotNull(relationshipData.get(0).getId());
    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 : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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

ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)60 Resource (io.crnk.core.engine.document.Resource)42 Document (io.crnk.core.engine.document.Document)40 Test (org.junit.Test)33 Relationship (io.crnk.core.engine.document.Relationship)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 Response (io.crnk.core.engine.dispatcher.Response)14 Task (io.crnk.core.mock.models.Task)13 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)11 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)11 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)11 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)9 Serializable (java.io.Serializable)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8 Project (io.crnk.core.mock.models.Project)8 ArrayList (java.util.ArrayList)8 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)6 LazyTask (io.crnk.core.mock.models.LazyTask)6 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)4