DecisionTaskMapper implements TaskMapper { @VisibleForTesting String getEvaluatedCaseValue(WorkflowTask taskToSchedule, Map<String, Object> taskInput) { String expression = taskToSchedule.getCaseExpression(); String caseValue; if (StringUtils.isNotBlank(expression)) { logger.debug("Case being evaluated using decision expression: {}", expression); try { Object returnValue = ScriptEvaluator.eval(expression, taskInput); caseValue = (returnValue == null) ? "null" : returnValue.toString(); } catch (ScriptException e) { String errorMsg = String.format("Error while evaluating script: %s", expression); logger.error(errorMsg, e); throw new TerminateWorkflowException(errorMsg); } } else { logger.debug("No Expression available on the decision task, case value being assigned as param name"); String paramName = taskToSchedule.getCaseValueParam(); caseValue = "" + taskInput.get(paramName); } return caseValue; }  @Override List<Task> getMappedTasks(TaskMapperContext taskMapperContext);  }
@Test public void getEvaluatedCaseValue() { WorkflowTask decisionTask = new WorkflowTask(); decisionTask.setType(TaskType.DECISION.name()); decisionTask.setName("Decision"); decisionTask.setTaskReferenceName("decisionTask"); decisionTask.setInputParameters(ip1); decisionTask.setDefaultCase(Arrays.asList(task1)); decisionTask.setCaseValueParam("case"); Map<String, List<WorkflowTask>> decisionCases = new HashMap<>(); decisionCases.put("0", Arrays.asList(task2)); decisionCases.put("1", Arrays.asList(task3)); decisionTask.setDecisionCases(decisionCases); Workflow workflowInstance = new Workflow(); workflowInstance.setWorkflowDefinition(new WorkflowDef()); Map<String, Object> workflowInput = new HashMap<>(); workflowInput.put("param1", "test1"); workflowInput.put("param2", "test2"); workflowInput.put("case", "0"); workflowInstance.setInput(workflowInput); Map<String, Object> input = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, null, null); assertEquals("0", decisionTaskMapper.getEvaluatedCaseValue(decisionTask, input)); }
@Test public void getEvaluatedCaseValueUsingExpression() { TaskDef taskDef = new TaskDef(); Map<String, Object> inputMap = new HashMap<>(); inputMap.put("Id", "${workflow.input.Id}"); List<Map<String, Object>> taskDefinitionInput = new LinkedList<>(); taskDefinitionInput.add(inputMap); WorkflowTask decisionTask = new WorkflowTask(); decisionTask.setType(TaskType.DECISION.name()); decisionTask.setName("Decision"); decisionTask.setTaskReferenceName("decisionTask"); decisionTask.setDefaultCase(Arrays.asList(task1)); decisionTask.setCaseValueParam("case"); decisionTask.getInputParameters().put("Id", "${workflow.input.Id}"); decisionTask.setCaseExpression("if ($.Id == null) 'bad input'; else if ( ($.Id != null && $.Id % 2 == 0)) 'even'; else 'odd'; "); Map<String, List<WorkflowTask>> decisionCases = new HashMap<>(); decisionCases.put("even", Arrays.asList(task2)); decisionCases.put("odd", Arrays.asList(task3)); decisionTask.setDecisionCases(decisionCases); WorkflowDef def = new WorkflowDef(); def.setSchemaVersion(2); Workflow workflowInstance = new Workflow(); workflowInstance.setWorkflowDefinition(def); Map<String, Object> workflowInput = new HashMap<>(); workflowInput.put("Id", "22"); workflowInstance.setInput(workflowInput); Map<String, Object> body = new HashMap<>(); body.put("input", taskDefinitionInput); taskDef.getInputTemplate().putAll(body); Map<String, Object> evaluatorInput = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, taskDef, null); assertEquals("even", decisionTaskMapper.getEvaluatedCaseValue(decisionTask, evaluatorInput)); }
@Test public void getEvaluatedCaseValueException() { TaskDef taskDef = new TaskDef(); Map<String, Object> inputMap = new HashMap<>(); inputMap.put("Id", "${workflow.input.Id}"); List<Map<String, Object>> taskDefinitionInput = new LinkedList<>(); taskDefinitionInput.add(inputMap); WorkflowTask decisionTask = new WorkflowTask(); decisionTask.setType(TaskType.DECISION.name()); decisionTask.setName("Decision"); decisionTask.setTaskReferenceName("decisionTask"); decisionTask.setDefaultCase(Collections.singletonList(task1)); decisionTask.setCaseValueParam("case"); decisionTask.getInputParameters().put("Id", "${workflow.input.Id}"); decisionTask.setCaseExpression("if ($Id == null) 'bad input'; else if ( ($Id != null && $Id % 2 == 0)) 'even'; else 'odd'; "); Map<String, List<WorkflowTask>> decisionCases = new HashMap<>(); decisionCases.put("even", Collections.singletonList(task2)); decisionCases.put("odd", Collections.singletonList(task3)); decisionTask.setDecisionCases(decisionCases); WorkflowDef def = new WorkflowDef(); def.setSchemaVersion(2); Workflow workflowInstance = new Workflow(); workflowInstance.setWorkflowDefinition(def); Map<String, Object> workflowInput = new HashMap<>(); workflowInput.put(".Id", "22"); workflowInstance.setInput(workflowInput); Map<String, Object> body = new HashMap<>(); body.put("input", taskDefinitionInput); taskDef.getInputTemplate().putAll(body); Map<String, Object> evaluatorInput = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, taskDef, null); expectedException.expect(TerminateWorkflowException.class); expectedException.expectMessage("Error while evaluating script: " + decisionTask.getCaseExpression()); decisionTaskMapper.getEvaluatedCaseValue(decisionTask, evaluatorInput); }