Skip to content

Commit

Permalink
[MODORDERS-1146] Remove itemId from title when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Jul 11, 2024
1 parent f76be71 commit 92f1917
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 16 deletions.
27 changes: 21 additions & 6 deletions src/main/java/org/folio/helper/BindHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,30 @@ public Future<Void> removeBinding(String pieceId, RequestContext requestContext)
logger.debug("removeBinding:: Removing binding for piece: {}", pieceId);
return pieceStorageService.getPieceById(pieceId, requestContext)
.compose(piece -> {
// Populate piecesByLineId to accommodate helper methods
piecesByLineId = Map.of(piece.getPoLineId(), Collections.singletonMap(piece.getId(), null));
// Remove binding for the piece
var bindItemId = piece.getBindItemId();
piece.withBindItemId(null).withIsBound(false);
// Update piece record
return removeForbiddenEntities(requestContext)
return removeForbiddenEntities(piece, requestContext)
.compose(v -> getValidPieces(requestContext))
.compose(piecesGroupedByPoLine -> storeUpdatedPieceRecords(piecesGroupedByPoLine, requestContext))
.mapEmpty();
.compose(piecesGroupedByPoLine -> clearTitleBindItemsIfNeeded(piece.getTitleId(), bindItemId, requestContext));
});
}

private Future<Void> removeForbiddenEntities(Piece piece, RequestContext requestContext) {
// Populate piecesByLineId used by removeForbiddenEntities and parent helper methods
piecesByLineId = Map.of(piece.getPoLineId(), Collections.singletonMap(piece.getId(), null));
return removeForbiddenEntities(requestContext);
}

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)
.compose(pieceCollection -> {
if (pieceCollection.getTotalRecords() != 0) {
return Future.succeededFuture();
}
return titlesService.getTitleById(titleId, requestContext)
.compose(title -> titlesService.saveTitle(title.withBindItemIds(List.of()), requestContext));
});
}

Expand Down
117 changes: 107 additions & 10 deletions src/test/java/org/folio/rest/impl/CheckinReceivingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
import static org.folio.service.inventory.InventoryItemManager.ITEM_DISPLAY_SUMMARY;
import static org.folio.service.inventory.InventoryItemManager.ITEM_ENUMERATION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -1103,7 +1102,6 @@ void testBindPiecesToTitleWithItem() {
assertThat(titleBindItemIds, hasSize(1));
assertThat(titleBindItemIds.get(0), is(newItemId));


var createdHoldings = getCreatedHoldings();
assertThat(createdHoldings, nullValue());

Expand Down Expand Up @@ -1324,51 +1322,150 @@ void testRemovePieceBinding() {
var order = getMinimalContentCompositePurchaseOrder()
.withWorkflowStatus(CompositePurchaseOrder.WorkflowStatus.OPEN);
var poLine = getMinimalContentCompositePoLine(order.getId());
var bindingPiece = getMinimalContentPiece(poLine.getId())
var title = getTitle(poLine);
var bindPiece1 = getMinimalContentPiece(poLine.getId())
.withTitleId(title.getId())
.withHoldingId(holdingId)
.withReceivingStatus(Piece.ReceivingStatus.RECEIVED)
.withFormat(Piece.Format.PHYSICAL);
var bindPiece2 = getMinimalContentPiece(poLine.getId())
.withId(UUID.randomUUID().toString())
.withTitleId(title.getId())
.withHoldingId(holdingId)
.withReceivingStatus(Piece.ReceivingStatus.RECEIVED)
.withFormat(Piece.Format.PHYSICAL);
var bindPieceIds = List.of(bindPiece1.getId(), bindPiece2.getId());

addMockEntry(PURCHASE_ORDER_STORAGE, order);
addMockEntry(PO_LINES_STORAGE, poLine);
addMockEntry(PIECES_STORAGE, bindingPiece);
addMockEntry(TITLES, getTitle(poLine));
addMockEntry(PIECES_STORAGE, bindPiece1);
addMockEntry(PIECES_STORAGE, bindPiece2);
addMockEntry(TITLES, title);

var bindPiecesCollection = new BindPiecesCollection()
.withPoLineId(poLine.getId())
.withBindItem(getMinimalContentBindItem()
.withLocationId(null)
.withHoldingId(holdingId))
.withBindPieceIds(List.of(bindingPiece.getId()));
.withBindPieceIds(bindPieceIds);

var bindResponse = verifyPostResponse(ORDERS_BIND_ENDPOINT, JsonObject.mapFrom(bindPiecesCollection).encode(),
prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10), APPLICATION_JSON, HttpStatus.HTTP_OK.toInt())
.as(BindPiecesResult.class);

assertThat(bindResponse.getPoLineId(), is(poLine.getId()));
assertThat(bindResponse.getBoundPieceIds(), contains(bindingPiece.getId()));
assertThat(bindResponse.getBoundPieceIds(), hasSize(2));
assertThat(bindResponse.getBoundPieceIds(), is(bindPieceIds));
assertThat(bindResponse.getItemId(), notNullValue());

var url = String.format(ORDERS_BIND_ID_ENDPOINT, bindingPiece.getId());
var bindItemId = bindResponse.getItemId();
var url = String.format(ORDERS_BIND_ID_ENDPOINT, bindPiece1.getId());
verifyDeleteResponse(url, "", HttpStatus.HTTP_NO_CONTENT.toInt());

var pieceUpdates = getPieceUpdates();
assertThat(pieceUpdates, notNullValue());
assertThat(pieceUpdates, hasSize(2));
assertThat(pieceUpdates, hasSize(3));

var pieceList = pieceUpdates.stream()
.map(json -> json.mapTo(Piece.class))
.filter(piece -> Objects.equals(bindingPiece.getId(), piece.getId()))
.filter(piece -> Objects.equals(bindPiece1.getId(), piece.getId()))
.sorted(Comparator.comparing(Piece::getIsBound))
.toList();
assertThat(pieceList.size(), is(2));

var pieceBefore = pieceList.get(1);
assertThat(pieceBefore.getIsBound(), is(true));
assertThat(pieceBefore.getBindItemId(), notNullValue());

var pieceAfter = pieceList.get(0);
assertThat(pieceAfter.getIsBound(), is(false));
assertThat(pieceAfter.getBindItemId(), nullValue());

var titleUpdates = getUpdatedTitles();
assertThat(titleUpdates, notNullValue());
assertThat(titleUpdates, hasSize(1));

var updatedTitleOpt = titleUpdates.stream()
.map(json -> json.mapTo(Title.class))
.filter(updatedTitle -> updatedTitle.getId().equals(pieceList.get(0).getTitleId()))
.findFirst();

assertThat(updatedTitleOpt.isPresent(), is(true));
var titleBindItemIds = updatedTitleOpt.get().getBindItemIds();
assertThat(titleBindItemIds, hasSize(1));
assertThat(titleBindItemIds.get(0), is(bindItemId));
}

@Test
void testRemovePieceBindingAndClearTitleBindItemIds() {
logger.info("=== Test DELETE Remove binding and clear title's bindItemIds");

var holdingId = "849241fa-4a14-4df5-b951-846dcd6cfc4d";
var order = getMinimalContentCompositePurchaseOrder()
.withWorkflowStatus(CompositePurchaseOrder.WorkflowStatus.OPEN);
var poLine = getMinimalContentCompositePoLine(order.getId());
var title = getTitle(poLine);
var bindPiece = getMinimalContentPiece(poLine.getId())
.withTitleId(title.getId())
.withHoldingId(holdingId)
.withReceivingStatus(Piece.ReceivingStatus.RECEIVED)
.withFormat(Piece.Format.PHYSICAL);

addMockEntry(PURCHASE_ORDER_STORAGE, order);
addMockEntry(PO_LINES_STORAGE, poLine);
addMockEntry(PIECES_STORAGE, bindPiece);
addMockEntry(TITLES, title);

var bindPiecesCollection = new BindPiecesCollection()
.withPoLineId(poLine.getId())
.withBindItem(getMinimalContentBindItem()
.withLocationId(null)
.withHoldingId(holdingId))
.withBindPieceIds(List.of(bindPiece.getId()));

var bindResponse = verifyPostResponse(ORDERS_BIND_ENDPOINT, JsonObject.mapFrom(bindPiecesCollection).encode(),
prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10), APPLICATION_JSON, HttpStatus.HTTP_OK.toInt())
.as(BindPiecesResult.class);

assertThat(bindResponse.getPoLineId(), is(poLine.getId()));
assertThat(bindResponse.getBoundPieceIds(), hasSize(1));
assertThat(bindResponse.getBoundPieceIds().get(0), is(bindPiece.getId()));
assertThat(bindResponse.getItemId(), notNullValue());

var url = String.format(ORDERS_BIND_ID_ENDPOINT, bindPiece.getId());
verifyDeleteResponse(url, "", HttpStatus.HTTP_NO_CONTENT.toInt());

var pieceUpdates = getPieceUpdates();
assertThat(pieceUpdates, notNullValue());
assertThat(pieceUpdates, hasSize(2));

var pieceList = pieceUpdates.stream()
.map(json -> json.mapTo(Piece.class))
.filter(piece -> Objects.equals(bindPiece.getId(), piece.getId()))
.sorted(Comparator.comparing(Piece::getIsBound))
.toList();
assertThat(pieceList.size(), is(2));

var pieceBefore = pieceList.get(1);
assertThat(pieceBefore.getIsBound(), is(true));
assertThat(pieceBefore.getBindItemId(), notNullValue());

var pieceAfter = pieceList.get(0);
assertThat(pieceAfter.getIsBound(), is(false));
assertThat(pieceAfter.getBindItemId(), nullValue());

var titleUpdates = getUpdatedTitles();
assertThat(titleUpdates, notNullValue());
assertThat(titleUpdates, hasSize(1));

var updatedTitleOpt = titleUpdates.stream()
.map(json -> json.mapTo(Title.class))
.filter(updatedTitle -> updatedTitle.getId().equals(pieceList.get(0).getTitleId()))
.findFirst();

assertThat(updatedTitleOpt.isPresent(), is(true));
var titleBindItemIds = updatedTitleOpt.get().getBindItemIds();
assertThat(titleBindItemIds, hasSize(0));
}

@Test
Expand Down

0 comments on commit 92f1917

Please sign in to comment.