@Test(expected = WebCertServiceException.class) public void testCreateUtkastCopyWhenCopyAlreadyExists() { 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); WebcertCertificateRelation webcertRelation = new WebcertCertificateRelation(INTYG_COPY_ID, RelationKod.KOPIA, LocalDateTime.now(), UtkastStatus.DRAFT_INCOMPLETE, false); when(certificateRelationService.getNewestRelationOfType(eq(INTYG_ID), eq(RelationKod.KOPIA), any())) .thenReturn(Optional.of(webcertRelation)); CreateUtkastFromTemplateRequest copyReq = buildUtkastCopyRequest(); setupMockForGettingUtlatande(); copyService.createUtkastCopy(copyReq); }
@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);  }