CallFunctionHandler extends ArtefactHandler<CallFunction, CallFunctionReportNode> { @Override public void init(ExecutionContext context) { super.init(context); functionExecutionService = context.get(FunctionExecutionService.class); functionAccessor = context.get(FunctionAccessor.class); functionRouter = context.get(FunctionRouter.class); reportNodeAttachmentManager = new ReportNodeAttachmentManager(context); dynamicJsonObjectResolver = new DynamicJsonObjectResolver(new DynamicJsonValueResolver(context.getExpressionHandler())); this.selectorHelper = new SelectorHelper(dynamicJsonObjectResolver); this.functionLocator = new FunctionLocator(functionAccessor, selectorHelper, context); }  @Override void init(ExecutionContext context); @Override CallFunctionReportNode createReportNode_(ReportNode parentNode, CallFunction testArtefact);  }
@Test public void test() { ExecutionContext executionContext = buildExecutionContext(); Function function = newFunction(FUNCTION_ID_SUCCESS); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); CallFunction callFunction = new CallFunction(); callFunction.setFunctionId(function.getId().toString()); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals(1, node.getAttachments().size()); AttachmentMeta attachment = node.getAttachments().get(0); Assert.assertEquals("Attachment1", attachment.getName()); Assert.assertEquals(1, node.getMeasures().size()); Assert.assertEquals("{\"Output1\":\"Value1\"}", node.getOutput()); Assert.assertEquals(ReportNodeStatus.PASSED, node.getStatus()); }
@Test public void testByAttributes() { ExecutionContext executionContext = buildExecutionContext(); Function function = newFunction("MyFunction"); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); CallFunction callFunction = new CallFunction(); callFunction.setFunction(new DynamicValue<String>("{\"name\":\"MyFunction\"}")); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals(1, node.getAttachments().size()); AttachmentMeta attachment = node.getAttachments().get(0); Assert.assertEquals("Attachment1", attachment.getName()); Assert.assertEquals(1, node.getMeasures().size()); Assert.assertEquals("{\"Output1\":\"Value1\"}", node.getOutput()); Assert.assertEquals(ReportNodeStatus.PASSED, node.getStatus()); }
@Test public void testDrainOutputToMap() { ExecutionContext executionContext = buildExecutionContext(); Function function = newFunction(FUNCTION_ID_SUCCESS); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); Map<String, String> map = new HashMap<>(); executionContext.getVariablesManager().putVariable(executionContext.getReport(),"map", map); CallFunction callFunction = new CallFunction(); callFunction.setFunctionId(function.getId().toString()); callFunction.setResultMap(new DynamicValue<String>("map")); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals(ReportNodeStatus.PASSED, node.getStatus()); Assert.assertEquals("Value1", map.get("Output1")); }
@SuppressWarnings("unchecked") @Test public void testDrainOutputToDataSetHandle() { ExecutionContext executionContext = buildExecutionContext(); Function function = newFunction(FUNCTION_ID_SUCCESS); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); List<Object> addedRows = new ArrayList<>(); DataSetHandle dataSetHandle = new DataSetHandle() { @Override public Object next() { return null; } @Override public void addRow(Object row) { addedRows.add(row); } }; executionContext.getVariablesManager().putVariable(executionContext.getReport(),"dataSet", dataSetHandle); CallFunction callFunction = new CallFunction(); callFunction.setFunctionId(function.getId().toString()); callFunction.setResultMap(new DynamicValue<String>("dataSet")); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals(ReportNodeStatus.PASSED, node.getStatus()); Assert.assertEquals("Value1", ((Map<String, String>)addedRows.get(0)).get("Output1")); }
@Test public void testError() { ExecutionContext executionContext = buildExecutionContext(); Function function = newFunction(FUNCTION_ID_ERROR); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); CallFunction callFunction = new CallFunction(); callFunction.setFunctionId(function.getId().toString()); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals("My Error", node.getError().getMsg()); }
@Test public void testSimulation() { ExecutionContext executionContext = buildExecutionContext(); executionContext.getExecutionParameters().setMode(ExecutionMode.SIMULATION); Function function = newFunction(FUNCTION_ID_SUCCESS); ((InMemoryFunctionAccessorImpl)executionContext.get(FunctionAccessor.class)).save(function); CallFunctionHandler handler = new CallFunctionHandler(); handler.init(executionContext); CallFunction callFunction = new CallFunction(); callFunction.setFunctionId(function.getId().toString()); CallFunctionReportNode node = (CallFunctionReportNode) execute(callFunction); Assert.assertEquals(ReportNodeStatus.PASSED, node.getStatus()); Assert.assertEquals("{}", node.getOutput()); Assert.assertNull(node.getError()); }