ArtefactFunctionHandler extends JsonBasedFunctionHandler { @Override protected Output<JsonObject> handle(Input<JsonObject> input) { OutputBuilder output = new OutputBuilder(); ExecutionContext executionContext = (ExecutionContext) getTokenReservationSession().get(AbstractFunctionHandler.EXECUTION_CONTEXT_KEY); if(executionContext == null) { output.setError("Running composite Keyword on agent not supported. Please change the keyword configuration accordingly."); } else { String planId = input.getProperties().get(PLANID_KEY); String parentReportId = input.getProperties().get(AbstractFunctionHandler.PARENTREPORTID_KEY); ReportNode parentNode; if(parentReportId!=null) { parentNode = executionContext.getReportNodeAccessor().get(parentReportId); if(parentNode == null) { parentNode = executionContext.getCurrentReportNode(); } } else { parentNode = executionContext.getCurrentReportNode(); } ReportNode previousCurrentNode = executionContext.getCurrentReportNode(); executionContext.setCurrentReportNode(parentNode); executionContext.getReportNodeCache().put(parentNode); AbstractArtefact artefact = executionContext.getPlanAccessor().get(planId).getRoot(); executionContext.getVariablesManager().putVariable(parentNode, INPUT, input.getPayload()); executionContext.getVariablesManager().putVariable(parentNode, VariableType.IMMUTABLE, OUTPUT, output); try { ReportNode node = executionContext.getArtefactHandlerManager().execute(artefact, parentNode); if(node.getStatus()== ReportNodeStatus.TECHNICAL_ERROR || node.getStatus()== ReportNodeStatus.FAILED) { Error error = new Error(); error.setCode(0); error.setMsg("Error in composite keyword"); error.setRoot(false); error.setType(node.getStatus().equals(ReportNodeStatus.FAILED)?ErrorType.BUSINESS:ErrorType.TECHNICAL); output.setError(error); } } finally { executionContext.getVariablesManager().removeVariable(parentNode, OUTPUT); executionContext.setCurrentReportNode(previousCurrentNode); } } return output.build(); }   static final String PLANID_KEY; }
@Test public void test() { ExecutionContext context = newExecutionContext(); Return r = new Return(); r.setOutput(new DynamicValue<String>("{\"Result\":{\"dynamic\":true,\"expression\":\"input.Input1\"}}")); Plan compositePlan = PlanBuilder.create().startBlock(BaseArtefacts.sequence()).add(r).endBlock().build(); context.getPlanAccessor().save(compositePlan); ReportNode parentNode = new ReportNode(); context.getReportNodeAccessor().save(parentNode); ArtefactFunctionHandler handler = createArtefactFunctionHandler(context); Input<JsonObject> input = new Input<>(); input.setPayload(Json.createObjectBuilder().add("Input1", "InputValue1").build()); Map<String, String> properties = getInputProperties(compositePlan, parentNode); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertNull(output.getError()); Assert.assertEquals("InputValue1", output.getPayload().getString("Result")); AtomicInteger count = new AtomicInteger(0); context.getReportNodeAccessor().getAll().forEachRemaining(n->count.incrementAndGet()); Assert.assertEquals(3, count.get()); }
@Test public void testError() { ExecutionContext context = newExecutionContext(); Script script = new Script(); script.setScript("output.setError('MyError'"); Plan compositePlan = PlanBuilder.create().startBlock(BaseArtefacts.sequence()).add(script).endBlock().build(); context.getPlanAccessor().save(compositePlan); ReportNode parentNode = new ReportNode(); context.getReportNodeAccessor().save(parentNode); ArtefactFunctionHandler handler = createArtefactFunctionHandler(context); Input<JsonObject> input = new Input<>(); Map<String, String> properties = getInputProperties(compositePlan, parentNode); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("Error in composite keyword", output.getError().getMsg()); Assert.assertEquals(ErrorType.TECHNICAL, output.getError().getType()); }