From 5bd43b6f64f68d357b698c07412852977671ccef Mon Sep 17 00:00:00 2001 From: Maksat <144414992+Maksat-Galymzhan@users.noreply.github.com> Date: Fri, 19 Jul 2024 20:12:52 +0500 Subject: [PATCH] CIRC-2117 Return empty result when search doesn't find anything (#1485) * CIRC-2117: return empty if no instance found --- .../infrastructure/storage/SearchRepository.java | 7 ++++--- src/test/java/api/ItemsByInstanceResourceTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/folio/circulation/infrastructure/storage/SearchRepository.java b/src/main/java/org/folio/circulation/infrastructure/storage/SearchRepository.java index a999c2158d..ed441ce461 100644 --- a/src/main/java/org/folio/circulation/infrastructure/storage/SearchRepository.java +++ b/src/main/java/org/folio/circulation/infrastructure/storage/SearchRepository.java @@ -1,6 +1,7 @@ package org.folio.circulation.infrastructure.storage; import static org.folio.circulation.support.StringUtil.urlEncode; +import static org.folio.circulation.support.results.Result.emptyAsync; import static org.folio.circulation.support.results.Result.failed; import static org.folio.circulation.support.results.ResultBinding.flatMapResult; import static org.folio.circulation.support.results.ResultBinding.mapResult; @@ -62,9 +63,9 @@ private Result> mapResponseToInstances(Response private CompletableFuture> updateItemDetails(SearchInstance searchInstance) { log.debug("updateItemDetails:: searchInstance {}", () -> searchInstance); - if (searchInstance == null) { - return CompletableFuture.completedFuture(failed(new BadRequestFailure( - "Search result is empty"))); + if (searchInstance == null || searchInstance.getId() == null) { + log.info("updateItemDetails:: searchInstance is empty"); + return emptyAsync(); } Map> itemsByTenant = searchInstance.getItems() diff --git a/src/test/java/api/ItemsByInstanceResourceTest.java b/src/test/java/api/ItemsByInstanceResourceTest.java index 71d3ce18b6..08d072d432 100644 --- a/src/test/java/api/ItemsByInstanceResourceTest.java +++ b/src/test/java/api/ItemsByInstanceResourceTest.java @@ -4,6 +4,7 @@ import static api.support.APITestContext.setTempTenantId; import static api.support.http.InterfaceUrls.itemsByInstanceUrl; import static api.support.matchers.JsonObjectMatcher.hasJsonPath; +import static org.folio.HttpStatus.HTTP_OK; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.hasItem; @@ -77,6 +78,17 @@ void canGetInstanceById() { hasJsonPath("tenantId", is(TENANT_ID_UNIVERSITY))))); } + @Test + void canGetEmptyResult() { + UUID instanceId = UUID.randomUUID(); + + ResourceClient.forSearchClient().replace(instanceId, new JsonObject()); + Response response = get(String.format("query=(id==%s)", instanceId), HTTP_OK.toInt()); + JsonObject responseJson = response.getJson(); + + assertThat(responseJson.isEmpty(), is(true)); + } + private Response get(String query, int expectedStatusCode) { return restAssuredClient.get(itemsByInstanceUrl(query), expectedStatusCode, "items-by-instance-request");