Skip to content

Commit

Permalink
moved validation of too complex placeholders to parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Mar 8, 2019
1 parent 44448fe commit 43f6944
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Expand Up @@ -42,6 +42,8 @@ public interface ExpressionResolver {
* {@code true} and placeholders could not be resolved.
* @throws org.eclipse.ditto.model.connectivity.UnresolvedPlaceholderException thrown if {@code allowUnresolved} was
* set to {@code false} and the passed in {@code expressionTemplate} could not be resolved
* @throws PlaceholderFunctionTooComplexException thrown if the {@code expressionTemplate} contains a placeholder
* function chain which is too complex (e.g. too much chained function calls)
*/
String resolve(String expressionTemplate, boolean allowUnresolved);

Expand Down
Expand Up @@ -143,6 +143,10 @@ private List<String> getPipelineStagesExpressions(final String template) {

while (matcher.find()) {
pipelineStagesExpressions.add(matcher.group().trim());

if (pipelineStagesExpressions.size() > MAX_COUNT_PIPELINE_FUNCTIONS + 1) { // +1 for the starting placeholder
throw PlaceholderFunctionTooComplexException.newBuilder(MAX_COUNT_PIPELINE_FUNCTIONS).build();
}
}
return pipelineStagesExpressions;
}
Expand All @@ -158,11 +162,6 @@ private Pipeline getPipelineFromExpressions(final List<String> pipelineStagesExp
final List<String> pipelineStages = pipelineStagesExpressions.stream()
.skip(1) // ignore first, as the first one is a placeholder that will be used as the input for the pipeline
.collect(Collectors.toList());

if (pipelineStages.size() > MAX_COUNT_PIPELINE_FUNCTIONS) {
throw PlaceholderFunctionTooComplexException.newBuilder(MAX_COUNT_PIPELINE_FUNCTIONS).build();
}

return new ImmutablePipeline(ImmutableFunctionExpression.INSTANCE, pipelineStages);
}

Expand Down
Expand Up @@ -152,6 +152,8 @@ public static String apply(final String template, final ExpressionResolver expre
* @return the template string with the resolved values
* @throws UnresolvedPlaceholderException if {@code allowUnresolved} is true and not all
* placeholders were resolved
* @throws PlaceholderFunctionTooComplexException thrown if the {@code template} contains a placeholder
* function chain which is too complex (e.g. too much chained function calls)
*/
public static String apply(final String template, final ExpressionResolver expressionResolver,
final boolean allowUnresolved) {
Expand All @@ -166,6 +168,8 @@ public static String apply(final String template, final ExpressionResolver expre
* @param template a string potentially containing placeholders to replace
* @param placeholders the {@link Placeholder}s to use for replacement
* @throws UnresolvedPlaceholderException in case the template's placeholders could not completely be resolved
* @throws PlaceholderFunctionTooComplexException thrown if the {@code template} contains a placeholder
* function chain which is too complex (e.g. too much chained function calls)
*/
public static void validate(final String template, final Placeholder<?>... placeholders) {
String replaced = template;
Expand Down
Expand Up @@ -12,7 +12,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -187,10 +186,9 @@ public void testResolveBarelyTooComplexFunctionChainWorks() {
@Test
public void testResolveTooComplexFunctionChainResultsInException() {
// 11 functions should fail:
assertThatThrownBy(() ->
assertThatExceptionOfType(PlaceholderFunctionTooComplexException.class).isThrownBy(() ->
underTest.resolve(
"{{ header:unknown | fn:default('fallback') | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() }}",
false))
.isInstanceOf(PlaceholderFunctionTooComplexException.class);
false));
}
}
Expand Up @@ -220,6 +220,9 @@ public void testValidateFails() {

assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{thing:nam}}espace }}{{ thing:name }}{{header:device-id }}", placeholders));

assertThatExceptionOfType(PlaceholderFunctionTooComplexException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{ header:unknown | fn:default('fallback') | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() }}", placeholders));
}

private static String filterChain(final String template, final FilterTuple... tuples) {
Expand Down

0 comments on commit 43f6944

Please sign in to comment.