diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index aae1b5b0c..ff2059bdc 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -489,7 +489,9 @@ "pathPattern": "/orders/pieces", "permissionsRequired": ["orders.pieces.collection.get"], "modulePermissions": [ - "orders-storage.pieces.collection.get" + "orders-storage.pieces.collection.get", + "consortia.user-tenants.collection.get", + "user-tenants.collection.get" ] }, { diff --git a/ramls/acq-models b/ramls/acq-models index f28215d2e..c1f931704 160000 --- a/ramls/acq-models +++ b/ramls/acq-models @@ -1 +1 @@ -Subproject commit f28215d2ee2b2582f96f65b91c388f4ac7395f46 +Subproject commit c1f931704fc6d2dedfc671e308b27f56499750a7 diff --git a/src/main/java/org/folio/config/ApplicationConfig.java b/src/main/java/org/folio/config/ApplicationConfig.java index face26fd4..563e2e3c9 100644 --- a/src/main/java/org/folio/config/ApplicationConfig.java +++ b/src/main/java/org/folio/config/ApplicationConfig.java @@ -31,6 +31,7 @@ import org.folio.service.caches.InventoryCache; import org.folio.service.configuration.ConfigurationEntriesService; import org.folio.service.consortium.ConsortiumConfigurationService; +import org.folio.service.consortium.ConsortiumUserTenantsRetriever; import org.folio.service.consortium.SharingInstanceService; import org.folio.service.exchange.ExchangeRateProviderResolver; import org.folio.service.exchange.FinanceExchangeRateService; @@ -521,8 +522,10 @@ PieceChangeReceiptStatusPublisher receiptStatusPublisher() { } @Bean - PieceStorageService pieceStorageService(RestClient restClient) { - return new PieceStorageService(restClient); + PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService, + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever, + RestClient restClient) { + return new PieceStorageService(consortiumConfigurationService, consortiumUserTenantsRetriever, restClient); } @Bean @@ -848,6 +851,11 @@ ConsortiumConfigurationService consortiumConfigurationService(RestClient restCli return new ConsortiumConfigurationService(restClient); } + @Bean + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever(RestClient restClient) { + return new ConsortiumUserTenantsRetriever(restClient); + } + @Bean SharingInstanceService sharingInstanceService(RestClient restClient) { return new SharingInstanceService(restClient); diff --git a/src/main/java/org/folio/helper/BindHelper.java b/src/main/java/org/folio/helper/BindHelper.java index 332da4ad8..24f35f5bf 100644 --- a/src/main/java/org/folio/helper/BindHelper.java +++ b/src/main/java/org/folio/helper/BindHelper.java @@ -93,7 +93,7 @@ private Future removeForbiddenEntities(Piece piece, RequestContext request private Future clearTitleBindItemsIfNeeded(String titleId, String bindItemId, RequestContext requestContext) { String query = String.format("titleId==%s and bindItemId==%s and isBound==true", titleId, bindItemId); - return pieceStorageService.getPieces(0, 0, query, requestContext) + return pieceStorageService.getAllPieces(0, 0, query, requestContext) .compose(pieceCollection -> { var totalRecords = pieceCollection.getTotalRecords(); if (totalRecords != 0) { diff --git a/src/main/java/org/folio/helper/CheckinReceivePiecesHelper.java b/src/main/java/org/folio/helper/CheckinReceivePiecesHelper.java index 40da3a97d..0429f3f9d 100644 --- a/src/main/java/org/folio/helper/CheckinReceivePiecesHelper.java +++ b/src/main/java/org/folio/helper/CheckinReceivePiecesHelper.java @@ -377,7 +377,7 @@ private List getSuccessfullyProcessedPieces(String poLineId, Map> getPiecesByPoLine(String poLineId, RequestContext requestContext) { String query = String.format("poLineId==%s", poLineId); - return pieceStorageService.getPieces(Integer.MAX_VALUE, 0, query, requestContext) + return pieceStorageService.getAllPieces(query, requestContext) .map(PieceCollection::getPieces); } diff --git a/src/main/java/org/folio/orders/utils/RequestContextUtil.java b/src/main/java/org/folio/orders/utils/RequestContextUtil.java index dbd6ac27c..8097f677d 100644 --- a/src/main/java/org/folio/orders/utils/RequestContextUtil.java +++ b/src/main/java/org/folio/orders/utils/RequestContextUtil.java @@ -22,4 +22,8 @@ public static RequestContext createContextWithNewTenantId(RequestContext request return new RequestContext(requestContext.getContext(), modifiedHeaders); } + public static String getUserIdFromContext(RequestContext requestContext) { + return requestContext.getHeaders().get(XOkapiHeaders.USER_ID); + } + } diff --git a/src/main/java/org/folio/orders/utils/ResourcePathResolver.java b/src/main/java/org/folio/orders/utils/ResourcePathResolver.java index f481313a6..376049038 100644 --- a/src/main/java/org/folio/orders/utils/ResourcePathResolver.java +++ b/src/main/java/org/folio/orders/utils/ResourcePathResolver.java @@ -5,6 +5,8 @@ import java.util.Map; import java.util.stream.Collectors; +import static org.folio.rest.RestConstants.PATH_PARAM_PLACE_HOLDER; + public class ResourcePathResolver { private ResourcePathResolver() { @@ -53,6 +55,8 @@ private ResourcePathResolver() { public static final String ROUTING_LISTS = "routingLists"; public static final String ORDER_SETTINGS = "orderSettings"; public static final String USERS = "users"; + public static final String CONSORTIA_USER_TENANTS = "consortia.user-tenants"; + private static final Map SUB_OBJECT_ITEM_APIS; private static final Map SUB_OBJECT_COLLECTION_APIS; @@ -96,6 +100,7 @@ private ResourcePathResolver() { apis.put(EXPORT_HISTORY, "/orders-storage/export-history"); apis.put(TAGS, "/tags"); apis.put(USERS, "/users"); + apis.put(CONSORTIA_USER_TENANTS, "/consortia/" + PATH_PARAM_PLACE_HOLDER + "/user-tenants"); apis.put(ORDER_SETTINGS, "/orders-storage/settings"); apis.put(ROUTING_LISTS, "/orders-storage/routing-lists"); diff --git a/src/main/java/org/folio/service/consortium/ConsortiumUserTenantsRetriever.java b/src/main/java/org/folio/service/consortium/ConsortiumUserTenantsRetriever.java new file mode 100644 index 000000000..292b8c0e4 --- /dev/null +++ b/src/main/java/org/folio/service/consortium/ConsortiumUserTenantsRetriever.java @@ -0,0 +1,49 @@ +package org.folio.service.consortium; + +import io.vertx.core.Future; +import io.vertx.core.json.JsonObject; +import org.folio.rest.core.RestClient; +import org.folio.rest.core.models.RequestContext; +import org.folio.rest.core.models.RequestEntry; + +import java.util.List; +import java.util.stream.IntStream; + +import static org.folio.orders.utils.RequestContextUtil.getUserIdFromContext; +import static org.folio.orders.utils.ResourcePathResolver.CONSORTIA_USER_TENANTS; +import static org.folio.orders.utils.ResourcePathResolver.resourcesPath; +import static org.folio.rest.RestConstants.PATH_PARAM_PLACE_HOLDER; +import static org.folio.service.pieces.util.UserTenantFields.COLLECTION_USER_TENANTS; +import static org.folio.service.pieces.util.UserTenantFields.TENANT_ID; +import static org.folio.service.pieces.util.UserTenantFields.USER_ID; + +public class ConsortiumUserTenantsRetriever { + + private static final String CONSORTIA_USER_TENANTS_ENDPOINT = resourcesPath(CONSORTIA_USER_TENANTS); + + private final RestClient restClient; + + public ConsortiumUserTenantsRetriever(RestClient restClient) { + this.restClient = restClient; + } + + public Future> getUserTenants(String consortiumId, RequestContext requestContext) { + var userId = getUserIdFromContext(requestContext); + var url = CONSORTIA_USER_TENANTS_ENDPOINT.replace(PATH_PARAM_PLACE_HOLDER, consortiumId); + var requestEntry = new RequestEntry(url) + .withOffset(0) + .withLimit(Integer.MAX_VALUE) + .withQueryParameter(USER_ID.getValue(), userId); + return restClient.getAsJsonObject(requestEntry, requestContext) + .map(this::extractTenantIds); + } + + private List extractTenantIds(JsonObject userTenantCollection) { + var userTenants = userTenantCollection.getJsonArray(COLLECTION_USER_TENANTS.getValue()); + return IntStream.range(0, userTenants.size()) + .mapToObj(userTenants::getJsonObject) + .map(userTenant -> userTenant.getString(TENANT_ID.getValue())) + .toList(); + } + +} diff --git a/src/main/java/org/folio/service/orders/HoldingsSummaryService.java b/src/main/java/org/folio/service/orders/HoldingsSummaryService.java index 19b999c95..225e62367 100644 --- a/src/main/java/org/folio/service/orders/HoldingsSummaryService.java +++ b/src/main/java/org/folio/service/orders/HoldingsSummaryService.java @@ -37,7 +37,7 @@ public Future getHoldingsSummary(String holdingId, Req var queryForPiece = String.format("?query=holdingId==%s", holdingId); var queryForLines = String.format("?query=locations=\"holdingId\" : \"%s\"", holdingId); - return pieceStorageService.getPieces(Integer.MAX_VALUE, 0, queryForPiece, requestContext) + return pieceStorageService.getAllPieces(queryForPiece, requestContext) .map(piecesCollection -> { Set lineIds = piecesCollection.getPieces().stream() .map(Piece::getPoLineId) diff --git a/src/main/java/org/folio/service/pieces/PieceStorageService.java b/src/main/java/org/folio/service/pieces/PieceStorageService.java index ba270d6b9..fe5b9d101 100644 --- a/src/main/java/org/folio/service/pieces/PieceStorageService.java +++ b/src/main/java/org/folio/service/pieces/PieceStorageService.java @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.logging.log4j.LogManager; @@ -24,6 +25,8 @@ import org.folio.rest.jaxrs.model.PieceCollection; import io.vertx.core.Future; +import org.folio.service.consortium.ConsortiumUserTenantsRetriever; +import org.folio.service.consortium.ConsortiumConfigurationService; public class PieceStorageService { private static final Logger logger = LogManager.getLogger(PieceStorageService.class); @@ -33,9 +36,15 @@ public class PieceStorageService { private static final String PIECE_STORAGE_ENDPOINT = resourcesPath(PIECES_STORAGE); private static final String PIECE_STORAGE_BY_ID_ENDPOINT = PIECE_STORAGE_ENDPOINT + "/{id}"; + private final ConsortiumConfigurationService consortiumConfigurationService; + private final ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever; private final RestClient restClient; - public PieceStorageService(RestClient restClient) { + public PieceStorageService(ConsortiumConfigurationService consortiumConfigurationService, + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever, + RestClient restClient) { + this.consortiumConfigurationService = consortiumConfigurationService; + this.consortiumUserTenantsRetriever = consortiumUserTenantsRetriever; this.restClient = restClient; } @@ -98,29 +107,53 @@ public Future deletePiecesByIds(List pieceIds, RequestContext requ public Future getExpectedPiecesByLineId(String poLineId, RequestContext requestContext) { String query = String.format(PIECES_BY_POL_ID_AND_STATUS_QUERY, poLineId, Piece.ReceivingStatus.EXPECTED.value()); - return getPieces(Integer.MAX_VALUE, 0, query, requestContext); + return getAllPieces(query, requestContext); } public Future> getPiecesByHoldingId(String holdingId, RequestContext requestContext) { if (holdingId != null) { String query = String.format(PIECES_BY_HOLDING_ID_QUERY, holdingId); - return getPieces(Integer.MAX_VALUE, 0, query, requestContext).map(PieceCollection::getPieces); + return getAllPieces(query, requestContext).map(PieceCollection::getPieces); } return Future.succeededFuture(Collections.emptyList()); } public Future getPieces(int limit, int offset, String query, RequestContext requestContext) { - RequestEntry requestEntry = new RequestEntry(PIECE_STORAGE_ENDPOINT).withQuery(query) - .withOffset(offset) - .withLimit(limit); + return getAllPieces(limit, offset, query, requestContext) + .compose(piecesCollection -> filterPiecesByUserTenantsIfNecessary(piecesCollection.getPieces(), requestContext) + .map(piecesCollection::withPieces)); + } + + public Future getAllPieces(String query, RequestContext requestContext) { + return getAllPieces(Integer.MAX_VALUE, 0, query, requestContext); + } + + public Future getAllPieces(int limit, int offset, String query, RequestContext requestContext) { + var requestEntry = new RequestEntry(PIECE_STORAGE_ENDPOINT).withQuery(query).withOffset(offset).withLimit(limit); return restClient.get(requestEntry, PieceCollection.class, requestContext); } + private Future> filterPiecesByUserTenantsIfNecessary(List pieces, RequestContext requestContext) { + return consortiumConfigurationService.getConsortiumConfiguration(requestContext) + .compose(consortiumConfiguration -> consortiumConfiguration + .map(configuration -> consortiumUserTenantsRetriever.getUserTenants(configuration.consortiumId(), requestContext) + .map(userTenants -> filterPiecesByUserTenants(pieces, userTenants))) + .orElse(Future.succeededFuture(pieces))); + } + + private List filterPiecesByUserTenants(List pieces, List userTenants) { + return pieces.stream() + .filter(piece -> Optional.ofNullable(piece.getReceivingTenantId()) + .map(userTenants::contains) + .orElse(true)) + .toList(); + } + public Future> getPiecesByIds(List pieceIds, RequestContext requestContext) { logger.debug("getPiecesByIds:: start to retrieving pieces by ids: {}", pieceIds); var futures = ofSubLists(new ArrayList<>(pieceIds), MAX_IDS_FOR_GET_RQ_15) .map(HelperUtils::convertIdsToCqlQuery) - .map(query -> getPieces(Integer.MAX_VALUE, 0, query, requestContext)) + .map(query -> getAllPieces(query, requestContext)) .toList(); return collectResultsOnSuccess(futures) .map(lists -> lists.stream() diff --git a/src/main/java/org/folio/service/pieces/util/UserTenantFields.java b/src/main/java/org/folio/service/pieces/util/UserTenantFields.java new file mode 100644 index 000000000..217c1a999 --- /dev/null +++ b/src/main/java/org/folio/service/pieces/util/UserTenantFields.java @@ -0,0 +1,19 @@ +package org.folio.service.pieces.util; + +public enum UserTenantFields { + + USER_ID("userId"), + TENANT_ID("tenantId"), + COLLECTION_USER_TENANTS("userTenants"); + + private final String value; + + UserTenantFields(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + +} diff --git a/src/test/java/org/folio/service/CirculationRequestsRetrieverTest.java b/src/test/java/org/folio/service/CirculationRequestsRetrieverTest.java index 93ccb3034..bb2f71a1d 100644 --- a/src/test/java/org/folio/service/CirculationRequestsRetrieverTest.java +++ b/src/test/java/org/folio/service/CirculationRequestsRetrieverTest.java @@ -11,6 +11,8 @@ import org.folio.rest.core.models.RequestContext; import org.folio.rest.core.models.RequestEntry; import org.folio.rest.jaxrs.model.PieceCollection; +import org.folio.service.consortium.ConsortiumConfigurationService; +import org.folio.service.consortium.ConsortiumUserTenantsRetriever; import org.folio.service.pieces.PieceStorageService; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -235,8 +237,10 @@ public RestClient restClient() { } @Bean - public PieceStorageService pieceStorageService(RestClient restClient) { - return spy(new PieceStorageService(restClient)); + public PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService, + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever, + RestClient restClient) { + return spy(new PieceStorageService(consortiumConfigurationService, consortiumUserTenantsRetriever, restClient)); } @Bean @@ -244,6 +248,16 @@ public CirculationRequestsRetriever circulationRequestsRetriever(PieceStorageSer return new CirculationRequestsRetriever(pieceStorageService, restClient); } + @Bean + ConsortiumConfigurationService consortiumConfigurationService(RestClient restClient) { + return new ConsortiumConfigurationService(restClient); + } + + @Bean + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever(RestClient restClient) { + return new ConsortiumUserTenantsRetriever(restClient); + } + } } diff --git a/src/test/java/org/folio/service/inventory/HoldingsSummaryServiceTest.java b/src/test/java/org/folio/service/inventory/HoldingsSummaryServiceTest.java index dbd65e531..9eef67bbc 100644 --- a/src/test/java/org/folio/service/inventory/HoldingsSummaryServiceTest.java +++ b/src/test/java/org/folio/service/inventory/HoldingsSummaryServiceTest.java @@ -69,7 +69,7 @@ void testGetHoldingSummaryById() throws IOException { when(purchaseOrderLineService.getOrderLines(anyString(), anyInt(), anyInt(), any())) .thenReturn(Future.succeededFuture(polines)); - when(pieceStorageService.getPieces(anyInt(), anyInt(), anyString(), any())) + when(pieceStorageService.getAllPieces(anyString(), any())) .thenReturn(Future.succeededFuture(pieces)); var hs = holdingsSummaryService.getHoldingsSummary(UUID.randomUUID().toString(), requestContext) diff --git a/src/test/java/org/folio/service/orders/lines/update/OrderLineUpdateInstanceHandlerTest.java b/src/test/java/org/folio/service/orders/lines/update/OrderLineUpdateInstanceHandlerTest.java index 41f7d91cc..8fb9947b0 100644 --- a/src/test/java/org/folio/service/orders/lines/update/OrderLineUpdateInstanceHandlerTest.java +++ b/src/test/java/org/folio/service/orders/lines/update/OrderLineUpdateInstanceHandlerTest.java @@ -39,6 +39,7 @@ import org.folio.service.caches.InventoryCache; import org.folio.service.configuration.ConfigurationEntriesService; import org.folio.service.consortium.ConsortiumConfigurationService; +import org.folio.service.consortium.ConsortiumUserTenantsRetriever; import org.folio.service.consortium.SharingInstanceService; import org.folio.service.inventory.InventoryHoldingManager; import org.folio.service.inventory.InventoryItemManager; @@ -221,19 +222,30 @@ static class ContextConfiguration { return new ProtectionService(acquisitionsUnitsService); } - @Bean PieceStorageService pieceStorageService(RestClient restClient) { - return new PieceStorageService(restClient); + @Bean PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService, + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever, + RestClient restClient) { + return new PieceStorageService(consortiumConfigurationService, consortiumUserTenantsRetriever, restClient); } + @Bean InventoryService inventoryService (RestClient restClient) { return new InventoryService(restClient); } + @Bean SharingInstanceService sharingInstanceService (RestClient restClient) { return new SharingInstanceService(restClient); } - @Bean ConsortiumConfigurationService consortiumConfigurationService (RestClient restClient) { + + @Bean + ConsortiumConfigurationService consortiumConfigurationService(RestClient restClient) { return new ConsortiumConfigurationService(restClient); } + @Bean + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever(RestClient restClient) { + return new ConsortiumUserTenantsRetriever(restClient); + } + @Bean PurchaseOrderStorageService purchaseOrderStorageService () { return mock(PurchaseOrderStorageService.class); } diff --git a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java index dc4c3bfb0..dfe81832f 100644 --- a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java +++ b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java @@ -1,6 +1,7 @@ package org.folio.service.pieces; import static io.vertx.core.Future.succeededFuture; +import static org.assertj.core.api.Assertions.assertThat; import static org.folio.TestConfig.autowireDependencies; import static org.folio.TestConfig.clearServiceInteractions; import static org.folio.TestConfig.clearVertxContext; @@ -8,6 +9,8 @@ import static org.folio.TestConfig.getVertx; import static org.folio.TestConfig.initSpringContext; import static org.folio.TestConfig.isVerticleNotDeployed; +import static org.folio.TestUtils.getMockAsJson; +import static org.folio.rest.impl.MockServer.BASE_MOCK_DATA_PATH; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -21,17 +24,21 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import org.folio.ApiTestSuite; +import org.folio.models.consortium.ConsortiumConfiguration; import org.folio.rest.core.RestClient; import org.folio.rest.core.models.RequestContext; import org.folio.rest.core.models.RequestEntry; import org.folio.rest.jaxrs.model.Piece; import org.folio.rest.jaxrs.model.PieceCollection; import org.folio.service.ProtectionService; +import org.folio.service.consortium.ConsortiumConfigurationService; +import org.folio.service.consortium.ConsortiumUserTenantsRetriever; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -52,9 +59,21 @@ @ExtendWith(VertxExtension.class) public class PieceStorageServiceTest { + private static final String USER_TENANTS_PATH = BASE_MOCK_DATA_PATH + "userTenants/"; + private static final String USER_TENANTS_MOCK = "userTenants"; + + private static final String PIECES_PATH = BASE_MOCK_DATA_PATH + "pieces/"; + private static final String PIECES_MOCK = "pieces-for-user-tenants"; + @Autowired PieceStorageService pieceStorageService; + @Autowired + ConsortiumConfigurationService consortiumConfigurationService; + + @Autowired + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever; + @Autowired private RestClient restClientMock; @@ -104,9 +123,10 @@ void testPiecesShouldBeReturnedByQuery(VertxTestContext vertxTestContext) { .withTotalRecords(1); when(restClientMock.get(any(RequestEntry.class), any(), any())).thenReturn(Future.succeededFuture(pieceCollection)); + when(consortiumConfigurationService.getConsortiumConfiguration(any(RequestContext.class))).thenReturn(Future.succeededFuture(Optional.empty())); String expectedQuery = String.format("id==%s", pieceId); - var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, expectedQuery, requestContext); + var future = pieceStorageService.getAllPieces(expectedQuery, requestContext); verify(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext)); vertxTestContext.assertComplete(future) @@ -126,18 +146,84 @@ void testShouldDeleteItems() { verify(pieceStorageService, times(1)).deletePiece(any(String.class), eq(requestContext)); } + @Test + void testGetPiecesFilterByUserTenants(VertxTestContext vertxTestContext) { + var userTenantsMockData = getMockAsJson(USER_TENANTS_PATH, USER_TENANTS_MOCK); + var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class); + var consortiumConfiguration = Optional.of(new ConsortiumConfiguration("tenantId0", UUID.randomUUID().toString())); + + doReturn(Future.succeededFuture(consortiumConfiguration)).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class)); + doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class)); + doReturn(Future.succeededFuture(userTenantsMockData)).when(restClientMock).getAsJsonObject(any(), any(RequestContext.class)); + + var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext); + + verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext)); + verify(restClientMock, times(1)).getAsJsonObject(any(), any(RequestContext.class)); + verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext)); + + vertxTestContext.assertComplete(future) + .onComplete(f -> { + var result = f.result(); + assertThat(result).isNotNull(); + assertThat(result.getTotalRecords()).isEqualTo(3); + assertThat(result.getPieces()).hasSize(2); + vertxTestContext.completeNow(); + }); + } + + @Test + void testGetPiecesFilterByUserTenantsNonECS(VertxTestContext vertxTestContext) { + var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class); + + doReturn(Future.succeededFuture(Optional.empty())).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class)); + doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class)); + + var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext); + + verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext)); + verify(restClientMock, times(0)).getAsJsonObject(any(), any(RequestContext.class)); + verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext)); + + vertxTestContext.assertComplete(future) + .onComplete(f -> { + var result = f.result(); + assertThat(result).isNotNull(); + assertThat(result.getTotalRecords()).isEqualTo(3); + assertThat(result.getPieces()).hasSize(3); + vertxTestContext.completeNow(); + }); + } + private static class ContextConfiguration { - @Bean RestClient restClient() { + @Bean + RestClient restClient() { return mock(RestClient.class); } - @Bean ProtectionService protectionService() { + @Bean + ProtectionService protectionService() { return mock(ProtectionService.class); } - @Bean PieceStorageService pieceStorageService(RestClient restClient, ProtectionService protectionService) { - return spy(new PieceStorageService(restClient)); + @Bean + PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService, + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever, + RestClient restClient) { + return spy(new PieceStorageService(consortiumConfigurationService, consortiumUserTenantsRetriever, restClient)); + } + + @Bean + ConsortiumConfigurationService consortiumConfigurationService(RestClient restClient) { + return spy(new ConsortiumConfigurationService(restClient)); } + + @Bean + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever(RestClient restClient) { + return spy(new ConsortiumUserTenantsRetriever(restClient)); + } + } + } diff --git a/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json b/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json new file mode 100644 index 000000000..00ace7c2d --- /dev/null +++ b/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json @@ -0,0 +1,16 @@ +{ + "pieces": [ + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff04", + "receivingTenantId": "tenantId1" + }, + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff05", + "receivingTenantId": "tenantId2" + }, + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff06" + } + ], + "totalRecords": 3 +} diff --git a/src/test/resources/mockdata/userTenants/userTenants.json b/src/test/resources/mockdata/userTenants/userTenants.json new file mode 100644 index 000000000..bcc9aee04 --- /dev/null +++ b/src/test/resources/mockdata/userTenants/userTenants.json @@ -0,0 +1,13 @@ +{ + "userTenants": [ + { + "id": "05c34b3d-33fa-46fc-8906-4ade19a570fd", + "userId": "440c89e3-7f6c-578a-9ea8-310dad23605e", + "username": "testUser", + "tenantId": "tenantId1", + "tenantName": "Tenant1", + "isPrimary": true + } + ], + "totalRecords": 1 +}