ExpressionResolver { public Map<String, Object> resolveExpressionsMap(final ExpressionEvaluator expressionEvaluator, final Map<String, ?> sourceMap) { final Map<String, Object> result = new LinkedHashMap<>(); sourceMap.forEach((key, value) -> result.put(key, resolveExpressions(expressionEvaluator, value))); return result; } ExpressionResolver(ExpressionManager expressionManager,
        ObjectMapper mapper,
        DelegateInterceptor delegateInterceptor); Map<String, Object> resolveExpressionsMap(final ExpressionEvaluator expressionEvaluator,
                                                     final Map<String, ?> sourceMap); boolean containsExpression(final Object source);  }
@Test public void resolveExpressionsMap_should_replaceExpressionByValue_when_stringIsAnExpression() { Expression expression = buildExpression("${name}"); given(expressionEvaluator.evaluate(expression, expressionManager, delegateInterceptor)).willReturn("John"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("name", "${name}")); assertThat(result).containsEntry("name", "John"); }
@Test public void resolveExpressionsMap_should_replaceExpressionByValue_when_stringContainsAnExpression() { Expression nameExpression = buildExpression("${name}"); given(expressionEvaluator.evaluate(nameExpression, expressionManager, delegateInterceptor)).willReturn("John"); Expression placeExpression = buildExpression("${place}"); given(expressionEvaluator.evaluate(placeExpression, expressionManager, delegateInterceptor)).willReturn("London"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("welcomeMessage", "Welcome to ${place}, ${name}!")); assertThat(result).containsEntry("welcomeMessage", "Welcome to London, John!"); }
@Test public void resolveExpressionsMap_should_returnItself_when_stringIsEmpty() { Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("empty", "")); assertThat(result).containsEntry("empty", ""); }
@Test public void resolveExpressionsMap_should_returnItself_when_stringIsNull() { Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("empty", null)); assertThat(result).containsEntry("empty", null); }
@Test public void resolveExpressionsMap_should_keepExpressionContent_when_notAbleToResolveExpressionInString() { Expression expression = buildExpression("${nonResolvableExpression}"); given(expressionEvaluator.evaluate(expression, expressionManager, delegateInterceptor)).willThrow(new ActivitiException("Invalid property")); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("result", "Welcome to ${nonResolvableExpression}!")); assertThat(result).containsEntry("result", "Welcome to ${nonResolvableExpression}!"); }
@Test public void resolveExpressionsMap_should_keepExpressionContent_when_notAbleToResolveIt() { Expression expression = buildExpression("${nonResolvableExpression}"); given(expressionEvaluator.evaluate(expression, expressionManager, delegateInterceptor)).willThrow(new ActivitiException("Invalid property")); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("result", "${nonResolvableExpression}")); assertThat(result).containsEntry("result", "${nonResolvableExpression}"); }
@Test public void resolveExpressionsMap_should_replaceExpressionByValue_when_ObjectNodeContainsAnExpression() throws IOException { Expression nameExpression = buildExpression("${name}"); given(expressionEvaluator.evaluate(nameExpression, expressionManager, delegateInterceptor)).willReturn("John"); Expression placeExpression = buildExpression("${place}"); given(expressionEvaluator.evaluate(placeExpression, expressionManager, delegateInterceptor)).willReturn(null); Expression ageExpression = buildExpression("${age}"); given(expressionEvaluator.evaluate(ageExpression, expressionManager, delegateInterceptor)).willReturn(30); JsonNode node = mapper.readTree("{\"name\":\"${name}\",\"place\":\"${place}\",\"age\":\"${age}\"}"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("node", node)); assertThat(result).containsEntry("node", map( "name", "John", "place", null, "age", 30 )); }
@Test public void resolveExpressionsMap_should_keepExpressionContent_when_ObjecNodeContainsAnExpressionUnableToBeResolved() throws IOException { Expression nameExpression = buildExpression("${name}"); given(expressionEvaluator.evaluate(nameExpression, expressionManager, delegateInterceptor)).willThrow(new ActivitiException("Invalid property")); JsonNode node = mapper.readTree("{\"name\":\"${name}\",\"age\": 30}"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("node", node)); assertThat(result).containsEntry("node", map( "name", "${name}", "age", 30 )); }
@Test public void resolveExpressionsMap_should_replaceExpressionByValue_when_ListContainsAnExpression() { Expression placeExpression = buildExpression("${place}"); given(expressionEvaluator.evaluate(placeExpression, expressionManager, delegateInterceptor)).willReturn("London"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("places", asList("${place}", "Paris", "Berlin"))); assertThat(result).containsEntry("places", asList("London", "Paris", "Berlin")); }
@Test public void resolveExpressionsMap_should_keepExpressionContent_when_ListContainsAnExpressionUnableToBeResolved() { Expression placeExpression = buildExpression("${place}"); given(expressionEvaluator.evaluate(placeExpression, expressionManager, delegateInterceptor)).willThrow(new ActivitiException("Invalid property")); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("places", asList("${place}", "Paris", "Berlin"))); assertThat(result).containsEntry("places", asList("${place}", "Paris", "Berlin")); }
@Test public void resolveExpressionsMap_should_replaceExpressionByValue_when_MapContainsAnExpression() { Expression playerExpression = buildExpression("${player}"); given(expressionEvaluator.evaluate(playerExpression, expressionManager, delegateInterceptor)).willReturn("Agatha"); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("players", map( "Red", "John", "Green", "Peter", "Blue", "Mary", "Yellow", "${player}" ))); assertThat(result).containsEntry("players", map( "Red", "John", "Green", "Peter", "Blue", "Mary", "Yellow", "Agatha" )); }
@Test public void resolveExpressionsMap_should_keepExpressionContent_when_MapContainsAnExpressionUnableToBeResolved() { Expression playerExpression = buildExpression("${player}"); given(expressionEvaluator.evaluate(playerExpression, expressionManager, delegateInterceptor)).willThrow(new ActivitiException("Invalid property")); Map<String, Object> players = map( "Red", "John", "Green", "Peter", "Blue", "Mary", "Yellow", "${player}" ); Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("players",players)); assertThat(result).containsEntry("players", players); }