StatesHistoryServiceImpl implements StatesHistoryService { @Transactional @Override public Long saveStateHistory(StateHistoryDTO state) { Assert.notNull(state); Assert.notNull(state.getStateDTO()); Assert.notNull(state.getStateDTO().getId()); StatesEntity statesEntity = statesRepository.findOne(state.getStateDTO().getId()); Assert.notNull(statesEntity); StatesHistoryEntity statesHistoryEntity = converterService.convert(state, StatesHistoryEntity.class); statesHistoryEntity .setCreationDate(state.getCreationDate() == null ? new Date() : state.getCreationDate()); applicationsService.saveOrUpdate(state.getApplicationDTO()); return statesHistoryRepository.saveAndFlush(statesHistoryEntity).getId(); }  @Transactional @Override void deleteStateHistoriesByApplication(ApplicationDTO applicationDTO); @Transactional @Override Long saveStateHistory(StateHistoryDTO state); @Transactional(readOnly = true) @Override List<StateHistoryDTO> getStateHistoriesByApplicationId(Long applicationId);  }
@Test(expected = IllegalArgumentException.class) public void saveStateShouldThrowIllegalArgumentExceptionWhenApplicationIdIsNull() { statesHistoryService.saveStateHistory(null); }
@Test(expected = IllegalArgumentException.class) public void saveStateHistoryShouldThrowIllegalArgumentExceptionWhenStateDtoIsNull() { StateHistoryDTO stateHistoryDTO = StateHistoryDTO.builder().stateDTO(null).build(); statesHistoryService.saveStateHistory(stateHistoryDTO); }
@Test(expected = IllegalArgumentException.class) public void saveStateHistoryShouldThrowIllegalArgumentExceptionWhenStateDtoIdIsNull() { StateDTO stateDTO = StateDTO.builder().id(null).build(); StateHistoryDTO stateHistoryDTO = StateHistoryDTO.builder().stateDTO(stateDTO).build(); statesHistoryService.saveStateHistory(stateHistoryDTO); }
@Test(expected = IllegalArgumentException.class) public void saveStateHistoryShouldThrowIllegalArgumentExceptionWhenStateEntityNotExists() { given(statesRepository.findOne(FIRST_ID)).willReturn(null); statesHistoryService.saveStateHistory(firstStateHistoryDtoWithStateDto); }
@Test(expected = IllegalArgumentException.class) public void saveStateHistoryShouldThrowIllegalArgumentExceptionWhenApplicationDtoIsNotValid() { given(statesRepository.findOne(FIRST_ID)).willReturn(firstStateEntity); given(converterService.convert(firstStateHistoryDtoWithStateDto, StatesHistoryEntity.class)) .willReturn(firstStatesHistoryEntity); given(applicationsService.saveOrUpdate(firstStateHistoryDtoWithStateDto.getApplicationDTO())) .willThrow(IllegalArgumentException.class); statesHistoryService.saveStateHistory(firstStateHistoryDtoWithStateDto); }
@Test public void saveStateHistoryShouldSaveWhenAllParamIsRightAndCreationDateIsNull() { firstStateHistoryDtoWithStateDto.setCreationDate(null); given(statesRepository.findOne(FIRST_ID)).willReturn(firstStateEntity); given(converterService.convert(firstStateHistoryDtoWithStateDto, StatesHistoryEntity.class)) .willReturn(firstStatesHistoryEntity); given(statesHistoryRepository.saveAndFlush(firstStatesHistoryEntity)) .willReturn(firstStatesHistoryEntity); Long result = statesHistoryService.saveStateHistory(firstStateHistoryDtoWithStateDto); assertThat(result, notNullValue()); assertThat(result, equalTo(FIRST_ID)); assertThat(firstStatesHistoryEntity.getCreationDate(), greaterThan(currentDateMinusSeconds(5L))); then(statesRepository).should().findOne(FIRST_ID); then(converterService).should() .convert(firstStateHistoryDtoWithStateDto, StatesHistoryEntity.class); then(statesHistoryRepository).should().saveAndFlush(firstStatesHistoryEntity); then(applicationsService).should() .saveOrUpdate(firstStateHistoryDtoWithStateDto.getApplicationDTO()); }
@Test public void saveStateHistoryShouldSaveWhenAllParamIsRightAndCreationDateIsExisting() { firstStateHistoryDtoWithStateDto.setCreationDate(presentDate); given(statesRepository.findOne(FIRST_ID)).willReturn(firstStateEntity); given(converterService.convert(firstStateHistoryDtoWithStateDto, StatesHistoryEntity.class)) .willReturn(firstStatesHistoryEntity); given(statesHistoryRepository.saveAndFlush(firstStatesHistoryEntity)) .willReturn(firstStatesHistoryEntity); Long result = statesHistoryService.saveStateHistory(firstStateHistoryDtoWithStateDto); assertThat(result, notNullValue()); assertThat(result, equalTo(FIRST_ID)); assertThat(firstStatesHistoryEntity.getCreationDate(), equalTo(presentDate)); then(statesRepository).should().findOne(FIRST_ID); then(converterService).should() .convert(firstStateHistoryDtoWithStateDto, StatesHistoryEntity.class); then(statesHistoryRepository).should().saveAndFlush(firstStatesHistoryEntity); then(applicationsService).should() .saveOrUpdate(firstStateHistoryDtoWithStateDto.getApplicationDTO()); }