This commit is contained in:
@@ -98,18 +98,18 @@ public class BookRuleEvaluatorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return switch (rule.getOperator()) {
|
return switch (rule.getOperator()) {
|
||||||
case EQUALS -> buildEquals(rule, cb, root, progressJoin);
|
case EQUALS -> buildEquals(rule, query, cb, root, progressJoin);
|
||||||
case NOT_EQUALS -> buildNotEquals(rule, cb, root, progressJoin);
|
case NOT_EQUALS -> buildNotEquals(rule, query, cb, root, progressJoin);
|
||||||
case CONTAINS -> buildContains(rule, cb, root, progressJoin);
|
case CONTAINS -> buildContains(rule, query, cb, root, progressJoin);
|
||||||
case DOES_NOT_CONTAIN -> {
|
case DOES_NOT_CONTAIN -> {
|
||||||
Predicate notContains = cb.not(buildContains(rule, cb, root, progressJoin));
|
Predicate notContains = cb.not(buildContains(rule, query, cb, root, progressJoin));
|
||||||
if (rule.getField() == RuleField.READ_STATUS) {
|
if (rule.getField() == RuleField.READ_STATUS) {
|
||||||
yield cb.or(cb.isNull(progressJoin.get("readStatus")), notContains);
|
yield cb.or(cb.isNull(progressJoin.get("readStatus")), notContains);
|
||||||
}
|
}
|
||||||
yield notContains;
|
yield notContains;
|
||||||
}
|
}
|
||||||
case STARTS_WITH -> buildStartsWith(rule, cb, root, progressJoin);
|
case STARTS_WITH -> buildStartsWith(rule, query, cb, root, progressJoin);
|
||||||
case ENDS_WITH -> buildEndsWith(rule, cb, root, progressJoin);
|
case ENDS_WITH -> buildEndsWith(rule, query, cb, root, progressJoin);
|
||||||
case GREATER_THAN -> buildGreaterThan(rule, cb, root, progressJoin);
|
case GREATER_THAN -> buildGreaterThan(rule, cb, root, progressJoin);
|
||||||
case GREATER_THAN_EQUAL_TO -> buildGreaterThanEqual(rule, cb, root, progressJoin);
|
case GREATER_THAN_EQUAL_TO -> buildGreaterThanEqual(rule, cb, root, progressJoin);
|
||||||
case LESS_THAN -> buildLessThan(rule, cb, root, progressJoin);
|
case LESS_THAN -> buildLessThan(rule, cb, root, progressJoin);
|
||||||
@@ -117,9 +117,9 @@ public class BookRuleEvaluatorService {
|
|||||||
case IN_BETWEEN -> buildInBetween(rule, cb, root, progressJoin);
|
case IN_BETWEEN -> buildInBetween(rule, cb, root, progressJoin);
|
||||||
case IS_EMPTY -> buildIsEmpty(rule, query, cb, root, progressJoin);
|
case IS_EMPTY -> buildIsEmpty(rule, query, cb, root, progressJoin);
|
||||||
case IS_NOT_EMPTY -> cb.not(buildIsEmpty(rule, query, cb, root, progressJoin));
|
case IS_NOT_EMPTY -> cb.not(buildIsEmpty(rule, query, cb, root, progressJoin));
|
||||||
case INCLUDES_ANY -> buildIncludesAny(rule, cb, root, progressJoin);
|
case INCLUDES_ANY -> buildIncludesAny(rule, query, cb, root, progressJoin);
|
||||||
case EXCLUDES_ALL -> buildExcludesAll(rule, cb, root, progressJoin);
|
case EXCLUDES_ALL -> buildExcludesAll(rule, query, cb, root, progressJoin);
|
||||||
case INCLUDES_ALL -> buildIncludesAll(rule, cb, root, progressJoin);
|
case INCLUDES_ALL -> buildIncludesAll(rule, query, cb, root, progressJoin);
|
||||||
case WITHIN_LAST -> buildWithinLast(rule, cb, root, progressJoin);
|
case WITHIN_LAST -> buildWithinLast(rule, cb, root, progressJoin);
|
||||||
case OLDER_THAN -> buildOlderThan(rule, cb, root, progressJoin);
|
case OLDER_THAN -> buildOlderThan(rule, cb, root, progressJoin);
|
||||||
case THIS_PERIOD -> buildThisPeriod(rule, cb, root, progressJoin);
|
case THIS_PERIOD -> buildThisPeriod(rule, cb, root, progressJoin);
|
||||||
@@ -510,11 +510,11 @@ public class BookRuleEvaluatorService {
|
|||||||
return cb.and(notRead, noLowerUnread, hasPriorRead);
|
return cb.and(notRead, noLowerUnread, hasPriorRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildEquals(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildEquals(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
List<String> ruleList = toStringList(rule.getValue());
|
List<String> ruleList = toStringList(rule.getValue());
|
||||||
|
|
||||||
if (isArrayField(rule.getField())) {
|
if (isArrayField(rule.getField())) {
|
||||||
return buildArrayFieldPredicate(rule.getField(), ruleList, cb, root, false);
|
return buildArrayFieldPredicate(rule.getField(), ruleList, query, cb, root, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression<?> field = getFieldExpression(rule.getField(), cb, root, progressJoin);
|
Expression<?> field = getFieldExpression(rule.getField(), cb, root, progressJoin);
|
||||||
@@ -537,40 +537,46 @@ public class BookRuleEvaluatorService {
|
|||||||
return cb.equal(cb.lower(field.as(String.class)), value.toString().toLowerCase());
|
return cb.equal(cb.lower(field.as(String.class)), value.toString().toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildNotEquals(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildNotEquals(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
Predicate negated = cb.not(buildEquals(rule, cb, root, progressJoin));
|
Predicate negated = cb.not(buildEquals(rule, query, cb, root, progressJoin));
|
||||||
if (rule.getField() == RuleField.READ_STATUS && !"UNSET".equals(String.valueOf(rule.getValue()))) {
|
if (rule.getField() == RuleField.READ_STATUS && !"UNSET".equals(String.valueOf(rule.getValue()))) {
|
||||||
return cb.or(cb.isNull(progressJoin.get("readStatus")), negated);
|
return cb.or(cb.isNull(progressJoin.get("readStatus")), negated);
|
||||||
}
|
}
|
||||||
return negated;
|
return negated;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildContains(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildContains(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
String ruleVal = rule.getValue().toString().toLowerCase();
|
String ruleVal = rule.getValue().toString().toLowerCase();
|
||||||
return buildStringPredicate(rule.getField(), root, progressJoin, cb,
|
return buildStringPredicate(rule.getField(), query, root, progressJoin, cb,
|
||||||
nameField -> cb.like(cb.lower(nameField), "%" + escapeLike(ruleVal) + "%"));
|
nameField -> cb.like(cb.lower(nameField), "%" + escapeLike(ruleVal) + "%"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildStartsWith(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildStartsWith(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
String ruleVal = rule.getValue().toString().toLowerCase();
|
String ruleVal = rule.getValue().toString().toLowerCase();
|
||||||
return buildStringPredicate(rule.getField(), root, progressJoin, cb,
|
return buildStringPredicate(rule.getField(), query, root, progressJoin, cb,
|
||||||
nameField -> cb.like(cb.lower(nameField), escapeLike(ruleVal) + "%"));
|
nameField -> cb.like(cb.lower(nameField), escapeLike(ruleVal) + "%"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildEndsWith(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildEndsWith(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
String ruleVal = rule.getValue().toString().toLowerCase();
|
String ruleVal = rule.getValue().toString().toLowerCase();
|
||||||
return buildStringPredicate(rule.getField(), root, progressJoin, cb,
|
return buildStringPredicate(rule.getField(), query, root, progressJoin, cb,
|
||||||
nameField -> cb.like(cb.lower(nameField), "%" + escapeLike(ruleVal)));
|
nameField -> cb.like(cb.lower(nameField), "%" + escapeLike(ruleVal)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildStringPredicate(RuleField field, Root<BookEntity> root,
|
private Predicate buildStringPredicate(RuleField field, CriteriaQuery<?> query, Root<BookEntity> root,
|
||||||
Join<BookEntity, UserBookProgressEntity> progressJoin,
|
Join<BookEntity, UserBookProgressEntity> progressJoin,
|
||||||
CriteriaBuilder cb,
|
CriteriaBuilder cb,
|
||||||
java.util.function.Function<Expression<String>, Predicate> predicateBuilder) {
|
java.util.function.Function<Expression<String>, Predicate> predicateBuilder) {
|
||||||
if (isArrayField(field)) {
|
if (isArrayField(field)) {
|
||||||
Join<?, ?> arrayJoin = createArrayFieldJoin(field, root);
|
Subquery<Long> subquery = query.subquery(Long.class);
|
||||||
|
Root<BookEntity> subRoot = subquery.from(BookEntity.class);
|
||||||
|
Join<?, ?> arrayJoin = createArrayFieldJoin(field, subRoot);
|
||||||
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
||||||
return predicateBuilder.apply(nameField);
|
subquery.select(cb.literal(1L)).where(
|
||||||
|
cb.equal(subRoot.get("id"), root.get("id")),
|
||||||
|
predicateBuilder.apply(nameField)
|
||||||
|
);
|
||||||
|
return cb.exists(subquery);
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression<?> fieldExpr = getFieldExpression(field, cb, root, progressJoin);
|
Expression<?> fieldExpr = getFieldExpression(field, cb, root, progressJoin);
|
||||||
@@ -664,21 +670,21 @@ public class BookRuleEvaluatorService {
|
|||||||
return cb.or(cb.isNull(field), cb.equal(cb.trim(field.as(String.class)), ""));
|
return cb.or(cb.isNull(field), cb.equal(cb.trim(field.as(String.class)), ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildIncludesAny(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildIncludesAny(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
List<String> ruleList = toStringList(rule.getValue());
|
List<String> ruleList = toStringList(rule.getValue());
|
||||||
|
|
||||||
if (isArrayField(rule.getField())) {
|
if (isArrayField(rule.getField())) {
|
||||||
return buildArrayFieldPredicate(rule.getField(), ruleList, cb, root, false);
|
return buildArrayFieldPredicate(rule.getField(), ruleList, query, cb, root, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin);
|
return buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildExcludesAll(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildExcludesAll(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
List<String> ruleList = toStringList(rule.getValue());
|
List<String> ruleList = toStringList(rule.getValue());
|
||||||
|
|
||||||
if (isArrayField(rule.getField())) {
|
if (isArrayField(rule.getField())) {
|
||||||
return cb.not(buildArrayFieldPredicate(rule.getField(), ruleList, cb, root, false));
|
return cb.not(buildArrayFieldPredicate(rule.getField(), ruleList, query, cb, root, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
Predicate negated = cb.not(buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin));
|
Predicate negated = cb.not(buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin));
|
||||||
@@ -688,11 +694,11 @@ public class BookRuleEvaluatorService {
|
|||||||
return negated;
|
return negated;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildIncludesAll(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
private Predicate buildIncludesAll(Rule rule, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
|
||||||
List<String> ruleList = toStringList(rule.getValue());
|
List<String> ruleList = toStringList(rule.getValue());
|
||||||
|
|
||||||
if (isArrayField(rule.getField())) {
|
if (isArrayField(rule.getField())) {
|
||||||
return buildArrayFieldPredicate(rule.getField(), ruleList, cb, root, true);
|
return buildArrayFieldPredicate(rule.getField(), ruleList, query, cb, root, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin);
|
return buildFieldInPredicate(rule.getField(), field -> field, ruleList, cb, progressJoin);
|
||||||
@@ -813,29 +819,41 @@ public class BookRuleEvaluatorService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate buildArrayFieldPredicate(RuleField field, List<String> values, CriteriaBuilder cb, Root<BookEntity> root, boolean includesAll) {
|
private Predicate buildArrayFieldPredicate(RuleField field, List<String> values, CriteriaQuery<?> query, CriteriaBuilder cb, Root<BookEntity> root, boolean includesAll) {
|
||||||
if (values.isEmpty()) {
|
if (values.isEmpty()) {
|
||||||
return cb.conjunction();
|
return cb.conjunction();
|
||||||
}
|
}
|
||||||
if (includesAll) {
|
if (includesAll) {
|
||||||
List<Predicate> predicates = values.stream()
|
List<Predicate> predicates = values.stream()
|
||||||
.map(value -> {
|
.map(value -> {
|
||||||
Join<?, ?> arrayJoin = createArrayFieldJoin(field, root);
|
Subquery<Long> subquery = query.subquery(Long.class);
|
||||||
|
Root<BookEntity> subRoot = subquery.from(BookEntity.class);
|
||||||
|
Join<?, ?> arrayJoin = createArrayFieldJoin(field, subRoot);
|
||||||
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
||||||
return cb.equal(cb.lower(nameField), value.toLowerCase());
|
subquery.select(cb.literal(1L)).where(
|
||||||
|
cb.equal(subRoot.get("id"), root.get("id")),
|
||||||
|
cb.equal(cb.lower(nameField), value.toLowerCase())
|
||||||
|
);
|
||||||
|
return cb.exists(subquery);
|
||||||
})
|
})
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
return cb.and(predicates.toArray(new Predicate[0]));
|
return cb.and(predicates.toArray(new Predicate[0]));
|
||||||
} else {
|
} else {
|
||||||
Join<?, ?> arrayJoin = createArrayFieldJoin(field, root);
|
Subquery<Long> subquery = query.subquery(Long.class);
|
||||||
|
Root<BookEntity> subRoot = subquery.from(BookEntity.class);
|
||||||
|
Join<?, ?> arrayJoin = createArrayFieldJoin(field, subRoot);
|
||||||
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
Expression<String> nameField = getArrayFieldNameExpression(field, arrayJoin);
|
||||||
|
|
||||||
List<String> lowerValues = values.stream()
|
List<String> lowerValues = values.stream()
|
||||||
.map(String::toLowerCase)
|
.map(String::toLowerCase)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return cb.lower(nameField).in(lowerValues);
|
subquery.select(cb.literal(1L)).where(
|
||||||
|
cb.equal(subRoot.get("id"), root.get("id")),
|
||||||
|
cb.lower(nameField).in(lowerValues)
|
||||||
|
);
|
||||||
|
return cb.exists(subquery);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+444
@@ -21,9 +21,13 @@ import org.springframework.data.jpa.domain.Specification;
|
|||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -313,6 +317,446 @@ class BookRuleEvaluatorFieldCoverageTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class MultiShelfFilterTests {
|
||||||
|
|
||||||
|
private Rule shelfRule(RuleOperator operator, Object value) {
|
||||||
|
Rule rule = new Rule();
|
||||||
|
rule.setField(RuleField.SHELF);
|
||||||
|
rule.setOperator(operator);
|
||||||
|
rule.setValue(value);
|
||||||
|
return rule;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Long> findMatchingIdsPaged(GroupRule group, int pageSize) {
|
||||||
|
Specification<BookEntity> spec = evaluator.toSpecification(group, user.getId());
|
||||||
|
Page<BookEntity> page = bookRepository.findAll(spec, PageRequest.of(0, pageSize));
|
||||||
|
return page.getContent().stream().map(BookEntity::getId).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void orGroup_multipleShelfEquals_noDuplicateRows() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Shelf A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Shelf B").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
|
||||||
|
BookEntity bookOnBoth = createBook("On Both Shelves");
|
||||||
|
bookOnBoth.setShelves(new HashSet<>(Set.of(shelf1, shelf2)));
|
||||||
|
em.merge(bookOnBoth);
|
||||||
|
|
||||||
|
BookEntity bookOnShelf1 = createBook("On Shelf A Only");
|
||||||
|
bookOnShelf1.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(bookOnShelf1);
|
||||||
|
|
||||||
|
BookEntity bookOnShelf2 = createBook("On Shelf B Only");
|
||||||
|
bookOnShelf2.setShelves(new HashSet<>(Set.of(shelf2)));
|
||||||
|
em.merge(bookOnShelf2);
|
||||||
|
|
||||||
|
BookEntity bookOnNeither = createBook("On No Shelf");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.OR);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelf1.getId()))),
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelf2.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(group);
|
||||||
|
assertThat(ids).containsExactlyInAnyOrder(bookOnBoth.getId(), bookOnShelf1.getId(), bookOnShelf2.getId());
|
||||||
|
assertThat(ids).doesNotContain(bookOnNeither.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void orGroup_multipleShelfEquals_paginationNotTruncated() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Page Shelf A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Page Shelf B").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
|
||||||
|
List<BookEntity> books = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
BookEntity book = createBook("Multi-shelf Book " + i);
|
||||||
|
book.setShelves(new HashSet<>(Set.of(shelf1, shelf2)));
|
||||||
|
em.merge(book);
|
||||||
|
books.add(book);
|
||||||
|
}
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.OR);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelf1.getId()))),
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelf2.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIdsPaged(group, 10);
|
||||||
|
assertThat(ids).hasSize(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void andGroup_inclusionAndExclusion_excludesCorrectly() {
|
||||||
|
ShelfEntity includeShelf = ShelfEntity.builder().user(user).name("Include Me").build();
|
||||||
|
ShelfEntity excludeShelf = ShelfEntity.builder().user(user).name("Exclude Me").build();
|
||||||
|
em.persist(includeShelf);
|
||||||
|
em.persist(excludeShelf);
|
||||||
|
|
||||||
|
BookEntity onIncludeOnly = createBook("Include Only");
|
||||||
|
onIncludeOnly.setShelves(new HashSet<>(Set.of(includeShelf)));
|
||||||
|
em.merge(onIncludeOnly);
|
||||||
|
|
||||||
|
BookEntity onBoth = createBook("On Both");
|
||||||
|
onBoth.setShelves(new HashSet<>(Set.of(includeShelf, excludeShelf)));
|
||||||
|
em.merge(onBoth);
|
||||||
|
|
||||||
|
BookEntity onExcludeOnly = createBook("Exclude Only");
|
||||||
|
onExcludeOnly.setShelves(new HashSet<>(Set.of(excludeShelf)));
|
||||||
|
em.merge(onExcludeOnly);
|
||||||
|
|
||||||
|
BookEntity onNeither = createBook("On Neither");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.AND);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(includeShelf.getId()))),
|
||||||
|
shelfRule(RuleOperator.EXCLUDES_ALL, List.of(String.valueOf(excludeShelf.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(group);
|
||||||
|
assertThat(ids).containsExactly(onIncludeOnly.getId());
|
||||||
|
assertThat(ids).doesNotContain(onBoth.getId(), onExcludeOnly.getId(), onNeither.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void includesAll_multipleShelvesRequired() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Required A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Required B").build();
|
||||||
|
ShelfEntity shelf3 = ShelfEntity.builder().user(user).name("Extra C").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
em.persist(shelf3);
|
||||||
|
|
||||||
|
BookEntity onAll = createBook("On All Three");
|
||||||
|
onAll.setShelves(new HashSet<>(Set.of(shelf1, shelf2, shelf3)));
|
||||||
|
em.merge(onAll);
|
||||||
|
|
||||||
|
BookEntity onFirstTwo = createBook("On First Two");
|
||||||
|
onFirstTwo.setShelves(new HashSet<>(Set.of(shelf1, shelf2)));
|
||||||
|
em.merge(onFirstTwo);
|
||||||
|
|
||||||
|
BookEntity onFirstOnly = createBook("On First Only");
|
||||||
|
onFirstOnly.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(onFirstOnly);
|
||||||
|
|
||||||
|
BookEntity onNone = createBook("On None");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(singleRule(RuleField.SHELF, RuleOperator.INCLUDES_ALL,
|
||||||
|
List.of(String.valueOf(shelf1.getId()), String.valueOf(shelf2.getId()))));
|
||||||
|
assertThat(ids).containsExactlyInAnyOrder(onAll.getId(), onFirstTwo.getId());
|
||||||
|
assertThat(ids).doesNotContain(onFirstOnly.getId(), onNone.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void includesAny_multipleShelvesMatch() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Any A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Any B").build();
|
||||||
|
ShelfEntity shelf3 = ShelfEntity.builder().user(user).name("Any C").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
em.persist(shelf3);
|
||||||
|
|
||||||
|
BookEntity onFirst = createBook("On First");
|
||||||
|
onFirst.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(onFirst);
|
||||||
|
|
||||||
|
BookEntity onSecond = createBook("On Second");
|
||||||
|
onSecond.setShelves(new HashSet<>(Set.of(shelf2)));
|
||||||
|
em.merge(onSecond);
|
||||||
|
|
||||||
|
BookEntity onThird = createBook("On Third Only");
|
||||||
|
onThird.setShelves(new HashSet<>(Set.of(shelf3)));
|
||||||
|
em.merge(onThird);
|
||||||
|
|
||||||
|
BookEntity onNone = createBook("On None");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(singleRule(RuleField.SHELF, RuleOperator.INCLUDES_ANY,
|
||||||
|
List.of(String.valueOf(shelf1.getId()), String.valueOf(shelf2.getId()))));
|
||||||
|
assertThat(ids).containsExactlyInAnyOrder(onFirst.getId(), onSecond.getId());
|
||||||
|
assertThat(ids).doesNotContain(onThird.getId(), onNone.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void excludesAll_multipleShelvesExcluded() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Excl A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Excl B").build();
|
||||||
|
ShelfEntity shelf3 = ShelfEntity.builder().user(user).name("Keep C").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
em.persist(shelf3);
|
||||||
|
|
||||||
|
BookEntity onExcluded1 = createBook("On Excluded 1");
|
||||||
|
onExcluded1.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(onExcluded1);
|
||||||
|
|
||||||
|
BookEntity onExcluded2 = createBook("On Excluded 2");
|
||||||
|
onExcluded2.setShelves(new HashSet<>(Set.of(shelf2)));
|
||||||
|
em.merge(onExcluded2);
|
||||||
|
|
||||||
|
BookEntity onBothExcluded = createBook("On Both Excluded");
|
||||||
|
onBothExcluded.setShelves(new HashSet<>(Set.of(shelf1, shelf2)));
|
||||||
|
em.merge(onBothExcluded);
|
||||||
|
|
||||||
|
BookEntity onKeep = createBook("On Keep Shelf");
|
||||||
|
onKeep.setShelves(new HashSet<>(Set.of(shelf3)));
|
||||||
|
em.merge(onKeep);
|
||||||
|
|
||||||
|
BookEntity onNone = createBook("On No Shelf");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(singleRule(RuleField.SHELF, RuleOperator.EXCLUDES_ALL,
|
||||||
|
List.of(String.valueOf(shelf1.getId()), String.valueOf(shelf2.getId()))));
|
||||||
|
assertThat(ids).contains(onKeep.getId(), onNone.getId());
|
||||||
|
assertThat(ids).doesNotContain(onExcluded1.getId(), onExcluded2.getId(), onBothExcluded.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nestedGroups_shelfAndTitleCombined() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Nested Shelf").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
|
||||||
|
BookEntity matchesBoth = createBook("Magic Book");
|
||||||
|
matchesBoth.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(matchesBoth);
|
||||||
|
|
||||||
|
BookEntity matchesShelfOnly = createBook("Normal Book");
|
||||||
|
matchesShelfOnly.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(matchesShelfOnly);
|
||||||
|
|
||||||
|
BookEntity matchesTitleOnly = createBook("Magic Elsewhere");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
Rule shelfEq = shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelf1.getId())));
|
||||||
|
|
||||||
|
Rule titleContains = new Rule();
|
||||||
|
titleContains.setField(RuleField.TITLE);
|
||||||
|
titleContains.setOperator(RuleOperator.CONTAINS);
|
||||||
|
titleContains.setValue("Magic");
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.AND);
|
||||||
|
group.setRules(List.of(shelfEq, titleContains));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(group);
|
||||||
|
assertThat(ids).containsExactly(matchesBoth.getId());
|
||||||
|
assertThat(ids).doesNotContain(matchesShelfOnly.getId(), matchesTitleOnly.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bookOnManyShelves_paginationReturnsCorrectCount() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Many A").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Many B").build();
|
||||||
|
ShelfEntity shelf3 = ShelfEntity.builder().user(user).name("Many C").build();
|
||||||
|
ShelfEntity shelf4 = ShelfEntity.builder().user(user).name("Many D").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
em.persist(shelf3);
|
||||||
|
em.persist(shelf4);
|
||||||
|
|
||||||
|
List<BookEntity> books = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
BookEntity book = createBook("Many Shelf Book " + i);
|
||||||
|
book.setShelves(new HashSet<>(Set.of(shelf1, shelf2, shelf3, shelf4)));
|
||||||
|
em.merge(book);
|
||||||
|
books.add(book);
|
||||||
|
}
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIdsPaged(
|
||||||
|
singleRule(RuleField.SHELF, RuleOperator.EQUALS, List.of(String.valueOf(shelf1.getId()))),
|
||||||
|
5);
|
||||||
|
assertThat(ids).hasSize(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void orGroup_includesAnyAndExcludesAll() {
|
||||||
|
ShelfEntity shelf1 = ShelfEntity.builder().user(user).name("Or Inc").build();
|
||||||
|
ShelfEntity shelf2 = ShelfEntity.builder().user(user).name("Or Exc").build();
|
||||||
|
em.persist(shelf1);
|
||||||
|
em.persist(shelf2);
|
||||||
|
|
||||||
|
BookEntity onShelf1 = createBook("On Shelf1");
|
||||||
|
onShelf1.setShelves(new HashSet<>(Set.of(shelf1)));
|
||||||
|
em.merge(onShelf1);
|
||||||
|
|
||||||
|
BookEntity onShelf2 = createBook("On Shelf2");
|
||||||
|
onShelf2.setShelves(new HashSet<>(Set.of(shelf2)));
|
||||||
|
em.merge(onShelf2);
|
||||||
|
|
||||||
|
BookEntity onBoth = createBook("On Both");
|
||||||
|
onBoth.setShelves(new HashSet<>(Set.of(shelf1, shelf2)));
|
||||||
|
em.merge(onBoth);
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.AND);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.INCLUDES_ANY, List.of(String.valueOf(shelf1.getId()), String.valueOf(shelf2.getId()))),
|
||||||
|
shelfRule(RuleOperator.EXCLUDES_ALL, List.of(String.valueOf(shelf2.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(group);
|
||||||
|
assertThat(ids).containsExactly(onShelf1.getId());
|
||||||
|
assertThat(ids).doesNotContain(onShelf2.getId(), onBoth.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bugReport2822_orShelfFilter_paginationTruncation() {
|
||||||
|
ShelfEntity shelfA = ShelfEntity.builder().user(user).name("Bug Shelf A").build();
|
||||||
|
ShelfEntity shelfB = ShelfEntity.builder().user(user).name("Bug Shelf B").build();
|
||||||
|
em.persist(shelfA);
|
||||||
|
em.persist(shelfB);
|
||||||
|
|
||||||
|
List<Long> expectedIds = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 20; i++) {
|
||||||
|
BookEntity book = createBook("Bug2822 Book " + i);
|
||||||
|
if (i < 10) {
|
||||||
|
book.setShelves(new HashSet<>(Set.of(shelfA, shelfB)));
|
||||||
|
} else if (i < 15) {
|
||||||
|
book.setShelves(new HashSet<>(Set.of(shelfA)));
|
||||||
|
} else {
|
||||||
|
book.setShelves(new HashSet<>(Set.of(shelfB)));
|
||||||
|
}
|
||||||
|
em.merge(book);
|
||||||
|
expectedIds.add(book.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
BookEntity unrelated = createBook("Bug2822 Unrelated");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.OR);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelfA.getId()))),
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(shelfB.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> pagedIds = findMatchingIdsPaged(group, 20);
|
||||||
|
assertThat(pagedIds).hasSize(20);
|
||||||
|
assertThat(pagedIds).containsExactlyInAnyOrderElementsOf(expectedIds);
|
||||||
|
assertThat(pagedIds).doesNotContain(unrelated.getId());
|
||||||
|
|
||||||
|
List<Long> allIds = findMatchingIds(group);
|
||||||
|
assertThat(allIds).hasSize(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bugReport2822_inclusionExclusion_exclusionActuallyWorks() {
|
||||||
|
ShelfEntity includeShelf = ShelfEntity.builder().user(user).name("Bug Include").build();
|
||||||
|
ShelfEntity excludeShelf = ShelfEntity.builder().user(user).name("Bug Exclude").build();
|
||||||
|
ShelfEntity extraShelf = ShelfEntity.builder().user(user).name("Bug Extra").build();
|
||||||
|
em.persist(includeShelf);
|
||||||
|
em.persist(excludeShelf);
|
||||||
|
em.persist(extraShelf);
|
||||||
|
|
||||||
|
BookEntity shouldAppear = createBook("Bug2822 Include Only");
|
||||||
|
shouldAppear.setShelves(new HashSet<>(Set.of(includeShelf)));
|
||||||
|
em.merge(shouldAppear);
|
||||||
|
|
||||||
|
BookEntity shouldAppear2 = createBook("Bug2822 Include + Extra");
|
||||||
|
shouldAppear2.setShelves(new HashSet<>(Set.of(includeShelf, extraShelf)));
|
||||||
|
em.merge(shouldAppear2);
|
||||||
|
|
||||||
|
BookEntity shouldNotAppear = createBook("Bug2822 Include + Exclude");
|
||||||
|
shouldNotAppear.setShelves(new HashSet<>(Set.of(includeShelf, excludeShelf)));
|
||||||
|
em.merge(shouldNotAppear);
|
||||||
|
|
||||||
|
BookEntity excludeOnly = createBook("Bug2822 Exclude Only");
|
||||||
|
excludeOnly.setShelves(new HashSet<>(Set.of(excludeShelf)));
|
||||||
|
em.merge(excludeOnly);
|
||||||
|
|
||||||
|
BookEntity excludeAndExtra = createBook("Bug2822 Exclude + Extra");
|
||||||
|
excludeAndExtra.setShelves(new HashSet<>(Set.of(excludeShelf, extraShelf)));
|
||||||
|
em.merge(excludeAndExtra);
|
||||||
|
|
||||||
|
BookEntity noShelf = createBook("Bug2822 No Shelf");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.AND);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(includeShelf.getId()))),
|
||||||
|
shelfRule(RuleOperator.EXCLUDES_ALL, List.of(String.valueOf(excludeShelf.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> ids = findMatchingIds(group);
|
||||||
|
assertThat(ids).containsExactlyInAnyOrder(shouldAppear.getId(), shouldAppear2.getId());
|
||||||
|
assertThat(ids).doesNotContain(
|
||||||
|
shouldNotAppear.getId(),
|
||||||
|
excludeOnly.getId(),
|
||||||
|
excludeAndExtra.getId(),
|
||||||
|
noShelf.getId()
|
||||||
|
);
|
||||||
|
|
||||||
|
List<Long> pagedIds = findMatchingIdsPaged(group, 50);
|
||||||
|
assertThat(pagedIds).hasSize(2);
|
||||||
|
assertThat(pagedIds).containsExactlyInAnyOrder(shouldAppear.getId(), shouldAppear2.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void stressTest_manyBooksManyShelves_correctCountWithPagination() {
|
||||||
|
ShelfEntity s1 = ShelfEntity.builder().user(user).name("Stress 1").build();
|
||||||
|
ShelfEntity s2 = ShelfEntity.builder().user(user).name("Stress 2").build();
|
||||||
|
ShelfEntity s3 = ShelfEntity.builder().user(user).name("Stress 3").build();
|
||||||
|
ShelfEntity s4 = ShelfEntity.builder().user(user).name("Stress 4").build();
|
||||||
|
ShelfEntity s5 = ShelfEntity.builder().user(user).name("Stress 5").build();
|
||||||
|
em.persist(s1);
|
||||||
|
em.persist(s2);
|
||||||
|
em.persist(s3);
|
||||||
|
em.persist(s4);
|
||||||
|
em.persist(s5);
|
||||||
|
|
||||||
|
for (int i = 0; i < 50; i++) {
|
||||||
|
BookEntity book = createBook("Stress Book " + i);
|
||||||
|
book.setShelves(new HashSet<>(Set.of(s1, s2, s3, s4, s5)));
|
||||||
|
em.merge(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
BookEntity outsideBook = createBook("Stress Outside");
|
||||||
|
em.flush();
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
GroupRule group = new GroupRule();
|
||||||
|
group.setJoin(JoinType.OR);
|
||||||
|
group.setRules(List.of(
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(s1.getId()))),
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(s2.getId()))),
|
||||||
|
shelfRule(RuleOperator.EQUALS, List.of(String.valueOf(s3.getId())))
|
||||||
|
));
|
||||||
|
|
||||||
|
List<Long> pagedIds = findMatchingIdsPaged(group, 50);
|
||||||
|
assertThat(pagedIds).hasSize(50);
|
||||||
|
|
||||||
|
List<Long> allIds = findMatchingIds(group);
|
||||||
|
assertThat(allIds).hasSize(50);
|
||||||
|
assertThat(allIds).doesNotContain(outsideBook.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class SubtitleFieldTests {
|
class SubtitleFieldTests {
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user