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());
}
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());
}
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());
}
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"));
}
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));
}
Aggregations