-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add utility for search query like or equal predicate
- Loading branch information
1 parent
05b44aa
commit 5a9c5c6
Showing
14 changed files
with
261 additions
and
13 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
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
81 changes: 81 additions & 0 deletions
81
extensions/jpa/runtime/src/test/java/org/tkit/quarkus/jpa/utils/QueryCriteriaUtilTest.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,81 @@ | ||
package org.tkit.quarkus.jpa.utils; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
import jakarta.persistence.criteria.*; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.stubbing.Answer; | ||
|
||
class QueryCriteriaUtilTest { | ||
|
||
@Test | ||
void createSearchStringPredicateTest() { | ||
|
||
final var result = new TestResult(); | ||
|
||
Predicate predicate = Mockito.mock(Predicate.class); | ||
CriteriaBuilder cb = Mockito.mock(CriteriaBuilder.class); | ||
Mockito.when(cb.lower(any())).thenReturn(null); | ||
Mockito.when(cb.like(any(), (String) any())).thenAnswer(invocation -> { | ||
result.like = true; | ||
result.searchString = invocation.getArgument(1); | ||
return predicate; | ||
}); | ||
Mockito.when(cb.equal(any(), (String) any())).thenAnswer((Answer<Predicate>) invocation -> { | ||
result.equal = true; | ||
result.searchString = invocation.getArgument(1); | ||
return predicate; | ||
}); | ||
|
||
Expression<String> expression = Mockito.mock(Expression.class); | ||
Mockito.when(cb.lower(any())).thenAnswer(invocation -> { | ||
result.lower = false; | ||
return expression; | ||
}); | ||
|
||
Predicate p = QueryCriteriaUtil.createSearchStringPredicate(cb, expression, "searchText", false); | ||
Assertions.assertNotNull(p); | ||
Assertions.assertFalse(result.like); | ||
Assertions.assertTrue(result.equal); | ||
Assertions.assertEquals("searchText", result.searchString); | ||
|
||
result.reset(); | ||
p = QueryCriteriaUtil.createSearchStringPredicate(cb, expression, "ThisIsNotSearchText", true); | ||
Assertions.assertNotNull(p); | ||
Assertions.assertFalse(result.like); | ||
Assertions.assertTrue(result.equal); | ||
Assertions.assertEquals("thisisnotsearchtext", result.searchString); | ||
|
||
result.reset(); | ||
p = QueryCriteriaUtil.createSearchStringPredicate(cb, expression, "?ThisIsNotSearchText", true); | ||
Assertions.assertNotNull(p); | ||
Assertions.assertFalse(result.equal); | ||
Assertions.assertTrue(result.like); | ||
Assertions.assertEquals("_thisisnotsearchtext", result.searchString); | ||
|
||
result.reset(); | ||
p = QueryCriteriaUtil.createSearchStringPredicate(cb, expression, "?ThisIsNotSearchText*", true); | ||
Assertions.assertNotNull(p); | ||
Assertions.assertFalse(result.equal); | ||
Assertions.assertTrue(result.like); | ||
Assertions.assertEquals("_thisisnotsearchtext%", result.searchString); | ||
} | ||
|
||
public static class TestResult { | ||
boolean like = false; | ||
boolean equal = false; | ||
boolean lower = false; | ||
|
||
String searchString = null; | ||
|
||
void reset() { | ||
like = false; | ||
equal = false; | ||
lower = false; | ||
searchString = null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.