@Test public void testCreateUtkastCopy() throws Exception { final String reference = "ref"; WebCertUser user = new WebCertUser(); user.setParameters(new IntegrationParameters(reference, "", "", "", "", "", "", "", "", false, false, false, true)); when(userService.getUser()).thenReturn(user); when(mockUtkastRepository.existsById(INTYG_ID)).thenReturn(Boolean.TRUE); UtkastBuilderResponse resp = createCopyUtkastBuilderResponse(); when(createUtkastCopyBuilder.populateCopyUtkastFromOrignalUtkast(any(CreateUtkastFromTemplateRequest.class), any(Person.class), any(boolean.class), any(boolean.class))).thenReturn(resp); Utkast utkast = new Utkast(); utkast.setStatus(UtkastStatus.DRAFT_LOCKED); utkast.setPatientPersonnummer(Personnummer.createPersonnummer("191212121212").orElse(null)); when(utkastService.getDraft(INTYG_ID, INTYG_TYPE)).thenReturn(utkast); CreateUtkastFromTemplateRequest copyReq = buildUtkastCopyRequest(); setupMockForGettingUtlatande(); CreateUtkastFromTemplateResponse utkastCopyResponse = copyService.createUtkastCopy(copyReq); assertNotNull(utkastCopyResponse); assertEquals(INTYG_COPY_ID, utkastCopyResponse.getNewDraftIntygId()); assertEquals(INTYG_TYPE, utkastCopyResponse.getNewDraftIntygType()); assertEquals(INTYG_ID, utkastCopyResponse.getOriginalIntygId()); verify(mockPUService).getPerson(PATIENT_SSN); verify(createUtkastCopyBuilder).populateCopyUtkastFromOrignalUtkast(any(CreateUtkastFromTemplateRequest.class), any(Person.class), any(boolean.class), eq(false)); verify(mockUtkastRepository).save(any(Utkast.class)); verify(referensService).saveReferens(eq(INTYG_COPY_ID), eq(reference)); verify(mockNotificationService).sendNotificationForDraftCreated(any(Utkast.class)); verify(certificateEventService) .createCertificateEventFromCopyUtkast(resp.getUtkast(), user.getHsaId(), EventCode.KOPIERATFRAN, INTYG_ID); verify(intygService).isRevoked(INTYG_ID, INTYG_TYPE, false); }
@Override public CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest) { String originalIntygId = copyRequest.getOriginalIntygId(); LOG.debug("Creating utkast from utkast certificate '{}'", originalIntygId); WebCertUser user = userService.getUser(); boolean coherentJournaling = isCoherentJournaling(user); try { if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } Utkast utkast = utkastService.getDraft(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp()); lockedDraftAccessServiceHelper.validateAccessToCopyLockedUtkast(utkast); addTestIntygFlagIfNecessaryToCopyRequest(copyRequest, utkast.isTestIntyg()); if (!UtkastStatus.DRAFT_LOCKED.equals(utkast.getStatus())) { final String message = "Copy failed due to wrong utkast status. Expected '%s' but was '%s'"; throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, String.format(message, UtkastStatus.DRAFT_LOCKED.name(), utkast.getStatus().name())); } if (utkast.getAterkalladDatum() != null) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the utkast is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } verifyNoDraftCopy(copyRequest.getOriginalIntygId(), "create utkast copy"); UtkastBuilderResponse builderResponse = buildUtkastCopyBuilderResponse(copyRequest, originalIntygId, coherentJournaling); Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.KOPIERATFRAN, originalIntygId); if (copyRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } return new CreateUtkastFromTemplateResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygTypeVersion(), savedUtkast.getIntygsId(), originalIntygId); } catch (ModuleException | ModuleNotFoundException me) { LOG.error("Module exception occured when trying to make a copy of " + originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me); } }
CopyUtkastServiceImpl implements CopyUtkastService { @Override public CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest) { String originalIntygId = copyRequest.getOriginalIntygId(); LOG.debug("Creating utkast from utkast certificate '{}'", originalIntygId); WebCertUser user = userService.getUser(); boolean coherentJournaling = isCoherentJournaling(user); try { if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } Utkast utkast = utkastService.getDraft(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp()); lockedDraftAccessServiceHelper.validateAccessToCopyLockedUtkast(utkast); addTestIntygFlagIfNecessaryToCopyRequest(copyRequest, utkast.isTestIntyg()); if (!UtkastStatus.DRAFT_LOCKED.equals(utkast.getStatus())) { final String message = "Copy failed due to wrong utkast status. Expected '%s' but was '%s'"; throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, String.format(message, UtkastStatus.DRAFT_LOCKED.name(), utkast.getStatus().name())); } if (utkast.getAterkalladDatum() != null) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the utkast is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } verifyNoDraftCopy(copyRequest.getOriginalIntygId(), "create utkast copy"); UtkastBuilderResponse builderResponse = buildUtkastCopyBuilderResponse(copyRequest, originalIntygId, coherentJournaling); Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.KOPIERATFRAN, originalIntygId); if (copyRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } return new CreateUtkastFromTemplateResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygTypeVersion(), savedUtkast.getIntygsId(), originalIntygId); } catch (ModuleException | ModuleNotFoundException me) { LOG.error("Module exception occured when trying to make a copy of " + originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me); } } }
CopyUtkastServiceImpl implements CopyUtkastService { @Override public CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest) { String originalIntygId = copyRequest.getOriginalIntygId(); LOG.debug("Creating utkast from utkast certificate '{}'", originalIntygId); WebCertUser user = userService.getUser(); boolean coherentJournaling = isCoherentJournaling(user); try { if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } Utkast utkast = utkastService.getDraft(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp()); lockedDraftAccessServiceHelper.validateAccessToCopyLockedUtkast(utkast); addTestIntygFlagIfNecessaryToCopyRequest(copyRequest, utkast.isTestIntyg()); if (!UtkastStatus.DRAFT_LOCKED.equals(utkast.getStatus())) { final String message = "Copy failed due to wrong utkast status. Expected '%s' but was '%s'"; throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, String.format(message, UtkastStatus.DRAFT_LOCKED.name(), utkast.getStatus().name())); } if (utkast.getAterkalladDatum() != null) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the utkast is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } verifyNoDraftCopy(copyRequest.getOriginalIntygId(), "create utkast copy"); UtkastBuilderResponse builderResponse = buildUtkastCopyBuilderResponse(copyRequest, originalIntygId, coherentJournaling); Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.KOPIERATFRAN, originalIntygId); if (copyRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } return new CreateUtkastFromTemplateResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygTypeVersion(), savedUtkast.getIntygsId(), originalIntygId); } catch (ModuleException | ModuleNotFoundException me) { LOG.error("Module exception occured when trying to make a copy of " + originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me); } }  }
CopyUtkastServiceImpl implements CopyUtkastService { @Override public CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest) { String originalIntygId = copyRequest.getOriginalIntygId(); LOG.debug("Creating utkast from utkast certificate '{}'", originalIntygId); WebCertUser user = userService.getUser(); boolean coherentJournaling = isCoherentJournaling(user); try { if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } Utkast utkast = utkastService.getDraft(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp()); lockedDraftAccessServiceHelper.validateAccessToCopyLockedUtkast(utkast); addTestIntygFlagIfNecessaryToCopyRequest(copyRequest, utkast.isTestIntyg()); if (!UtkastStatus.DRAFT_LOCKED.equals(utkast.getStatus())) { final String message = "Copy failed due to wrong utkast status. Expected '%s' but was '%s'"; throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, String.format(message, UtkastStatus.DRAFT_LOCKED.name(), utkast.getStatus().name())); } if (utkast.getAterkalladDatum() != null) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the utkast is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } verifyNoDraftCopy(copyRequest.getOriginalIntygId(), "create utkast copy"); UtkastBuilderResponse builderResponse = buildUtkastCopyBuilderResponse(copyRequest, originalIntygId, coherentJournaling); Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.KOPIERATFRAN, originalIntygId); if (copyRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } return new CreateUtkastFromTemplateResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygTypeVersion(), savedUtkast.getIntygsId(), originalIntygId); } catch (ModuleException | ModuleNotFoundException me) { LOG.error("Module exception occured when trying to make a copy of " + originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me); } }  @Override CreateCompletionCopyResponse createCompletion(CreateCompletionCopyRequest copyRequest); @Override CreateRenewalCopyResponse createRenewalCopy(CreateRenewalCopyRequest copyRequest); @Override @Transactional CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest); @Override CreateUtkastFromTemplateResponse createUtkastFromSignedTemplate(CreateUtkastFromTemplateRequest templateRequest); @Override CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest); }
CopyUtkastServiceImpl implements CopyUtkastService { @Override public CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest) { String originalIntygId = copyRequest.getOriginalIntygId(); LOG.debug("Creating utkast from utkast certificate '{}'", originalIntygId); WebCertUser user = userService.getUser(); boolean coherentJournaling = isCoherentJournaling(user); try { if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } Utkast utkast = utkastService.getDraft(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp()); lockedDraftAccessServiceHelper.validateAccessToCopyLockedUtkast(utkast); addTestIntygFlagIfNecessaryToCopyRequest(copyRequest, utkast.isTestIntyg()); if (!UtkastStatus.DRAFT_LOCKED.equals(utkast.getStatus())) { final String message = "Copy failed due to wrong utkast status. Expected '%s' but was '%s'"; throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, String.format(message, UtkastStatus.DRAFT_LOCKED.name(), utkast.getStatus().name())); } if (utkast.getAterkalladDatum() != null) { LOG.debug("Cannot create utkast from utkast certificate with id '{}', the utkast is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked"); } verifyNoDraftCopy(copyRequest.getOriginalIntygId(), "create utkast copy"); UtkastBuilderResponse builderResponse = buildUtkastCopyBuilderResponse(copyRequest, originalIntygId, coherentJournaling); Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.KOPIERATFRAN, originalIntygId); if (copyRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } return new CreateUtkastFromTemplateResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygTypeVersion(), savedUtkast.getIntygsId(), originalIntygId); } catch (ModuleException | ModuleNotFoundException me) { LOG.error("Module exception occured when trying to make a copy of " + originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me); } }  @Override CreateCompletionCopyResponse createCompletion(CreateCompletionCopyRequest copyRequest); @Override CreateRenewalCopyResponse createRenewalCopy(CreateRenewalCopyRequest copyRequest); @Override @Transactional CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest); @Override CreateUtkastFromTemplateResponse createUtkastFromSignedTemplate(CreateUtkastFromTemplateRequest templateRequest); @Override CreateUtkastFromTemplateResponse createUtkastCopy(CreateUtkastFromTemplateRequest copyRequest);  }