PatientServiceImpl implements PatientService { @Override @Transactional public void updatePatient(PatientProfileDto patientDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = patientDto.getUsername(); if (!currentUser.getUsername().equals(username)) throw new AuthenticationFailedException( "Username does not match current active user."); Users user = usersRepository.loadUserByUsername(username); if (user != null) if (!passwordEncoder.matches(patientDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Patient patient = patientProfileDtoToPatientMapper.map(patientDto); final String mrn = patient.getMedicalRecordNumber(); Assert.hasText(mrn, "MRN cannot be retrieved from database!"); PixManagerBean pixManagerBean = pixService.updatePatient(TypeConverter .patientProfileDtoToPixPatientDto(patientDto, mrn)); Assert.isTrue( pixManagerBean.isSuccess(), "Patient cannot be updated on MPI! Error: " + pixManagerBean.getUpdateMessage()); final String eid = pixService.getEid(mrn); Assert.hasText(eid, "EID cannot be retrieved from MPI!"); patient.setEnterpriseIdentifier(eid); patientRepository.save(patient); try { emailSender.sendMessage(patientDto.getFirstName() + " " + patientDto.getLastName(), patientDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } PatientServiceImpl(
			PatientRepository patientRepository,
			PatientLegalRepresentativeAssociationRepository patientLegalRepresentativeAssociationRepository,
			ModelMapper modelMapper,
			UserContext userContext,
			DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper,
			UsersRepository usersRepository, PasswordEncoder passwordEncoder,
			EmailSender emailSender, PixService pixService); @Override long countAllPatients(); @Override PatientProfileDto findPatient(Long id); @Override List<PatientAdminDto> findAllPatientByFirstNameAndLastName(
			String[] tokens); @Override PatientProfileDto findByUsername(String username); @Override Long findIdByUsername(String username); @Override String findUsernameById(long id); @Override String findPatientEmailByUsername(String username); @Override PatientProfileDto findPatientProfileByUsername(String username); @Override PatientConnectionDto findPatientConnectionByUsername(String username); @Override PatientConnectionDto findPatientConnectionByPatient(Patient patient); @Override PatientConnectionDto findPatientConnectionById(long id); @Override List<PatientProfileDto> findPatientEntries(int pageNumber,
			int pageSize); @Override @Transactional PatientProfileDto savePatient(PatientProfileDto patientDto); @Override @Transactional void updatePatient(PatientProfileDto patientDto); @Override @Transactional void updateEid(String username); @Override @Transactional void updateEid(long id); @Override List<AddConsentIndividualProviderDto> findAddConsentIndividualProviderDtoByUsername(
			String username); @Override List<AddConsentOrganizationalProviderDto> findAddConsentOrganizationalProviderDtoByPatientId(
			long pateintId); @Override List<AddConsentIndividualProviderDto> findAddConsentIndividualProviderDtoByPatientId(
			long pateintId); @Override List<AddConsentOrganizationalProviderDto> findAddConsentOrganizationalProviderDtoByUsername(
			String username); @Override boolean isLegalRepForCurrentUser(Long legalRepId); @Override List<RecentPatientDto> findRecentPatientDtosById(List<String> ids); static final Integer LAST_N_DIGITS_OF_SSN; }
@Test public void testUpdatePatient_when_username_does_not_match_and_so_authentication_fails() { AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class); when(authenticatedUser.getUsername()).thenReturn("TheUsername"); when(userContext.getCurrentUser()).thenReturn(authenticatedUser); Users user = mock(Users.class); when(user.getUsername()).thenReturn("AnotherUsername"); PatientProfileDto patientProfileDtoInput = mock(PatientProfileDto.class); when(patientProfileDtoInput.getUsername()) .thenReturn("AnotherUsername"); try { sut.updatePatient(patientProfileDtoInput); } catch (AuthenticationFailedException e) { assertEquals(e.getMessage(), "Username does not match current active user."); } verify(patientRepository, times(0)).save(any(Patient.class)); }
@Test public void testUpdatePatient_when_username_matches_but_password_is_wrong() { final String username = "TheUsername"; AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class); when(authenticatedUser.getUsername()).thenReturn(username); when(userContext.getCurrentUser()).thenReturn(authenticatedUser); Users user = mock(Users.class); when(user.getUsername()).thenReturn(username); when(user.getPassword()).thenReturn("hashedpassword"); when(usersRepository.loadUserByUsername(anyString())).thenReturn(user); when(passwordEncoder.matches("rawpassword", "hashedpassword")) .thenReturn(false); PatientProfileDto patientProfileDtoInput = mock(PatientProfileDto.class); Patient patient = mock(Patient.class); when(patientProfileDtoInput.getUsername()).thenReturn(username); when(patientProfileDtoInput.getPassword()).thenReturn("rawpassword"); when(patientProfileDtoInput.getFirstName()).thenReturn("albert"); when(patientProfileDtoInput.getLastName()).thenReturn("Smith"); when(patientProfileDtoInput.getEmail()).thenReturn( "albert.smith@consent2share.com"); when(patientProfileDtoToPatientMapper.map(patientProfileDtoInput)) .thenReturn(patient); try { sut.updatePatient(patientProfileDtoInput); } catch (AuthenticationFailedException e) { assertEquals(e.getMessage(), "Password is incorrect."); } verify(patientRepository, times(0)).save(patient); }