PipelineFunctionFilter implements PipelineFunction { @Override public PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses, final ExpressionResolver expressionResolver) { final Map<String, String> parameters = parseAndResolve(paramsIncludingParentheses, expressionResolver); return value.onResolved(valueThatShouldBeFilteredConditionally -> { final Optional<FilterFunction> rqlFunctionOpt = FilterFunctions.fromName(parameters.get(RqlFunctionParam.NAME)); final boolean shouldKeepValue = rqlFunctionOpt.map(rqlFunction -> { final String filterValue = parameters.get(FilterValueParam.NAME); final String comparedValue = parameters.get(ComparedValueParam.NAME); return rqlFunction.apply(filterValue, comparedValue); }).orElse(false); if (shouldKeepValue) { return PipelineElement.resolved(valueThatShouldBeFilteredConditionally); } else { return PipelineElement.unresolved(); } }); }  @Override String getName(); @Override Signature getSignature(); @Override PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses,
            final ExpressionResolver expressionResolver);  }
@Test public void applyReturnsExistingValueIfFilterConditionSucceeds() { when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("true")); final String params = String.format("(%s,\"%s\",\"%s\")", "header:reply-to", "eq", "true"); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).contains(KNOWN_VALUE); }
@Test public void applyReturnsUnresolvedValueIfFilterConditionFails() { when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("false")); final String params = String.format("(%s,\"%s\",\"%s\")", "header:reply-to", "eq", "true"); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); }
@Test public void applyReturnsUnresolvedValueIfUnsupportedRqlFunctionDefined() { when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("false")); final String params = String.format("(%s,\"%s\",\"%s\")", "header:reply-to", "na", "true"); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); }
@Test public void eqSucceedsWithBothValuesUnresolved() { when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "eq", "header:some-header"); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).contains(KNOWN_VALUE); }
@Test public void eqFailsWithOneUnresolvedValue() { final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "eq", "header:some-header"); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("true")); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.resolved("true")); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); }
@Test public void neFailsWithBothValuesUnresolved() { when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "ne", "header:some-header"); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); }
@Test public void neSucceedsWithOneUnresolvedValue() { final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "ne", "header:some-header"); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("true")); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).contains(KNOWN_VALUE); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.resolved("true")); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).contains(KNOWN_VALUE); }
@Test public void filterSucceedsWithBothValuesUnresolved() { final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "like", "header:some-header"); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).contains(KNOWN_VALUE); }
@Test public void filterFailsWithOneUnresolvedValue() { final String params = String.format("(%s,\"%s\",%s)", "header:reply-to", "like", "header:some-header"); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.resolved("true")); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.unresolved()); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); when(expressionResolver.resolveAsPipelineElement("header:reply-to")) .thenReturn(PipelineElement.unresolved()); when(expressionResolver.resolveAsPipelineElement("header:some-header")) .thenReturn(PipelineElement.resolved("*2?7")); assertThat(underTest.apply(KNOWN_INPUT, params, expressionResolver)).isEmpty(); }