PasswordChangeServiceImpl implements PasswordChangeService { @Transactional @Override public Long saveOrUpdate(PasswordHistoryDTO passwordHistoryDTO) { Assert.notNull(passwordHistoryDTO); Assert.notNull(passwordHistoryDTO.getUserId()); PasswordHistoryEntity entity = converterService.convert(passwordHistoryDTO, PasswordHistoryEntity.class); UserEntity userEntity = userRepository.findOne(passwordHistoryDTO.getUserId()); Assert.notNull(userEntity); entity.setUserEntity(userEntity); try { Long retId = passwordHistoryRepository.save(entity).getId(); if (passwordHistoryRepository.findByUserEntity(entity.getUserEntity()).size() > PASSWORD_HISTORY_LIMIT) { deleteOldestPassword(passwordHistoryDTO.getUserId()); } return retId; } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userId = passwordHistoryDTO.getUserId().toString(); throw new DuplicateRecordException(userId, "Duplication occurred when saving password history for user with ID: " + userId, ex); } }  @Transactional @Override Long saveOrUpdate(PasswordHistoryDTO passwordHistoryDTO); @Transactional(readOnly = true) @Override List<String> getOldPasswords(Long userId); @Transactional @Override void deleteOldestPassword(Long userId);  }
@Test public void saveOrUpdateShouldNotDeleteOldestPasswordWhenPasswordHistoryEqualToLimit() { List<PasswordHistoryEntity> passwordHistoryEntityList = getPasswordHistoryEntitylistWithElements(PASSWORD_HISTORY_LIMIT); given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.save(passwordHistoryEntityWithUserEntity)) .willReturn(passwordHistoryEntityWithUserEntity); given(this.passwordHistoryRepository.findByUserEntity(userEntityWithId)) .willReturn(passwordHistoryEntityList); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); then(this.passwordHistoryRepository).should(never()).delete(passwordHistoryEntityList.get(0)); then(this.userRepository).should().findOne(USER_ID); then(this.passwordHistoryRepository).should().save(passwordHistoryEntityWithUserEntity); then(this.passwordHistoryRepository).should().findByUserEntity(userEntityWithId); }
@Test(expected = IllegalArgumentException.class) public void saveOrUpdateShouldThrowIAEWhenPasswordHistoryDtoIsNull() { this.passwordChangeService.saveOrUpdate(null); }
@Test(expected = IllegalArgumentException.class) public void saveOrUpdateShouldThrowIAEWhenPasswordHistoryUserIdIsNull() { PasswordHistoryDTO passwordHistoryDTO = PasswordHistoryDTO.builder() .userId(null) .build(); this.passwordChangeService.saveOrUpdate(passwordHistoryDTO); }
@Test(expected = DataAccessException.class) public void saveOrUpdateDelegateDataAccessExceptionFromUserRepository() { given(this.userRepository.findOne(USER_ID)).willThrow(DataIntegrityViolationException.class); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); }
@Test(expected = IllegalArgumentException.class) public void saveOrUpdateShouldThrowIAEWhenUserCannotBeFoundById() { given(this.userRepository.findOne(USER_ID)).willReturn(NULL_USER_ENTITY); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); }
@Test(expected = DuplicateRecordException.class) public void saveOrUpdateShouldThrowDuplicateRecordExceptionBecauseOfConstraintViolationException() { given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.save(passwordHistoryEntityWithUserEntity)) .willThrow(ConstraintViolationException.class); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); }
@Test(expected = DuplicateRecordException.class) public void saveOrUpdateShouldThrowDuplicateRecordExceptionBecauseOfDataIntegrityViolationException() { given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.save(passwordHistoryEntityWithUserEntity)) .willThrow(DataIntegrityViolationException.class); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); }
@Test public void saveOrUpdateShouldDeleteOldestPasswordWhenPasswordHistoryLongerThanLimit() { List<PasswordHistoryEntity> passwordHistoryEntityList = getPasswordHistoryEntitylistWithElements(PASSWORD_HISTORY_LIMIT + 1); given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.save(passwordHistoryEntityWithUserEntity)) .willReturn(passwordHistoryEntityWithUserEntity); given(this.passwordHistoryRepository.findByUserEntity(userEntityWithId)) .willReturn(passwordHistoryEntityList); given(this.passwordHistoryRepository.findTop1ByUserEntityIdOrderByChangeDate(USER_ID)) .willReturn(passwordHistoryEntityList.get(0)); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); then(this.passwordHistoryRepository).should().delete(passwordHistoryEntityList.get(0)); then(this.userRepository).should().findOne(USER_ID); then(this.passwordHistoryRepository).should().save(passwordHistoryEntityWithUserEntity); then(this.passwordHistoryRepository).should().findByUserEntity(userEntityWithId); then(this.passwordHistoryRepository).should().findTop1ByUserEntityIdOrderByChangeDate(USER_ID); }
@Test public void saveOrUpdateShouldNotDeleteOldestPasswordWhenPasswordHistoryShorterThanLimit() { List<PasswordHistoryEntity> passwordHistoryEntityList = getPasswordHistoryEntitylistWithElements(PASSWORD_HISTORY_LIMIT - 1); given(this.userRepository.findOne(USER_ID)).willReturn(userEntityWithId); given(this.passwordHistoryRepository.save(passwordHistoryEntityWithUserEntity)) .willReturn(passwordHistoryEntityWithUserEntity); given(this.passwordHistoryRepository.findByUserEntity(userEntityWithId)) .willReturn(passwordHistoryEntityList); this.passwordChangeService.saveOrUpdate(passwordHistoryDtoWithUserId); then(this.passwordHistoryRepository).should(never()).delete(passwordHistoryEntityList.get(0)); then(this.userRepository).should().findOne(USER_ID); then(this.passwordHistoryRepository).should().save(passwordHistoryEntityWithUserEntity); then(this.passwordHistoryRepository).should().findByUserEntity(userEntityWithId); }