Search in sources :

Example 1 with VvmApplicable

use of org.openlmis.stockmanagement.domain.common.VvmApplicable in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidator method validate.

/**
 * Validates whether the vvm applicables have proper vvm status (if applicable).
 * Throws ValidationMessageException if any of items is in invalid state.
 *  @param vvmApplicables list of items to test
 * @param messageKey error message key for exception
 * @param ignoreMissingOrderable whether should
 */
public void validate(List<? extends VvmApplicable> vvmApplicables, String messageKey, boolean ignoreMissingOrderable) {
    Set<UUID> orderableIds = vvmApplicables.stream().map(VvmApplicable::getOrderableId).collect(Collectors.toSet());
    List<OrderableDto> orderables = orderableReferenceDataService.findByIds(orderableIds);
    Map<UUID, OrderableDto> groupById = orderables.stream().collect(Collectors.toMap(OrderableDto::getId, orderable -> orderable));
    for (VvmApplicable item : vvmApplicables) {
        OrderableDto orderable = groupById.get(item.getOrderableId());
        if (null == orderable) {
            if (ignoreMissingOrderable) {
                continue;
            } else {
                throw new ValidationMessageException(ERROR_EVENT_ORDERABLE_INVALID);
            }
        }
        boolean useVvm = false;
        boolean hasVvmStatus = false;
        if (orderable.getExtraData() != null) {
            useVvm = Boolean.parseBoolean(orderable.getExtraData().get(USE_VVM));
        }
        if (item.getExtraData() != null) {
            hasVvmStatus = item.getExtraData().get(VVM_STATUS) != null;
        }
        if (!useVvm && hasVvmStatus) {
            throw new ValidationMessageException(messageKey);
        }
    }
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) Component(org.springframework.stereotype.Component) List(java.util.List) ERROR_EVENT_ORDERABLE_INVALID(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Map(java.util.Map) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

Example 2 with VvmApplicable

use of org.openlmis.stockmanagement.domain.common.VvmApplicable in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldRejectIfOrderableDisabledVvmAndLineItemHasVvmStatus.

@Test(expected = ValidationMessageException.class)
public void shouldRejectIfOrderableDisabledVvmAndLineItemHasVvmStatus() {
    OrderableDto orderable = generateOrderable();
    orderable.setExtraData(Collections.singletonMap("useVVM", "false"));
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    lineItem.setExtraData(Collections.singletonMap("vvmStatus", "status"));
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Example 3 with VvmApplicable

use of org.openlmis.stockmanagement.domain.common.VvmApplicable in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldNotRejectIfOrderableDisabledVvmAndLineItemHasNoVvmStatus.

@Test
public void shouldNotRejectIfOrderableDisabledVvmAndLineItemHasNoVvmStatus() {
    OrderableDto orderable = generateOrderable();
    orderable.setExtraData(Collections.singletonMap("useVVM", "false"));
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Example 4 with VvmApplicable

use of org.openlmis.stockmanagement.domain.common.VvmApplicable in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldNotRejectIfOrderableEnabledVvmAndLineItemHasVvmStatus.

@Test
public void shouldNotRejectIfOrderableEnabledVvmAndLineItemHasVvmStatus() {
    OrderableDto orderable = generateOrderable();
    orderable.setExtraData(Collections.singletonMap("useVVM", "true"));
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    lineItem.setExtraData(Collections.singletonMap("vvmStatus", "status"));
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Example 5 with VvmApplicable

use of org.openlmis.stockmanagement.domain.common.VvmApplicable in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldRejectIfOrderableNotConfiguredVvmAndLineItemHasVvmStatus.

@Test(expected = ValidationMessageException.class)
public void shouldRejectIfOrderableNotConfiguredVvmAndLineItemHasVvmStatus() {
    OrderableDto orderable = generateOrderable();
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    lineItem.setExtraData(Collections.singletonMap("vvmStatus", "status"));
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Aggregations

VvmApplicable (org.openlmis.stockmanagement.domain.common.VvmApplicable)5 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)5 Test (org.junit.Test)4 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)1 ERROR_EVENT_ORDERABLE_INVALID (org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID)1 OrderableReferenceDataService (org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1