Lambda extends WorkflowSystemTask { @Override public boolean execute(Workflow workflow, Task task, WorkflowExecutor executor) { Map<String, Object> taskInput = task.getInputData(); Map<String, Object> taskOutput = task.getOutputData(); String scriptExpression; try { scriptExpression = (String) taskInput.get(QUERY_EXPRESSION_PARAMETER); if (StringUtils.isNotBlank(scriptExpression)) { String scriptExpressionBuilder = "function scriptFun(){" + scriptExpression + "} scriptFun();"; logger.debug("scriptExpressionBuilder: {}, task: {}" , scriptExpressionBuilder, task.getTaskId()); Object returnValue = ScriptEvaluator.eval(scriptExpressionBuilder, taskInput); taskOutput.put("result", returnValue); task.setStatus(Task.Status.COMPLETED); } else { logger.error("Empty {} in Lambda task. ", QUERY_EXPRESSION_PARAMETER); task.setReasonForIncompletion("Empty '" + QUERY_EXPRESSION_PARAMETER + "' in Lambda task's input parameters. A non-empty String value must be provided."); task.setStatus(Task.Status.FAILED); } } catch (Exception e) { logger.error("Failed to execute Lambda Task: {} in workflow: {}", task.getTaskId(), workflow.getWorkflowId(), e); task.setStatus(Task.Status.FAILED); task.setReasonForIncompletion(e.getMessage()); taskOutput.put("error", e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); } return true; } Lambda(); @Override boolean execute(Workflow workflow, Task task, WorkflowExecutor executor); static final String TASK_NAME; }
@Test public void start() throws Exception { Lambda lambda = new Lambda(); Map inputObj = new HashMap(); inputObj.put("a",1); Task task = new Task(); task.getInputData().put("input", inputObj); lambda.execute(workflow, task, executor); assertEquals(Task.Status.FAILED, task.getStatus()); task = new Task(); task.getInputData().put("input", inputObj); task.getInputData().put("scriptExpression", "if ($.input.a==1){return 1}else{return 0 } "); lambda.execute(workflow, task, executor); assertEquals(Task.Status.COMPLETED, task.getStatus()); assertEquals(task.getOutputData().toString(), "{result=1}"); task = new Task(); task.getInputData().put("input", inputObj); task.getInputData().put("scriptExpression", "if ($.a.size==1){return 1}else{return 0 } "); lambda.execute(workflow, task, executor); assertEquals(Task.Status.FAILED, task.getStatus()); }