StatesHistoryServiceImpl implements StatesHistoryService { @Transactional(readOnly = true) @Override public List<StateHistoryDTO> getStateHistoriesByApplicationId(Long applicationId) { Assert.notNull(applicationId); ApplicationEntity applicationEntity = applicationsRepository.findOne(applicationId); Assert.notNull(applicationEntity); if (applicationEntity.isDeleted()) { return Collections.emptyList(); } List<StatesHistoryEntity> statesHistoryEntities = statesHistoryRepository.findByApplicationEntityOrderByCreationDateDesc(applicationEntity); return converterService.convert(statesHistoryEntities, StateHistoryDTO.class); }  @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 getStatesByApplicationIdShouldThrowIllegalArgumentExceptionWhenApplicationIdIsNull() { statesHistoryService.getStateHistoriesByApplicationId(null); }
@Test(expected = IllegalArgumentException.class) public void getStatesByApplicationIdShouldThrowIllegalArgumentExceptionWhenApplicationIsNull() { given(applicationsRepository.findOne(FIRST_ID)).willReturn(null); statesHistoryService.getStateHistoriesByApplicationId(FIRST_ID); }
@Test public void getStatesByApplicationIdShouldReturnAnEmptyList() { given(applicationsRepository.findOne(FIRST_ID)).willReturn( APPLICATION_ENTITY_WITHOUT_STATE_HISTORY); given(statesHistoryRepository .findByApplicationEntityOrderByCreationDateDesc(APPLICATION_ENTITY_WITHOUT_STATE_HISTORY)) .willReturn(EMPTY_STATE_HISTORY_ENTITY_LIST); given(converterService.convert(EMPTY_STATE_HISTORY_ENTITY_LIST, StateHistoryDTO.class)) .willReturn(EMPTY_STATE_HISTORY_VIEW_DTO_LIST); List<StateHistoryDTO> stateHistoryDTOList = statesHistoryService.getStateHistoriesByApplicationId(FIRST_ID); assertThat(stateHistoryDTOList, notNullValue()); assertThat(stateHistoryDTOList.isEmpty(), is(true)); then(applicationsRepository).should().findOne(FIRST_ID); then(statesHistoryRepository).should() .findByApplicationEntityOrderByCreationDateDesc(APPLICATION_ENTITY_WITHOUT_STATE_HISTORY); }
@Test public void getStatesByApplicationIdShouldReturnAnEmptyListForDeletedApplication() { given(applicationsRepository.findOne(FIRST_ID)).willReturn(deletedApplicationEntity); List<StateHistoryDTO> stateHistory = statesHistoryService.getStateHistoriesByApplicationId(FIRST_ID); assertThat(stateHistory, notNullValue()); assertThat(stateHistory.isEmpty(), is(true)); }
@Test public void getStatesByApplicationIdShouldReturnAListWithSingleElement() { given(applicationsRepository.findOne(FIRST_ID)).willReturn(firstApplicationEntity); given(statesHistoryRepository .findByApplicationEntityOrderByCreationDateDesc(firstApplicationEntity)) .willReturn(statesHistoryEntityListWithSingleElement); given(converterService .convert(statesHistoryEntityListWithSingleElement, StateHistoryDTO.class)) .willReturn(stateViewHistoryDTOListWithSingleElement); List<StateHistoryDTO> stateHistoryDTOList = statesHistoryService.getStateHistoriesByApplicationId(FIRST_ID); assertThat(stateHistoryDTOList, notNullValue()); assertThat(stateHistoryDTOList.isEmpty(), is(false)); assertThat(stateHistoryDTOList, equalTo(stateViewHistoryDTOListWithSingleElement)); then(applicationsRepository).should().findOne(FIRST_ID); then(statesHistoryRepository).should() .findByApplicationEntityOrderByCreationDateDesc(firstApplicationEntity); }
@Test public void getStatesByApplicationIdShouldReturnAListWithThreeElement() { given(applicationsRepository.findOne(FIRST_ID)).willReturn(firstApplicationEntity); given(statesHistoryRepository .findByApplicationEntityOrderByCreationDateDesc(firstApplicationEntity)) .willReturn(statesHistoryEntityListWithThreeElements); given(converterService .convert(statesHistoryEntityListWithThreeElements, StateHistoryDTO.class)) .willReturn(stateViewHistoryDTOListWithThreeElements); List<StateHistoryDTO> stateHistoryDTOList = statesHistoryService.getStateHistoriesByApplicationId(FIRST_ID); assertThat(stateHistoryDTOList, notNullValue()); assertThat(stateHistoryDTOList.isEmpty(), is(false)); assertThat(stateHistoryDTOList, equalTo(stateViewHistoryDTOListWithThreeElements)); then(applicationsRepository).should().findOne(FIRST_ID); then(statesHistoryRepository).should() .findByApplicationEntityOrderByCreationDateDesc(firstApplicationEntity); }