|
13 | 13 |
|
14 | 14 | import static org.awaitility.Awaitility.await; |
15 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
16 | 17 | import static org.junit.jupiter.api.Assertions.assertThrows; |
17 | 18 | import static org.junit.jupiter.api.Assertions.assertTrue; |
18 | 19 |
|
| 20 | +import java.util.EnumMap; |
19 | 21 | import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.stream.Collectors; |
20 | 25 |
|
21 | 26 | import org.junit.jupiter.api.AfterAll; |
22 | 27 | import org.junit.jupiter.api.BeforeAll; |
23 | 28 | import org.junit.jupiter.api.Test; |
24 | 29 | import org.kitodo.MockDatabase; |
| 30 | +import org.kitodo.data.database.beans.Process; |
25 | 31 | import org.kitodo.data.database.beans.Task; |
| 32 | +import org.kitodo.data.database.beans.User; |
26 | 33 | import org.kitodo.data.database.enums.TaskStatus; |
27 | 34 | import org.kitodo.data.database.exceptions.DAOException; |
| 35 | +import org.kitodo.data.database.persistence.TaskDAO; |
28 | 36 | import org.kitodo.production.helper.Helper; |
29 | 37 | import org.kitodo.production.services.ServiceManager; |
30 | 38 |
|
@@ -272,4 +280,72 @@ public void shouldFindDistinctTitles() throws Exception { |
272 | 280 | title = taskTitlesDistinct.get(2); |
273 | 281 | assertEquals("Closed", title, "Incorrect sorting of distinct titles for tasks!"); |
274 | 282 | } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void shouldLoadTaskTitlesForProcesses() throws Exception { |
| 286 | + List<Integer> ids = List.of(1, 2); |
| 287 | + Map<Integer, Map<TaskStatus, List<String>>> actualMap = taskService.loadTaskTitlesForProcesses(ids); |
| 288 | + |
| 289 | + for (Integer id : ids) { |
| 290 | + Process p = ServiceManager.getProcessService().getById(id); |
| 291 | + Map<TaskStatus, List<String>> processResults = actualMap.get(id); |
| 292 | + |
| 293 | + for (TaskStatus status : List.of(TaskStatus.OPEN, TaskStatus.INWORK)) { |
| 294 | + List<String> expected = p.getTasks().stream() |
| 295 | + .filter(t -> t.getProcessingStatus() == status) |
| 296 | + .map(Task::getTitle) |
| 297 | + .collect(Collectors.toList()); |
| 298 | + |
| 299 | + List<String> actual = processResults.getOrDefault(status, List.of()); |
| 300 | + |
| 301 | + assertEquals(expected.size(), actual.size(), "Size mismatch for " + status); |
| 302 | + assertTrue(actual.containsAll(expected), "Content mismatch for " + status); |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + public void shouldLoadTaskStatusCountsForProcesses() throws Exception { |
| 309 | + TaskDAO dao = new TaskDAO(); |
| 310 | + List<Process> processes = ServiceManager.getProcessService().getAll(); |
| 311 | + |
| 312 | + List<Integer> rootIds = processes.stream() |
| 313 | + .map(Process::getId) |
| 314 | + .collect(Collectors.toList()); |
| 315 | + |
| 316 | + Map<Integer, EnumMap<TaskStatus, Integer>> actual = |
| 317 | + dao.loadTaskStatusCountsForProcesses(rootIds); |
| 318 | + |
| 319 | + for (Process root : processes) { |
| 320 | + EnumMap<TaskStatus, Integer> expected = new EnumMap<>(TaskStatus.class); |
| 321 | + for (TaskStatus s : TaskStatus.values()) { |
| 322 | + expected.put(s, 0); |
| 323 | + } |
| 324 | + collectCounts(root, expected); |
| 325 | + EnumMap<TaskStatus, Integer> actualCounts = actual.get(root.getId()); |
| 326 | + assertNotNull(actualCounts, "No result returned for process " + root.getId()); |
| 327 | + |
| 328 | + for (TaskStatus status : TaskStatus.values()) { |
| 329 | + assertEquals( |
| 330 | + expected.get(status), |
| 331 | + actualCounts.get(status), |
| 332 | + "Mismatch for process " + root.getId() + " and status " + status |
| 333 | + ); |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + private void collectCounts(Process process, EnumMap<TaskStatus, Integer> counts) { |
| 339 | + if (Objects.isNull(process)) { |
| 340 | + return; |
| 341 | + } |
| 342 | + for (Task task : process.getTasks()) { |
| 343 | + counts.merge(task.getProcessingStatus(), 1, Integer::sum); |
| 344 | + } |
| 345 | + if (Objects.nonNull(process.getChildren())) { |
| 346 | + for (Process child : process.getChildren()) { |
| 347 | + collectCounts(child, counts); |
| 348 | + } |
| 349 | + } |
| 350 | + } |
275 | 351 | } |
0 commit comments