Search in sources :

Example 6 with ValidationMessageException

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

the class StockCardTemplatesControllerIntegrationTest method throwValidationExceptionWith.

private void throwValidationExceptionWith(String exceptionKey) throws Exception {
    // given
    Mockito.doThrow(new ValidationMessageException(new Message(exceptionKey, "some id"))).when(stockCardTemplateService).saveOrUpdate(any());
    // when
    ResultActions resultActions = mvc.perform(post(STOCK_CARD_TEMPLATE_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(new StockCardTemplate())));
    // then
    resultActions.andExpect(status().isBadRequest());
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) ResultActions(org.springframework.test.web.servlet.ResultActions)

Example 7 with ValidationMessageException

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

the class StockEventsControllerIntegrationTest method shouldReturnBadRequestOnCreateStockEventWhenEntityInvalid.

@Test
public void shouldReturnBadRequestOnCreateStockEventWhenEntityInvalid() {
    // given
    mockHasPermissions();
    mockPassValidation();
    StockEventDto stockEvent = generateStockEvent();
    when(stockEventProcessor.process(any(StockEventDto.class))).thenThrow(new ValidationMessageException(ERROR_REASON_ASSIGNMENT_NOT_FOUND));
    // when
    restAssured.given().header(HttpHeaders.AUTHORIZATION, getTokenHeader()).contentType(APPLICATION_JSON).body(stockEvent).when().post(RESOURCE_URL).then().statusCode(400);
    // then
    assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}
Also used : ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) Test(org.junit.Test)

Example 8 with ValidationMessageException

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

the class JasperTemplateService method createTemplate.

/**
 * Save report file as ".jasper" in byte array in Template class.
 * If report is not valid throw exception.
 *
 * @param template The template to insert parameters to
 * @param inputStream input stream of the file
 */
private void createTemplate(JasperTemplate template, InputStream inputStream) {
    try {
        JasperReport report = JasperCompileManager.compileReport(inputStream);
        String reportType = report.getProperty("reportType");
        if (reportType != null) {
            template.setType(reportType);
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(report);
        template.setData(bos.toByteArray());
    } catch (JRException ex) {
        throw new ValidationMessageException(ex, new Message(ERROR_REPORTING_FILE_INVALID, ex.getMessage()));
    } catch (IOException ex) {
        throw new ValidationMessageException(ex, new Message(ERROR_IO, ex.getMessage()));
    }
}
Also used : JRException(net.sf.jasperreports.engine.JRException) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) JasperReport(net.sf.jasperreports.engine.JasperReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 9 with ValidationMessageException

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

the class PhysicalInventoryService method deletePhysicalInventory.

/**
 * Delete draft.
 *
 * @param id physical inventory id.
 */
public void deletePhysicalInventory(UUID id) {
    PhysicalInventory foundInventory = physicalInventoriesRepository.findOne(id);
    if (foundInventory != null) {
        checkPermission(foundInventory.getProgramId(), foundInventory.getFacilityId());
        if (!foundInventory.getIsDraft()) {
            throw new ValidationMessageException(ERROR_PHYSICAL_INVENTORY_IS_SUBMITTED);
        }
        physicalInventoriesRepository.delete(foundInventory);
    } else {
        throw new ResourceNotFoundException(new Message(ERROR_PHYSICAL_INVENTORY_NOT_FOUND, id));
    }
}
Also used : PhysicalInventory(org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) ResourceNotFoundException(org.openlmis.stockmanagement.exception.ResourceNotFoundException)

Example 10 with ValidationMessageException

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

the class SourceDestinationBaseService method doAssign.

/**
 * Create a new assignment.
 *
 * @param assignment assignment
 * @param errorKey   error message key
 * @param repository assignment repository
 * @param <T>        assignment type
 * @return created assignment.
 */
protected <T extends SourceDestinationAssignment> ValidSourceDestinationDto doAssign(T assignment, String errorKey, SourceDestinationAssignmentRepository<T> repository) {
    UUID referenceId = assignment.getNode().getReferenceId();
    boolean isRefFacility = facilityRefDataService.exists(referenceId);
    boolean isOrganization = organizationRepository.exists(referenceId);
    if (isRefFacility || isOrganization) {
        assignment.setNode(findOrCreateNode(referenceId, isRefFacility));
        return createAssignmentDto(repository.save(assignment));
    }
    throw new ValidationMessageException(new Message(errorKey));
}
Also used : Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

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