SubWorkflow extends WorkflowSystemTask { @Override public void cancel(Workflow workflow, Task task, WorkflowExecutor provider) { String workflowId = task.getSubWorkflowId(); if(StringUtils.isEmpty(workflowId)) { return; } Workflow subWorkflow = provider.getWorkflow(workflowId, true); subWorkflow.setStatus(WorkflowStatus.TERMINATED); provider.terminateWorkflow(subWorkflow, "Parent workflow has been terminated with status " + workflow.getStatus(), null); } @VisibleForTesting  SubWorkflow(); @Inject  SubWorkflow(ObjectMapper objectMapper); @SuppressWarnings("unchecked") @Override void start(Workflow workflow, Task task, WorkflowExecutor provider); @Override boolean execute(Workflow workflow, Task task, WorkflowExecutor provider); @Override void cancel(Workflow workflow, Task task, WorkflowExecutor provider); @Override boolean isAsync(); @Override boolean isAsyncComplete(Task task); static final String NAME; }
@Test public void testCancelWithWorkflowId() { WorkflowDef workflowDef = new WorkflowDef(); Workflow workflowInstance = new Workflow(); Workflow subWorkflowInstance = new Workflow(); workflowInstance.setWorkflowDefinition(workflowDef); Task task = new Task(); Map<String, Object> outputData = new HashMap<>(); task.setOutputData(outputData); task.setSubWorkflowId("sub-workflow-id"); Map<String, Object> inputData = new HashMap<>(); inputData.put("subWorkflowName", "UnitWorkFlow"); inputData.put("subWorkflowVersion", 2); task.setInputData(inputData); when(workflowExecutor.startWorkflow(eq("UnitWorkFlow"), eq(2), eq(inputData), eq(null), any(), any(), any(), eq(null), eq(null))) .thenReturn("workflow_1"); when(workflowExecutor.getWorkflow(eq("sub-workflow-id"), eq(true))) .thenReturn(subWorkflowInstance); workflowInstance.setStatus(Workflow.WorkflowStatus.TIMED_OUT); subWorkflow.cancel(workflowInstance, task, workflowExecutor); assertEquals(Workflow.WorkflowStatus.TERMINATED, subWorkflowInstance.getStatus()); }
@Test public void testCancelWithoutWorkflowId() { WorkflowDef workflowDef = new WorkflowDef(); Workflow workflowInstance = new Workflow(); Workflow subWorkflowInstance = new Workflow(); workflowInstance.setWorkflowDefinition(workflowDef); Task task = new Task(); Map<String, Object> outputData = new HashMap<>(); task.setOutputData(outputData); Map<String, Object> inputData = new HashMap<>(); inputData.put("subWorkflowName", "UnitWorkFlow"); inputData.put("subWorkflowVersion", 2); task.setInputData(inputData); when(workflowExecutor.startWorkflow(eq("UnitWorkFlow"), eq(2), eq(inputData), eq(null), any(), any(), any(), eq(null), eq(null))) .thenReturn("workflow_1"); when(workflowExecutor.getWorkflow(eq("sub-workflow-id"), eq(false))) .thenReturn(subWorkflowInstance); subWorkflow.cancel(workflowInstance, task, workflowExecutor); assertEquals(Workflow.WorkflowStatus.RUNNING, subWorkflowInstance.getStatus()); }