Search in sources :

Example 31 with Message

use of org.openlmis.stockmanagement.util.Message in project openlmis-stockmanagement by OpenLMIS.

the class ApprovedOrderableValidator method validate.

/**
 * Validate if the orderable in stock event is in the approved list.
 *
 * @param stockEventDto the event to be validated.
 */
public void validate(StockEventDto stockEventDto) {
    LOGGER.debug("Validate approved product reference data service");
    UUID facility = stockEventDto.getFacilityId();
    UUID program = stockEventDto.getProgramId();
    // that is other validator's job
    if (!stockEventDto.hasLineItems() || facility == null || program == null) {
        return;
    }
    List<UUID> nonApprovedIds = findNonApprovedIds(stockEventDto, stockEventDto.getContext().getAllApprovedProducts());
    if (!isEmpty(nonApprovedIds)) {
        List<OrderableDto> orderables = orderableReferenceDataService.findByIds(nonApprovedIds);
        String codes = orderables.stream().map(OrderableDto::getProductCode).collect(Collectors.joining(", "));
        throw new ValidationMessageException(new Message(ERROR_ORDERABLE_NOT_IN_APPROVED_LIST, codes));
    }
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

Example 32 with Message

use of org.openlmis.stockmanagement.util.Message in project openlmis-stockmanagement by OpenLMIS.

the class OrderableLotDuplicationValidator method validate.

@Override
public void validate(StockEventDto stockEventDto) {
    // duplication is not allow in physical inventory, but is allowed in adjustment
    if (!stockEventDto.hasLineItems() || !stockEventDto.isPhysicalInventory()) {
        return;
    }
    Set<OrderableLotIdentity> nonDuplicates = new HashSet<>();
    Set<OrderableLotIdentity> duplicates = stockEventDto.getLineItems().stream().map(OrderableLotIdentity::identityOf).filter(lotIdentity -> !nonDuplicates.add(lotIdentity)).collect(toSet());
    if (duplicates.size() > 0) {
        throw new ValidationMessageException(new Message(ERROR_EVENT_ORDERABLE_LOT_DUPLICATION, duplicates));
    }
}
Also used : HashSet(java.util.HashSet) ERROR_EVENT_ORDERABLE_LOT_DUPLICATION(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_LOT_DUPLICATION) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Message(org.openlmis.stockmanagement.util.Message) Component(org.springframework.stereotype.Component) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Set(java.util.Set) Collectors.toSet(java.util.stream.Collectors.toSet) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) HashSet(java.util.HashSet)

Example 33 with Message

use of org.openlmis.stockmanagement.util.Message in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryAdjustmentReasonsValidator method validateReason.

private void validateReason(StockEventDto event, UUID reasonId) {
    if (reasonId == null) {
        throw new ValidationMessageException(new Message(ERROR_PHYSICAL_INVENTORY_DISCREPANCY_REASON_NOT_PROVIDED));
    }
    UUID facilityType = getFacilityType(event);
    UUID programId = event.getProgramId();
    if (!isReasonValid(programId, facilityType, reasonId)) {
        throwException(programId, facilityType, reasonId);
    }
}
Also used : Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

Example 34 with Message

use of org.openlmis.stockmanagement.util.Message in project openlmis-stockmanagement by OpenLMIS.

the class ActiveStockCardsValidator method checkAllStockCardsCovered.

private void checkAllStockCardsCovered(StockEventDto stockEventDto) {
    List<OrderableLotIdentity> coveredIdentities = stockEventDto.getLineItems().stream().map(OrderableLotIdentity::identityOf).collect(toList());
    boolean anyMissing = stockCardRepository.getIdentitiesBy(stockEventDto.getProgramId(), stockEventDto.getFacilityId()).stream().anyMatch(identity -> !coveredIdentities.contains(identity));
    if (anyMissing) {
        throw new ValidationMessageException(new Message(ERROR_PHYSICAL_INVENTORY_NOT_INCLUDE_ACTIVE_STOCK_CARD));
    }
}
Also used : Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)

Example 35 with Message

use of org.openlmis.stockmanagement.util.Message in project openlmis-stockmanagement by OpenLMIS.

the class StockEventValidationsServiceTest method shouldNotRunNextValidatorIfPreviousValidatorFailed.

@Test
public void shouldNotRunNextValidatorIfPreviousValidatorFailed() throws Exception {
    // given
    StockEventDto stockEventDto = StockEventDtoDataBuilder.createStockEventDto();
    doThrow(new ValidationMessageException(new Message("some error"))).when(validator1).validate(stockEventDto);
    // when:
    try {
        stockEventValidationsService.validate(stockEventDto);
    } catch (ValidationMessageException ex) {
        // then:
        verify(validator1, times(1)).validate(stockEventDto);
        verify(validator2, never()).validate(stockEventDto);
    }
}
Also used : Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Message (org.openlmis.stockmanagement.util.Message)35 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)19 Test (org.junit.Test)13 PermissionMessageException (org.openlmis.stockmanagement.exception.PermissionMessageException)13 ResultActions (org.springframework.test.web.servlet.ResultActions)9 UUID (java.util.UUID)8 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)6 StockEventDtoDataBuilder.createNoSourceDestinationStockEventDto (org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createNoSourceDestinationStockEventDto)4 StockEventDtoDataBuilder.createStockEventDto (org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto)4 ResourceNotFoundException (org.openlmis.stockmanagement.exception.ResourceNotFoundException)3 IOException (java.io.IOException)2 JasperTemplate (org.openlmis.stockmanagement.domain.JasperTemplate)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 PhysicalInventory (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory)2 StockCardTemplate (org.openlmis.stockmanagement.domain.template.StockCardTemplate)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1