Search in sources :

Example 61 with Resource

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

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

Example 63 with Resource

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

the class InconsistentRelationshipTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestResourceIdRefId(new ResourceIdentifier("3", "schedules"));
    }
    if (setRelatedEntity) {
        entity.setTestResourceIdRef(schedule);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testResourceIdRef"));
    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("testResourceIdRef").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(setRelatedEntity ? 0 : 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 64 with Resource

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

the class DynamicRelationshipRepository method createResource.

private Resource createResource() {
    ObjectMapper mapper = new ObjectMapper();
    Resource resource = new Resource();
    resource.setId("john");
    resource.setType(resourceType);
    try {
        resource.getAttributes().put("value", mapper.readTree("\"doe\""));
    } catch (IOException e) {
        throw new IllegalArgumentException();
    }
    return resource;
}
Also used : Resource(io.crnk.core.engine.document.Resource) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 65 with Resource

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

the class ResourceInformationTest method toResourceIdentifier.

@Test
public void toResourceIdentifier() {
    Resource resource = new Resource();
    resource.setId("13");
    resource.setType("tasks");
    Task task = new Task();
    task.setId(13L);
    ResourceIdentifier expected = new ResourceIdentifier("13", "tasks");
    Assert.assertSame(expected, sut.toResourceIdentifier(expected));
    Assert.assertNull(sut.toResourceIdentifier(null));
    Assert.assertEquals(expected, sut.toResourceIdentifier("13"));
    Assert.assertEquals(expected, sut.toResourceIdentifier(13L));
    Assert.assertEquals(expected, sut.toResourceIdentifier(task));
    Assert.assertEquals(expected, sut.toResourceIdentifier(resource));
    Assert.assertNotSame(resource, sut.toResourceIdentifier(resource));
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) 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