JsonJqTransform extends WorkflowSystemTask { @Override public void start(Workflow workflow, Task task, WorkflowExecutor executor) { final Map<String, Object> taskInput = task.getInputData(); final Map<String, Object> taskOutput = task.getOutputData(); final String queryExpression = (String) taskInput.get(QUERY_EXPRESSION_PARAMETER); if (queryExpression == null) { task.setReasonForIncompletion("Missing '" + QUERY_EXPRESSION_PARAMETER + "' in input parameters"); task.setStatus(Task.Status.FAILED); return; } try { final JsonNode input = objectMapper.valueToTree(taskInput); final JsonQuery query = queryCache.get(queryExpression); final Scope childScope = Scope.newChildScope(rootScope); final List<JsonNode> result = query.apply(childScope, input); task.setStatus(Task.Status.COMPLETED); if (result == null) { taskOutput.put(OUTPUT_RESULT, null); taskOutput.put(OUTPUT_RESULT_LIST, null); } else if (result.isEmpty()) { taskOutput.put(OUTPUT_RESULT, null); taskOutput.put(OUTPUT_RESULT_LIST, result); } else { taskOutput.put(OUTPUT_RESULT, result.get(0)); taskOutput.put(OUTPUT_RESULT_LIST, result); } } catch (final Exception e) { logger.error("Error executing task: {} in workflow: {}", task.getTaskId(), workflow.getWorkflowId(), e); task.setStatus(Task.Status.FAILED); final String message = extractFirstValidMessage(e); task.setReasonForIncompletion(message); taskOutput.put(OUTPUT_ERROR, message); } } @Inject  JsonJqTransform(ObjectMapper objectMapper); @Override void start(Workflow workflow, Task task, WorkflowExecutor executor);  }
@Test public void dataShouldBeCorrectlySelected() { final JsonJqTransform t = new JsonJqTransform(objectMapper); final Workflow w = new Workflow(); final Task task = new Task(); final Map<String, Object> inputData = new HashMap<>(); inputData.put("queryExpression", ".inputJson.key[0]"); final Map<String, Object> inputJson = new HashMap<>(); inputJson.put("key", Collections.singletonList("VALUE")); inputData.put("inputJson", inputJson); task.setInputData(inputData); task.setOutputData(new HashMap<>()); t.start(w, task, null); Assert.assertNull(task.getOutputData().get("error")); Assert.assertEquals("\"VALUE\"", task.getOutputData().get("result").toString()); Assert.assertEquals("[\"VALUE\"]", task.getOutputData().get("resultList").toString()); }
@Test public void simpleErrorShouldBeDisplayed() { final JsonJqTransform t = new JsonJqTransform(objectMapper); final Workflow w = new Workflow(); final Task task = new Task(); final Map<String, Object> inputData = new HashMap<>(); inputData.put("queryExpression", "{"); task.setInputData(inputData); task.setOutputData(new HashMap<>()); t.start(w, task, null); Assert.assertTrue(((String)task.getOutputData().get("error")).startsWith("Encountered \"<EOF>\" at line 1, column 1.")); }
@Test public void nestedExceptionsWithNACausesShouldBeDisregarded() { final JsonJqTransform t = new JsonJqTransform(objectMapper); final Workflow w = new Workflow(); final Task task = new Task(); final Map<String, Object> inputData = new HashMap<>(); inputData.put("queryExpression", "{officeID: (.inputJson.OIDs | unique)[], requestedIndicatorList: .inputJson.requestedindicatorList}"); final Map<String, Object> inputJson = new HashMap<>(); inputJson.put("OIDs", Collections.singletonList("VALUE")); final Map<String, Object> indicatorList = new HashMap<>(); indicatorList.put("indicator", "AFA"); indicatorList.put("value", false); inputJson.put("requestedindicatorList", Collections.singletonList(indicatorList)); inputData.put("inputJson", inputJson); task.setInputData(inputData); task.setOutputData(new HashMap<>()); t.start(w, task, null); Assert.assertTrue(((String)task.getOutputData().get("error")).startsWith("Encountered \" \"[\" \"[ \"\" at line 1")); }