-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ilike operator for Criterion (#4114)
- Loading branch information
Showing
7 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
core/common/lib/query-lib/src/main/java/org/eclipse/edc/query/IlikeOperatorPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.query; | ||
|
||
import org.eclipse.edc.spi.query.OperatorPredicate; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class IlikeOperatorPredicate implements OperatorPredicate { | ||
@Override | ||
public boolean test(Object property, Object operandRight) { | ||
if (operandRight instanceof String stringOperand) { | ||
var regexPattern = Pattern.quote(stringOperand.toLowerCase()) | ||
.replace("%", "\\E.*\\Q") | ||
.replace("_", "\\E.\\Q"); | ||
|
||
return Pattern.compile("^" + regexPattern + "$") | ||
.matcher(property.toString().toLowerCase()) | ||
.matches(); | ||
} | ||
|
||
return false; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../common/lib/query-lib/src/test/java/org/eclipse/edc/query/IlikeOperatorPredicateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.query; | ||
|
||
import org.eclipse.edc.spi.query.OperatorPredicate; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class IlikeOperatorPredicateTest { | ||
|
||
private final OperatorPredicate predicate = new IlikeOperatorPredicate(); | ||
|
||
@Test | ||
void shouldHandlePercentAtTheStartOfTheString() { | ||
assertThat(predicate.test("THIS IS A TEST", "%test")).isTrue(); | ||
assertThat(predicate.test("NOT TESTED", "%test")).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldHandlePercentAtTheEndOfTheString() { | ||
assertThat(predicate.test("TEST VALID", "test%")).isTrue(); | ||
assertThat(predicate.test(" TEST INVALID", "test%")).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldHandlePercentAtTheStartAndEndOfTheString() { | ||
assertThat(predicate.test("THIS TEST VALID", "%test%")).isTrue(); | ||
assertThat(predicate.test("TEST ALSO VALID", "%test%")).isTrue(); | ||
assertThat(predicate.test("VALID IS THE TEST", "%test%")).isTrue(); | ||
assertThat(predicate.test("INVALID", "%test%")).isFalse(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters