Search in sources :

Example 11 with Project

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

the class InMemoryEvaluatorTest method setup.

@Before
public void setup() {
    Project project1 = new Project();
    project1.setId(13L);
    Project project2 = new Project();
    project2.setId(14L);
    tasks = new ArrayList<>();
    for (long i = 0; i < 5; i++) {
        Task task = new Task();
        task.setId(i);
        task.setName("test" + i);
        task.setProjects(new ArrayList<Project>());
        tasks.add(task);
        if (i == 0) {
            task.getProjects().add(project1);
        }
        if (i < 2) {
            task.getProjects().add(project2);
        }
    }
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) Before(org.junit.Before)

Example 12 with Project

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

the class OppositeFowardingRelationshipRepositoryTest method checkFindOneTargetFromCollection.

@Test
public void checkFindOneTargetFromCollection() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(Arrays.asList(task));
    projectRepository.save(project);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    Project foundProject = (Project) relRepository.findOneTarget(13L, "project", querySpec);
    Assert.assertEquals(42L, foundProject.getId().longValue());
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 13 with Project

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

the class OwnerFowardingRelationshipRepositoryTest method checkSetRelation.

@Test
public void checkSetRelation() {
    taskProjectRepository.setRelation(task, 42L, "project");
    Assert.assertEquals(42L, task.getProject().getId().longValue());
    Project target = (Project) taskProjectRepository.findOneTarget(task.getId(), "project", new QuerySpec(Task.class));
    Assert.assertSame(42L, target.getId().longValue());
}
Also used : Project(io.crnk.core.mock.models.Project) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 14 with Project

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

the class QuerySpecRepositoryTest method checkCrud.

@SuppressWarnings({ "unchecked" })
private void checkCrud(QueryAdapter queryAdapter) {
    // setup data
    Project project = new Project();
    project.setId(3L);
    project.setName("myProject");
    projectAdapter.create(project, queryAdapter);
    Task task = new Task();
    task.setId(2L);
    task.setName("myTask");
    task.setProject(project);
    task.setProjects(Arrays.asList(project));
    taskAdapter.create(task, queryAdapter);
    // adapter
    List<Task> tasks = (List<Task>) taskAdapter.findAll(queryAdapter).getEntity();
    Assert.assertEquals(1, tasks.size());
    Assert.assertEquals(task, taskAdapter.findOne(2L, queryAdapter).getEntity());
    tasks = (List<Task>) taskAdapter.findAll(Arrays.asList(2L), queryAdapter).getEntity();
    Assert.assertEquals(1, tasks.size());
    // relation adapter
    ResourceField projectField = resourceRegistry.getEntry(Task.class).getResourceInformation().findRelationshipFieldByName("project");
    ResourceField tasksField = resourceRegistry.getEntry(Project.class).getResourceInformation().findRelationshipFieldByName("tasks");
    projectRelAdapter.setRelation(task, project.getId(), projectField, queryAdapter);
    Assert.assertNotNull(task.getProject());
    Assert.assertEquals(1, project.getTasks().size());
    JsonApiResponse response = projectRelAdapter.findOneTarget(2L, projectField, queryAdapter);
    Assert.assertEquals(project.getId(), ((Project) response.getEntity()).getId());
    projectRelAdapter.setRelation(task, null, projectField, queryAdapter);
    response = projectRelAdapter.findOneTarget(2L, projectField, queryAdapter);
    Assert.assertNull(task.getProject());
    // warning: bidirectionality not properly implemented here, would
    // require changes to the model used in many other places
    task.setProject(null);
    project.getTasks().clear();
    tasksRelAdapter.addRelations(project, Arrays.asList(task.getId()), tasksField, queryAdapter);
    Assert.assertEquals(project, task.getProject());
    Assert.assertEquals(1, project.getTasks().size());
    List<Project> projects = (List<Project>) tasksRelAdapter.findManyTargets(3L, tasksField, queryAdapter).getEntity();
    Assert.assertEquals(1, projects.size());
    tasksRelAdapter.removeRelations(project, Arrays.asList(task.getId()), tasksField, queryAdapter);
    Assert.assertEquals(0, project.getTasks().size());
    // fix bidirectionality
    task.setProject(null);
    projects = (List<Project>) tasksRelAdapter.findManyTargets(3L, tasksField, queryAdapter).getEntity();
    Assert.assertEquals(0, projects.size());
    tasksRelAdapter.setRelations(project, Arrays.asList(task.getId()), tasksField, queryAdapter);
    Assert.assertEquals(project, task.getProject());
    Assert.assertEquals(1, project.getTasks().size());
    projects = (List<Project>) tasksRelAdapter.findManyTargets(3L, tasksField, queryAdapter).getEntity();
    Assert.assertEquals(1, projects.size());
    // check bulk find
    Map<?, JsonApiResponse> bulkMap = tasksRelAdapter.findBulkManyTargets(Arrays.asList(3L), tasksField, queryAdapter);
    Assert.assertEquals(1, bulkMap.size());
    Assert.assertTrue(bulkMap.containsKey(3L));
    projects = (List<Project>) bulkMap.get(3L).getEntity();
    Assert.assertEquals(1, projects.size());
    bulkMap = projectRelAdapter.findBulkOneTargets(Arrays.asList(2L), projectField, queryAdapter);
    Assert.assertEquals(1, bulkMap.size());
    Assert.assertTrue(bulkMap.containsKey(2L));
    Assert.assertNotNull(bulkMap.get(2L));
    // deletion
    taskAdapter.delete(task.getId(), queryAdapter);
    tasks = (List<Task>) taskAdapter.findAll(queryAdapter).getEntity();
    Assert.assertEquals(0, tasks.size());
    tasks = (List<Task>) taskAdapter.findAll(Arrays.asList(2L), queryAdapter).getEntity();
    Assert.assertEquals(0, tasks.size());
}
Also used : Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Example 15 with Project

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

the class OppositeFowardingRelationshipRepositoryTest method checkFindTargetWithNotLoadedRelationship.

@Test
public void checkFindTargetWithNotLoadedRelationship() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(null);
    projectRepository.save(project);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    try {
        relRepository.findOneTarget(13L, "project", querySpec);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertTrue(e.getMessage().contains("field tasks is null for"));
    }
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Aggregations

Project (io.crnk.core.mock.models.Project)68 Test (org.junit.Test)54 Task (io.crnk.core.mock.models.Task)44 Document (io.crnk.core.engine.document.Document)27 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 Resource (io.crnk.core.engine.document.Resource)24 Response (io.crnk.core.engine.dispatcher.Response)15 Relationship (io.crnk.core.engine.document.Relationship)14 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)14 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)12 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)12 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)11 LazyTask (io.crnk.core.mock.models.LazyTask)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)8 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)8 ProjectRepository (io.crnk.core.mock.repository.ProjectRepository)7 TaskRepository (io.crnk.core.mock.repository.TaskRepository)7 QueryParams (io.crnk.legacy.queryParams.QueryParams)7 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6