Skip to content

Commit

Permalink
fix: ignore case in http header filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Jan 9, 2025
1 parent 53d0ed1 commit ce8911e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ protected Specification<ENTITY> buildStringSpecification(StringFilter filter, Si
protected Specification<ENTITY> buildSpecification(StringFilter filter, Function<Root<ENTITY>, Expression<String>> 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) {
Expand Down Expand Up @@ -360,6 +362,17 @@ protected <X> Specification<ENTITY> equalsSpecification(Function<Root<ENTITY>, E
return (root, query, builder) -> builder.equal(metaclassFunction.apply(root), value);
}

/**
* Generic method, which based on a Root&lt;ENTITY&gt; 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<ENTITY> equalsUpperSpecification(Function<Root<ENTITY>, Expression<String>> 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&lt;ENTITY&gt; returns an Expression which type is the same as the given 'value' type.
*
Expand All @@ -379,9 +392,8 @@ protected <X> Specification<ENTITY> notEqualsSpecification(Function<Root<ENTITY>
* @param value a {@link java.lang.String} object.
* @return a {@link org.springframework.data.jpa.domain.Specification} object.
*/
protected Specification<ENTITY> likeUpperSpecification(Function<Root<ENTITY>, Expression<String>> metaclassFunction,
String value) {
return (root, query, builder) -> builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value));
protected Specification<ENTITY> likeUpperSpecification(Function<Root<ENTITY>, Expression<String>> metaclassFunction, String value) {
return (root, query, builder) -> builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value.toUpperCase()));
}

/**
Expand All @@ -391,9 +403,8 @@ protected Specification<ENTITY> likeUpperSpecification(Function<Root<ENTITY>, Ex
* @param value a {@link java.lang.String} object.
* @return a {@link org.springframework.data.jpa.domain.Specification} object.
*/
protected Specification<ENTITY> doesNotContainSpecification(Function<Root<ENTITY>, Expression<String>> metaclassFunction,
String value) {
return (root, query, builder) -> builder.not(builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value)));
protected Specification<ENTITY> doesNotContainSpecification(Function<Root<ENTITY>, Expression<String>> metaclassFunction, String value) {
return (root, query, builder) -> builder.not(builder.like(builder.upper(metaclassFunction.apply(root).as(String.class)), wrapLikeQuery(value.toUpperCase())));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private Specification<ScenarioExecution> createSpecificationFromMessageHeaderFil
}

Specification<ScenarioExecution> messageHeaderKeyEqualsSpecification = buildSpecification(
new StringFilter().setEquals(messageHeaderFilter.key),
new StringFilter().setEqualsIgnoreCase(messageHeaderFilter.key),
root -> joinMessageHeaders(root).get(MessageHeader_.name));

var messageHeaderValueSpecification = switch (messageHeaderFilter.operator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class StringFilter extends Filter<String> {

private String contains;
private String doesNotContain;
private String equalsIgnoreCase;

/**
* <p>Constructor for StringFilter.</p>
Expand Down Expand Up @@ -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) {
Expand All @@ -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} */
Expand All @@ -138,6 +149,7 @@ public String toString() {
+ (getNotIn() != null ? "notIn=" + getNotIn() + ", " : "")
+ (getContains() != null ? "contains=" + getContains() + ", " : "")
+ (getDoesNotContain() != null ? "doesNotContain=" + getDoesNotContain() : "")
+ (getEqualsIgnoreCase() != null ? "equalsIgnoreCase=" + getEqualsIgnoreCase() : "")
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ void selectWithJoinToTestResult() {
public static Stream<Arguments> 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)
);
}

Expand Down

0 comments on commit ce8911e

Please sign in to comment.