@Test public void shouldNotSaveFacilityApprovedProductAndThrowAnExceptionWhenProgramProductDoesNotExist() throws Exception { FacilityTypeApprovedProduct facilityTypeApprovedProduct = make(a(defaultFacilityApprovedProduct)); Long programId = 1L; Long productId = 2L; when(programService.getIdForCode(defaultProgramCode)).thenReturn(programId); when(productService.getIdForCode(defaultProductCode)).thenReturn(productId); doThrow(new DataException("abc")).when(programProductService).getIdByProgramIdAndProductId(programId, productId); expectedException.expect(DataException.class); expectedException.expectMessage("abc"); service.save(facilityTypeApprovedProduct); verify(programProductService).getIdByProgramIdAndProductId(programId, productId); verify(repository, never()).insert(facilityTypeApprovedProduct); }
public void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct) { fillProgramProductIds(facilityTypeApprovedProduct); FacilityType facilityType = facilityService.getFacilityTypeByCode(facilityTypeApprovedProduct.getFacilityType()); if (facilityType == null) throw new DataException(FACILITY_TYPE_DOES_NOT_EXIST); facilityTypeApprovedProduct.getFacilityType().setId(facilityType.getId()); if (facilityTypeApprovedProduct.getId() != null) { if(repository.get(facilityTypeApprovedProduct.getId()) == null){ throw new DataException(FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST); } repository.update(facilityTypeApprovedProduct); } else { repository.insert(facilityTypeApprovedProduct); } }
FacilityApprovedProductService { public void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct) { fillProgramProductIds(facilityTypeApprovedProduct); FacilityType facilityType = facilityService.getFacilityTypeByCode(facilityTypeApprovedProduct.getFacilityType()); if (facilityType == null) throw new DataException(FACILITY_TYPE_DOES_NOT_EXIST); facilityTypeApprovedProduct.getFacilityType().setId(facilityType.getId()); if (facilityTypeApprovedProduct.getId() != null) { if(repository.get(facilityTypeApprovedProduct.getId()) == null){ throw new DataException(FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST); } repository.update(facilityTypeApprovedProduct); } else { repository.insert(facilityTypeApprovedProduct); } } }
FacilityApprovedProductService { public void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct) { fillProgramProductIds(facilityTypeApprovedProduct); FacilityType facilityType = facilityService.getFacilityTypeByCode(facilityTypeApprovedProduct.getFacilityType()); if (facilityType == null) throw new DataException(FACILITY_TYPE_DOES_NOT_EXIST); facilityTypeApprovedProduct.getFacilityType().setId(facilityType.getId()); if (facilityTypeApprovedProduct.getId() != null) { if(repository.get(facilityTypeApprovedProduct.getId()) == null){ throw new DataException(FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST); } repository.update(facilityTypeApprovedProduct); } else { repository.insert(facilityTypeApprovedProduct); } }  }
FacilityApprovedProductService { public void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct) { fillProgramProductIds(facilityTypeApprovedProduct); FacilityType facilityType = facilityService.getFacilityTypeByCode(facilityTypeApprovedProduct.getFacilityType()); if (facilityType == null) throw new DataException(FACILITY_TYPE_DOES_NOT_EXIST); facilityTypeApprovedProduct.getFacilityType().setId(facilityType.getId()); if (facilityTypeApprovedProduct.getId() != null) { if(repository.get(facilityTypeApprovedProduct.getId()) == null){ throw new DataException(FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST); } repository.update(facilityTypeApprovedProduct); } else { repository.insert(facilityTypeApprovedProduct); } }  List<FacilityTypeApprovedProduct> getFullSupplyFacilityApprovedProductByFacilityAndProgram(Long facilityId, Long programId); List<FacilityTypeApprovedProduct> getNonFullSupplyFacilityApprovedProductByFacilityAndProgram(Long facilityId, Long programId); void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct); FacilityTypeApprovedProduct getFacilityApprovedProductByProgramProductAndFacilityTypeCode(FacilityTypeApprovedProduct facilityTypeApprovedProduct); List<FacilityTypeApprovedProduct> getAllBy(Long facilityTypeId, Long programId, String searchParam, Pagination pagination); Integer getTotalSearchResultCount(Long facilityTypeId, Long programId, String searchParam); void saveAll(List<FacilityTypeApprovedProduct> facilityTypeApprovedProducts, Long userId); void delete(Long id); }
FacilityApprovedProductService { public void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct) { fillProgramProductIds(facilityTypeApprovedProduct); FacilityType facilityType = facilityService.getFacilityTypeByCode(facilityTypeApprovedProduct.getFacilityType()); if (facilityType == null) throw new DataException(FACILITY_TYPE_DOES_NOT_EXIST); facilityTypeApprovedProduct.getFacilityType().setId(facilityType.getId()); if (facilityTypeApprovedProduct.getId() != null) { if(repository.get(facilityTypeApprovedProduct.getId()) == null){ throw new DataException(FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST); } repository.update(facilityTypeApprovedProduct); } else { repository.insert(facilityTypeApprovedProduct); } }  List<FacilityTypeApprovedProduct> getFullSupplyFacilityApprovedProductByFacilityAndProgram(Long facilityId, Long programId); List<FacilityTypeApprovedProduct> getNonFullSupplyFacilityApprovedProductByFacilityAndProgram(Long facilityId, Long programId); void save(FacilityTypeApprovedProduct facilityTypeApprovedProduct); FacilityTypeApprovedProduct getFacilityApprovedProductByProgramProductAndFacilityTypeCode(FacilityTypeApprovedProduct facilityTypeApprovedProduct); List<FacilityTypeApprovedProduct> getAllBy(Long facilityTypeId, Long programId, String searchParam, Pagination pagination); Integer getTotalSearchResultCount(Long facilityTypeId, Long programId, String searchParam); void saveAll(List<FacilityTypeApprovedProduct> facilityTypeApprovedProducts, Long userId); void delete(Long id); static final String FACILITY_TYPE_DOES_NOT_EXIST; static final String FACILITY_APPROVED_PRODUCT_DOES_NOT_EXIST; }