Search in sources :

Example 56 with Document

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

the class IncludeLookupSetterBaseTest method includeManyRelationLookup.

@Test
public void includeManyRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProjects"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("includedProjects");
    assertNotNull(relationship);
    assertEquals(1, relationship.getCollectionData().get().size());
}
Also used : 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 57 with Document

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

the class IncludeLookupSetterBaseTest method includePropertiesProviderNonOverwriteRelationshipLookup.

@Test
public void includePropertiesProviderNonOverwriteRelationshipLookup() throws Exception {
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
                return "false";
            }
            return null;
        }
    };
    mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    Project project = new Project();
    project.setId(12L);
    Task task = new Task();
    task.setId(1L);
    task.setProject(project);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("project");
    assertNotNull(relationship);
    assertEquals(relationship.getSingleData().get().getId(), "12");
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) Project(io.crnk.core.mock.models.Project) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) 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 58 with Document

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

the class IncludeLookupSetterBaseTest method includeOneRelationLookup.

@Test
public void includeOneRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProject"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("includedProject");
    assertNotNull(relationship);
    assertNotNull(relationship.getSingleData());
    List<Resource> included = document.getIncluded();
    assertEquals(1, included.size());
}
Also used : 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 59 with Document

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

the class LookupAlwaysRelationIdLookupTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestLookupAlwaysId(3L);
    }
    if (setRelatedEntity) {
        entity.setTestLookupAlways(schedule);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testLookupAlways"));
    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("testLookupAlways").getSingleData();
    Assert.assertTrue(data.isPresent());
    if (setRelatedId) {
        Assert.assertNotNull(data.get());
        Assert.assertEquals(1, document.getIncluded().size());
        Assert.assertEquals("3", document.getIncluded().get(0).getId());
        Assert.assertEquals(1, scheduleRepository.getNumFindAll());
    } else {
        Assert.assertNull(data.get());
        Assert.assertEquals(0, scheduleRepository.getNumFindAll());
    }
}
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 60 with Document

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

the class LookupNotNullRelationIdLookupTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestLookupWhenNullId(3L);
    }
    if (setRelatedEntity) {
        entity.setTestLookupWhenNull(schedule);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testLookupWhenNull"));
    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("testLookupWhenNull").getSingleData();
    Assert.assertTrue(data.isPresent());
    if (setRelatedId) {
        Assert.assertNotNull(data.get());
        Assert.assertEquals(1, document.getIncluded().size());
        Assert.assertEquals("3", document.getIncluded().get(0).getId());
        if (setRelatedEntity) {
            Assert.assertEquals(0, scheduleRepository.getNumFindAll());
        } else {
            Assert.assertEquals(1, scheduleRepository.getNumFindAll());
        }
    } 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)

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