Search in sources :

Example 6 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class InheritanceClientTest method doTestIncludePoloymorphCollection.

private void doTestIncludePoloymorphCollection(boolean include) {
    QuerySpec querySpec = new QuerySpec(Project.class);
    if (include) {
        querySpec.includeRelation(Arrays.asList("tasks"));
    }
    List<Project> projects = projectRepo.findAll(querySpec);
    Assert.assertEquals(1, projects.size());
    Project project = projects.get(0);
    List<Task> tasks = project.getTasks();
    if (include) {
        Assert.assertFalse(tasks instanceof ObjectProxy);
    } else {
        ObjectProxy proxy = (ObjectProxy) tasks;
        Assert.assertFalse(proxy.isLoaded());
    }
    if (tasks.get(0).getName().equals("baseTask")) {
        Assert.assertEquals("baseTask", tasks.get(0).getName());
        Assert.assertEquals("taskSubType", tasks.get(1).getName());
    } else {
        Assert.assertEquals("baseTask", tasks.get(1).getName());
        Assert.assertEquals("taskSubType", tasks.get(0).getName());
    }
}
Also used : Project(io.crnk.test.mock.models.Project) Task(io.crnk.test.mock.models.Task) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 7 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class AbstractProxiedObjectsClientTest method proxyForCollection.

private void proxyForCollection(boolean set) {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("project");
    scheduleRepo.create(schedule);
    Task task = new Task();
    task.setId(2L);
    task.setName("test");
    taskRepo.create(task);
    taskScheduleRepo.setRelation(task, schedule.getId(), "schedule");
    // collection must be available as proxy
    QuerySpec querySpec = new QuerySpec(Task.class);
    schedule = scheduleRepo.findOne(1L, querySpec);
    Collection<Task> proxiedTasks = set ? schedule.getTasks() : schedule.getTasksList();
    Assert.assertNotNull(proxiedTasks);
    // check status without loading
    ObjectProxy proxy = (ObjectProxy) proxiedTasks;
    Assert.assertFalse(proxy.isLoaded());
    Assert.assertNotNull(proxy.getUrl());
    Assert.assertFalse(proxy.isLoaded());
    // lazy load
    Assert.assertEquals(1, proxiedTasks.size());
    Assert.assertTrue(proxy.isLoaded());
    task = proxiedTasks.iterator().next();
    Assert.assertEquals(2L, task.getId().longValue());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 8 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class AbstractProxiedObjectsClientTest method saveLazyCollectionChange.

@Test
public void saveLazyCollectionChange() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("project");
    scheduleRepo.create(schedule);
    Task task = new Task();
    task.setId(2L);
    task.setName("test");
    taskRepo.create(task);
    QuerySpec querySpec = new QuerySpec(Task.class);
    schedule = scheduleRepo.findOne(1L, querySpec);
    Collection<Task> proxiedTasks = schedule.getTasks();
    ObjectProxy proxy = (ObjectProxy) proxiedTasks;
    Assert.assertFalse(proxy.isLoaded());
    // add task to collection
    proxiedTasks.add(task);
    Assert.assertTrue(proxy.isLoaded());
    Assert.assertEquals(1, proxiedTasks.size());
    scheduleRepo.save(schedule);
    schedule = scheduleRepo.findOne(1L, querySpec);
    proxiedTasks = schedule.getTasks();
    Assert.assertEquals(1, proxiedTasks.size());
    // remove task from collection
    proxiedTasks.remove(task);
    Assert.assertEquals(1, proxiedTasks.size());
    scheduleRepo.save(schedule);
    schedule = scheduleRepo.findOne(1L, querySpec);
    proxiedTasks = schedule.getTasks();
    Assert.assertEquals(1, proxiedTasks.size());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Aggregations

ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)8 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 Task (io.crnk.test.mock.models.Task)5 Test (org.junit.Test)5 Schedule (io.crnk.test.mock.models.Schedule)4 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)2 Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 ResourceMapper (io.crnk.core.engine.internal.document.mapper.ResourceMapper)1 ResourceMappingConfig (io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 ResourceList (io.crnk.core.resource.list.ResourceList)1 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)1 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)1 RelatedEntity (io.crnk.jpa.model.RelatedEntity)1 TestEntity (io.crnk.jpa.model.TestEntity)1 Project (io.crnk.test.mock.models.Project)1 ScheduleRepository (io.crnk.test.mock.repository.ScheduleRepository)1