@Test public void testUpdateAdministrator_when_authentication_success() throws AuthenticationFailedException { 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); AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class); Staff admin = mock(Staff.class); when(adminProfileDtoInput.getUsername()).thenReturn(username); when(adminProfileDtoInput.getPassword()).thenReturn("rawpassword"); when(adminProfileDtoInput.getFirstName()).thenReturn("albert"); when(adminProfileDtoInput.getLastName()).thenReturn("Smith"); when(adminProfileDtoInput.getEmail()).thenReturn( "albert.smith@consent2share.com"); when(adminProfileDtoToAdministratorMapper.map(adminProfileDtoInput)) .thenReturn(admin); sut.updateAdministrator(adminProfileDtoInput); verify(administratorRepository, times(1)).save(admin); }
@Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.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(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } }
AdminServiceImpl implements AdminService { @Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.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(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } }
AdminServiceImpl implements AdminService { @Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.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(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } AdminServiceImpl(
			Integer accountVerificationTokenExpireInHours,
			StaffRepository administratorRepository,
			PatientRepository patientRepository,
			DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper,
			AdminProfileDtoToAdministratorMapper adminProfileDtoToAdministratorMapper,
			ModelMapper modelMapper,
			UserContext userContext,
			PasswordEncoder passwordEncoder,
			EmailSender emailSender,
			TokenGenerator tokenGenerator,
			EmailTokenRepository emailTokenRepository,
			UsersRepository usersRepository,
			AdministrativeGenderCodeRepository administrativeGenderCodeRepository,
			MrnService mrnService, PixService pixService); }
AdminServiceImpl implements AdminService { @Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.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(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } AdminServiceImpl(
			Integer accountVerificationTokenExpireInHours,
			StaffRepository administratorRepository,
			PatientRepository patientRepository,
			DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper,
			AdminProfileDtoToAdministratorMapper adminProfileDtoToAdministratorMapper,
			ModelMapper modelMapper,
			UserContext userContext,
			PasswordEncoder passwordEncoder,
			EmailSender emailSender,
			TokenGenerator tokenGenerator,
			EmailTokenRepository emailTokenRepository,
			UsersRepository usersRepository,
			AdministrativeGenderCodeRepository administrativeGenderCodeRepository,
			MrnService mrnService, PixService pixService); @Override AdminProfileDto findAdminProfileByUsername(String username); @Override @Transactional void updateAdministrator(AdminProfileDto adminProfileDto); @Override @Transactional void updatePatient(PatientProfileDto patientProfileDto); @Override AdminCreatePatientResponseDto createPatientAccount(
			BasicPatientAccountDto basicPatientAccountDto); @Override Boolean sendLoginInformationEmail(long patientId, String linkUrl); }
AdminServiceImpl implements AdminService { @Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.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(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } AdminServiceImpl(
			Integer accountVerificationTokenExpireInHours,
			StaffRepository administratorRepository,
			PatientRepository patientRepository,
			DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper,
			AdminProfileDtoToAdministratorMapper adminProfileDtoToAdministratorMapper,
			ModelMapper modelMapper,
			UserContext userContext,
			PasswordEncoder passwordEncoder,
			EmailSender emailSender,
			TokenGenerator tokenGenerator,
			EmailTokenRepository emailTokenRepository,
			UsersRepository usersRepository,
			AdministrativeGenderCodeRepository administrativeGenderCodeRepository,
			MrnService mrnService, PixService pixService); @Override AdminProfileDto findAdminProfileByUsername(String username); @Override @Transactional void updateAdministrator(AdminProfileDto adminProfileDto); @Override @Transactional void updatePatient(PatientProfileDto patientProfileDto); @Override AdminCreatePatientResponseDto createPatientAccount(
			BasicPatientAccountDto basicPatientAccountDto); @Override Boolean sendLoginInformationEmail(long patientId, String linkUrl);  }