Search in sources :

Example 51 with ResourceIdentifier

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

the class SerializeEagerRelationIdLookupTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestSerializeEagerId(3L);
    }
    if (setRelatedEntity) {
        entity.setTestSerializeEager(schedule);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    // since eager no inclusion necessary
    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("testSerializeEager").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 52 with ResourceIdentifier

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

the class RelationIdClientTest method checkResourceIdentifierField.

@Test
public void checkResourceIdentifierField() {
    Schedule schedule = new Schedule();
    schedule.setId(13L);
    schedule.setName("mySchedule");
    Schedule savedSchedule = scheduleRepo.create(schedule);
    RelationIdTestResource resource = new RelationIdTestResource();
    resource.setId(14L);
    resource.setName("test");
    resource.setTestResourceIdRefId(new ResourceIdentifier("13", "schedules"));
    ResourceRepositoryV2<RelationIdTestResource, Serializable> repository = client.getRepositoryForType(RelationIdTestResource.class);
    RelationIdTestResource createdResource = repository.create(resource);
    Assert.assertEquals(resource.getTestResourceIdRefId(), createdResource.getTestResourceIdRefId());
    RelationIdTestResource serverResource = RelationIdTestRepository.resources.get(14L);
    Assert.assertEquals(resource.getTestResourceIdRefId(), serverResource.getTestResourceIdRefId());
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    RelationIdTestResource getResource = repository.findOne(14L, querySpec);
    Assert.assertNull(getResource.getTestResourceIdRefId());
    Assert.assertNull(createdResource.getTestResourceIdRef());
    querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testResourceIdRef"));
    getResource = repository.findOne(14L, querySpec);
    Assert.assertEquals(resource.getTestResourceIdRefId(), getResource.getTestResourceIdRefId());
    Assert.assertNotNull(getResource.getTestResourceIdRef());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) Schedule(io.crnk.test.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationIdTestResource(io.crnk.test.mock.models.RelationIdTestResource) Test(org.junit.Test)

Example 53 with ResourceIdentifier

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

the class RelationshipRepositoryStubImpl method executeWithIds.

private void executeWithIds(String requestUrl, HttpMethod method, Iterable<?> targetIds) {
    Document document = new Document();
    ArrayList<ResourceIdentifier> resourceIdentifiers = new ArrayList<>();
    for (Object targetId : targetIds) {
        resourceIdentifiers.add(sourceResourceInformation.toResourceIdentifier(targetId));
    }
    document.setData(Nullable.of((Object) resourceIdentifiers));
    doExecute(requestUrl, method, document);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ArrayList(java.util.ArrayList) Document(io.crnk.core.engine.document.Document)

Example 54 with ResourceIdentifier

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

the class IncludeLookupSetterInheritanceTest method testPolymorhRelationship.

@Test
public void testPolymorhRelationship() 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);
    List<ResourceIdentifier> projects = relationship.getCollectionData().get();
    assertEquals(2, projects.size());
    List<Resource> included = document.getIncluded();
    assertEquals(2, included.size());
    Assert.assertEquals("projects", included.get(1).getType());
    Assert.assertEquals("fancy-projects", included.get(0).getType());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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 55 with ResourceIdentifier

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

the class LookupNoneRelationIdLookupTest method check.

private void check(boolean setRelatedEntity, boolean setRelatedId) {
    RelationIdTestResource entity = new RelationIdTestResource();
    entity.setId(2L);
    entity.setName("test");
    if (setRelatedId) {
        entity.setTestLookupNoneId(3L);
    }
    if (setRelatedEntity) {
        entity.setTestLookupNone(schedule);
    }
    QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
    querySpec.includeRelation(Arrays.asList("testLookupNone"));
    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("testLookupNone").getSingleData();
    Assert.assertTrue(data.isPresent());
    Assert.assertEquals(0, scheduleRepository.getNumFindAll());
    if (setRelatedEntity) {
        Assert.assertNotNull(data.get());
        Assert.assertEquals(1, document.getIncluded().size());
        Assert.assertEquals("3", document.getIncluded().get(0).getId());
    } else if (setRelatedId) {
        Assert.fail("without lookup related entity should be set if related id is set");
    } 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

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