Search in sources :

Example 1 with StockCardTemplate

use of org.openlmis.stockmanagement.domain.template.StockCardTemplate in project openlmis-stockmanagement by OpenLMIS.

the class StockCardTemplatesControllerIntegrationTest method shouldReturn403WhenCreateTemplatePermissionNotFound.

@Test
public void shouldReturn403WhenCreateTemplatePermissionNotFound() throws Exception {
    // given
    Mockito.doThrow(new PermissionMessageException(new Message(ERROR_NO_FOLLOWING_PERMISSION, STOCK_CARD_TEMPLATES_MANAGE))).when(permissionService).canCreateStockCardTemplate();
    // 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().isForbidden());
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) Message(org.openlmis.stockmanagement.util.Message) ResultActions(org.springframework.test.web.servlet.ResultActions) PermissionMessageException(org.openlmis.stockmanagement.exception.PermissionMessageException) Test(org.junit.Test)

Example 2 with StockCardTemplate

use of org.openlmis.stockmanagement.domain.template.StockCardTemplate 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 3 with StockCardTemplate

use of org.openlmis.stockmanagement.domain.template.StockCardTemplate in project openlmis-stockmanagement by OpenLMIS.

the class StockCardTemplatesControllerIntegrationTest method shouldReturn401WhenUserUnauthorized.

@Test
public void shouldReturn401WhenUserUnauthorized() throws Exception {
    // given
    Mockito.doThrow(new AuthenticationException("MANAGE_STOCK_CARD_TEMPLATES")).when(permissionService).canCreateStockCardTemplate();
    // 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().isUnauthorized());
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) AuthenticationException(org.openlmis.stockmanagement.exception.AuthenticationException) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test)

Example 4 with StockCardTemplate

use of org.openlmis.stockmanagement.domain.template.StockCardTemplate in project openlmis-stockmanagement by OpenLMIS.

the class StockCardTemplatesRepositoryIntegrationTest method shouldSearchForStockCardTemplateByFacilityTypeAndProgram.

@Test
public void shouldSearchForStockCardTemplateByFacilityTypeAndProgram() throws Exception {
    // given
    StockCardTemplate template = createTemplate();
    stockCardTemplatesRepository.save(template);
    // when
    StockCardTemplate found = stockCardTemplatesRepository.findByProgramIdAndFacilityTypeId(template.getProgramId(), template.getFacilityTypeId());
    // then
    AvailableStockCardFields packSize = found.getStockCardFields().get(0).getAvailableStockCardFields();
    AvailableStockCardLineItemFields docNumber = found.getStockCardLineItemFields().get(0).getAvailableStockCardLineItemFields();
    assertThat(packSize.getName(), is("packSize"));
    assertThat(docNumber.getName(), is("documentNumber"));
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) AvailableStockCardFields(org.openlmis.stockmanagement.domain.template.AvailableStockCardFields) AvailableStockCardLineItemFields(org.openlmis.stockmanagement.domain.template.AvailableStockCardLineItemFields) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with StockCardTemplate

use of org.openlmis.stockmanagement.domain.template.StockCardTemplate in project openlmis-stockmanagement by OpenLMIS.

the class StockCardTemplateService method saveOrUpdate.

/**
 * Save or update stock card template by facility type id and program id.
 *
 * @param templateDto object to save or update.
 * @return the saved or updated object.
 */
@Transactional
public StockCardTemplateDto saveOrUpdate(StockCardTemplateDto templateDto) {
    checkProgramAndFacilityTypeIdNotNull(templateDto);
    checkFieldsDuplication(templateDto);
    StockCardTemplate template = templateDto.toModel(findAllFieldsFrom(cardFieldsRepo).collect(toList()), findAllFieldsFrom(lineItemFieldsRepo).collect(toList()));
    StockCardTemplate found = templateRepository.findByProgramIdAndFacilityTypeId(template.getProgramId(), template.getFacilityTypeId());
    if (found != null) {
        template.setId(found.getId());
        templateRepository.delete(found);
    } else {
        programFacilityTypeExistenceService.checkProgramAndFacilityTypeExist(template.getProgramId(), template.getFacilityTypeId());
    }
    return StockCardTemplateDto.from(templateRepository.save(template));
}
Also used : StockCardTemplate(org.openlmis.stockmanagement.domain.template.StockCardTemplate) Transactional(javax.transaction.Transactional)

Aggregations

StockCardTemplate (org.openlmis.stockmanagement.domain.template.StockCardTemplate)8 Test (org.junit.Test)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 AvailableStockCardFields (org.openlmis.stockmanagement.domain.template.AvailableStockCardFields)2 AvailableStockCardLineItemFields (org.openlmis.stockmanagement.domain.template.AvailableStockCardLineItemFields)2 Message (org.openlmis.stockmanagement.util.Message)2 Transactional (javax.transaction.Transactional)1 StockCardFields (org.openlmis.stockmanagement.domain.template.StockCardFields)1 StockCardLineItemFields (org.openlmis.stockmanagement.domain.template.StockCardLineItemFields)1 StockCardTemplateDto (org.openlmis.stockmanagement.dto.StockCardTemplateDto)1 AuthenticationException (org.openlmis.stockmanagement.exception.AuthenticationException)1 PermissionMessageException (org.openlmis.stockmanagement.exception.PermissionMessageException)1 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1