@Test public void testDeleteDraftWrongVersion() { when(utkastRepository.findById(INTYG_ID)).thenReturn(Optional.ofNullable(utkast)); try { utkastService.deleteUnsignedDraft(INTYG_ID, utkast.getVersion() - 1); Assert.fail("OptimisticLockException expected"); } catch (OptimisticLockException e) { } verify(utkastRepository).findById(INTYG_ID); verifyNoMoreInteractions(utkastRepository); verifyNoInteractions(notificationService); verifyNoInteractions(logService); verifyNoInteractions(monitoringService); }
@Override @Transactional public void deleteUnsignedDraft(String intygId, long version) { LOG.debug("Deleting utkast '{}'", intygId); Utkast utkast = utkastRepository.findById(intygId).orElse(null); verifyUtkastExists(utkast, intygId, null, "The draft could not be deleted since it could not be found"); if (utkast.getVersion() != version) { LOG.debug("Utkast '{}' was concurrently modified", intygId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Intyg '{}' can not be deleted since it is no longer a draft", intygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "The draft can not be deleted since it is no longer a draft"); } utkastRepository.delete(utkast); LOG.debug("Deleted draft '{}'", utkast.getIntygsId()); monitoringService.logUtkastDeleted(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.DELETED); generateCertificateEvent(utkast, EventCode.RADERAT); LogRequest logRequest = logRequestFactory.createLogRequestFromUtkast(utkast); logService.logDeleteIntyg(logRequest); }
UtkastServiceImpl implements UtkastService { @Override @Transactional public void deleteUnsignedDraft(String intygId, long version) { LOG.debug("Deleting utkast '{}'", intygId); Utkast utkast = utkastRepository.findById(intygId).orElse(null); verifyUtkastExists(utkast, intygId, null, "The draft could not be deleted since it could not be found"); if (utkast.getVersion() != version) { LOG.debug("Utkast '{}' was concurrently modified", intygId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Intyg '{}' can not be deleted since it is no longer a draft", intygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "The draft can not be deleted since it is no longer a draft"); } utkastRepository.delete(utkast); LOG.debug("Deleted draft '{}'", utkast.getIntygsId()); monitoringService.logUtkastDeleted(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.DELETED); generateCertificateEvent(utkast, EventCode.RADERAT); LogRequest logRequest = logRequestFactory.createLogRequestFromUtkast(utkast); logService.logDeleteIntyg(logRequest); } }
UtkastServiceImpl implements UtkastService { @Override @Transactional public void deleteUnsignedDraft(String intygId, long version) { LOG.debug("Deleting utkast '{}'", intygId); Utkast utkast = utkastRepository.findById(intygId).orElse(null); verifyUtkastExists(utkast, intygId, null, "The draft could not be deleted since it could not be found"); if (utkast.getVersion() != version) { LOG.debug("Utkast '{}' was concurrently modified", intygId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Intyg '{}' can not be deleted since it is no longer a draft", intygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "The draft can not be deleted since it is no longer a draft"); } utkastRepository.delete(utkast); LOG.debug("Deleted draft '{}'", utkast.getIntygsId()); monitoringService.logUtkastDeleted(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.DELETED); generateCertificateEvent(utkast, EventCode.RADERAT); LogRequest logRequest = logRequestFactory.createLogRequestFromUtkast(utkast); logService.logDeleteIntyg(logRequest); }  }
UtkastServiceImpl implements UtkastService { @Override @Transactional public void deleteUnsignedDraft(String intygId, long version) { LOG.debug("Deleting utkast '{}'", intygId); Utkast utkast = utkastRepository.findById(intygId).orElse(null); verifyUtkastExists(utkast, intygId, null, "The draft could not be deleted since it could not be found"); if (utkast.getVersion() != version) { LOG.debug("Utkast '{}' was concurrently modified", intygId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Intyg '{}' can not be deleted since it is no longer a draft", intygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "The draft can not be deleted since it is no longer a draft"); } utkastRepository.delete(utkast); LOG.debug("Deleted draft '{}'", utkast.getIntygsId()); monitoringService.logUtkastDeleted(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.DELETED); generateCertificateEvent(utkast, EventCode.RADERAT); LogRequest logRequest = logRequestFactory.createLogRequestFromUtkast(utkast); logService.logDeleteIntyg(logRequest); }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId); }
UtkastServiceImpl implements UtkastService { @Override @Transactional public void deleteUnsignedDraft(String intygId, long version) { LOG.debug("Deleting utkast '{}'", intygId); Utkast utkast = utkastRepository.findById(intygId).orElse(null); verifyUtkastExists(utkast, intygId, null, "The draft could not be deleted since it could not be found"); if (utkast.getVersion() != version) { LOG.debug("Utkast '{}' was concurrently modified", intygId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Intyg '{}' can not be deleted since it is no longer a draft", intygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "The draft can not be deleted since it is no longer a draft"); } utkastRepository.delete(utkast); LOG.debug("Deleted draft '{}'", utkast.getIntygsId()); monitoringService.logUtkastDeleted(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.DELETED); generateCertificateEvent(utkast, EventCode.RADERAT); LogRequest logRequest = logRequestFactory.createLogRequestFromUtkast(utkast); logService.logDeleteIntyg(logRequest); }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId);  }