Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,8 +69,8 @@ public CompletableFuture<Result<MultipleRecords<T>>> findByIdIndexAndQuery(
public CompletableFuture<Result<MultipleRecords<T>>> 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));
}

Expand All @@ -87,8 +90,8 @@ public CompletableFuture<Result<MultipleRecords<T>>> find(
public CompletableFuture<Result<MultipleRecords<T>>> 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));
}

Expand All @@ -115,6 +118,12 @@ private static List<Result<CqlQuery>> combineQueries(List<Result<CqlQuery>> quer
.toList();
}

private static boolean hasNoValidValues(MultipleCqlIndexValuesCriteria criteria) {
return criteria.getValues().stream()
.filter(Objects::nonNull)
.noneMatch(StringUtils::isNotBlank);
}

private List<Result<CqlQuery>> buildBatchQueriesByIndexName(MultipleCqlIndexValuesCriteria criteria) {
val indexName = criteria.getIndexName();
val indexOperator = criteria.getIndexOperator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,74 @@ void shouldAssumeNoRecordsAreFoundWhenSearchingForNoIds()
verify(queryFinder, times(0)).findByQuery(any(), any());
}

@Test
void shouldAssumeNoRecordsAreFoundWhenSearchingForOnlyNullIds()
throws InterruptedException, ExecutionException, TimeoutException {

final FindWithMultipleCqlIndexValues<JsonObject> fetcher
= new CqlIndexValuesFinder<>(queryFinder);

final Collection<String> nullIds = new ArrayList<>();
nullIds.add(null);
nullIds.add(null);

final CompletableFuture<Result<MultipleRecords<JsonObject>>> futureResult
= fetcher.findByIds(nullIds);

final MultipleRecords<JsonObject> 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<JsonObject> fetcher
= new CqlIndexValuesFinder<>(queryFinder);

final Collection<String> blankIds = new ArrayList<>();
blankIds.add("");
blankIds.add(" ");

final CompletableFuture<Result<MultipleRecords<JsonObject>>> futureResult
= fetcher.findByIds(blankIds);

final MultipleRecords<JsonObject> 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<JsonObject> fetcher
= new CqlIndexValuesFinder<>(queryFinder);

final Collection<String> blankIds = new ArrayList<>();
blankIds.add("");
blankIds.add(null);

final Result<CqlQuery> openStatusQuery = exactMatch("status", "Open");

final CompletableFuture<Result<MultipleRecords<JsonObject>>> futureResult
= fetcher.findByIdIndexAndQuery(blankIds, "itemId", openStatusQuery);

final MultipleRecords<JsonObject> 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<String> generateIds(int size) {
return Stream.generate(UUID::randomUUID)
.map(UUID::toString)
Expand Down
Loading