@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); }
@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); } }
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); } } }
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); } }  }
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); }
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);  }