Search in sources :

Example 46 with Relationship

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

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

Example 48 with Relationship

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

the class IncludeLookupSetterBaseTest method paginationMustOnlyHappenRootButNotInclusions.

@Test
public void paginationMustOnlyHappenRootButNotInclusions() {
    HierarchicalTask hDetached = new HierarchicalTask();
    hDetached.setId(1L);
    hDetached.setName("");
    QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
    querySpec.setOffset(0L);
    querySpec.setLimit(1L);
    querySpec.includeRelation(Arrays.asList("children"));
    Document document = mapper.toDocument(toResponse(hDetached), toAdapter(querySpec));
    Relationship childrenRelationship = document.getSingleData().get().getRelationships().get("children");
    List<ResourceIdentifier> childIds = childrenRelationship.getCollectionData().get();
    Assert.assertEquals(2, childIds.size());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Relationship(io.crnk.core.engine.document.Relationship) 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)

Example 49 with Relationship

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

the class ResourceFieldContributorTest method checkInclusionUponRequest.

@Test
public void checkInclusionUponRequest() {
    TaskRepository repo = new TaskRepository();
    Task task = new Task();
    task.setId(1L);
    task.setName("someTask");
    repo.save(task);
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    HttpRequestContextProvider httpRequestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
    HttpRequestContext request = Mockito.mock(HttpRequestContext.class);
    httpRequestContextProvider.onRequestStarted(request);
    Map<String, Set<String>> parameters = new HashMap<>();
    parameters.put("include", Sets.newHashSet("contributedProject"));
    Response response = requestDispatcher.dispatchRequest("tasks", "GET", parameters, null, null);
    Document document = response.getDocument();
    Resource resource = document.getCollectionData().get().get(0);
    Assert.assertEquals("1", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertEquals("someTask", resource.getAttributes().get("name").asText());
    Relationship relationship = resource.getRelationships().get("contributedProject");
    Assert.assertNotNull(relationship);
    ResourceIdentifier relationshipId = relationship.getSingleData().get();
    Assert.assertEquals("projects", relationshipId.getType());
    Assert.assertEquals("11", relationshipId.getId());
    List<Resource> included = document.getIncluded();
    Assert.assertEquals(1, included.size());
    Resource includedResource = included.get(0);
    Assert.assertEquals("projects", includedResource.getType());
    Assert.assertEquals("11", includedResource.getId());
    Assert.assertEquals("someProject", includedResource.getAttributes().get("name").asText());
}
Also used : Task(io.crnk.core.mock.models.Task) Set(java.util.Set) HashMap(java.util.HashMap) ProjectToTaskRepository(io.crnk.core.mock.repository.ProjectToTaskRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) Test(org.junit.Test)

Example 50 with Relationship

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

the class PerTypeIncludeBehaviorTest 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(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)

Aggregations

Relationship (io.crnk.core.engine.document.Relationship)54 Resource (io.crnk.core.engine.document.Resource)48 Document (io.crnk.core.engine.document.Document)39 Test (org.junit.Test)39 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)26 Task (io.crnk.core.mock.models.Task)22 Response (io.crnk.core.engine.dispatcher.Response)14 Project (io.crnk.core.mock.models.Project)14 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)13 LazyTask (io.crnk.core.mock.models.LazyTask)12 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)9 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)4