Search in sources :

Example 1 with RelationIdTestResource

use of io.crnk.core.mock.models.RelationIdTestResource in project crnk-framework by crnk-project.

the class ImplicitOwnerBasedRelationshipRepositoryTest method setup.

@Before
public void setup() {
    MockRepositoryUtil.clear();
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
    boot.boot();
    resourceRegistry = boot.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(RelationIdTestResource.class);
    relRepository = new ImplicitOwnerBasedRelationshipRepository(RelationIdTestResource.class, Schedule.class);
    relRepository.setResourceRegistry(resourceRegistry);
    taskProjectRepository = new ImplicitOwnerBasedRelationshipRepository(Task.class, Project.class);
    taskProjectRepository.setResourceRegistry(resourceRegistry);
    testRepository = (RelationIdTestRepository) entry.getResourceRepository().getResourceRepository();
    testRepository.setResourceRegistry(resourceRegistry);
    resource = new RelationIdTestResource();
    resource.setId(2L);
    resource.setName("relationId");
    testRepository.create(resource);
    scheduleRepository = new ScheduleRepositoryImpl();
    schedule3 = new Schedule();
    schedule3.setId(3L);
    schedule3.setName("schedule");
    scheduleRepository.create(schedule3);
    for (int i = 0; i < 10; i++) {
        schedule = new Schedule();
        schedule.setId(4L + i);
        schedule.setName("schedule");
        scheduleRepository.create(schedule);
        projectRepository = new ProjectRepository();
        project = new Project();
        project.setId(42L + i);
        project.setName("project");
        projectRepository.save(project);
        taskRepository = new TaskRepository();
        task = new Task();
        task.setId(13L + i);
        task.setName("task");
        taskRepository.save(task);
    }
}
Also used : Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ScheduleRepositoryImpl(io.crnk.core.mock.repository.ScheduleRepositoryImpl) Project(io.crnk.core.mock.models.Project) CrnkBoot(io.crnk.core.boot.CrnkBoot) ImplicitOwnerBasedRelationshipRepository(io.crnk.core.repository.implicit.ImplicitOwnerBasedRelationshipRepository) Schedule(io.crnk.core.mock.models.Schedule) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) Before(org.junit.Before)

Example 2 with RelationIdTestResource

use of io.crnk.core.mock.models.RelationIdTestResource in project crnk-framework by crnk-project.

the class ResourceIdControllerTest method checkPatchResource.

private Resource checkPatchResource(Resource resource) {
    JsonPath path = pathBuilder.build("/relationIdTest/" + resource.getId());
    resource.getRelationships().put("testLookupAlways", new Relationship(schedule2Id));
    Document newDocument = createDocument(resource);
    // WHEN PATCH
    ResourcePatch sut = new ResourcePatch(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = sut.handle(path, emptyTaskQuery, null, newDocument);
    // THEN PATCH
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.OK_200);
    Resource updatedResource = taskResponse.getDocument().getSingleData().get();
    Assert.assertEquals(resource.getType(), updatedResource.getType());
    Assert.assertEquals(resource.getId(), updatedResource.getId());
    Assert.assertEquals(resource.getAttributes().get("name"), updatedResource.getAttributes().get("name"));
    Relationship relationship = updatedResource.getRelationships().get("testLookupAlways");
    Assert.assertTrue(relationship.getData().isPresent());
    Assert.assertEquals(schedule2Id, relationship.getData().get());
    // validate relationship not accessed, only id set => performance
    RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
    Assert.assertEquals(schedule2.getId(), entity.getTestLookupAlwaysId());
    Assert.assertNull(entity.getTestLookupAlways());
    return updatedResource;
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Relationship(io.crnk.core.engine.document.Relationship) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) Resource(io.crnk.core.engine.document.Resource) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource)

Example 3 with RelationIdTestResource

use of io.crnk.core.mock.models.RelationIdTestResource 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 4 with RelationIdTestResource

use of io.crnk.core.mock.models.RelationIdTestResource 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 5 with RelationIdTestResource

use of io.crnk.core.mock.models.RelationIdTestResource 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)

Aggregations

RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)16 QuerySpec (io.crnk.core.queryspec.QuerySpec)13 Document (io.crnk.core.engine.document.Document)12 Resource (io.crnk.core.engine.document.Resource)11 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)9 Response (io.crnk.core.engine.dispatcher.Response)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)3 Schedule (io.crnk.core.mock.models.Schedule)3 ScheduleRepositoryImpl (io.crnk.core.mock.repository.ScheduleRepositoryImpl)3 Before (org.junit.Before)3 CrnkBoot (io.crnk.core.boot.CrnkBoot)2 Relationship (io.crnk.core.engine.document.Relationship)2 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)2 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)2 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)2 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)2 Project (io.crnk.core.mock.models.Project)2 Task (io.crnk.core.mock.models.Task)2 ProjectRepository (io.crnk.core.mock.repository.ProjectRepository)2 TaskRepository (io.crnk.core.mock.repository.TaskRepository)2