From fb024a8ccb2cfd8662166ceb6ce0088933ddd5bd Mon Sep 17 00:00:00 2001 From: Serhii Nosko Date: Wed, 3 Apr 2024 16:22:48 +0300 Subject: [PATCH] MODORDERS-1072. Add "Display to public" to the Piece schema with validation logic (#881) * MODORDERS-1072. Add "Display to public" to the Piece schema with validation logic --- .../rest/core/exceptions/ErrorCodes.java | 1 + .../service/orders/OrderRolloverService.java | 1 + .../flows/DefaultPieceFlowsValidator.java | 10 ++++ .../flows/DefaultPieceFlowsValidatorTest.java | 49 +++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/src/main/java/org/folio/rest/core/exceptions/ErrorCodes.java b/src/main/java/org/folio/rest/core/exceptions/ErrorCodes.java index ac38ab0f0..e79a11145 100644 --- a/src/main/java/org/folio/rest/core/exceptions/ErrorCodes.java +++ b/src/main/java/org/folio/rest/core/exceptions/ErrorCodes.java @@ -102,6 +102,7 @@ public enum ErrorCodes { PIECE_HOLDING_REFERENCE_IS_NOT_ALLOWED_ERROR("holdingReferenceIsNotAllowed", "Holding reference is not allowed in the Piece for Pending order. Please set location reference"), POSTGRE_SQL_ERROR("pgException", "PostgreSQL exception"), PIECE_FORMAT_IS_NOT_VALID_ERROR("pieceFormatIsNotValid", "Piece format %s is not compatible with purchase line %s"), + PIECE_DISPLAY_ON_HOLDINGS_IS_NOT_CONSISTENT("pieceDisplayOnHoldingsIsNotConsistent", "Display On Holdings could not be set to false when Display To Public is true"), CREATE_PIECE_FOR_PENDING_ORDER_ERROR("createPiecePendingOrderError", "Creating piece for pending order is not possible. Please open order."), CREATE_ITEM_FOR_PIECE_IS_NOT_ALLOWED_ERROR("createItemForPieceIsNotAllowedError", "Create item for piece format %s is not allowed. Please check inventory option in the purchase order line %s"), NOT_FOUND("notFound", "Not Found"), diff --git a/src/main/java/org/folio/service/orders/OrderRolloverService.java b/src/main/java/org/folio/service/orders/OrderRolloverService.java index 3e1111f26..2435247ea 100644 --- a/src/main/java/org/folio/service/orders/OrderRolloverService.java +++ b/src/main/java/org/folio/service/orders/OrderRolloverService.java @@ -108,6 +108,7 @@ public OrderRolloverService(FundService fundService, PurchaseOrderLineService pu public Future rollover(LedgerFiscalYearRollover ledgerFYRollover, RequestContext requestContext) { return prepareRollover(ledgerFYRollover, requestContext) + // order rollover should be executed asynchronously, because of it .onSuccess is used here .onSuccess(v -> ledgerRolloverProgressService.getRolloversProgressByRolloverId(ledgerFYRollover.getId(), requestContext) .compose(progress -> startRollover(ledgerFYRollover, progress, requestContext))); } diff --git a/src/main/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidator.java b/src/main/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidator.java index e1f175434..8584d6524 100644 --- a/src/main/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidator.java +++ b/src/main/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidator.java @@ -1,6 +1,7 @@ package org.folio.service.pieces.flows; import static org.folio.rest.core.exceptions.ErrorCodes.CREATE_ITEM_FOR_PIECE_IS_NOT_ALLOWED_ERROR; +import static org.folio.rest.core.exceptions.ErrorCodes.PIECE_DISPLAY_ON_HOLDINGS_IS_NOT_CONSISTENT; import java.util.ArrayList; import java.util.Collections; @@ -34,6 +35,8 @@ public void isPieceRequestValid(Piece pieceToCreate, CompositePoLine originPoLin combinedErrors.addAll(pieceLocationErrors); List pieceFormatErrors = Optional.ofNullable(PieceValidatorUtil.validatePieceFormat(pieceToCreate, originPoLine)).orElse(new ArrayList<>()); combinedErrors.addAll(pieceFormatErrors); + List displayOnHoldingsErrors = validateDisplayOnHoldingsConsistency(pieceToCreate); + combinedErrors.addAll(displayOnHoldingsErrors); if (CollectionUtils.isNotEmpty(combinedErrors)) { Errors errors = new Errors().withErrors(combinedErrors).withTotalRecords(combinedErrors.size()); logger.error("Validation error : " + JsonObject.mapFrom(errors).encodePrettily()); @@ -49,6 +52,13 @@ public static List validateItemCreateFlag(Piece pieceToCreate, CompositeP return Collections.emptyList(); } + public static List validateDisplayOnHoldingsConsistency(Piece piece) { + if (Boolean.FALSE.equals(piece.getDisplayOnHolding()) && Boolean.TRUE.equals(piece.getDisplayToPublic())) { + return List.of(PIECE_DISPLAY_ON_HOLDINGS_IS_NOT_CONSISTENT.toError()); + } + return Collections.emptyList(); + } + public static boolean isCreateHoldingForPiecePossible(Piece pieceToCreate, CompositePoLine originPoLine) { Piece.Format pieceFormat = pieceToCreate.getFormat(); return (pieceFormat == Piece.Format.ELECTRONIC && PoLineCommonUtil.isHoldingUpdateRequiredForEresource(originPoLine)) || diff --git a/src/test/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidatorTest.java b/src/test/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidatorTest.java index 7a865a3c6..b7a66c0d4 100644 --- a/src/test/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidatorTest.java +++ b/src/test/java/org/folio/service/pieces/flows/DefaultPieceFlowsValidatorTest.java @@ -1,6 +1,7 @@ package org.folio.service.pieces.flows; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import java.util.UUID; @@ -14,6 +15,8 @@ import org.folio.rest.jaxrs.model.Piece; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; public class DefaultPieceFlowsValidatorTest { private DefaultPieceFlowsValidator defaultPieceFlowsValidator = new DefaultPieceFlowsValidator(); @@ -95,4 +98,50 @@ void createPieceIsValid() { defaultPieceFlowsValidator.isPieceRequestValid(piece, originPoLine, true); } + + @ParameterizedTest + @CsvSource(value = {"true:true", + "true:false", + "false:false"}, delimiter = ':') + void createPieceWithValidDisplayFlagsCombinations(Boolean displayOnHoldings, Boolean displayToPublic) { + String orderId = UUID.randomUUID().toString(); + String locationId = UUID.randomUUID().toString(); + String lineId = UUID.randomUUID().toString(); + Piece piece = new Piece().withPoLineId(lineId).withLocationId(locationId).withFormat(Piece.Format.ELECTRONIC) + .withDisplayOnHolding(displayOnHoldings) + .withDisplayToPublic(displayToPublic); + Location loc = new Location().withLocationId(locationId).withQuantityElectronic(1).withQuantity(1); + Cost cost = new Cost().withQuantityElectronic(1); + Eresource eresource = new Eresource().withCreateInventory(Eresource.CreateInventory.INSTANCE_HOLDING_ITEM); + CompositePoLine originPoLine = new CompositePoLine().withIsPackage(true).withPurchaseOrderId(orderId) + .withOrderFormat(CompositePoLine.OrderFormat.ELECTRONIC_RESOURCE).withId(lineId) + .withEresource(eresource) + .withLocations(List.of(loc)).withCost(cost); + + defaultPieceFlowsValidator.isPieceRequestValid(piece, originPoLine, true); + } + + @Test + void createPieceWithInvalidDisplayFlagsCombination() { + String orderId = UUID.randomUUID().toString(); + String locationId = UUID.randomUUID().toString(); + String lineId = UUID.randomUUID().toString(); + Piece piece = new Piece().withPoLineId(lineId).withLocationId(locationId).withFormat(Piece.Format.ELECTRONIC) + .withDisplayOnHolding(false) + .withDisplayToPublic(true); + Location loc = new Location().withLocationId(locationId).withQuantityElectronic(1).withQuantity(1); + Cost cost = new Cost().withQuantityElectronic(1); + Eresource eresource = new Eresource().withCreateInventory(Eresource.CreateInventory.INSTANCE_HOLDING_ITEM); + CompositePoLine originPoLine = new CompositePoLine().withIsPackage(true).withPurchaseOrderId(orderId) + .withOrderFormat(CompositePoLine.OrderFormat.ELECTRONIC_RESOURCE).withId(lineId) + .withEresource(eresource) + .withLocations(List.of(loc)).withCost(cost); + + HttpException exception = Assertions.assertThrows(HttpException.class, () -> { + defaultPieceFlowsValidator.isPieceRequestValid(piece, originPoLine, true); + }); + boolean isErrorPresent = exception.getErrors().getErrors().stream() + .anyMatch(error -> error.getCode().equals(ErrorCodes.PIECE_DISPLAY_ON_HOLDINGS_IS_NOT_CONSISTENT.getCode())); + assertTrue(isErrorPresent); + } }