From f9b66409ca90d889f3808e16356a0e1b9167f652 Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Thu, 9 Jan 2025 10:22:20 +0100 Subject: [PATCH] fix: ignore case in http header filter --- .../simulator/service/QueryService.java | 23 ++++++++++++++----- .../ScenarioExecutionQueryService.java | 2 +- .../service/filter/StringFilter.java | 18 ++++++++++++--- .../ScenarioExecutionQueryServiceIT.java | 6 +++-- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/QueryService.java b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/QueryService.java index 1fda17e0e..7f85e5972 100644 --- a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/QueryService.java +++ b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/QueryService.java @@ -102,6 +102,8 @@ protected Specification buildStringSpecification(StringFilter filter, Si protected Specification buildSpecification(StringFilter filter, Function, Expression> metaclassFunction) { if (filter.getEquals() != null) { return equalsSpecification(metaclassFunction, filter.getEquals()); + } else if (filter.getEqualsIgnoreCase() != null) { + return equalsUpperSpecification(metaclassFunction, filter.getEqualsIgnoreCase()); } else if (filter.getIn() != null) { return valueIn(metaclassFunction, filter.getIn()); } else if (filter.getNotIn() != null) { @@ -360,6 +362,17 @@ protected Specification equalsSpecification(Function, E return (root, query, builder) -> builder.equal(metaclassFunction.apply(root), value); } + /** + * Generic method, which based on a Root<ENTITY> returns an Expression which type is the same as the given 'value' type, ignoring case. + * + * @param metaclassFunction function which returns the column which is used for filtering. + * @param value the actual value to filter for. + * @return a Specification. + */ + protected Specification equalsUpperSpecification(Function, Expression> metaclassFunction, String value) { + return (root, query, builder) -> builder.equal(builder.upper(metaclassFunction.apply(root).as(String.class)), value.toUpperCase()); + } + /** * Generic method, which based on a Root<ENTITY> returns an Expression which type is the same as the given 'value' type. * @@ -379,9 +392,8 @@ protected Specification notEqualsSpecification(Function * @param value a {@link java.lang.String} object. * @return a {@link org.springframework.data.jpa.domain.Specification} object. */ - protected Specification likeUpperSpecification(Function, Expression> metaclassFunction, - String value) { - return (root, query, builder) -> builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value)); + protected Specification likeUpperSpecification(Function, Expression> metaclassFunction, String value) { + return (root, query, builder) -> builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value.toUpperCase())); } /** @@ -391,9 +403,8 @@ protected Specification likeUpperSpecification(Function, Ex * @param value a {@link java.lang.String} object. * @return a {@link org.springframework.data.jpa.domain.Specification} object. */ - protected Specification doesNotContainSpecification(Function, Expression> metaclassFunction, - String value) { - return (root, query, builder) -> builder.not(builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value))); + protected Specification doesNotContainSpecification(Function, Expression> metaclassFunction, String value) { + return (root, query, builder) -> builder.not(builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value.toUpperCase()))); } /** diff --git a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/ScenarioExecutionQueryService.java b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/ScenarioExecutionQueryService.java index a0c6d0888..c87f861e6 100644 --- a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/ScenarioExecutionQueryService.java +++ b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/ScenarioExecutionQueryService.java @@ -252,7 +252,7 @@ private Specification createSpecificationFromMessageHeaderFil } Specification messageHeaderKeyEqualsSpecification = buildSpecification( - new StringFilter().setEquals(messageHeaderFilter.key), + new StringFilter().setEqualsIgnoreCase(messageHeaderFilter.key), root -> joinMessageHeaders(root).get(MessageHeader_.name)); var messageHeaderValueSpecification = switch (messageHeaderFilter.operator) { diff --git a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/filter/StringFilter.java b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/filter/StringFilter.java index 1c91d1ef0..f7347c2cc 100644 --- a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/filter/StringFilter.java +++ b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/service/filter/StringFilter.java @@ -40,6 +40,7 @@ public class StringFilter extends Filter { private String contains; private String doesNotContain; + private String equalsIgnoreCase; /** *

Constructor for StringFilter.

@@ -104,6 +105,15 @@ public StringFilter setDoesNotContain(String doesNotContain) { return this; } + public String getEqualsIgnoreCase() { + return equalsIgnoreCase; + } + + public StringFilter setEqualsIgnoreCase(String equalsIgnoreCase) { + this.equalsIgnoreCase = equalsIgnoreCase; + return this; + } + /** {@inheritDoc} */ @Override public boolean equals(Object o) { @@ -117,14 +127,15 @@ public boolean equals(Object o) { return false; } StringFilter that = (StringFilter) o; - return Objects.equals(contains, that.contains) && - Objects.equals(doesNotContain, that.doesNotContain); + return Objects.equals(contains, that.contains) + && Objects.equals(doesNotContain, that.doesNotContain) + && Objects.equals(equalsIgnoreCase, that.equalsIgnoreCase); } /** {@inheritDoc} */ @Override public int hashCode() { - return Objects.hash(super.hashCode(), contains, doesNotContain); + return Objects.hash(super.hashCode(), contains, doesNotContain, equalsIgnoreCase); } /** {@inheritDoc} */ @@ -138,6 +149,7 @@ public String toString() { + (getNotIn() != null ? "notIn=" + getNotIn() + ", " : "") + (getContains() != null ? "contains=" + getContains() + ", " : "") + (getDoesNotContain() != null ? "doesNotContain=" + getDoesNotContain() : "") + + (getEqualsIgnoreCase() != null ? "equalsIgnoreCase=" + getEqualsIgnoreCase() : "") + "]"; } } diff --git a/simulator-spring-boot/src/test/java/org/citrusframework/simulator/service/ScenarioExecutionQueryServiceIT.java b/simulator-spring-boot/src/test/java/org/citrusframework/simulator/service/ScenarioExecutionQueryServiceIT.java index 974b6888d..f8c2429a8 100644 --- a/simulator-spring-boot/src/test/java/org/citrusframework/simulator/service/ScenarioExecutionQueryServiceIT.java +++ b/simulator-spring-boot/src/test/java/org/citrusframework/simulator/service/ScenarioExecutionQueryServiceIT.java @@ -258,8 +258,10 @@ void selectWithJoinToTestResult() { public static Stream selectWithJoinToMessageHeader() { return Stream.of( arguments("83def191b1dda4c79c00ae4c443f0ca2", 0), - arguments(TRACEPARENT + "=" + MESSAGE_1_TRACEPARENT, 1), - arguments(TRACEPARENT + "~1344094d192deb39a02025c6f9a67e3d", 2) + arguments(TRACEPARENT.toLowerCase() + "=" + MESSAGE_1_TRACEPARENT.toLowerCase(), 1), + arguments(TRACEPARENT.toLowerCase() + "=" + MESSAGE_1_TRACEPARENT.toUpperCase(), 1), + arguments(TRACEPARENT.toUpperCase() + "=" + MESSAGE_1_TRACEPARENT.toLowerCase(), 1), + arguments(TRACEPARENT.toLowerCase() + "~1344094d192deb39a02025c6f9a67e3d", 2) ); }