ExtensionsVariablesMappingProvider implements VariablesCalculator { public Map<String, Object> calculateOutPutVariables(MappingExecutionContext mappingExecutionContext, Map<String, Object> availableVariables) { Extension extensions = processExtensionService.getExtensionsForId(mappingExecutionContext.getProcessDefinitionId()); if (!extensions.hasMapping(mappingExecutionContext.getActivityId())) { return emptyMap(); } if (extensions.shouldMapAllOutputs(mappingExecutionContext.getActivityId())) { return (availableVariables != null ? new HashMap<>(availableVariables) : emptyMap()); } if (availableVariables != null && !availableVariables.isEmpty()) { if (expressionResolver.containsExpression(availableVariables)) { throw new ActivitiIllegalArgumentException("Expressions are not allowed as variable values in the output mapping"); } return calculateOutPutVariables(mappingExecutionContext, extensions, availableVariables); } else { return emptyMap(); } } ExtensionsVariablesMappingProvider(ProcessExtensionService processExtensionService,
                                    ExpressionResolver expressionResolver); Map<String, Object> calculateInputVariables(DelegateExecution execution); Map<String, Object> calculateOutPutVariables(MappingExecutionContext mappingExecutionContext,
                                                        Map<String, Object> availableVariables);  }
@Test public void calculateOutputVariablesShouldDoMappingWhenThereIsMappingSet() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); ProcessExtensionModel extensions = objectMapper.readValue(new File("src/test/resources/task-variable-mapping-extensions.json"), ProcessExtensionModel.class); Extension processExtensions = extensions.getExtensions("Process_taskVarMapping"); DelegateExecution execution = buildExecution(processExtensions); ExpressionResolver expressionResolver = ExpressionResolverHelper.initContext(execution, processExtensions); ReflectionTestUtils.setField(variablesMappingProvider, "expressionResolver", expressionResolver); Map<String, Object> entityVariables = singletonMap("task_output_variable_name_1", "var-one"); ExpressionResolverHelper.setExecutionVariables(execution, entityVariables); Map<String, Object> outPutVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), entityVariables); assertThat(outPutVariables.get("process_variable_outputmap_1")).isEqualTo("var-one"); assertThat(outPutVariables).doesNotContainKey("property-with-no-default-value"); }
@Test public void calculateOutputVariablesShouldPassAllVariablesWhenThereIsNoMapping() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); ProcessExtensionModel extensions = objectMapper.readValue(new File("src/test/resources/task-variable-no-mapping-extensions.json"), ProcessExtensionModel.class); DelegateExecution execution = buildExecution(extensions.getExtensions("Process_taskVariableNoMapping")); Map<String, Object> taskVariables = map( "task_output_variable_name_1", "var-one", "non-mapped-output_variable_name_2", "var-two" ); Map<String, Object> outPutVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), taskVariables); assertThat(outPutVariables).isEqualTo(taskVariables); }
@Test public void calculateOutputVariablesShouldNotPassAnyVariablesWhenTheMappingIsEmpty() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); ProcessExtensionModel extensions = objectMapper.readValue(new File("src/test/resources/task-variable-empty-mapping-extensions.json"), ProcessExtensionModel.class); DelegateExecution execution = buildExecution(extensions.getExtensions("Process_taskVariableEmptyMapping")); Map<String, Object> taskVariables = map( "task_output_variable_name_1", "var-one", "non-mapped-output_variable_name_2", "var-two" ); Map<String, Object> outputVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), taskVariables); assertThat(outputVariables).isEmpty(); }
@Test public void should_substituteExpressions_when_expressionIsInOutputMappingValue() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-output-value.json", "Process_expressionMappingOutputValue"); Map<String, Object> outputVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), map( "task_input_variable_name_1", "variable_value_1", "task_input_variable_name_2", "static_value_2" )); assertThat(outputVariables).isNotEmpty(); assertThat(outputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly(tuple("process_variable_3", "variable_value_1"), tuple("process_variable_4", "static_value_2")); }
@Test public void should_notSubstituteExpressions_when_expressionIsInOutputMappingVariable() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-output-variable.json", "Process_expressionMappingOutputVariable"); Map<String, Object> taskVariables = map( "task_input_variable_name_1", "variable_value_1", "task_input_variable_name_2", "static_value_2" ); Map<String, Object> outputVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), taskVariables); assertThat(outputVariables).isNotEmpty(); assertThat(outputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly(tuple("process_variable_4", "static_value_2")); }
@Test public void should_throwActivitiIllegalArgumentException_when_expressionIsOutputMapping() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-output-value.json", "Process_expressionMappingOutputValue"); assertThatExceptionOfType(ActivitiIllegalArgumentException.class) .isThrownBy(() -> variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), map( "task_input_variable_name_1", "variable_value_1", "task_input_variable_name_2", "${expression}" ))); }
@Test public void should_returnEmptyOutputMapping_when_thereIsNoAvaliableVariableInTask() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-output-value.json", "Process_expressionMappingOutputValue"); Map<String, Object> outputMapping = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution), null); assertThat(outputMapping).isEmpty(); }
@Test public void should_returnEmptyOutputMapping_when_thereIsAnEmptyValueInOutputMappingVariable() throws Exception { DelegateExecution execution = initExpressionResolverTest("no-value-in-output-mapping-variable.json", "Process_noValueOutputMappingVariable"); Map<String, Object> outputMapping = variablesMappingProvider.calculateOutPutVariables( buildMappingExecutionContext(execution), singletonMap("not_matching_variable", "variable_value_1")); assertThat(outputMapping).isEmpty(); }