PasswordChangeServiceImpl implements PasswordChangeService { @Transactional(readOnly = true) @Override public List<String> getOldPasswords(Long userId) { Assert.notNull(userId); List<PasswordHistoryEntity> oldPasswords = passwordHistoryRepository.findByUserEntity(userRepository.findOne(userId)); List<String> passwords = new ArrayList<>(); for (PasswordHistoryEntity pass : oldPasswords) { passwords.add(pass.getPassword()); } return passwords; }  @Transactional @Override Long saveOrUpdate(PasswordHistoryDTO passwordHistoryDTO); @Transactional(readOnly = true) @Override List<String> getOldPasswords(Long userId); @Transactional @Override void deleteOldestPassword(Long userId);  }
@Test(expected = IllegalArgumentException.class) public void getOldPasswordShouldThrowIAEWhenUserIdIsNull() { this.passwordChangeService.getOldPasswords(null); }
@Test(expected = DataAccessException.class) public void getOldPasswordShouldDelegateDataAccessExceptionFromUserRepository() { given(this.userRepository.findOne(USER_ID)).willThrow(DataIntegrityViolationException.class); this.passwordChangeService.getOldPasswords(USER_ID); }
@Test(expected = IllegalArgumentException.class) public void getOldPasswordShouldThrowIAEWhenUserCannotBeFoundById() { given(this.userRepository.findOne(USER_ID)).willReturn(NULL_USER_ENTITY); given(this.passwordHistoryRepository.findByUserEntity(NULL_USER_ENTITY)) .willThrow(IllegalArgumentException.class); this.passwordChangeService.getOldPasswords(USER_ID); }
@Test public void getOldPasswordShouldReturnEmptyListWhenNoPasswordHistoryHasBeenFound() { given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.findByUserEntity(userEntityWithId)) .willReturn(EMPTY_PASSWORD_HISTORY_ENTITY_LIST); List<String> result = this.passwordChangeService.getOldPasswords(USER_ID); assertThat(result, notNullValue()); assertThat(result.isEmpty(), is(true)); then(this.userRepository).should().findOne(USER_ID); then(this.passwordHistoryRepository).should().findByUserEntity(userEntityWithId); }
@Test public void getOldPasswordShouldExtractPasswordFromHistoryWhenThereIsPasswordHistory() { final List<PasswordHistoryEntity> passwordHistoryEntityList = getPasswordHistoryEntitylistWithElements(2); final List<String> expectedPasswords = Arrays.asList( passwordHistoryEntityList.get(0).getPassword(), passwordHistoryEntityList.get(1).getPassword() ); given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.findByUserEntity(userEntityWithId)) .willReturn(passwordHistoryEntityList); List<String> result = this.passwordChangeService.getOldPasswords(USER_ID); assertThat(result, notNullValue()); assertThat(result.isEmpty(), is(false)); assertThat(result, equalTo(expectedPasswords)); then(this.userRepository).should().findOne(USER_ID); then(this.passwordHistoryRepository).should().findByUserEntity(userEntityWithId); }