ExtensionsVariablesMappingProvider implements VariablesCalculator { public Map<String, Object> calculateInputVariables(DelegateExecution execution) { Extension extensions = processExtensionService.getExtensionsForId(execution.getProcessDefinitionId()); Map<String, Object> constants = calculateConstants(execution, extensions); if (!extensions.hasMapping(execution.getCurrentActivityId())) { return constants; } Map<String, Object> inboundVariables; if (extensions.shouldMapAllInputs(execution.getCurrentActivityId())) { inboundVariables = execution.getVariables(); } else { inboundVariables = calculateInputVariables(execution, extensions); } inboundVariables = expressionResolver.resolveExpressionsMap(new VariableScopeExpressionEvaluator(execution), inboundVariables); inboundVariables.putAll(constants); return inboundVariables; } ExtensionsVariablesMappingProvider(ProcessExtensionService processExtensionService,
                                    ExpressionResolver expressionResolver); Map<String, Object> calculateInputVariables(DelegateExecution execution); Map<String, Object> calculateOutPutVariables(MappingExecutionContext mappingExecutionContext,
                                                        Map<String, Object> availableVariables);  }
@Test public void calculateInputVariablesShouldDoMappingWhenThereIsMappingSet() 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); given(execution.getVariable("process_variable_inputmap_1")).willReturn("new-input-value"); given(execution.getVariable("property-with-no-default-value")).willReturn(null); ExpressionResolver expressionResolver = ExpressionResolverHelper.initContext(execution, processExtensions); ReflectionTestUtils.setField(variablesMappingProvider, "expressionResolver", expressionResolver); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables.get("task_input_variable_name_1")).isEqualTo("new-input-value"); assertThat(inputVariables).doesNotContainKeys("task_input_variable_mapped_with_null_process_variable"); }
@Test public void calculateInputVariablesShouldPassAllVariablesWhenThereIsNoMapping() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); ProcessExtensionModel extensions = objectMapper.readValue(new File("src/test/resources/task-variable-no-mapping-extensions.json"), ProcessExtensionModel.class); Extension processExtensions = extensions.getExtensions("Process_taskVariableNoMapping"); DelegateExecution execution = buildExecution(processExtensions); ExpressionResolver expressionResolver = ExpressionResolverHelper.initContext(execution, processExtensions); ReflectionTestUtils.setField(variablesMappingProvider, "expressionResolver", expressionResolver); Map<String, Object> variables = map( "var-one", "one", "var-two", 2 ); given(execution.getVariables()).willReturn(variables); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isEqualTo(variables); }
@Test public void calculateInputVariablesShouldNotPassAnyVariablesWhenTheMappingIsEmpty() 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> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isEmpty(); }
@Test public void calculateInputVariablesShouldPassOnlyConstantsWhenTheMappingIsEmpty() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); ProcessExtensionModel extensions = objectMapper.readValue(new File("src/test/resources/task-variable-empty-mapping-with-constants-extensions.json"), ProcessExtensionModel.class); DelegateExecution execution = buildExecution(extensions.getExtensions("Process_taskVariableEmptyMappingWithContants")); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isNotEmpty(); assertThat(inputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly( tuple("process_constant_1_2", "constant_2_value"), tuple("process_constant_inputmap_2", "constant_value")); }
@Test public void should_substituteExpressions_when_expressionIsInInputMappingValue() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-input-value.json", "Process_expressionMappingInputValue"); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isNotEmpty(); assertThat(inputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly(tuple("process_constant_1", "constant_1_value"), tuple("process_constant_2", "constant_2_value"), tuple("task_input_variable_name_1", "variable_value_1"), tuple("task_input_variable_name_2", "variable_value_1")); }
@Test public void should_notSubstituteExpressions_when_expressionIsInInputMappingVariable() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-mapping-input-variable.json", "Process_expressionMappingInputVariable"); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isNotEmpty(); assertThat(inputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly(tuple("process_constant_1", "constant_1_value"), tuple("process_constant_2", "constant_2_value"), tuple("task_input_variable_name_2", "static_value_1")); }
@Test public void should_substituteExpressions_when_expressionIsInProperties() throws Exception { DelegateExecution execution = initExpressionResolverTest("expression-in-properties.json", "Process_expressionProperty"); Map<String, Object> var1 = map( "prop1", "property 1", "prop2", "expressionResolved", "prop3", asList("1", "this expressionResolved is OK", "2") ); Map<String, Object> inputVariables = variablesMappingProvider.calculateInputVariables(execution); assertThat(inputVariables).isNotEmpty(); assertThat(inputVariables.entrySet()).extracting(Map.Entry::getKey, Map.Entry::getValue) .containsOnly(tuple("process_constant_1", "constant_1_value"), tuple("process_constant_2", "constant_2_value"), tuple("task_input_variable_name_1", var1), tuple("task_input_variable_name_2", "static_value_1")); }