@Test public void testCreateDraftCertificateValidationError() { ResultValidator resultValidator = mock(ResultValidator.class); CreateDraftCertificateType certificateType = createCertificateType(); when(resultValidator.hasErrors()).thenReturn(true); when(mockValidator.validate(any(Intyg.class))).thenReturn(resultValidator); CreateDraftCertificateResponseType response = responder.createDraftCertificate(LOGICAL_ADDR, certificateType); assertNotNull(response); assertEquals(response.getResult().getResultCode(), ResultCodeType.ERROR); assertEquals(response.getResult().getErrorId(), ErrorIdType.VALIDATION_ERROR); }
@Override public CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters) { Intyg utkastsParams = parameters.getIntyg(); String invokingUserHsaId = utkastsParams.getSkapadAv().getPersonalId().getExtension(); String invokingUnitHsaId = utkastsParams.getSkapadAv().getEnhet().getEnhetsId().getExtension(); IntygUser user; try { user = webcertUserDetailsService.loadUserByHsaId(invokingUserHsaId); } catch (Exception e) { return createMIUErrorResponse(utkastsParams); } ResultValidator resultsValidator = validator.validate(utkastsParams); if (resultsValidator.hasErrors()) { return createValidationErrorResponse(resultsValidator); } if (!HoSPersonHelper.findVardenhetEllerMottagning(user, invokingUnitHsaId).isPresent()) { return createMIUErrorResponse(utkastsParams); } user.changeValdVardenhet(invokingUnitHsaId); webcertUserDetailsService.decorateIntygUserWithAvailableFeatures(user); ResultValidator appErrorsValidator = validator.validateApplicationErrors(utkastsParams, user); if (appErrorsValidator.hasErrors()) { return createApplicationErrorResponse(appErrorsValidator); } LOG.debug("Creating draft for invoker '{}' on unit '{}'", utkastsParams.getSkapadAv().getPersonalId().getExtension(), invokingUnitHsaId); String intygsTyp = moduleRegistry.getModuleIdFromExternalId(utkastsParams.getTypAvIntyg().getCode()); String latestIntygTypeVersion = intygTextsService.getLatestVersion(intygsTyp); Personnummer personnummer = Personnummer.createPersonnummer( utkastsParams.getPatient().getPersonId().getExtension()) .orElseThrow(() -> new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Failed to create valid personnummer for createDraft request")); SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(personnummer); if (AuthoritiesHelperUtil.mayNotCreateUtkastForSekretessMarkerad(sekretessStatus, user, intygsTyp)) { return createErrorResponse("Intygstypen " + intygsTyp.toUpperCase() + " kan inte utfärdas för patienter med skyddade personuppgifter", ErrorIdType.APPLICATION_ERROR); } ModuleApi moduleApi = null; try { moduleApi = moduleRegistry.getModuleApi(intygsTyp, latestIntygTypeVersion); } catch (ModuleNotFoundException e) { LOG.error("Could not get module api", e); return createErrorResponse("Internal error. Could not get module api.", ErrorIdType.APPLICATION_ERROR); } Map<String, Map<String, PreviousIntyg>> intygstypToPreviousIntyg = utkastService.checkIfPersonHasExistingIntyg(personnummer, user, null); ValidateDraftCreationResponse validateDraftCreationResponse = AuthoritiesHelperUtil.performUniqueAndModuleValidation(user, intygsTyp, intygstypToPreviousIntyg, moduleApi); if (validateDraftCreationResponse != null && validateDraftCreationResponse.getResultCode() == ResultCodeType.ERROR) { return createErrorResponse(validateDraftCreationResponse.getMessage(), ErrorIdType.APPLICATION_ERROR); } if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_TAK_KONTROLL).isVerified()) { TakResult takResult = takService.verifyTakningForCareUnit(invokingUnitHsaId, intygsTyp, SchemaVersion.VERSION_3, user); if (!takResult.isValid()) { String error = Joiner.on("; ").join(takResult.getErrorMessages()); LOG.warn("Invalid TAK result for unit '{}'. Returning APPLICATION_ERROR: {}", invokingUnitHsaId, error); return createErrorResponse(error, ErrorIdType.APPLICATION_ERROR); } } Utkast utkast = createNewDraft(utkastsParams, latestIntygTypeVersion, user); return createSuccessResponse(utkast.getIntygsId(), invokingUnitHsaId, validateDraftCreationResponse); }
CreateDraftCertificateResponderImpl implements CreateDraftCertificateResponderInterface { @Override public CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters) { Intyg utkastsParams = parameters.getIntyg(); String invokingUserHsaId = utkastsParams.getSkapadAv().getPersonalId().getExtension(); String invokingUnitHsaId = utkastsParams.getSkapadAv().getEnhet().getEnhetsId().getExtension(); IntygUser user; try { user = webcertUserDetailsService.loadUserByHsaId(invokingUserHsaId); } catch (Exception e) { return createMIUErrorResponse(utkastsParams); } ResultValidator resultsValidator = validator.validate(utkastsParams); if (resultsValidator.hasErrors()) { return createValidationErrorResponse(resultsValidator); } if (!HoSPersonHelper.findVardenhetEllerMottagning(user, invokingUnitHsaId).isPresent()) { return createMIUErrorResponse(utkastsParams); } user.changeValdVardenhet(invokingUnitHsaId); webcertUserDetailsService.decorateIntygUserWithAvailableFeatures(user); ResultValidator appErrorsValidator = validator.validateApplicationErrors(utkastsParams, user); if (appErrorsValidator.hasErrors()) { return createApplicationErrorResponse(appErrorsValidator); } LOG.debug("Creating draft for invoker '{}' on unit '{}'", utkastsParams.getSkapadAv().getPersonalId().getExtension(), invokingUnitHsaId); String intygsTyp = moduleRegistry.getModuleIdFromExternalId(utkastsParams.getTypAvIntyg().getCode()); String latestIntygTypeVersion = intygTextsService.getLatestVersion(intygsTyp); Personnummer personnummer = Personnummer.createPersonnummer( utkastsParams.getPatient().getPersonId().getExtension()) .orElseThrow(() -> new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Failed to create valid personnummer for createDraft request")); SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(personnummer); if (AuthoritiesHelperUtil.mayNotCreateUtkastForSekretessMarkerad(sekretessStatus, user, intygsTyp)) { return createErrorResponse("Intygstypen " + intygsTyp.toUpperCase() + " kan inte utfärdas för patienter med skyddade personuppgifter", ErrorIdType.APPLICATION_ERROR); } ModuleApi moduleApi = null; try { moduleApi = moduleRegistry.getModuleApi(intygsTyp, latestIntygTypeVersion); } catch (ModuleNotFoundException e) { LOG.error("Could not get module api", e); return createErrorResponse("Internal error. Could not get module api.", ErrorIdType.APPLICATION_ERROR); } Map<String, Map<String, PreviousIntyg>> intygstypToPreviousIntyg = utkastService.checkIfPersonHasExistingIntyg(personnummer, user, null); ValidateDraftCreationResponse validateDraftCreationResponse = AuthoritiesHelperUtil.performUniqueAndModuleValidation(user, intygsTyp, intygstypToPreviousIntyg, moduleApi); if (validateDraftCreationResponse != null && validateDraftCreationResponse.getResultCode() == ResultCodeType.ERROR) { return createErrorResponse(validateDraftCreationResponse.getMessage(), ErrorIdType.APPLICATION_ERROR); } if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_TAK_KONTROLL).isVerified()) { TakResult takResult = takService.verifyTakningForCareUnit(invokingUnitHsaId, intygsTyp, SchemaVersion.VERSION_3, user); if (!takResult.isValid()) { String error = Joiner.on("; ").join(takResult.getErrorMessages()); LOG.warn("Invalid TAK result for unit '{}'. Returning APPLICATION_ERROR: {}", invokingUnitHsaId, error); return createErrorResponse(error, ErrorIdType.APPLICATION_ERROR); } } Utkast utkast = createNewDraft(utkastsParams, latestIntygTypeVersion, user); return createSuccessResponse(utkast.getIntygsId(), invokingUnitHsaId, validateDraftCreationResponse); } }
CreateDraftCertificateResponderImpl implements CreateDraftCertificateResponderInterface { @Override public CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters) { Intyg utkastsParams = parameters.getIntyg(); String invokingUserHsaId = utkastsParams.getSkapadAv().getPersonalId().getExtension(); String invokingUnitHsaId = utkastsParams.getSkapadAv().getEnhet().getEnhetsId().getExtension(); IntygUser user; try { user = webcertUserDetailsService.loadUserByHsaId(invokingUserHsaId); } catch (Exception e) { return createMIUErrorResponse(utkastsParams); } ResultValidator resultsValidator = validator.validate(utkastsParams); if (resultsValidator.hasErrors()) { return createValidationErrorResponse(resultsValidator); } if (!HoSPersonHelper.findVardenhetEllerMottagning(user, invokingUnitHsaId).isPresent()) { return createMIUErrorResponse(utkastsParams); } user.changeValdVardenhet(invokingUnitHsaId); webcertUserDetailsService.decorateIntygUserWithAvailableFeatures(user); ResultValidator appErrorsValidator = validator.validateApplicationErrors(utkastsParams, user); if (appErrorsValidator.hasErrors()) { return createApplicationErrorResponse(appErrorsValidator); } LOG.debug("Creating draft for invoker '{}' on unit '{}'", utkastsParams.getSkapadAv().getPersonalId().getExtension(), invokingUnitHsaId); String intygsTyp = moduleRegistry.getModuleIdFromExternalId(utkastsParams.getTypAvIntyg().getCode()); String latestIntygTypeVersion = intygTextsService.getLatestVersion(intygsTyp); Personnummer personnummer = Personnummer.createPersonnummer( utkastsParams.getPatient().getPersonId().getExtension()) .orElseThrow(() -> new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Failed to create valid personnummer for createDraft request")); SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(personnummer); if (AuthoritiesHelperUtil.mayNotCreateUtkastForSekretessMarkerad(sekretessStatus, user, intygsTyp)) { return createErrorResponse("Intygstypen " + intygsTyp.toUpperCase() + " kan inte utfärdas för patienter med skyddade personuppgifter", ErrorIdType.APPLICATION_ERROR); } ModuleApi moduleApi = null; try { moduleApi = moduleRegistry.getModuleApi(intygsTyp, latestIntygTypeVersion); } catch (ModuleNotFoundException e) { LOG.error("Could not get module api", e); return createErrorResponse("Internal error. Could not get module api.", ErrorIdType.APPLICATION_ERROR); } Map<String, Map<String, PreviousIntyg>> intygstypToPreviousIntyg = utkastService.checkIfPersonHasExistingIntyg(personnummer, user, null); ValidateDraftCreationResponse validateDraftCreationResponse = AuthoritiesHelperUtil.performUniqueAndModuleValidation(user, intygsTyp, intygstypToPreviousIntyg, moduleApi); if (validateDraftCreationResponse != null && validateDraftCreationResponse.getResultCode() == ResultCodeType.ERROR) { return createErrorResponse(validateDraftCreationResponse.getMessage(), ErrorIdType.APPLICATION_ERROR); } if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_TAK_KONTROLL).isVerified()) { TakResult takResult = takService.verifyTakningForCareUnit(invokingUnitHsaId, intygsTyp, SchemaVersion.VERSION_3, user); if (!takResult.isValid()) { String error = Joiner.on("; ").join(takResult.getErrorMessages()); LOG.warn("Invalid TAK result for unit '{}'. Returning APPLICATION_ERROR: {}", invokingUnitHsaId, error); return createErrorResponse(error, ErrorIdType.APPLICATION_ERROR); } } Utkast utkast = createNewDraft(utkastsParams, latestIntygTypeVersion, user); return createSuccessResponse(utkast.getIntygsId(), invokingUnitHsaId, validateDraftCreationResponse); }  }
CreateDraftCertificateResponderImpl implements CreateDraftCertificateResponderInterface { @Override public CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters) { Intyg utkastsParams = parameters.getIntyg(); String invokingUserHsaId = utkastsParams.getSkapadAv().getPersonalId().getExtension(); String invokingUnitHsaId = utkastsParams.getSkapadAv().getEnhet().getEnhetsId().getExtension(); IntygUser user; try { user = webcertUserDetailsService.loadUserByHsaId(invokingUserHsaId); } catch (Exception e) { return createMIUErrorResponse(utkastsParams); } ResultValidator resultsValidator = validator.validate(utkastsParams); if (resultsValidator.hasErrors()) { return createValidationErrorResponse(resultsValidator); } if (!HoSPersonHelper.findVardenhetEllerMottagning(user, invokingUnitHsaId).isPresent()) { return createMIUErrorResponse(utkastsParams); } user.changeValdVardenhet(invokingUnitHsaId); webcertUserDetailsService.decorateIntygUserWithAvailableFeatures(user); ResultValidator appErrorsValidator = validator.validateApplicationErrors(utkastsParams, user); if (appErrorsValidator.hasErrors()) { return createApplicationErrorResponse(appErrorsValidator); } LOG.debug("Creating draft for invoker '{}' on unit '{}'", utkastsParams.getSkapadAv().getPersonalId().getExtension(), invokingUnitHsaId); String intygsTyp = moduleRegistry.getModuleIdFromExternalId(utkastsParams.getTypAvIntyg().getCode()); String latestIntygTypeVersion = intygTextsService.getLatestVersion(intygsTyp); Personnummer personnummer = Personnummer.createPersonnummer( utkastsParams.getPatient().getPersonId().getExtension()) .orElseThrow(() -> new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Failed to create valid personnummer for createDraft request")); SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(personnummer); if (AuthoritiesHelperUtil.mayNotCreateUtkastForSekretessMarkerad(sekretessStatus, user, intygsTyp)) { return createErrorResponse("Intygstypen " + intygsTyp.toUpperCase() + " kan inte utfärdas för patienter med skyddade personuppgifter", ErrorIdType.APPLICATION_ERROR); } ModuleApi moduleApi = null; try { moduleApi = moduleRegistry.getModuleApi(intygsTyp, latestIntygTypeVersion); } catch (ModuleNotFoundException e) { LOG.error("Could not get module api", e); return createErrorResponse("Internal error. Could not get module api.", ErrorIdType.APPLICATION_ERROR); } Map<String, Map<String, PreviousIntyg>> intygstypToPreviousIntyg = utkastService.checkIfPersonHasExistingIntyg(personnummer, user, null); ValidateDraftCreationResponse validateDraftCreationResponse = AuthoritiesHelperUtil.performUniqueAndModuleValidation(user, intygsTyp, intygstypToPreviousIntyg, moduleApi); if (validateDraftCreationResponse != null && validateDraftCreationResponse.getResultCode() == ResultCodeType.ERROR) { return createErrorResponse(validateDraftCreationResponse.getMessage(), ErrorIdType.APPLICATION_ERROR); } if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_TAK_KONTROLL).isVerified()) { TakResult takResult = takService.verifyTakningForCareUnit(invokingUnitHsaId, intygsTyp, SchemaVersion.VERSION_3, user); if (!takResult.isValid()) { String error = Joiner.on("; ").join(takResult.getErrorMessages()); LOG.warn("Invalid TAK result for unit '{}'. Returning APPLICATION_ERROR: {}", invokingUnitHsaId, error); return createErrorResponse(error, ErrorIdType.APPLICATION_ERROR); } } Utkast utkast = createNewDraft(utkastsParams, latestIntygTypeVersion, user); return createSuccessResponse(utkast.getIntygsId(), invokingUnitHsaId, validateDraftCreationResponse); }  @Override CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters); }
CreateDraftCertificateResponderImpl implements CreateDraftCertificateResponderInterface { @Override public CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters) { Intyg utkastsParams = parameters.getIntyg(); String invokingUserHsaId = utkastsParams.getSkapadAv().getPersonalId().getExtension(); String invokingUnitHsaId = utkastsParams.getSkapadAv().getEnhet().getEnhetsId().getExtension(); IntygUser user; try { user = webcertUserDetailsService.loadUserByHsaId(invokingUserHsaId); } catch (Exception e) { return createMIUErrorResponse(utkastsParams); } ResultValidator resultsValidator = validator.validate(utkastsParams); if (resultsValidator.hasErrors()) { return createValidationErrorResponse(resultsValidator); } if (!HoSPersonHelper.findVardenhetEllerMottagning(user, invokingUnitHsaId).isPresent()) { return createMIUErrorResponse(utkastsParams); } user.changeValdVardenhet(invokingUnitHsaId); webcertUserDetailsService.decorateIntygUserWithAvailableFeatures(user); ResultValidator appErrorsValidator = validator.validateApplicationErrors(utkastsParams, user); if (appErrorsValidator.hasErrors()) { return createApplicationErrorResponse(appErrorsValidator); } LOG.debug("Creating draft for invoker '{}' on unit '{}'", utkastsParams.getSkapadAv().getPersonalId().getExtension(), invokingUnitHsaId); String intygsTyp = moduleRegistry.getModuleIdFromExternalId(utkastsParams.getTypAvIntyg().getCode()); String latestIntygTypeVersion = intygTextsService.getLatestVersion(intygsTyp); Personnummer personnummer = Personnummer.createPersonnummer( utkastsParams.getPatient().getPersonId().getExtension()) .orElseThrow(() -> new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Failed to create valid personnummer for createDraft request")); SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(personnummer); if (AuthoritiesHelperUtil.mayNotCreateUtkastForSekretessMarkerad(sekretessStatus, user, intygsTyp)) { return createErrorResponse("Intygstypen " + intygsTyp.toUpperCase() + " kan inte utfärdas för patienter med skyddade personuppgifter", ErrorIdType.APPLICATION_ERROR); } ModuleApi moduleApi = null; try { moduleApi = moduleRegistry.getModuleApi(intygsTyp, latestIntygTypeVersion); } catch (ModuleNotFoundException e) { LOG.error("Could not get module api", e); return createErrorResponse("Internal error. Could not get module api.", ErrorIdType.APPLICATION_ERROR); } Map<String, Map<String, PreviousIntyg>> intygstypToPreviousIntyg = utkastService.checkIfPersonHasExistingIntyg(personnummer, user, null); ValidateDraftCreationResponse validateDraftCreationResponse = AuthoritiesHelperUtil.performUniqueAndModuleValidation(user, intygsTyp, intygstypToPreviousIntyg, moduleApi); if (validateDraftCreationResponse != null && validateDraftCreationResponse.getResultCode() == ResultCodeType.ERROR) { return createErrorResponse(validateDraftCreationResponse.getMessage(), ErrorIdType.APPLICATION_ERROR); } if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_TAK_KONTROLL).isVerified()) { TakResult takResult = takService.verifyTakningForCareUnit(invokingUnitHsaId, intygsTyp, SchemaVersion.VERSION_3, user); if (!takResult.isValid()) { String error = Joiner.on("; ").join(takResult.getErrorMessages()); LOG.warn("Invalid TAK result for unit '{}'. Returning APPLICATION_ERROR: {}", invokingUnitHsaId, error); return createErrorResponse(error, ErrorIdType.APPLICATION_ERROR); } } Utkast utkast = createNewDraft(utkastsParams, latestIntygTypeVersion, user); return createSuccessResponse(utkast.getIntygsId(), invokingUnitHsaId, validateDraftCreationResponse); }  @Override CreateDraftCertificateResponseType createDraftCertificate(String logicalAddress, CreateDraftCertificateType parameters);  }