diff --git a/src/main/java/org/folio/circulation/support/fetching/CqlIndexValuesFinder.java b/src/main/java/org/folio/circulation/support/fetching/CqlIndexValuesFinder.java index 7e4254525b..7c020e27c7 100644 --- a/src/main/java/org/folio/circulation/support/fetching/CqlIndexValuesFinder.java +++ b/src/main/java/org/folio/circulation/support/fetching/CqlIndexValuesFinder.java @@ -15,9 +15,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.circulation.domain.MultipleRecords; @@ -66,8 +69,8 @@ public CompletableFuture>> findByIdIndexAndQuery( public CompletableFuture>> find( MultipleCqlIndexValuesCriteria criteria) { - if (criteria.getValues().isEmpty()) { - log.info("find:: search criteria is empty"); + if (hasNoValidValues(criteria)) { + log.info("find:: search criteria is empty or contains only blank values"); return completedFuture(of(MultipleRecords::empty)); } @@ -87,8 +90,8 @@ public CompletableFuture>> find( public CompletableFuture>> find( MultipleCqlIndexValuesCriteria criteria, int limit) { - if (criteria.getValues().isEmpty()) { - log.info("find:: search criteria is empty"); + if (hasNoValidValues(criteria)) { + log.info("find:: search criteria is empty or contains only blank values"); return completedFuture(of(MultipleRecords::empty)); } @@ -115,6 +118,12 @@ private static List> combineQueries(List> quer .toList(); } + private static boolean hasNoValidValues(MultipleCqlIndexValuesCriteria criteria) { + return criteria.getValues().stream() + .filter(Objects::nonNull) + .noneMatch(StringUtils::isNotBlank); + } + private List> buildBatchQueriesByIndexName(MultipleCqlIndexValuesCriteria criteria) { val indexName = criteria.getIndexName(); val indexOperator = criteria.getIndexOperator(); diff --git a/src/test/java/org/folio/circulation/support/fetching/FindMultipleRecordsByIdTests.java b/src/test/java/org/folio/circulation/support/fetching/FindMultipleRecordsByIdTests.java index 9fb0b1bd3f..5b09a319a6 100644 --- a/src/test/java/org/folio/circulation/support/fetching/FindMultipleRecordsByIdTests.java +++ b/src/test/java/org/folio/circulation/support/fetching/FindMultipleRecordsByIdTests.java @@ -135,6 +135,74 @@ void shouldAssumeNoRecordsAreFoundWhenSearchingForNoIds() verify(queryFinder, times(0)).findByQuery(any(), any()); } + @Test + void shouldAssumeNoRecordsAreFoundWhenSearchingForOnlyNullIds() + throws InterruptedException, ExecutionException, TimeoutException { + + final FindWithMultipleCqlIndexValues fetcher + = new CqlIndexValuesFinder<>(queryFinder); + + final Collection nullIds = new ArrayList<>(); + nullIds.add(null); + nullIds.add(null); + + final CompletableFuture>> futureResult + = fetcher.findByIds(nullIds); + + final MultipleRecords result = getFutureResultValue(futureResult); + + assertThat("Should assume no records are found when all ids are null", + result.isEmpty(), is(true)); + + verify(queryFinder, times(0)).findByQuery(any(), any()); + } + + @Test + void shouldAssumeNoRecordsAreFoundWhenSearchingForOnlyBlankIds() + throws InterruptedException, ExecutionException, TimeoutException { + + final FindWithMultipleCqlIndexValues fetcher + = new CqlIndexValuesFinder<>(queryFinder); + + final Collection blankIds = new ArrayList<>(); + blankIds.add(""); + blankIds.add(" "); + + final CompletableFuture>> futureResult + = fetcher.findByIds(blankIds); + + final MultipleRecords result = getFutureResultValue(futureResult); + + assertThat("Should assume no records are found when all ids are blank", + result.isEmpty(), is(true)); + + verify(queryFinder, times(0)).findByQuery(any(), any()); + } + + @Test + void shouldAssumeNoRecordsAreFoundWhenSearchingForBlankItemIds() + throws InterruptedException, ExecutionException, TimeoutException { + + final FindWithMultipleCqlIndexValues fetcher + = new CqlIndexValuesFinder<>(queryFinder); + + final Collection blankIds = new ArrayList<>(); + blankIds.add(""); + blankIds.add(null); + + final Result openStatusQuery = exactMatch("status", "Open"); + + final CompletableFuture>> futureResult + = fetcher.findByIdIndexAndQuery(blankIds, "itemId", openStatusQuery); + + final MultipleRecords result = getFutureResultValue(futureResult); + + assertThat("Should assume no records are found when all item ids are blank or null", + result.isEmpty(), is(true)); + + verify(queryFinder, times(0)).findByQuery(any(), any()); + } + private Collection generateIds(int size) { return Stream.generate(UUID::randomUUID) .map(UUID::toString)