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);
}
}
}
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());
}
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());
}
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());
}
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"));
}
}
Aggregations