@Test public void getCandidateMetaDataWhenMatchingCriterias() { String intygIdCandidate = "correct-candidate-intygid"; String intygTypeCandidate = "lisjp"; String intygTypeVersion = "1.0"; String intygType = "ag7804"; when(webCertUser.getValdVardgivare()).thenReturn(createSelectableVardenhet("correct-vg-hsaid")); when(webCertUser.getValdVardenhet()).thenReturn(createSelectableVardenhet("correct-ve-hsaid")); when(webCertUser.getHsaId()).thenReturn("correct-user-hsaid"); when(draftAccessService.allowToCopyFromCandidate(anyString(), any(Personnummer.class))). thenReturn(AccessResult.create(AccessResultCode.NO_PROBLEM, "")); Optional<GetCopyFromCriteria> copyFromCriteria = Optional.of(new GetCopyFromCriteria(intygTypeCandidate, "1", 10)); when(ag7804ModuleApiV1Mock.getCopyFromCriteria()).thenReturn(copyFromCriteria); Patient patient = createPatient("Lilltolvan", "Tolvansson", createPnr("20121212-1212")); Set<String> validIntygType = new HashSet<>(); validIntygType.add(copyFromCriteria.get().getIntygType()); when(logRequestFactory.createLogRequestFromUtkast(any(Utkast.class), anyBoolean())).thenReturn(new LogRequest()); when(webCertUser.isLakare()).thenReturn(true); List<Utkast> candidates = Arrays.asList( createCandidate(UtkastStatus.SIGNED, "correct-ve-hsaid", null, intygIdCandidate, intygTypeCandidate, intygTypeVersion, LocalDateTime.now(), "correct-user-hsaid")); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(patient.getPersonId().getPersonnummerWithDash(), validIntygType)) .thenReturn(candidates); assertTrue(utkastCandidateService.getCandidateMetaData(ag7804ModuleApiV1Mock, intygType, patient, false).isPresent()); candidates = Arrays.asList( createCandidate(UtkastStatus.SIGNED, "correct-ve-hsaid", null, "intygId", intygTypeCandidate, intygTypeVersion, LocalDateTime.now(), "other-user-hsaid")); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(patient.getPersonId().getPersonnummerWithDash(), validIntygType)) .thenReturn(candidates); assertTrue(utkastCandidateService.getCandidateMetaData(ag7804ModuleApiV1Mock, intygType, patient, false).isPresent()); candidates = Arrays.asList( createCandidate(UtkastStatus.SIGNED, "correct-veunder-hsaid", null, "intygId", intygTypeCandidate, intygTypeVersion, LocalDateTime.now(), "other-user-hsaid")); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(patient.getPersonId().getPersonnummerWithDash(), validIntygType)) .thenReturn(candidates); assertTrue(utkastCandidateService.getCandidateMetaData(ag7804ModuleApiV1Mock, intygType, patient, false).isPresent()); when(webCertUser.isLakare()).thenReturn(false); candidates = Arrays.asList( createCandidate(UtkastStatus.SIGNED, "correct-ve-hsaid", null, "intygId", intygTypeCandidate, intygTypeVersion, LocalDateTime.now(), "other-user-hsaid")); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(patient.getPersonId().getPersonnummerWithDash(), validIntygType)) .thenReturn(candidates); assertTrue(utkastCandidateService.getCandidateMetaData(ag7804ModuleApiV1Mock, intygType, patient, false).isPresent()); verify(logService, times(4)).logReadIntyg(any(LogRequest.class), any(LogUser.class)); }
public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } } }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  Optional<UtkastCandidateMetaData> getCandidateMetaData(
        ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling); }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  Optional<UtkastCandidateMetaData> getCandidateMetaData(
        ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling);  }