DoWhileTaskMapper implements TaskMapper { @Override public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) { logger.debug("TaskMapperContext {} in DoWhileTaskMapper", taskMapperContext); WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule(); Workflow workflowInstance = taskMapperContext.getWorkflowInstance(); Task task = workflowInstance.getTaskByRefName(taskToSchedule.getTaskReferenceName()); if (task != null && task.getStatus().isTerminal()) { return Collections.emptyList(); } String taskId = taskMapperContext.getTaskId(); List<Task> tasksToBeScheduled = new ArrayList<>(); int retryCount = taskMapperContext.getRetryCount(); TaskDef taskDefinition = Optional.ofNullable(taskMapperContext.getTaskDefinition()) .orElseGet(() -> Optional.ofNullable(metadataDAO.getTaskDef(taskToSchedule.getName())) .orElseGet(TaskDef::new)); Task loopTask = new Task(); loopTask.setTaskType(SystemTaskType.DO_WHILE.name()); loopTask.setTaskDefName(taskToSchedule.getName()); loopTask.setReferenceTaskName(taskToSchedule.getTaskReferenceName()); loopTask.setWorkflowInstanceId(workflowInstance.getWorkflowId()); loopTask.setCorrelationId(workflowInstance.getCorrelationId()); loopTask.setWorkflowType(workflowInstance.getWorkflowName()); loopTask.setScheduledTime(System.currentTimeMillis()); loopTask.setTaskId(taskId); loopTask.setIteration(1); loopTask.setStatus(Task.Status.IN_PROGRESS); loopTask.setWorkflowTask(taskToSchedule); loopTask.setRateLimitPerFrequency(taskDefinition.getRateLimitPerFrequency()); loopTask.setRateLimitFrequencyInSeconds(taskDefinition.getRateLimitFrequencyInSeconds()); tasksToBeScheduled.add(loopTask); List<WorkflowTask> loopOverTasks = taskToSchedule.getLoopOver(); List<Task> tasks2 = taskMapperContext.getDeciderService() .getTasksToBeScheduled(workflowInstance, loopOverTasks.get(0), retryCount); tasks2.forEach(t -> { t.setReferenceTaskName(TaskUtils.appendIteration(t.getReferenceTaskName(), loopTask.getIteration())); t.setIteration(loopTask.getIteration()); }); tasksToBeScheduled.addAll(tasks2); return tasksToBeScheduled; } DoWhileTaskMapper(MetadataDAO metadataDAO); @Override List<Task> getMappedTasks(TaskMapperContext taskMapperContext);  }
@Test public void getMappedTasks() { Mockito.doReturn(Arrays.asList(task1)).when(deciderService).getTasksToBeScheduled(workflow, workflowTask1, 0); List<Task> mappedTasks = new DoWhileTaskMapper(metadataDAO).getMappedTasks(taskMapperContext); assertNotNull(mappedTasks); assertEquals(mappedTasks.size(), 2); assertEquals("task1__1", mappedTasks.get(1).getReferenceTaskName()); assertEquals(1, mappedTasks.get(1).getIteration()); assertEquals(SystemTaskType.DO_WHILE.name(), mappedTasks.get(0).getTaskType()); }
@Test public void shouldNotScheduleCompletedTask() { task1.setStatus(Task.Status.COMPLETED); List<Task> mappedTasks = new DoWhileTaskMapper(metadataDAO).getMappedTasks(taskMapperContext); assertNotNull(mappedTasks); assertEquals(mappedTasks.size(), 1); }