Skip to content

Commit

Permalink
Simplify API of PipelineElement to allow to resolve to a single value…
Browse files Browse the repository at this point in the history
… and avoid having Collections.singletonList all over the place

Signed-off-by: Yannic Klem <Yannic.Klem@bosch.io>
  • Loading branch information
Yannic92 committed Mar 3, 2022
1 parent 943405a commit c383144
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 62 deletions.
Expand Up @@ -102,7 +102,6 @@ private Function<String, PipelineElement> createReplacerFunction(final DittoHead
.build();
}
return Optional.ofNullable(placeholderResolver.apply(dittoHeaders))
.map(Collections::singletonList)
.map(PipelineElement::resolved)
.orElse(PipelineElement.unresolved());
};
Expand Down
Expand Up @@ -93,15 +93,15 @@ default Stream<String> resolvePartially(final String expressionTemplate,
throw e;
} else {
// placeholder is not supported; return the expression without resolution.
return PipelineElement.resolved(Collections.singletonList("{{" + expression + "}}"));
return PipelineElement.resolved("{{" + expression + "}}");
}
}

return pipelineElement.onUnresolved(() -> {
if (forbiddenUnresolvedExpressionPrefixes.stream().anyMatch(expression::startsWith)) {
throw UnresolvedPlaceholderException.newBuilder(expression).build();
}
return PipelineElement.resolved(Collections.singletonList("{{" + expression + "}}"));
return PipelineElement.resolved("{{" + expression + "}}");
});
}).toStream();
}
Expand Down
Expand Up @@ -110,7 +110,7 @@ private PipelineElement resolveSinglePlaceholder(final String placeholderInPipel
return resolvedValues.size() > 0 ? PipelineElement.resolved(resolvedValues) : PipelineElement.unresolved();
} else {
// validation mode: all placeholders resolve to dummy value.
return PipelineElement.resolved(Collections.singletonList(placeholderReplacementInValidation));
return PipelineElement.resolved(placeholderReplacementInValidation);
}
}

Expand Down
Expand Up @@ -82,7 +82,7 @@ public interface PipelineElement extends Iterable<String> {
* @return the mapped resolved value.
*/
default PipelineElement map(final Function<String, String> mapper) {
return onResolved(mapper.andThen(Collections::singletonList).andThen(PipelineElement::resolved));
return onResolved(mapper.andThen(PipelineElement::resolved));
}

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ static <T> PipelineElementVisitor.Builder<T> newVisitorBuilder() {
}

/**
* Creat a pipeline element containing a resolved value.
* Creat a pipeline element containing the resolved values.
*
* @param values the resolved values.
* @return the pipeline element.
Expand All @@ -141,6 +141,17 @@ static PipelineElement resolved(final Collection<String> values) {
return values.size() > 0 ? PipelineElementResolved.of(values) : PipelineElement.unresolved();
}


/**
* Creat a pipeline element containing a resolved value.
*
* @param value the resolved value.
* @return the pipeline element.
*/
static PipelineElement resolved(final String value) {
return resolved(Collections.singletonList(value));
}

/**
* Get the unique pipeline element signifying deletion of the whole string containing the pipeline.
*
Expand Down
Expand Up @@ -57,7 +57,7 @@ public PipelineElement apply(final PipelineElement value, final String paramsInc
if (filterValuesArePreviousValues) {
return PipelineElement.resolved(filteredValues);
} else if (shouldKeepAnyValue) {
return PipelineElement.resolved(Collections.singletonList(valueThatShouldBeFilteredConditionally));
return PipelineElement.resolved(valueThatShouldBeFilteredConditionally);
}
return PipelineElement.unresolved();
});
Expand Down
Expand Up @@ -219,12 +219,12 @@ private static Optional<PipelineElement> apply(final Matcher matcher, final int
final String singleQuotedStringConstant = matcher.group(buildSingleQuotedConstantGroupName(parameterIndex));

if (singleQuotedStringConstant != null) {
return Optional.of(PipelineElement.resolved(Collections.singletonList(singleQuotedStringConstant)));
return Optional.of(PipelineElement.resolved(singleQuotedStringConstant));
} else {
final String doubleQuotedStringConstant =
matcher.group(buildDoubleQuotedConstantGroupName(parameterIndex));
if (doubleQuotedStringConstant != null) {
return Optional.of(PipelineElement.resolved(Collections.singletonList(doubleQuotedStringConstant)));
return Optional.of(PipelineElement.resolved(doubleQuotedStringConstant));
}
}

Expand Down
Expand Up @@ -46,8 +46,7 @@ public PipelineElement apply(final PipelineElement value, final String paramsInc

return value.onResolved(previousStage -> {
if (previousStage.contains(splitValue)) {
return PipelineElement.resolved(
Collections.singletonList(previousStage.substring(0, previousStage.indexOf(splitValue))));
return PipelineElement.resolved(previousStage.substring(0, previousStage.indexOf(splitValue)));
} else {
return PipelineElement.unresolved();
}
Expand Down
Expand Up @@ -87,38 +87,38 @@ public void testCompletenessOfRegisteredFunctions() {
@Test
public void testUnknownFunction() {
assertThatExceptionOfType(PlaceholderFunctionUnknownException.class).isThrownBy(() ->
UNDER_TEST.resolve("fn:unknown", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER));
UNDER_TEST.resolve("fn:unknown", PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER));
}

@Test
public void testFunctionUpper() {
assertThat(UNDER_TEST.resolve("fn:upper()", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
assertThat(UNDER_TEST.resolve("fn:upper()", PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.contains(HEADER_VAL.toUpperCase());
}

@Test
public void testFunctionUpperWrongSignature() {
assertThatExceptionOfType(PlaceholderFunctionSignatureInvalidException.class).isThrownBy(() ->
UNDER_TEST.resolve("fn:upper('foo')", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)),
UNDER_TEST.resolve("fn:upper('foo')", PipelineElement.resolved(HEADER_VAL),
EXPRESSION_RESOLVER));
}

@Test
public void testFunctionLower() {
assertThat(UNDER_TEST.resolve("fn:lower()", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
assertThat(UNDER_TEST.resolve("fn:lower()", PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.contains(HEADER_VAL.toLowerCase());
}

@Test
public void testFunctionLowerWrongSignature() {
assertThatExceptionOfType(PlaceholderFunctionUnknownException.class).isThrownBy(() ->
UNDER_TEST.resolve("fn:lower", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER));
UNDER_TEST.resolve("fn:lower", PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER));
}

@Test
public void testFunctionDefaultWhenInputPresent() {
assertThat(
UNDER_TEST.resolve("fn:default('constant')", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
UNDER_TEST.resolve("fn:default('constant')", PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.contains(HEADER_VAL);
}

Expand Down Expand Up @@ -148,35 +148,35 @@ public void testFunctionDefaultWithWrongSignature() {

@Test
public void testFunctionSubstringBefore() {
assertThat(UNDER_TEST.resolve("fn:substring-before(\"s\")", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)),
assertThat(UNDER_TEST.resolve("fn:substring-before(\"s\")", PipelineElement.resolved(HEADER_VAL),
EXPRESSION_RESOLVER)).contains("caMelCa");
}

@Test
public void testFunctionSubstringAfter() {
assertThat(UNDER_TEST.resolve("fn:substring-after(\"s\")", PipelineElement.resolved(Collections.singletonList(HEADER_VAL)),
assertThat(UNDER_TEST.resolve("fn:substring-after(\"s\")", PipelineElement.resolved(HEADER_VAL),
EXPRESSION_RESOLVER))
.contains("edStuffFOOO");
}

@Test
public void testFunctionFilterWhenConditionSucceeds() {
assertThat(UNDER_TEST.resolve("fn:filter('true','eq','true')",
PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.contains(HEADER_VAL);
}

@Test
public void testFunctionFilterWhenConditionFails() {
assertThat(UNDER_TEST.resolve("fn:filter('false','eq','true')",
PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.isEmpty();
}

@Test
public void testFunctionFilterWhenConditionSucceedsWithPlaceholder() {
assertThat(UNDER_TEST.resolve(String.format("fn:filter(header:foo1,'eq','%s')", HEADER_VAL),
PipelineElement.resolved(Collections.singletonList(HEADER_VAL)), EXPRESSION_RESOLVER))
PipelineElement.resolved(HEADER_VAL), EXPRESSION_RESOLVER))
.contains(HEADER_VAL);
}

Expand Down
Expand Up @@ -48,10 +48,10 @@ public class ImmutablePipelineTest {
"fn:substring-before(':')",
"fn:unknown('foo')"
);
private static final PipelineElement PIPELINE_INPUT = PipelineElement.resolved(Collections.singletonList("my-gateway:my-thing"));
private static final PipelineElement PIPELINE_INPUT = PipelineElement.resolved("my-gateway:my-thing");
private static final List<PipelineElement> RESPONSES = Arrays.asList(
PipelineElement.resolved(Collections.singletonList("my-gateway")),
PipelineElement.resolved(Collections.singletonList("my-gateway"))
PipelineElement.resolved("my-gateway"),
PipelineElement.resolved("my-gateway")
);

@Mock
Expand Down
Expand Up @@ -43,7 +43,7 @@ public void getName() {

@Test
public void applyReturnsExistingValue() {
final PipelineElement input = PipelineElement.resolved(Collections.singletonList(KNOWN_VALUE));
final PipelineElement input = PipelineElement.resolved(KNOWN_VALUE);
final String params = "(\"" + KNOWN_FALLBACK + "\")";
assertThat(function.apply(input, params, expressionResolver)).contains(KNOWN_VALUE);
}
Expand All @@ -60,7 +60,7 @@ public void applyReturnsDefaultPlaceholder() {
final PipelineElement input = PipelineElement.unresolved();
final String params = "(" + KNOWN_PLACEHOLDER + ")";
when(expressionResolver.resolveAsPipelineElement(anyString()))
.thenReturn(PipelineElement.resolved(Collections.singletonList(KNOWN_VALUE)));
.thenReturn(PipelineElement.resolved(KNOWN_VALUE));

assertThat(function.apply(input, params, expressionResolver)).contains(KNOWN_VALUE);

Expand Down

0 comments on commit c383144

Please sign in to comment.