Search in sources :

Example 46 with ResourceIdentifier

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

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

the class NestedRelationIdLookupTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestNestedId(child.getId());
    }
    if (setRelatedEntity) {
        entity.setTestNested(child);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testNested", "schedule"));
    Document document = mapper.toDocument(toResponse(entity), toAdapter(querySpec));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("relationIdTest", resource.getType());
    Assert.assertEquals("test", resource.getAttributes().get("name").asText());
    Nullable<ResourceIdentifier> data = resource.getRelationships().get("testNested").getSingleData();
    Assert.assertTrue(data.isPresent());
    if (setRelatedId) {
        Assert.assertNotNull(data.get());
        Assert.assertEquals(2, document.getIncluded().size());
    } else {
        Assert.assertNull(data.get());
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource)

Example 48 with ResourceIdentifier

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

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

the class DocumentMapperUtilTest method toResourceIds.

@Test
public void toResourceIds() {
    Task task = new Task();
    task.setId(12L);
    List<ResourceIdentifier> ids = util.toResourceIds(Arrays.asList(task));
    ResourceIdentifier id = ids.get(0);
    Assert.assertEquals("tasks", id.getType());
    Assert.assertEquals("12", id.getId());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Task(io.crnk.core.mock.models.Task) Test(org.junit.Test)

Example 50 with ResourceIdentifier

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

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