@Test public void testRemoveWorkflow() { when(executionDAO.getWorkflow(anyString(), anyBoolean())).thenReturn(new Workflow()); executionDAOFacade.removeWorkflow("workflowId", false); verify(indexDAO, never()).updateWorkflow(any(), any(), any()); verify(indexDAO, times(1)).removeWorkflow(anyString()); }
@Test public void tesGetWorkflowById() throws Exception { when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(new Workflow()); Workflow workflow = executionDAOFacade.getWorkflowById("workflowId", true); assertNotNull(workflow); verify(indexDAO, never()).get(any(), any()); when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(null); InputStream stream = ExecutionDAOFacadeTest.class.getResourceAsStream("/test.json"); byte[] bytes = IOUtils.toByteArray(stream); String jsonString = new String(bytes); when(indexDAO.get(any(), any())).thenReturn(jsonString); workflow = executionDAOFacade.getWorkflowById("workflowId", true); assertNotNull(workflow); verify(indexDAO, times(1)).get(any(), any()); }
@Test public void testGetWorkflowsByCorrelationId() { when(executionDAO.canSearchAcrossWorkflows()).thenReturn(true); when(executionDAO.getWorkflowsByCorrelationId(any(), anyBoolean())).thenReturn(Collections.singletonList(new Workflow())); List<Workflow> workflows = executionDAOFacade.getWorkflowsByCorrelationId("correlationId", true); assertNotNull(workflows); assertEquals(1, workflows.size()); verify(indexDAO, never()).searchWorkflows(anyString(), anyString(), anyInt(), anyInt(), any()); when(executionDAO.canSearchAcrossWorkflows()).thenReturn(false); List<String> workflowIds = new ArrayList<>(); workflowIds.add("workflowId"); SearchResult<String> searchResult = new SearchResult<>(); searchResult.setResults(workflowIds); when(indexDAO.searchWorkflows(anyString(), anyString(), anyInt(), anyInt(), any())).thenReturn(searchResult); when(executionDAO.getWorkflow("workflowId", true)).thenReturn(new Workflow()); workflows = executionDAOFacade.getWorkflowsByCorrelationId("correlationId", true); assertNotNull(workflows); assertEquals(1, workflows.size()); }
@Test public void testGetExecutionStatus() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); when(mockWorkflowService.getExecutionStatus(anyString(), anyBoolean())).thenReturn(workflow); assertEquals(workflow, workflowResource.getExecutionStatus("w123", true)); }
@Test public void testGetExecutionStatus() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); when(mockExecutionService.getExecutionStatus(anyString(), anyBoolean())).thenReturn(workflow); assertEquals(workflow, workflowService.getExecutionStatus("w123", true)); }
@Test public void testSearchWorkflows() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); WorkflowSummary workflowSummary = new WorkflowSummary(workflow); List<WorkflowSummary> listOfWorkflowSummary = new ArrayList<WorkflowSummary>() {{ add(workflowSummary); }}; SearchResult<WorkflowSummary> searchResult = new SearchResult<WorkflowSummary>(100, listOfWorkflowSummary); when(mockExecutionService.search(anyString(), anyString(), anyInt(), anyInt(), anyListOf(String.class))).thenReturn(searchResult); assertEquals(searchResult, workflowService.searchWorkflows(0,100,"asc", "*", "*")); }
@SuppressWarnings("unchecked") @Test public void testUploadWorkflowPayload() throws IOException { AtomicInteger uploadCount = new AtomicInteger(0); InputStream stream = ExternalPayloadStorageUtilsTest.class.getResourceAsStream("/payload.json"); Map<String, Object> payload = objectMapper.readValue(stream, Map.class); when(externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, ExternalPayloadStorage.PayloadType.WORKFLOW_OUTPUT, "")).thenReturn(location); doAnswer(invocation -> { uploadCount.incrementAndGet(); return null; }).when(externalPayloadStorage).upload(anyString(), any(), anyLong()); Workflow workflow = new Workflow(); workflow.setOutput(payload); externalPayloadStorageUtils.verifyAndUpload(workflow, ExternalPayloadStorage.PayloadType.WORKFLOW_OUTPUT); assertNull(workflow.getOutput()); assertEquals(1, uploadCount.get()); assertNotNull(workflow.getExternalOutputPayloadStoragePath()); }
@Test public void getWorkflows() { Workflow workflow = new Workflow(); workflow.setCorrelationId("123"); ArrayList<Workflow> listOfWorkflows = new ArrayList<Workflow>() {{ add(workflow); }}; when(mockWorkflowService.getWorkflows(anyString(), anyString(), anyBoolean(), anyBoolean())).thenReturn(listOfWorkflows); assertEquals(listOfWorkflows, workflowResource.getWorkflows("test1", "123", true, true)); }
@Test public void testGetWorklfowsSingleCorrelationId() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); List<Workflow> workflowArrayList = new ArrayList<Workflow>() {{ add(workflow); }}; when(mockExecutionService.getWorkflowInstances(anyString(), anyString(), anyBoolean(), anyBoolean())) .thenReturn(workflowArrayList); assertEquals(workflowArrayList, workflowService.getWorkflows("test", "c123", true, true)); }
@Test(expected = ApplicationException.class) public void testRetryWorkflowNoTasks() { Workflow workflow = new Workflow(); workflow.setWorkflowId("ApplicationException"); workflow.setStatus(Workflow.WorkflowStatus.FAILED); //noinspection unchecked workflow.setTasks(new ArrayList()); when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow); workflowExecutor.retry(workflow.getWorkflowId()); }
@Test(expected = ApplicationException.class) public void testRetryNonTerminalWorkflow() { Workflow workflow = new Workflow(); workflow.setWorkflowId("testRetryNonTerminalWorkflow"); workflow.setStatus(Workflow.WorkflowStatus.COMPLETED); when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow); workflowExecutor.retry(workflow.getWorkflowId()); }
@Test public void testGetWorklfowsMultipleCorrelationId() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); List<Workflow> workflowArrayList = new ArrayList<Workflow>() {{ add(workflow); }}; List<String> correlationIdList = new ArrayList<String>() {{ add("c123"); }}; Map<String, List<Workflow>> workflowMap = new HashMap<>(); workflowMap.put("c123", workflowArrayList); when(mockExecutionService.getWorkflowInstances(anyString(), anyString(), anyBoolean(), anyBoolean())) .thenReturn(workflowArrayList); assertEquals(workflowMap, workflowService.getWorkflows("test", true, true, correlationIdList)); }
@Test public void testGetWorklfowsMultipleCorrelationId() { Workflow workflow = new Workflow(); workflow.setCorrelationId("c123"); List<Workflow> workflowArrayList = new ArrayList<Workflow>() {{ add(workflow); }}; List<String> correlationIdList = new ArrayList<String>() {{ add("c123"); }}; Map<String, List<Workflow>> workflowMap = new HashMap<>(); workflowMap.put("c123", workflowArrayList); when(mockWorkflowService.getWorkflows(anyString(), anyBoolean(), anyBoolean(), anyListOf(String.class))) .thenReturn(workflowMap); assertEquals(workflowMap, workflowResource.getWorkflows("test", true, true, correlationIdList)); }
@Test public void testGetTaskInputV1() { Map<String, Object> ip = new HashMap<>(); ip.put("workflowInputParam", "workflow.input.requestId"); ip.put("taskOutputParam", "task2.output.location"); WorkflowDef def = new WorkflowDef(); def.setSchemaVersion(1); Workflow workflow = new Workflow(); workflow.setWorkflowDefinition(def); workflow.getInput().put("requestId", "request id 001"); Task task = new Task(); task.setReferenceTaskName("task2"); task.getOutputData().put("location", "http://location"); task.getOutputData().put("isPersonActive", true); workflow.getTasks().add(task); Map<String, Object> taskInput = parametersUtils.getTaskInput(ip, workflow, null, null); assertNotNull(taskInput); assertTrue(taskInput.containsKey("workflowInputParam")); assertTrue(taskInput.containsKey("taskOutputParam")); assertEquals("request id 001", taskInput.get("workflowInputParam")); assertEquals("http://location", taskInput.get("taskOutputParam")); }
@Test public void testCompleteTask() throws Exception { TaskDetails taskDetails = new TaskDetails(); taskDetails.setWorkflowId("${workflowId}"); taskDetails.setTaskRefName("testTask"); Action action = new Action(); action.setAction(Type.complete_task); action.setComplete_task(taskDetails); Object payload = new ObjectMapper().readValue("{\"workflowId\":\"workflow_1\"}", Object.class); Task task = new Task(); task.setReferenceTaskName("testTask"); Workflow workflow = new Workflow(); workflow.getTasks().add(task); when(workflowExecutor.getWorkflow(eq("workflow_1"), anyBoolean())).thenReturn(workflow); actionProcessor.execute(action, payload, "testEvent", "testMessage"); ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class); verify(workflowExecutor).updateTask(argumentCaptor.capture()); assertEquals(Status.COMPLETED, argumentCaptor.getValue().getStatus()); assertEquals("testMessage", argumentCaptor.getValue().getOutputData().get("conductor.event.messageId")); assertEquals("testEvent", argumentCaptor.getValue().getOutputData().get("conductor.event.name")); assertEquals("workflow_1", argumentCaptor.getValue().getOutputData().get("workflowId")); assertEquals("testTask", argumentCaptor.getValue().getOutputData().get("taskRefName")); }
@SuppressWarnings("unchecked") @Test public void testUpdateWorkflowOutput() { Workflow workflow = new Workflow(); workflow.setWorkflowDefinition(new WorkflowDef()); deciderService.updateWorkflowOutput(workflow, null); assertNotNull(workflow.getOutput()); assertTrue(workflow.getOutput().isEmpty()); Task task = new Task(); Map<String, Object> taskOutput = new HashMap<>(); taskOutput.put("taskKey", "taskValue"); task.setOutputData(taskOutput); workflow.getTasks().add(task); WorkflowDef workflowDef = new WorkflowDef(); when(metadataDAO.get(anyString(), anyInt())).thenReturn(Optional.of(workflowDef)); deciderService.updateWorkflowOutput(workflow, null); assertNotNull(workflow.getOutput()); assertEquals("taskValue", workflow.getOutput().get("taskKey")); }
@Test public void testCaseStatement() { WorkflowDef def = createConditionalWF(); Workflow wf = new Workflow(); wf.setWorkflowDefinition(def); wf.setCreateTime(0L); wf.setWorkflowId("a"); wf.setCorrelationId("b"); wf.setStatus(WorkflowStatus.RUNNING); DeciderOutcome outcome = deciderService.decide(wf); List<Task> scheduledTasks = outcome.tasksToBeScheduled; assertNotNull(scheduledTasks); assertEquals(2, scheduledTasks.size()); assertEquals(Status.IN_PROGRESS, scheduledTasks.get(0).getStatus()); assertEquals(Status.SCHEDULED, scheduledTasks.get(1).getStatus()); }
@Test public void testGetTaskByRef() { Workflow workflow = new Workflow(); Task t1 = new Task(); t1.setReferenceTaskName("ref"); t1.setSeq(0); t1.setStatus(Status.TIMED_OUT); Task t2 = new Task(); t2.setReferenceTaskName("ref"); t2.setSeq(1); t2.setStatus(Status.FAILED); Task t3 = new Task(); t3.setReferenceTaskName("ref"); t3.setSeq(2); t3.setStatus(Status.COMPLETED); workflow.getTasks().add(t1); workflow.getTasks().add(t2); workflow.getTasks().add(t3); Task task = workflow.getTaskByRefName("ref"); assertNotNull(task); assertEquals(Status.COMPLETED, task.getStatus()); assertEquals(t3.getSeq(), task.getSeq()); }
@Test public void getMappedTasks() throws Exception { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setType(TaskType.JOIN.name()); taskToSchedule.setJoinOn(Arrays.asList("task1, task2")); String taskId = IDGenerator.generate(); WorkflowDef wd = new WorkflowDef(); Workflow w = new Workflow(); w.setWorkflowDefinition(wd); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(wd) .withWorkflowInstance(w) .withTaskDefinition(new TaskDef()) .withTaskToSchedule(taskToSchedule) .withRetryCount(0) .withTaskId(taskId) .build(); List<Task> mappedTasks = new JoinTaskMapper().getMappedTasks(taskMapperContext); assertNotNull(mappedTasks); assertEquals(SystemTaskType.JOIN.name(), mappedTasks.get(0).getTaskType()); }
@Test public void getMappedTasks() throws Exception { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setName("simple_task"); taskToSchedule.setTaskDefinition(new TaskDef("simple_task")); String taskId = IDGenerator.generate(); String retriedTaskId = IDGenerator.generate(); WorkflowDef wd = new WorkflowDef(); Workflow w = new Workflow(); w.setWorkflowDefinition(wd); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(wd) .withWorkflowInstance(w) .withTaskDefinition(new TaskDef()) .withTaskToSchedule(taskToSchedule) .withTaskInput(new HashMap<>()) .withRetryCount(0) .withRetryTaskId(retriedTaskId) .withTaskId(taskId) .build(); List<Task> mappedTasks = simpleTaskMapper.getMappedTasks(taskMapperContext); assertNotNull(mappedTasks); assertEquals(1, mappedTasks.size()); }