From 2bbc4972259255e049f2f490ca538f3c237b7967 Mon Sep 17 00:00:00 2001 From: Yusuf Murodov <160738549+yusuf-murodov-epam@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:10:27 +0500 Subject: [PATCH] MSEARCH-855: Update consortium library, campus, institution endpoints (#682) --- NEWS.md | 3 +- descriptors/ModuleDescriptor-template.json | 2 +- .../SearchConsortiumController.java | 9 ++-- .../ConsortiumCampusRepository.java | 20 +++++++-- .../ConsortiumInstitutionRepository.java | 20 +++++++-- .../ConsortiumLibraryRepository.java | 20 +++++++-- .../consortium/ConsortiumCampusService.java | 6 ++- .../ConsortiumInstitutionService.java | 6 ++- .../consortium/ConsortiumLibraryService.java | 6 ++- .../parameters/id-query-param.yaml | 6 +++ .../search-consortium-campuses.yaml | 1 + .../search-consortium-institutions.yaml | 1 + .../search-consortium-libraries.yaml | 1 + .../schemas/entity/consortiumCampus.yaml | 3 ++ .../schemas/entity/consortiumInstitution.yaml | 3 ++ .../schemas/entity/consortiumLibrary.yaml | 3 ++ .../ConsortiumSearchCampusesIT.java | 23 +++++++++- .../ConsortiumSearchInstitutionsIT.java | 23 +++++++++- .../ConsortiumSearchLibrariesIT.java | 23 +++++++++- .../ConsortiumCampusRepositoryTest.java | 43 +++++++++++++++++-- .../ConsortiumInstitutionRepositoryTest.java | 43 +++++++++++++++++-- .../ConsortiumLibraryRepositoryTest.java | 42 ++++++++++++++++-- .../ConsortiumCampusServiceTest.java | 9 ++-- .../ConsortiumInstitutionServiceTest.java | 9 ++-- .../ConsortiumLibraryServiceTest.java | 9 ++-- 25 files changed, 287 insertions(+), 47 deletions(-) create mode 100644 src/main/resources/swagger.api/parameters/id-query-param.yaml diff --git a/NEWS.md b/NEWS.md index 93e8fdb56..debaf9836 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,7 +8,7 @@ ### New APIs versions * Provides `indices v0.8` * Provides `search v1.3` -* Provides `consortium-search v2.0` +* Provides `consortium-search v2.1` * Provides `browse v1.4` * Requires `locations v3.0` * Requires `instance-storage v10.3 or v11.0` @@ -48,6 +48,7 @@ * Modify usage of shelving order for call number browse ([MSEARCH-831](https://folio-org.atlassian.net/browse/MSEARCH-831)) * Implement new re-index flow for instance records ([MSEARCH-793](https://folio-org.atlassian.net/issues/MSEARCH-793), [MSEARCH-794](https://folio-org.atlassian.net/issues/MSEARCH-794), [MSEARCH-796](https://folio-org.atlassian.net/issues/MSEARCH-796), [MSEARCH-797](https://folio-org.atlassian.net/issues/MSEARCH-797), [MSEARCH-798](https://folio-org.atlassian.net/issues/MSEARCH-798), [MSEARCH-799](https://folio-org.atlassian.net/issues/MSEARCH-799), [MSEARCH-800](https://folio-org.atlassian.net/issues/MSEARCH-800), [MSEARCH-801](https://folio-org.atlassian.net/issues/MSEARCH-801), [MSEARCH-802](https://folio-org.atlassian.net/issues/MSEARCH-802)) * Implement Linked Data HUB index and search API ([MSEARCH-844](https://folio-org.atlassian.net/browse/MSEARCH-844)) +* Extend consortium library, campus, institution API with id param ([MSEARCH-855](https://folio-org.atlassian.net/browse/MSEARCH-855)) ### Bug fixes * Do not delete kafka topics if collection topic is enabled ([MSEARCH-725](https://folio-org.atlassian.net/browse/MSEARCH-725)) diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index 5520175a6..5acfe5540 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -218,7 +218,7 @@ }, { "id": "consortium-search", - "version": "2.0", + "version": "2.1", "handlers": [ { "methods": [ diff --git a/src/main/java/org/folio/search/controller/SearchConsortiumController.java b/src/main/java/org/folio/search/controller/SearchConsortiumController.java index 585d99374..6c624b0eb 100644 --- a/src/main/java/org/folio/search/controller/SearchConsortiumController.java +++ b/src/main/java/org/folio/search/controller/SearchConsortiumController.java @@ -107,11 +107,12 @@ public ResponseEntity getConsortiumLocations(Strin @Override public ResponseEntity getConsortiumCampuses(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var result = campusService.fetchCampuses(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var result = campusService.fetchCampuses(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder); return ResponseEntity.ok(new ConsortiumCampusCollection() @@ -122,11 +123,12 @@ public ResponseEntity getConsortiumCampuses(String t @Override public ResponseEntity getConsortiumLibraries(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var result = libraryService.fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var result = libraryService.fetchLibraries(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder); return ResponseEntity.ok(new ConsortiumLibraryCollection() @@ -137,11 +139,12 @@ public ResponseEntity getConsortiumLibraries(String @Override public ResponseEntity getConsortiumInstitutions(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var result = institutionService.fetchInstitutions(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var result = institutionService.fetchInstitutions(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder); return ResponseEntity.ok(new ConsortiumInstitutionCollection() diff --git a/src/main/java/org/folio/search/repository/ConsortiumCampusRepository.java b/src/main/java/org/folio/search/repository/ConsortiumCampusRepository.java index a30ce9cce..9407eb238 100644 --- a/src/main/java/org/folio/search/repository/ConsortiumCampusRepository.java +++ b/src/main/java/org/folio/search/repository/ConsortiumCampusRepository.java @@ -1,6 +1,7 @@ package org.folio.search.repository; import static org.folio.search.model.types.ResourceType.CAMPUS; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.SearchUtils.performExceptionalOperation; import static org.opensearch.search.sort.SortOrder.ASC; @@ -36,27 +37,38 @@ public class ConsortiumCampusRepository { public SearchResult fetchCampuses(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var sourceBuilder = getSearchSourceBuilder(tenantId, limit, offset, sortBy, sortOrder); + var sourceBuilder = getSearchSourceBuilder(tenantId, id, limit, offset, sortBy, sortOrder); var response = search(sourceBuilder, tenantHeader); return documentConverter.convertToSearchResult(response, ConsortiumCampus.class); } @NotNull private static SearchSourceBuilder getSearchSourceBuilder(String tenantId, + String campusId, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { var sourceBuilder = new SearchSourceBuilder(); + var boolQuery = QueryBuilders.boolQuery(); + Optional.ofNullable(tenantId) - .ifPresent(id -> sourceBuilder - .query(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id)))); + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id))); + + Optional.ofNullable(campusId) + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(ID_FIELD, id))); + + if (boolQuery.hasClauses()) { + sourceBuilder.query(boolQuery); + } return sourceBuilder .from(offset) diff --git a/src/main/java/org/folio/search/repository/ConsortiumInstitutionRepository.java b/src/main/java/org/folio/search/repository/ConsortiumInstitutionRepository.java index 86330d8f4..46e19810c 100644 --- a/src/main/java/org/folio/search/repository/ConsortiumInstitutionRepository.java +++ b/src/main/java/org/folio/search/repository/ConsortiumInstitutionRepository.java @@ -1,6 +1,7 @@ package org.folio.search.repository; import static org.folio.search.model.types.ResourceType.INSTITUTION; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.SearchUtils.performExceptionalOperation; import static org.opensearch.search.sort.SortOrder.ASC; @@ -36,27 +37,38 @@ public class ConsortiumInstitutionRepository { public SearchResult fetchInstitutions(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var sourceBuilder = getSearchSourceBuilder(tenantId, limit, offset, sortBy, sortOrder); + var sourceBuilder = getSearchSourceBuilder(tenantId, id, limit, offset, sortBy, sortOrder); var response = search(sourceBuilder, tenantHeader); return documentConverter.convertToSearchResult(response, ConsortiumInstitution.class); } @NotNull private static SearchSourceBuilder getSearchSourceBuilder(String tenantId, + String institutionId, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { var sourceBuilder = new SearchSourceBuilder(); + var boolQuery = QueryBuilders.boolQuery(); + Optional.ofNullable(tenantId) - .ifPresent(id -> sourceBuilder - .query(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id)))); + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id))); + + Optional.ofNullable(institutionId) + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(ID_FIELD, id))); + + if (boolQuery.hasClauses()) { + sourceBuilder.query(boolQuery); + } return sourceBuilder .from(offset) diff --git a/src/main/java/org/folio/search/repository/ConsortiumLibraryRepository.java b/src/main/java/org/folio/search/repository/ConsortiumLibraryRepository.java index 9a294e985..f11601947 100644 --- a/src/main/java/org/folio/search/repository/ConsortiumLibraryRepository.java +++ b/src/main/java/org/folio/search/repository/ConsortiumLibraryRepository.java @@ -1,6 +1,7 @@ package org.folio.search.repository; import static org.folio.search.model.types.ResourceType.LIBRARY; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.SearchUtils.performExceptionalOperation; import static org.opensearch.search.sort.SortOrder.ASC; @@ -36,27 +37,38 @@ public class ConsortiumLibraryRepository { public SearchResult fetchLibraries(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - var sourceBuilder = getSearchSourceBuilder(tenantId, limit, offset, sortBy, sortOrder); + var sourceBuilder = getSearchSourceBuilder(tenantId, id, limit, offset, sortBy, sortOrder); var response = search(sourceBuilder, tenantHeader); return documentConverter.convertToSearchResult(response, ConsortiumLibrary.class); } @NotNull private static SearchSourceBuilder getSearchSourceBuilder(String tenantId, + String libraryId, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { var sourceBuilder = new SearchSourceBuilder(); + var boolQuery = QueryBuilders.boolQuery(); + Optional.ofNullable(tenantId) - .ifPresent(id -> sourceBuilder - .query(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id)))); + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(TENANT_ID_FIELD_NAME, id))); + + Optional.ofNullable(libraryId) + .ifPresent(id -> boolQuery + .filter(QueryBuilders.termQuery(ID_FIELD, id))); + + if (boolQuery.hasClauses()) { + sourceBuilder.query(boolQuery); + } return sourceBuilder .from(offset) diff --git a/src/main/java/org/folio/search/service/consortium/ConsortiumCampusService.java b/src/main/java/org/folio/search/service/consortium/ConsortiumCampusService.java index 3fa1a6151..a7fd6b695 100644 --- a/src/main/java/org/folio/search/service/consortium/ConsortiumCampusService.java +++ b/src/main/java/org/folio/search/service/consortium/ConsortiumCampusService.java @@ -20,20 +20,22 @@ public class ConsortiumCampusService { public SearchResult fetchCampuses(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - log.info("fetching consortium campuses for tenant: {}, tenantId: {}, sortBy: {}", + log.info("fetching consortium campuses for tenant: {}, tenantId: {}, id: {}, sortBy: {}", tenantHeader, tenantId, + id, sortBy); validateSortByValue(sortBy); return executor.execute( tenantHeader, - () -> repository.fetchCampuses(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)); + () -> repository.fetchCampuses(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder)); } private void validateSortByValue(String sortBy) { diff --git a/src/main/java/org/folio/search/service/consortium/ConsortiumInstitutionService.java b/src/main/java/org/folio/search/service/consortium/ConsortiumInstitutionService.java index f9811a177..8eb1fdf17 100644 --- a/src/main/java/org/folio/search/service/consortium/ConsortiumInstitutionService.java +++ b/src/main/java/org/folio/search/service/consortium/ConsortiumInstitutionService.java @@ -20,20 +20,22 @@ public class ConsortiumInstitutionService { public SearchResult fetchInstitutions(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - log.info("fetching consortium institution for tenant: {}, tenantId: {}, sortBy: {}", + log.info("fetching consortium institution for tenant: {}, tenantId: {}, id: {}, sortBy: {}", tenantHeader, tenantId, + id, sortBy); validateSortByValue(sortBy); return executor.execute( tenantHeader, - () -> repository.fetchInstitutions(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)); + () -> repository.fetchInstitutions(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder)); } private void validateSortByValue(String sortBy) { diff --git a/src/main/java/org/folio/search/service/consortium/ConsortiumLibraryService.java b/src/main/java/org/folio/search/service/consortium/ConsortiumLibraryService.java index 9135afbf7..7edea2d91 100644 --- a/src/main/java/org/folio/search/service/consortium/ConsortiumLibraryService.java +++ b/src/main/java/org/folio/search/service/consortium/ConsortiumLibraryService.java @@ -20,20 +20,22 @@ public class ConsortiumLibraryService { public SearchResult fetchLibraries(String tenantHeader, String tenantId, + String id, Integer limit, Integer offset, String sortBy, SortOrder sortOrder) { - log.info("fetching consortium libraries for tenant: {}, tenantId: {}, sortBy: {}", + log.info("fetching consortium libraries for tenant: {}, tenantId: {}, id: {}, sortBy: {}", tenantHeader, tenantId, + id, sortBy); validateSortByValue(sortBy); return executor.execute( tenantHeader, - () -> repository.fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)); + () -> repository.fetchLibraries(tenantHeader, tenantId, id, limit, offset, sortBy, sortOrder)); } private void validateSortByValue(String sortBy) { diff --git a/src/main/resources/swagger.api/parameters/id-query-param.yaml b/src/main/resources/swagger.api/parameters/id-query-param.yaml new file mode 100644 index 000000000..f06a67d2b --- /dev/null +++ b/src/main/resources/swagger.api/parameters/id-query-param.yaml @@ -0,0 +1,6 @@ +in: query +name: id +description: "ID to filter by" +required: false +schema: + type: string diff --git a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-campuses.yaml b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-campuses.yaml index 4a761c4de..381a10e2e 100644 --- a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-campuses.yaml +++ b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-campuses.yaml @@ -6,6 +6,7 @@ get: - search-consortium parameters: - $ref: '../../parameters/tenant-id-query-param.yaml' + - $ref: '../../parameters/id-query-param.yaml' - $ref: '../../parameters/consortium-location-unit-limit-param.yaml' - $ref: '../../parameters/offset-param.yaml' - $ref: '../../parameters/sort-by-campus-name-param.yaml' diff --git a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-institutions.yaml b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-institutions.yaml index 54f6525e3..45f61e358 100644 --- a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-institutions.yaml +++ b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-institutions.yaml @@ -6,6 +6,7 @@ get: - search-consortium parameters: - $ref: '../../parameters/tenant-id-query-param.yaml' + - $ref: '../../parameters/id-query-param.yaml' - $ref: '../../parameters/consortium-location-unit-limit-param.yaml' - $ref: '../../parameters/offset-param.yaml' - $ref: '../../parameters/sort-by-institution-name-param.yaml' diff --git a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-libraries.yaml b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-libraries.yaml index efb6fa229..fc71a906d 100644 --- a/src/main/resources/swagger.api/paths/search-consortium/search-consortium-libraries.yaml +++ b/src/main/resources/swagger.api/paths/search-consortium/search-consortium-libraries.yaml @@ -6,6 +6,7 @@ get: - search-consortium parameters: - $ref: '../../parameters/tenant-id-query-param.yaml' + - $ref: '../../parameters/id-query-param.yaml' - $ref: '../../parameters/consortium-location-unit-limit-param.yaml' - $ref: '../../parameters/offset-param.yaml' - $ref: '../../parameters/sort-by-library-name-param.yaml' diff --git a/src/main/resources/swagger.api/schemas/entity/consortiumCampus.yaml b/src/main/resources/swagger.api/schemas/entity/consortiumCampus.yaml index 26103e4fe..cabb8290e 100644 --- a/src/main/resources/swagger.api/schemas/entity/consortiumCampus.yaml +++ b/src/main/resources/swagger.api/schemas/entity/consortiumCampus.yaml @@ -9,6 +9,9 @@ properties: name: description: Campus name type: string + code: + description: Campus code + type: string institutionId: description: The UUID of the institution, the first-level location unit, this (shelf) campus belongs to. type: string diff --git a/src/main/resources/swagger.api/schemas/entity/consortiumInstitution.yaml b/src/main/resources/swagger.api/schemas/entity/consortiumInstitution.yaml index 090d224eb..e8afd7881 100644 --- a/src/main/resources/swagger.api/schemas/entity/consortiumInstitution.yaml +++ b/src/main/resources/swagger.api/schemas/entity/consortiumInstitution.yaml @@ -9,5 +9,8 @@ properties: name: description: Institution name type: string + code: + description: Institution code + type: string metadata: $ref: "../dto/common/metadata.yaml" diff --git a/src/main/resources/swagger.api/schemas/entity/consortiumLibrary.yaml b/src/main/resources/swagger.api/schemas/entity/consortiumLibrary.yaml index ae6253168..572184d3e 100644 --- a/src/main/resources/swagger.api/schemas/entity/consortiumLibrary.yaml +++ b/src/main/resources/swagger.api/schemas/entity/consortiumLibrary.yaml @@ -12,5 +12,8 @@ properties: campusId: description: The UUID of the campus, the second-level location unit, this (shelf) library belongs to. type: string + code: + description: Library code + type: string metadata: $ref: "../dto/common/metadata.yaml" diff --git a/src/test/java/org/folio/search/controller/ConsortiumSearchCampusesIT.java b/src/test/java/org/folio/search/controller/ConsortiumSearchCampusesIT.java index f6decb3e9..399db5d08 100644 --- a/src/test/java/org/folio/search/controller/ConsortiumSearchCampusesIT.java +++ b/src/test/java/org/folio/search/controller/ConsortiumSearchCampusesIT.java @@ -79,7 +79,7 @@ void doGetConsortiumCampuses_returns200AndRecords() { } @Test - void doGetConsortiumCampuses_returns200AndRecords_withAllQueryParams() { + void doGetConsortiumCampuses_returns200AndRecords_withTenantAndSortQueryParams() { List> queryParams = List.of( pair("tenantId", "consortium"), pair("limit", "5"), @@ -99,6 +99,27 @@ void doGetConsortiumCampuses_returns200AndRecords_withAllQueryParams() { assertThat(actual.getCampuses().get(1).getName()).isEqualTo("My campus 2"); } + @Test + void doGetConsortiumCampuses_returns200AndRecords_withAllQueryParams() { + List> queryParams = List.of( + pair("tenantId", "consortium"), + pair("id", "83891666-dcb6-4cd7-ad3a-f4b305abfe21"), + pair("limit", "5"), + pair("offset", "0"), + pair("sortBy", "name"), + pair("sortOrder", "asc") + ); + + var result = doGet(consortiumCampusesSearchPath(queryParams), CENTRAL_TENANT_ID); + var actual = parseResponse(result, ConsortiumCampusCollection.class); + + assertThat(actual.getCampuses()).hasSize(1); + assertThat(actual.getTotalRecords()).isEqualTo(1); + assertThat(actual.getCampuses().get(0).getTenantId()).isEqualTo(CENTRAL_TENANT_ID); + assertThat(actual.getCampuses().get(0).getName()).isEqualTo("My campus 1"); + assertThat(actual.getCampuses().get(0).getCode()).isEqualTo("MC1"); + } + private static void saveCampusRecords() { getCampusesSampleAsMap().stream() .flatMap(campus -> Stream.of( diff --git a/src/test/java/org/folio/search/controller/ConsortiumSearchInstitutionsIT.java b/src/test/java/org/folio/search/controller/ConsortiumSearchInstitutionsIT.java index 42a09a2fd..702610e31 100644 --- a/src/test/java/org/folio/search/controller/ConsortiumSearchInstitutionsIT.java +++ b/src/test/java/org/folio/search/controller/ConsortiumSearchInstitutionsIT.java @@ -78,7 +78,7 @@ void doGetConsortiumInstitutions_returns200AndRecords() { } @Test - void doGetConsortiumInstitutions_returns200AndRecords_withAllQueryParams() { + void doGetConsortiumInstitutions_returns200AndRecords_withTenantAndSortQueryParams() { List> queryParams = List.of( pair("tenantId", "consortium"), pair("limit", "5"), @@ -98,6 +98,27 @@ void doGetConsortiumInstitutions_returns200AndRecords_withAllQueryParams() { assertThat(actual.getInstitutions().get(1).getName()).isEqualTo("My institution 2"); } + @Test + void doGetConsortiumInstitutions_returns200AndRecords_withAllQueryParams() { + List> queryParams = List.of( + pair("tenantId", "consortium"), + pair("id", "52e92db5-34f4-4741-af40-e23c4d9d3bf1"), + pair("limit", "5"), + pair("offset", "0"), + pair("sortBy", "name"), + pair("sortOrder", "asc") + ); + + var result = doGet(consortiumInstitutionsSearchPath(queryParams), CENTRAL_TENANT_ID); + var actual = parseResponse(result, ConsortiumInstitutionCollection.class); + + assertThat(actual.getInstitutions()).hasSize(1); + assertThat(actual.getTotalRecords()).isEqualTo(1); + assertThat(actual.getInstitutions().get(0).getTenantId()).isEqualTo(CENTRAL_TENANT_ID); + assertThat(actual.getInstitutions().get(0).getName()).isEqualTo("My institution 1"); + assertThat(actual.getInstitutions().get(0).getCode()).isEqualTo("MI1"); + } + private static void saveInstitutionRecords() { getInstitutionsSampleAsMap().stream() .flatMap(institution -> Stream.of( diff --git a/src/test/java/org/folio/search/controller/ConsortiumSearchLibrariesIT.java b/src/test/java/org/folio/search/controller/ConsortiumSearchLibrariesIT.java index 30e9565b9..19c9951ec 100644 --- a/src/test/java/org/folio/search/controller/ConsortiumSearchLibrariesIT.java +++ b/src/test/java/org/folio/search/controller/ConsortiumSearchLibrariesIT.java @@ -79,7 +79,7 @@ void doGetConsortiumLibraries_returns200AndRecords() { } @Test - void doGetConsortiumLibraries_returns200AndRecords_withAllQueryParams() { + void doGetConsortiumLibraries_returns200AndRecords_withTenantAndSortQueryParams() { List> queryParams = List.of( pair("tenantId", "consortium"), pair("limit", "5"), @@ -99,6 +99,27 @@ void doGetConsortiumLibraries_returns200AndRecords_withAllQueryParams() { assertThat(actual.getLibraries().get(1).getName()).isEqualTo("My library 2"); } + @Test + void doGetConsortiumLibraries_returns200AndRecords_withAllQueryParams() { + List> queryParams = List.of( + pair("tenantId", "consortium"), + pair("id", "83891666-dcb6-4cd7-ad3a-f4b305abfe21"), + pair("limit", "5"), + pair("offset", "0"), + pair("sortBy", "name"), + pair("sortOrder", "asc") + ); + + var result = doGet(consortiumLibrariesSearchPath(queryParams), CENTRAL_TENANT_ID); + var actual = parseResponse(result, ConsortiumLibraryCollection.class); + + assertThat(actual.getLibraries()).hasSize(1); + assertThat(actual.getTotalRecords()).isEqualTo(1); + assertThat(actual.getLibraries().get(0).getTenantId()).isEqualTo(CENTRAL_TENANT_ID); + assertThat(actual.getLibraries().get(0).getName()).isEqualTo("My library 1"); + assertThat(actual.getLibraries().get(0).getCode()).isEqualTo("ML1"); + } + private static void saveLibraryRecords() { getLibrariesSampleAsMap().stream() .flatMap(library -> Stream.of( diff --git a/src/test/java/org/folio/search/repository/ConsortiumCampusRepositoryTest.java b/src/test/java/org/folio/search/repository/ConsortiumCampusRepositoryTest.java index d07bf1e5f..6d4181aa9 100644 --- a/src/test/java/org/folio/search/repository/ConsortiumCampusRepositoryTest.java +++ b/src/test/java/org/folio/search/repository/ConsortiumCampusRepositoryTest.java @@ -2,9 +2,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.folio.search.model.types.ResourceType.CAMPUS; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.TestConstants.INDEX_NAME; import static org.folio.search.utils.TestConstants.MEMBER_TENANT_ID; +import static org.folio.search.utils.TestConstants.RESOURCE_ID; import static org.folio.search.utils.TestConstants.TENANT_ID; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; @@ -67,7 +69,7 @@ void fetchCampuses_positive() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumCampus.class)).thenReturn(searchResult); - var actual = repository.fetchCampuses(TENANT_ID, null, limit, offset, sortBy, null); + var actual = repository.fetchCampuses(TENANT_ID, null, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -99,7 +101,7 @@ void fetchCampuses_positive_withTenantFilter() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumCampus.class)).thenReturn(searchResult); - var actual = repository.fetchCampuses(TENANT_ID, MEMBER_TENANT_ID, limit, offset, sortBy, null); + var actual = repository.fetchCampuses(TENANT_ID, MEMBER_TENANT_ID, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -123,6 +125,41 @@ void fetchCampuses_positive_withTenantFilter() throws IOException { }); } + @Test + void fetchCampuses_positive_withIdFilter() throws IOException { + var limit = 123; + var offset = 321; + var sortBy = "test"; + var searchResponse = mock(SearchResponse.class); + var searchResult = Mockito.>mock(); + + when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); + when(documentConverter.convertToSearchResult(searchResponse, ConsortiumCampus.class)).thenReturn(searchResult); + + var actual = repository.fetchCampuses(TENANT_ID, null, RESOURCE_ID, limit, offset, sortBy, null); + + assertThat(actual).isEqualTo(searchResult); + + assertThat(requestCaptor.getValue()) + .matches(request -> request.indices().length == 1 && request.indices()[0].equals(INDEX_NAME)) + .satisfies(request -> { + var source = request.source(); + assertThat(source.size()).isEqualTo(limit); + assertThat(source.from()).isEqualTo(offset); + assertThat(source.sorts()).hasSize(1); + assertThat(source.sorts().get(0)).isInstanceOf(FieldSortBuilder.class); + + var sort = (FieldSortBuilder) source.sorts().get(0); + assertThat(sort.getFieldName()).isEqualTo(sortBy); + assertThat(sort.order()).isEqualTo(SortOrder.ASC); + assertThat(source.query()).isInstanceOf(BoolQueryBuilder.class); + + var query = (BoolQueryBuilder) source.query(); + assertThat(query.filter()) + .isEqualTo(List.of(QueryBuilders.termQuery(ID_FIELD, RESOURCE_ID))); + }); + } + @Test void fetchCampuses_positive_sortDesc() throws IOException { var limit = 123; @@ -134,7 +171,7 @@ void fetchCampuses_positive_sortDesc() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumCampus.class)).thenReturn(searchResult); - var actual = repository.fetchCampuses(TENANT_ID, null, limit, offset, sortBy, + var actual = repository.fetchCampuses(TENANT_ID, null, null, limit, offset, sortBy, org.folio.search.domain.dto.SortOrder.DESC); assertThat(actual).isEqualTo(searchResult); diff --git a/src/test/java/org/folio/search/repository/ConsortiumInstitutionRepositoryTest.java b/src/test/java/org/folio/search/repository/ConsortiumInstitutionRepositoryTest.java index 8ac92f64d..042d86bed 100644 --- a/src/test/java/org/folio/search/repository/ConsortiumInstitutionRepositoryTest.java +++ b/src/test/java/org/folio/search/repository/ConsortiumInstitutionRepositoryTest.java @@ -2,9 +2,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.folio.search.model.types.ResourceType.INSTITUTION; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.TestConstants.INDEX_NAME; import static org.folio.search.utils.TestConstants.MEMBER_TENANT_ID; +import static org.folio.search.utils.TestConstants.RESOURCE_ID; import static org.folio.search.utils.TestConstants.TENANT_ID; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; @@ -67,7 +69,7 @@ void fetchInstitutions_positive() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumInstitution.class)).thenReturn(searchResult); - var actual = repository.fetchInstitutions(TENANT_ID, null, limit, offset, sortBy, null); + var actual = repository.fetchInstitutions(TENANT_ID, null, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -99,7 +101,7 @@ void fetchInstitutions_positive_withTenantFilter() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumInstitution.class)).thenReturn(searchResult); - var actual = repository.fetchInstitutions(TENANT_ID, MEMBER_TENANT_ID, limit, offset, sortBy, null); + var actual = repository.fetchInstitutions(TENANT_ID, MEMBER_TENANT_ID, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -123,6 +125,41 @@ void fetchInstitutions_positive_withTenantFilter() throws IOException { }); } + @Test + void fetchInstitutions_positive_withIdFilter() throws IOException { + var limit = 123; + var offset = 321; + var sortBy = "test"; + var searchResponse = mock(SearchResponse.class); + var searchResult = Mockito.>mock(); + + when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); + when(documentConverter.convertToSearchResult(searchResponse, ConsortiumInstitution.class)).thenReturn(searchResult); + + var actual = repository.fetchInstitutions(TENANT_ID, null, RESOURCE_ID, limit, offset, sortBy, null); + + assertThat(actual).isEqualTo(searchResult); + + assertThat(requestCaptor.getValue()) + .matches(request -> request.indices().length == 1 && request.indices()[0].equals(INDEX_NAME)) + .satisfies(request -> { + var source = request.source(); + assertThat(source.size()).isEqualTo(limit); + assertThat(source.from()).isEqualTo(offset); + assertThat(source.sorts()).hasSize(1); + assertThat(source.sorts().get(0)).isInstanceOf(FieldSortBuilder.class); + + var sort = (FieldSortBuilder) source.sorts().get(0); + assertThat(sort.getFieldName()).isEqualTo(sortBy); + assertThat(sort.order()).isEqualTo(SortOrder.ASC); + assertThat(source.query()).isInstanceOf(BoolQueryBuilder.class); + + var query = (BoolQueryBuilder) source.query(); + assertThat(query.filter()) + .isEqualTo(List.of(QueryBuilders.termQuery(ID_FIELD, RESOURCE_ID))); + }); + } + @Test void fetchInstitutions_positive_sortDesc() throws IOException { var limit = 123; @@ -134,7 +171,7 @@ void fetchInstitutions_positive_sortDesc() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumInstitution.class)).thenReturn(searchResult); - var actual = repository.fetchInstitutions(TENANT_ID, null, limit, offset, sortBy, + var actual = repository.fetchInstitutions(TENANT_ID, null, null, limit, offset, sortBy, org.folio.search.domain.dto.SortOrder.DESC); assertThat(actual).isEqualTo(searchResult); diff --git a/src/test/java/org/folio/search/repository/ConsortiumLibraryRepositoryTest.java b/src/test/java/org/folio/search/repository/ConsortiumLibraryRepositoryTest.java index 4f97f2834..2365d3616 100644 --- a/src/test/java/org/folio/search/repository/ConsortiumLibraryRepositoryTest.java +++ b/src/test/java/org/folio/search/repository/ConsortiumLibraryRepositoryTest.java @@ -2,9 +2,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.folio.search.model.types.ResourceType.LIBRARY; +import static org.folio.search.utils.SearchUtils.ID_FIELD; import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; import static org.folio.search.utils.TestConstants.INDEX_NAME; import static org.folio.search.utils.TestConstants.MEMBER_TENANT_ID; +import static org.folio.search.utils.TestConstants.RESOURCE_ID; import static org.folio.search.utils.TestConstants.TENANT_ID; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; @@ -67,7 +69,7 @@ void fetchLibraries_positive() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumLibrary.class)).thenReturn(searchResult); - var actual = repository.fetchLibraries(TENANT_ID, null, limit, offset, sortBy, null); + var actual = repository.fetchLibraries(TENANT_ID, null, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -88,6 +90,40 @@ void fetchLibraries_positive() throws IOException { }); } + @Test + void fetchLibraries_positive_withIdFilter() throws IOException { + var limit = 123; + var offset = 321; + var sortBy = "test"; + var searchResponse = mock(SearchResponse.class); + var searchResult = Mockito.>mock(); + + when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); + when(documentConverter.convertToSearchResult(searchResponse, ConsortiumLibrary.class)).thenReturn(searchResult); + + var actual = repository.fetchLibraries(TENANT_ID, null, RESOURCE_ID, limit, offset, sortBy, null); + + assertThat(actual).isEqualTo(searchResult); + + assertThat(requestCaptor.getValue()) + .matches(request -> request.indices().length == 1 && request.indices()[0].equals(INDEX_NAME)) + .satisfies(request -> { + var source = request.source(); + assertThat(source.size()).isEqualTo(limit); + assertThat(source.from()).isEqualTo(offset); + assertThat(source.sorts()).hasSize(1); + assertThat(source.sorts().get(0)).isInstanceOf(FieldSortBuilder.class); + + var sort = (FieldSortBuilder) source.sorts().get(0); + assertThat(sort.getFieldName()).isEqualTo(sortBy); + assertThat(sort.order()).isEqualTo(SortOrder.ASC); + + var query = (BoolQueryBuilder) source.query(); + assertThat(query.filter()) + .isEqualTo(List.of(QueryBuilders.termQuery(ID_FIELD, RESOURCE_ID))); + }); + } + @Test void fetchLibraries_positive_withTenantFilter() throws IOException { var limit = 123; @@ -99,7 +135,7 @@ void fetchLibraries_positive_withTenantFilter() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumLibrary.class)).thenReturn(searchResult); - var actual = repository.fetchLibraries(TENANT_ID, MEMBER_TENANT_ID, limit, offset, sortBy, null); + var actual = repository.fetchLibraries(TENANT_ID, MEMBER_TENANT_ID, null, limit, offset, sortBy, null); assertThat(actual).isEqualTo(searchResult); @@ -134,7 +170,7 @@ void fetchLibraries_positive_sortDesc() throws IOException { when(client.search(requestCaptor.capture(), eq(DEFAULT))).thenReturn(searchResponse); when(documentConverter.convertToSearchResult(searchResponse, ConsortiumLibrary.class)).thenReturn(searchResult); - var actual = repository.fetchLibraries(TENANT_ID, null, limit, offset, sortBy, + var actual = repository.fetchLibraries(TENANT_ID, null, null, limit, offset, sortBy, org.folio.search.domain.dto.SortOrder.DESC); assertThat(actual).isEqualTo(searchResult); diff --git a/src/test/java/org/folio/search/service/consortium/ConsortiumCampusServiceTest.java b/src/test/java/org/folio/search/service/consortium/ConsortiumCampusServiceTest.java index 259ca95ef..8d0766c3f 100644 --- a/src/test/java/org/folio/search/service/consortium/ConsortiumCampusServiceTest.java +++ b/src/test/java/org/folio/search/service/consortium/ConsortiumCampusServiceTest.java @@ -50,20 +50,21 @@ public class ConsortiumCampusServiceTest { void fetchCampuses_ValidSortBy(String sortBy) { var tenantHeader = CONSORTIUM_TENANT; var tenantId = CONSORTIUM_TENANT; + var campusId = ID; var sortOrder = SortOrder.ASC; var limit = 10; var offset = 0; var searchResult = prepareSearchResult(); - when(repository.fetchCampuses(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)) + when(repository.fetchCampuses(tenantHeader, tenantId, campusId, limit, offset, sortBy, sortOrder)) .thenReturn(searchResult); when(executor.execute(eq(tenantId), any(Supplier.class))) .thenAnswer(invocation -> ((Supplier) invocation.getArgument(1)).get()); - var actual = service.fetchCampuses(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var actual = service.fetchCampuses(tenantHeader, tenantId, campusId, limit, offset, sortBy, sortOrder); assertThat(actual).isEqualTo(searchResult); - verify(repository).fetchCampuses(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + verify(repository).fetchCampuses(tenantHeader, tenantId, campusId, limit, offset, sortBy, sortOrder); verify(executor).execute(eq(tenantId), any(Supplier.class)); } @@ -74,7 +75,7 @@ void fetchCampuses_InvalidSortBy() { var offset = 0; Assertions.assertThrows(IllegalArgumentException.class, () -> - service.fetchCampuses(CONSORTIUM_TENANT, CONSORTIUM_TENANT, limit, offset, "invalid", sortOrder) + service.fetchCampuses(CONSORTIUM_TENANT, CONSORTIUM_TENANT, ID, limit, offset, "invalid", sortOrder) ); } diff --git a/src/test/java/org/folio/search/service/consortium/ConsortiumInstitutionServiceTest.java b/src/test/java/org/folio/search/service/consortium/ConsortiumInstitutionServiceTest.java index 3f852a527..cec229bb5 100644 --- a/src/test/java/org/folio/search/service/consortium/ConsortiumInstitutionServiceTest.java +++ b/src/test/java/org/folio/search/service/consortium/ConsortiumInstitutionServiceTest.java @@ -50,20 +50,21 @@ public class ConsortiumInstitutionServiceTest { void fetchInstitutions_ValidSortBy(String sortBy) { var tenantHeader = CONSORTIUM_TENANT; var tenantId = CONSORTIUM_TENANT; + var institutionId = ID; var sortOrder = SortOrder.ASC; var limit = 10; var offset = 0; var searchResult = prepareSearchResult(); - when(repository.fetchInstitutions(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)) + when(repository.fetchInstitutions(tenantHeader, tenantId, institutionId, limit, offset, sortBy, sortOrder)) .thenReturn(searchResult); when(executor.execute(eq(tenantId), any(Supplier.class))) .thenAnswer(invocation -> ((Supplier) invocation.getArgument(1)).get()); - var actual = service.fetchInstitutions(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var actual = service.fetchInstitutions(tenantHeader, tenantId, institutionId, limit, offset, sortBy, sortOrder); assertThat(actual).isEqualTo(searchResult); - verify(repository).fetchInstitutions(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + verify(repository).fetchInstitutions(tenantHeader, tenantId, institutionId, limit, offset, sortBy, sortOrder); verify(executor).execute(eq(tenantId), any(Supplier.class)); } @@ -74,7 +75,7 @@ void fetchLibraries_InvalidSortBy() { var offset = 0; Assertions.assertThrows(IllegalArgumentException.class, () -> - service.fetchInstitutions(CONSORTIUM_TENANT, CONSORTIUM_TENANT, limit, offset, "invalid", sortOrder) + service.fetchInstitutions(CONSORTIUM_TENANT, CONSORTIUM_TENANT, ID, limit, offset, "invalid", sortOrder) ); } diff --git a/src/test/java/org/folio/search/service/consortium/ConsortiumLibraryServiceTest.java b/src/test/java/org/folio/search/service/consortium/ConsortiumLibraryServiceTest.java index f41c45b3c..f5fd3ece4 100644 --- a/src/test/java/org/folio/search/service/consortium/ConsortiumLibraryServiceTest.java +++ b/src/test/java/org/folio/search/service/consortium/ConsortiumLibraryServiceTest.java @@ -50,20 +50,21 @@ public class ConsortiumLibraryServiceTest { void fetchLibraries_ValidSortBy(String sortBy) { var tenantHeader = CONSORTIUM_TENANT; var tenantId = CONSORTIUM_TENANT; + var libraryId = ID; var sortOrder = SortOrder.ASC; var limit = 10; var offset = 0; var searchResult = prepareSearchResult(); - when(repository.fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)) + when(repository.fetchLibraries(tenantHeader, tenantId, libraryId, limit, offset, sortBy, sortOrder)) .thenReturn(searchResult); when(executor.execute(eq(tenantId), any(Supplier.class))) .thenAnswer(invocation -> ((Supplier) invocation.getArgument(1)).get()); - var actual = service.fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + var actual = service.fetchLibraries(tenantHeader, tenantId, libraryId, limit, offset, sortBy, sortOrder); assertThat(actual).isEqualTo(searchResult); - verify(repository).fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); + verify(repository).fetchLibraries(tenantHeader, tenantId, libraryId, limit, offset, sortBy, sortOrder); verify(executor).execute(eq(tenantId), any(Supplier.class)); } @@ -74,7 +75,7 @@ void fetchLibraries_InvalidSortBy() { var offset = 0; Assertions.assertThrows(IllegalArgumentException.class, () -> - service.fetchLibraries(CONSORTIUM_TENANT, CONSORTIUM_TENANT, limit, offset, "invalid", sortOrder) + service.fetchLibraries(CONSORTIUM_TENANT, CONSORTIUM_TENANT, ID, limit, offset, "invalid", sortOrder) ); }