Search in sources :

Example 21 with ValidationMessageException

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

the class PhysicalInventoryControllerIntegrationTest method shouldReturnBadRequestWhenInventoryIsSubmitted.

@Test
public void shouldReturnBadRequestWhenInventoryIsSubmitted() {
    // given
    UUID physicalInventoryId = UUID.randomUUID();
    doThrow(new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED)).when(physicalInventoryService).deletePhysicalInventory(physicalInventoryId);
    // when
    restAssured.given().header(HttpHeaders.AUTHORIZATION, getTokenHeader()).contentType(APPLICATION_JSON).pathParam("id", physicalInventoryId).when().delete(ID_URL).then().statusCode(400);
    // then
    assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
Also used : ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID) Test(org.junit.Test)

Example 22 with ValidationMessageException

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

the class ValidReasonAssignmentController method removeReasonAssignment.

/**
 * Remove a reason assignment.
 *
 * @param assignmentId reason assignment id.
 * @return No content status.
 */
@RequestMapping(value = "/validReasons/{id}", method = DELETE)
public ResponseEntity removeReasonAssignment(@PathVariable("id") UUID assignmentId) {
    LOGGER.debug("Try to remove reason assignment {}", assignmentId);
    permissionService.canManageReasons();
    if (!reasonAssignmentRepository.exists(assignmentId)) {
        throw new ValidationMessageException(new Message(ERROR_REASON_ASSIGNMENT_NOT_FOUND));
    }
    reasonAssignmentRepository.delete(assignmentId);
    return new ResponseEntity<>(null, NO_CONTENT);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with ValidationMessageException

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

the class PhysicalInventoryTemplateController method downloadXmlTemplate.

/**
 * Download report template with ".jrxml" format(extension) for Physical Inventory from database.
 *
 * @param response HttpServletResponse object.
 */
@GetMapping
@ResponseBody
public void downloadXmlTemplate(HttpServletResponse response) throws IOException {
    LOGGER.debug("Checking right to view Physical Inventory template");
    permissionService.canManageSystemSettings();
    JasperTemplate piPrintTemplate = templateService.getByName(PRINT_PI);
    if (piPrintTemplate == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Physical Inventory template does not exist.");
    } else {
        response.setContentType("application/xml");
        response.addHeader("Content-Disposition", "attachment; filename=piPrint" + ".jrxml");
        File file = templateService.convertJasperToXml(piPrintTemplate);
        try (InputStream fis = new FileInputStream(file);
            InputStream bis = new BufferedInputStream(fis)) {
            IOUtils.copy(bis, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException ex) {
            throw new ValidationMessageException(ex, new Message(ERROR_IO, ex.getMessage()));
        }
    }
}
Also used : Message(org.openlmis.stockmanagement.util.Message) BufferedInputStream(java.io.BufferedInputStream) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) JasperTemplate(org.openlmis.stockmanagement.domain.JasperTemplate) FileInputStream(java.io.FileInputStream) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with ValidationMessageException

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

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

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