UserDefinedTaskMapper implements TaskMapper { @Override public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) throws TerminateWorkflowException { logger.debug("TaskMapperContext {} in UserDefinedTaskMapper", taskMapperContext); WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule(); Workflow workflowInstance = taskMapperContext.getWorkflowInstance(); String taskId = taskMapperContext.getTaskId(); int retryCount = taskMapperContext.getRetryCount(); TaskDef taskDefinition = Optional.ofNullable(taskMapperContext.getTaskDefinition()) .orElseGet(() -> Optional.ofNullable(metadataDAO.getTaskDef(taskToSchedule.getName())) .orElseThrow(() -> { String reason = String.format("Invalid task specified. Cannot find task by name %s in the task definitions", taskToSchedule.getName()); return new TerminateWorkflowException(reason); })); Map<String, Object> input = parametersUtils.getTaskInputV2(taskToSchedule.getInputParameters(), workflowInstance, taskId, taskDefinition); Task userDefinedTask = new Task(); userDefinedTask.setTaskType(taskToSchedule.getType()); userDefinedTask.setTaskDefName(taskToSchedule.getName()); userDefinedTask.setReferenceTaskName(taskToSchedule.getTaskReferenceName()); userDefinedTask.setWorkflowInstanceId(workflowInstance.getWorkflowId()); userDefinedTask.setWorkflowType(workflowInstance.getWorkflowName()); userDefinedTask.setCorrelationId(workflowInstance.getCorrelationId()); userDefinedTask.setScheduledTime(System.currentTimeMillis()); userDefinedTask.setTaskId(taskId); userDefinedTask.setInputData(input); userDefinedTask.setStatus(Task.Status.SCHEDULED); userDefinedTask.setRetryCount(retryCount); userDefinedTask.setCallbackAfterSeconds(taskToSchedule.getStartDelay()); userDefinedTask.setWorkflowTask(taskToSchedule); userDefinedTask.setWorkflowPriority(workflowInstance.getPriority()); userDefinedTask.setRateLimitPerFrequency(taskDefinition.getRateLimitPerFrequency()); userDefinedTask.setRateLimitFrequencyInSeconds(taskDefinition.getRateLimitFrequencyInSeconds()); return Collections.singletonList(userDefinedTask); } UserDefinedTaskMapper(ParametersUtils parametersUtils, MetadataDAO metadataDAO); @Override List<Task> getMappedTasks(TaskMapperContext taskMapperContext); static final Logger logger; }
@Test public void getMappedTasks() { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setName("user_task"); taskToSchedule.setType(TaskType.USER_DEFINED.name()); taskToSchedule.setTaskDefinition(new TaskDef("user_task")); String taskId = IDGenerator.generate(); String retriedTaskId = IDGenerator.generate(); Workflow workflow = new Workflow(); WorkflowDef workflowDef = new WorkflowDef(); workflow.setWorkflowDefinition(workflowDef); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(workflowDef) .withWorkflowInstance(workflow) .withTaskDefinition(new TaskDef()) .withTaskToSchedule(taskToSchedule) .withTaskInput(new HashMap<>()) .withRetryCount(0) .withRetryTaskId(retriedTaskId) .withTaskId(taskId) .build(); List<Task> mappedTasks = userDefinedTaskMapper.getMappedTasks(taskMapperContext); assertEquals(1, mappedTasks.size()); assertEquals(TaskType.USER_DEFINED.name(), mappedTasks.get(0).getTaskType()); }
@Test public void getMappedTasksException() { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setName("user_task"); taskToSchedule.setType(TaskType.USER_DEFINED.name()); String taskId = IDGenerator.generate(); String retriedTaskId = IDGenerator.generate(); Workflow workflow = new Workflow(); WorkflowDef workflowDef = new WorkflowDef(); workflow.setWorkflowDefinition(workflowDef); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(workflowDef) .withWorkflowInstance(workflow) .withTaskToSchedule(taskToSchedule) .withTaskInput(new HashMap<>()) .withRetryCount(0) .withRetryTaskId(retriedTaskId) .withTaskId(taskId) .build(); expectedException.expect(TerminateWorkflowException.class); expectedException.expectMessage(String.format("Invalid task specified. Cannot find task by name %s in the task definitions", taskToSchedule.getName())); userDefinedTaskMapper.getMappedTasks(taskMapperContext); }