Skip to content

Commit

Permalink
[MODORDERS-1174] Fix incorrect url and unnecessary filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 13, 2024
1 parent fd36c89 commit 0604721
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 15 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.getPieces(0, 0, query, requestContext)
return pieceStorageService.getAllPieces(query, requestContext)
.compose(pieceCollection -> {
var totalRecords = pieceCollection.getTotalRecords();
if (totalRecords != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private List<Piece> getSuccessfullyProcessedPieces(String poLineId, Map<String,

private Future<List<Piece>> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -98,7 +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/:id/user-tenants");
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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand All @@ -28,10 +29,10 @@ public ConsortiumUserTenantsRetriever(RestClient restClient) {

public Future<List<String>> getUserTenants(String consortiumId, RequestContext requestContext) {
var userId = getUserIdFromContext(requestContext);
var requestEntry = new RequestEntry(CONSORTIA_USER_TENANTS_ENDPOINT)
var url = CONSORTIA_USER_TENANTS_ENDPOINT.replace(PATH_PARAM_PLACE_HOLDER, consortiumId);
var requestEntry = new RequestEntry(url)
.withOffset(0)
.withLimit(Integer.MAX_VALUE)
.withId(consortiumId)
.withQueryParameter(USER_ID.getValue(), userId);
return restClient.getAsJsonObject(requestEntry, requestContext)
.map(this::extractTenantIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Future<HoldingSummaryCollection> 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<String> lineIds = piecesCollection.getPieces().stream()
.map(Piece::getPoLineId)
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/folio/service/pieces/PieceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,27 @@ public Future<Void> deletePiecesByIds(List<String> pieceIds, RequestContext requ

public Future<PieceCollection> 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<List<Piece>> 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<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);
}

public Future<PieceCollection> getPieces(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(pieces -> new PieceCollection().withPieces(pieces).withTotalRecords(pieces.size()));
.compose(piecesCollection -> filterPiecesByUserTenantsIfNecessary(piecesCollection.getPieces(), requestContext)
.map(piecesCollection::withPieces));
}

private Future<List<Piece>> filterPiecesByUserTenantsIfNecessary(List<Piece> pieces, RequestContext requestContext) {
Expand All @@ -145,7 +150,7 @@ public Future<List<Piece>> getPiecesByIds(List<String> pieceIds, 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void testPiecesShouldBeReturnedByQuery(VertxTestContext vertxTestContext) {
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)
Expand Down 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.getPieces(Integer.MAX_VALUE, 0, null, requestContext);
var future = pieceStorageService.getAllPieces(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 @@ -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.getPieces(Integer.MAX_VALUE, 0, null, requestContext);
var future = pieceStorageService.getAllPieces(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 0604721

Please sign in to comment.