From 6d7a7f1dc82cf572cf75e2ed4c2c479d1a57ca8e Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Mon, 16 Sep 2024 12:19:32 +0400 Subject: [PATCH] [MODORDERS-1174] Optimize method calls --- src/main/java/org/folio/helper/BindHelper.java | 2 +- .../folio/service/pieces/PieceStorageService.java | 15 +++++++++------ .../service/pieces/PieceStorageServiceTest.java | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/folio/helper/BindHelper.java b/src/main/java/org/folio/helper/BindHelper.java index 14b2076ff..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.getAllPieces(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/service/pieces/PieceStorageService.java b/src/main/java/org/folio/service/pieces/PieceStorageService.java index b815d337d..fe5b9d101 100644 --- a/src/main/java/org/folio/service/pieces/PieceStorageService.java +++ b/src/main/java/org/folio/service/pieces/PieceStorageService.java @@ -118,16 +118,19 @@ public Future> getPiecesByHoldingId(String holdingId, RequestContext return Future.succeededFuture(Collections.emptyList()); } + public Future getPieces(int limit, int offset, String query, RequestContext requestContext) { + return getAllPieces(limit, offset, query, requestContext) + .compose(piecesCollection -> filterPiecesByUserTenantsIfNecessary(piecesCollection.getPieces(), requestContext) + .map(piecesCollection::withPieces)); + } + public Future getAllPieces(String query, RequestContext requestContext) { - var requestEntry = new RequestEntry(PIECE_STORAGE_ENDPOINT).withQuery(query).withOffset(0).withLimit(Integer.MAX_VALUE); - return restClient.get(requestEntry, PieceCollection.class, requestContext); + return getAllPieces(Integer.MAX_VALUE, 0, query, requestContext); } - public Future getPieces(int limit, int offset, String query, RequestContext 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) - .compose(piecesCollection -> filterPiecesByUserTenantsIfNecessary(piecesCollection.getPieces(), requestContext) - .map(piecesCollection::withPieces)); + return restClient.get(requestEntry, PieceCollection.class, requestContext); } private Future> filterPiecesByUserTenantsIfNecessary(List pieces, RequestContext requestContext) { diff --git a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java index f9e3673a5..dfe81832f 100644 --- a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java +++ b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java @@ -156,7 +156,7 @@ void testGetPiecesFilterByUserTenants(VertxTestContext vertxTestContext) { 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.getAllPieces(null, requestContext); + 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)); @@ -166,7 +166,7 @@ void testGetPiecesFilterByUserTenants(VertxTestContext vertxTestContext) { .onComplete(f -> { var result = f.result(); assertThat(result).isNotNull(); - assertThat(result.getTotalRecords()).isEqualTo(2); + assertThat(result.getTotalRecords()).isEqualTo(3); assertThat(result.getPieces()).hasSize(2); vertxTestContext.completeNow(); }); @@ -179,7 +179,7 @@ void testGetPiecesFilterByUserTenantsNonECS(VertxTestContext vertxTestContext) { 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.getAllPieces(null, requestContext); + 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));