@Test(expected = WebCertServiceException.class) public void testCreateReplacementCopyFailedIfAlreadyReplacedBySignedIntyg() throws Exception { final String reference = "ref"; WebCertUser user = new WebCertUser(); user.setParameters(new IntegrationParameters(reference, "", "", "", "", "", "", "", "", false, false, false, true)); when(userService.getUser()).thenReturn(user); UtkastBuilderResponse resp = createCopyUtkastBuilderResponse(); WebcertCertificateRelation ersattRelation = new WebcertCertificateRelation(INTYG_ID, RelationKod.ERSATT, LocalDateTime.now(), UtkastStatus.SIGNED, false); when(certificateRelationService.getNewestRelationOfType(INTYG_ID, RelationKod.ERSATT, Arrays.asList(UtkastStatus.values()))) .thenReturn(Optional.of(ersattRelation)); setupMockForGettingUtlatande(); CreateReplacementCopyRequest copyReq = buildReplacementCopyRequest(); CreateReplacementCopyResponse copyResp = copyService.createReplacementCopy(copyReq); assertNotNull(copyResp); assertEquals(INTYG_COPY_ID, copyResp.getNewDraftIntygId()); assertEquals(INTYG_TYPE, copyResp.getNewDraftIntygType()); verify(mockPUService).getPerson(PATIENT_SSN); verify(createReplacementUtkastBuilder).populateCopyUtkastFromSignedIntyg(any(CreateReplacementCopyRequest.class), any(Person.class), any(boolean.class), any(boolean.class)); verify(mockUtkastRepository).save(any(Utkast.class)); verify(mockNotificationService).sendNotificationForDraftCreated(any(Utkast.class)); verify(certificateEventService) .createCertificateEventFromCopyUtkast(resp.getUtkast(), user.getHsaId(), EventCode.ERSATTER, INTYG_ID); verify(userService).getUser(); verify(logService).logCreateIntyg(any(LogRequest.class)); verify(intygService).isRevoked(INTYG_ID, INTYG_TYPE, false); }
@Override @Transactional public CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest) { String originalIntygId = replacementRequest.getOriginalIntygId(); WebCertUser user = userService.getUser(); LOG.debug("Creating replacement copy of intyg '{}'", originalIntygId); try { final Utlatande utlatande = utkastServiceHelper.getUtlatande(replacementRequest.getOriginalIntygId(), replacementRequest.getOriginalIntygTyp(), replacementRequest.isCoherentJournaling(), false); certificateAccessServiceHelper.validateAccessToReplaceIntyg(utlatande); addTestIntygFlagIfNecessaryToCopyRequest(replacementRequest, utlatande.getGrundData().isTestIntyg()); if (intygService.isRevoked(replacementRequest.getOriginalIntygId(), replacementRequest.getTyp(), replacementRequest.isCoherentJournaling())) { LOG.debug("Cannot create replacement certificate for id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can not create replacement copy - Original certificate is revoked"); } verifyNotReplaced(originalIntygId, "create replacement"); verifyNotComplementedWithSigned(originalIntygId, "create replacement"); verifySigned(utlatande, "create replacement"); UtkastBuilderResponse builderResponse = buildReplacementUtkastBuilderResponse(replacementRequest, originalIntygId); if (replacementRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.ERSATTER, originalIntygId); monitoringService.logIntygCopiedReplacement(savedUtkast.getIntygsId(), originalIntygId); return new CreateReplacementCopyResponse(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 @Transactional public CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest) { String originalIntygId = replacementRequest.getOriginalIntygId(); WebCertUser user = userService.getUser(); LOG.debug("Creating replacement copy of intyg '{}'", originalIntygId); try { final Utlatande utlatande = utkastServiceHelper.getUtlatande(replacementRequest.getOriginalIntygId(), replacementRequest.getOriginalIntygTyp(), replacementRequest.isCoherentJournaling(), false); certificateAccessServiceHelper.validateAccessToReplaceIntyg(utlatande); addTestIntygFlagIfNecessaryToCopyRequest(replacementRequest, utlatande.getGrundData().isTestIntyg()); if (intygService.isRevoked(replacementRequest.getOriginalIntygId(), replacementRequest.getTyp(), replacementRequest.isCoherentJournaling())) { LOG.debug("Cannot create replacement certificate for id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can not create replacement copy - Original certificate is revoked"); } verifyNotReplaced(originalIntygId, "create replacement"); verifyNotComplementedWithSigned(originalIntygId, "create replacement"); verifySigned(utlatande, "create replacement"); UtkastBuilderResponse builderResponse = buildReplacementUtkastBuilderResponse(replacementRequest, originalIntygId); if (replacementRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.ERSATTER, originalIntygId); monitoringService.logIntygCopiedReplacement(savedUtkast.getIntygsId(), originalIntygId); return new CreateReplacementCopyResponse(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 @Transactional public CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest) { String originalIntygId = replacementRequest.getOriginalIntygId(); WebCertUser user = userService.getUser(); LOG.debug("Creating replacement copy of intyg '{}'", originalIntygId); try { final Utlatande utlatande = utkastServiceHelper.getUtlatande(replacementRequest.getOriginalIntygId(), replacementRequest.getOriginalIntygTyp(), replacementRequest.isCoherentJournaling(), false); certificateAccessServiceHelper.validateAccessToReplaceIntyg(utlatande); addTestIntygFlagIfNecessaryToCopyRequest(replacementRequest, utlatande.getGrundData().isTestIntyg()); if (intygService.isRevoked(replacementRequest.getOriginalIntygId(), replacementRequest.getTyp(), replacementRequest.isCoherentJournaling())) { LOG.debug("Cannot create replacement certificate for id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can not create replacement copy - Original certificate is revoked"); } verifyNotReplaced(originalIntygId, "create replacement"); verifyNotComplementedWithSigned(originalIntygId, "create replacement"); verifySigned(utlatande, "create replacement"); UtkastBuilderResponse builderResponse = buildReplacementUtkastBuilderResponse(replacementRequest, originalIntygId); if (replacementRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.ERSATTER, originalIntygId); monitoringService.logIntygCopiedReplacement(savedUtkast.getIntygsId(), originalIntygId); return new CreateReplacementCopyResponse(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 @Transactional public CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest) { String originalIntygId = replacementRequest.getOriginalIntygId(); WebCertUser user = userService.getUser(); LOG.debug("Creating replacement copy of intyg '{}'", originalIntygId); try { final Utlatande utlatande = utkastServiceHelper.getUtlatande(replacementRequest.getOriginalIntygId(), replacementRequest.getOriginalIntygTyp(), replacementRequest.isCoherentJournaling(), false); certificateAccessServiceHelper.validateAccessToReplaceIntyg(utlatande); addTestIntygFlagIfNecessaryToCopyRequest(replacementRequest, utlatande.getGrundData().isTestIntyg()); if (intygService.isRevoked(replacementRequest.getOriginalIntygId(), replacementRequest.getTyp(), replacementRequest.isCoherentJournaling())) { LOG.debug("Cannot create replacement certificate for id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can not create replacement copy - Original certificate is revoked"); } verifyNotReplaced(originalIntygId, "create replacement"); verifyNotComplementedWithSigned(originalIntygId, "create replacement"); verifySigned(utlatande, "create replacement"); UtkastBuilderResponse builderResponse = buildReplacementUtkastBuilderResponse(replacementRequest, originalIntygId); if (replacementRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.ERSATTER, originalIntygId); monitoringService.logIntygCopiedReplacement(savedUtkast.getIntygsId(), originalIntygId); return new CreateReplacementCopyResponse(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 @Transactional public CreateReplacementCopyResponse createReplacementCopy(CreateReplacementCopyRequest replacementRequest) { String originalIntygId = replacementRequest.getOriginalIntygId(); WebCertUser user = userService.getUser(); LOG.debug("Creating replacement copy of intyg '{}'", originalIntygId); try { final Utlatande utlatande = utkastServiceHelper.getUtlatande(replacementRequest.getOriginalIntygId(), replacementRequest.getOriginalIntygTyp(), replacementRequest.isCoherentJournaling(), false); certificateAccessServiceHelper.validateAccessToReplaceIntyg(utlatande); addTestIntygFlagIfNecessaryToCopyRequest(replacementRequest, utlatande.getGrundData().isTestIntyg()); if (intygService.isRevoked(replacementRequest.getOriginalIntygId(), replacementRequest.getTyp(), replacementRequest.isCoherentJournaling())) { LOG.debug("Cannot create replacement certificate for id '{}', the certificate is revoked", originalIntygId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can not create replacement copy - Original certificate is revoked"); } verifyNotReplaced(originalIntygId, "create replacement"); verifyNotComplementedWithSigned(originalIntygId, "create replacement"); verifySigned(utlatande, "create replacement"); UtkastBuilderResponse builderResponse = buildReplacementUtkastBuilderResponse(replacementRequest, originalIntygId); if (replacementRequest.isDjupintegrerad()) { checkIntegreradEnhet(builderResponse); } Utkast savedUtkast = saveAndNotify(builderResponse, user, EventCode.ERSATTER, originalIntygId); monitoringService.logIntygCopiedReplacement(savedUtkast.getIntygsId(), originalIntygId); return new CreateReplacementCopyResponse(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);  }