Search in sources :

Example 1 with ValidationMessageException

use of org.openlmis.stockmanagement.exception.ValidationMessageException 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 ValidationMessageException

use of org.openlmis.stockmanagement.exception.ValidationMessageException in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryController method print.

/**
 * Print out physical inventory as a PDF file.
 *
 * @param id The UUID of the stock event to print
 * @param format The report format
 * @return ResponseEntity with the "#200 OK" HTTP response status and PDF file on success, or
 *     ResponseEntity containing the error description status.
 */
@GetMapping(value = ID_PATH_VARIABLE, params = "format")
@ResponseBody
public ModelAndView print(@PathVariable("id") UUID id, @RequestParam String format) throws JasperReportViewException {
    checkPermission(id);
    checkFormat(format.toLowerCase());
    JasperTemplate printTemplate = templateService.getByName(PRINT_PI);
    if (printTemplate == null) {
        throw new ValidationMessageException(new Message(ERROR_REPORTING_TEMPLATE_NOT_FOUND_WITH_NAME, PRINT_PI));
    }
    JasperReportsMultiFormatView jasperView = jasperReportService.getJasperReportsView(printTemplate);
    return new ModelAndView(jasperView, getParams(id, format));
}
Also used : Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) ModelAndView(org.springframework.web.servlet.ModelAndView) JasperReportsMultiFormatView(org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView) JasperTemplate(org.openlmis.stockmanagement.domain.JasperTemplate) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with ValidationMessageException

use of org.openlmis.stockmanagement.exception.ValidationMessageException in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryValidator method validateDraft.

/**
 * Check for physical inventory dto's validity.
 * Throws {@link ValidationMessageException} if an error found.
 * @param inventory physical inventory to validate.
 */
public void validateDraft(PhysicalInventoryDto inventory, UUID id) {
    if (!inventory.getId().equals(id)) {
        throw new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_ID_MISMATCH);
    }
    PhysicalInventory foundInventory = physicalInventoriesRepository.findOne(id);
    if (foundInventory != null && !foundInventory.getIsDraft()) {
        throw new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED);
    }
    List<PhysicalInventoryLineItemDto> lineItems = inventory.getLineItems();
    validateLineItems(lineItems);
    vvmValidator.validate(lineItems, ERROR_PHYSICAL_INVENTORY_ORDERABLE_DISABLED_VVM, false);
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) PhysicalInventoryLineItemDto(org.openlmis.stockmanagement.dto.PhysicalInventoryLineItemDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException)

Example 4 with ValidationMessageException

use of org.openlmis.stockmanagement.exception.ValidationMessageException in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryControllerIntegrationTest method shouldReturnBadRequestOnCreateEmptyDraftWhenEntityInvalid.

@Test
public void shouldReturnBadRequestOnCreateEmptyDraftWhenEntityInvalid() {
    // given
    PhysicalInventoryDto expectedDraft = generateDraft();
    when(physicalInventoryService.createNewDraft(expectedDraft)).thenThrow(new ValidationMessageException(ERROR_PROGRAM_ID_MISSING));
    // when
    restAssured.given().header(HttpHeaders.AUTHORIZATION, getTokenHeader()).contentType(APPLICATION_JSON).body(expectedDraft).when().post(RESOURCE_URL).then().statusCode(400);
    // then
    assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
Also used : PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Test(org.junit.Test)

Example 5 with ValidationMessageException

use of org.openlmis.stockmanagement.exception.ValidationMessageException in project openlmis-stockmanagement by OpenLMIS.

the class PhysicalInventoryControllerIntegrationTest method shouldReturnBadRequestOnSaveDraftWhenEntityInvalid.

@Test
public void shouldReturnBadRequestOnSaveDraftWhenEntityInvalid() {
    // given
    UUID physicalInventoryId = UUID.randomUUID();
    PhysicalInventoryDto expectedDraft = generatePhysicalInventory();
    when(physicalInventoryService.saveDraft(expectedDraft, physicalInventoryId)).thenThrow(new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_LINE_ITEMS_MISSING));
    // when
    restAssured.given().header(HttpHeaders.AUTHORIZATION, getTokenHeader()).contentType(APPLICATION_JSON).pathParam("id", physicalInventoryId).body(expectedDraft).when().put(ID_URL).then().statusCode(400);
    // then
    assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
Also used : PhysicalInventoryDto(org.openlmis.stockmanagement.dto.PhysicalInventoryDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)30 Message (org.openlmis.stockmanagement.util.Message)19 UUID (java.util.UUID)10 Test (org.junit.Test)10 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)4 IOException (java.io.IOException)3 UUID.randomUUID (java.util.UUID.randomUUID)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Set (java.util.Set)2 JRException (net.sf.jasperreports.engine.JRException)2 JasperTemplate (org.openlmis.stockmanagement.domain.JasperTemplate)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 PhysicalInventory (org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory)2 PhysicalInventoryDto (org.openlmis.stockmanagement.dto.PhysicalInventoryDto)2 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Component (org.springframework.stereotype.Component)2 ResultActions (org.springframework.test.web.servlet.ResultActions)2