Skip to content

Commit

Permalink
[MODORDERS-1174] Optimize method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 16, 2024
1 parent 0604721 commit 6d7a7f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/folio/helper/BindHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private Future<Void> removeForbiddenEntities(Piece piece, RequestContext request

private Future<Void> 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) {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/folio/service/pieces/PieceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,19 @@ public Future<List<Piece>> getPiecesByHoldingId(String holdingId, RequestContext
return Future.succeededFuture(Collections.emptyList());
}

public Future<PieceCollection> 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<PieceCollection> 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<PieceCollection> getPieces(int limit, int offset, String query, RequestContext requestContext) {
public Future<PieceCollection> 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<List<Piece>> filterPiecesByUserTenantsIfNecessary(List<Piece> pieces, RequestContext requestContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
});
Expand All @@ -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));
Expand Down

0 comments on commit 6d7a7f1

Please sign in to comment.