PatientServiceImpl extends
		gov.samhsa.consent2share.service.patient.PatientServiceImpl { @Override @Transactional public void updatePatient(PatientProfileDto patientProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = patientProfileDto.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(patientProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Patient initialpatient = patientRepository.findByUsername(username); patientProfileDto.setEnterpriseIdentifier(initialpatient .getEnterpriseIdentifier()); patientProfileDto.setMedicalRecordNumber(initialpatient .getMedicalRecordNumber()); Patient patient = patientProfileDtoToPatientMapper .map(patientProfileDto); patientRepository.save(patient); spiritQueryService.updatePatient(patientProfileDto); try { emailSender.sendMessage(patientProfileDto.getFirstName() + " " + patientProfileDto.getLastName(), patientProfileDto.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,
			SpiritQueryService spiritQueryService); @Override @Transactional void updatePatient(PatientProfileDto patientProfileDto);  }
@Test public void testUpdatePatient_when_authentication_success() throws AuthenticationFailedException, SpiritClientNotAvailableException { 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(true); 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); Patient initialpatient = mock(Patient.class); when(patientRepository.findByUsername(username)).thenReturn( initialpatient); when(initialpatient.getEnterpriseIdentifier()).thenReturn("FAM.123"); sut.updatePatient(patientProfileDtoInput); verify(patientRepository, times(1)).save(patient); }
@Test public void testUpdatePatient_when_username_does_not_match_and_so_authentication_fails() throws SpiritClientNotAvailableException { 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() throws SpiritClientNotAvailableException { 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); }