StateFlowServiceImpl implements StateFlowService { @Transactional(readOnly = true) @Override public Collection<StateFlowDTO> getStateFlowDTOByFromStateDTO(StateDTO statesDTO) { Assert.notNull(statesDTO); Assert.notNull(statesDTO.getId()); Assert.notNull(statesDTO.getName()); List<StateFlowEntity> stateFlowEntities = stateFlowRepository.findByFromStateEntity(converterService.convert(statesDTO, StatesEntity.class)); return converterService.convert(stateFlowEntities, StateFlowDTO.class); }  @Transactional(readOnly = true) @Override Collection<StateFlowDTO> getStateFlowDTOByFromStateDTO(StateDTO statesDTO);  }
@Test(expected = IllegalArgumentException.class) public void getStateFlowDTOByFromStateDTOShouldThrowIllegalArgumentExceptionWHenStateDtoIsNull() { stateFlowService.getStateFlowDTOByFromStateDTO(null); }
@Test(expected = IllegalArgumentException.class) public void getStateFlowDTOByFromStateDTOShouldThrowIllegalArgumentExceptionWhenIdIsNullInStateDto() { StateDTO stateDTO = StateDTO.builder().id(null).name(FIRST_STATE_NAME).build(); stateFlowService.getStateFlowDTOByFromStateDTO(stateDTO); }
@Test(expected = IllegalArgumentException.class) public void getStateFlowDTOByFromStateDTOShouldThrowIllegalArgumentExceptionWhenNameIsNullInStateDto() { StateDTO stateDTO = StateDTO.builder().id(FIRST_STATE_ID).name(null).build(); stateFlowService.getStateFlowDTOByFromStateDTO(stateDTO); }
@Test public void getStateFlowDTOByFromStateDTOShouldReturnEmptyListWhenInvalidStateDtoIsGiven() { StateDTO invalidStateDTO = StateDTO.builder().id(INVALID_STATE_ID).name(INVALID_STATE_NAME).build(); StatesEntity invalidStatesEntity = StatesEntity.builder().id(INVALID_STATE_ID).name(INVALID_STATE_NAME).build(); given(converterService.convert(invalidStateDTO, StatesEntity.class)) .willReturn(invalidStatesEntity); given(stateFlowRepository.findByFromStateEntity(invalidStatesEntity)) .willReturn(EMPTY_STATE_FLOW_ENTITY_LIST); Collection<StateFlowDTO> result = stateFlowService.getStateFlowDTOByFromStateDTO(invalidStateDTO); assertThat(result, notNullValue()); assertThat(result, empty()); then(converterService).should().convert(invalidStateDTO, StatesEntity.class); then(stateFlowRepository).should().findByFromStateEntity(invalidStatesEntity); }
@Test public void getStateFlowDTOByFromStateDTOShouldReturnListWithSingleElementWhenStateDtoHasSingleStateFlow() { StateDTO stateDTO = StateDTO.builder().id(STATE_WITH_SINGLE_STATE_FLOW_ID).name( STATE_WITH_SINGLE_STATE_FLOW_NAME).build(); StatesEntity statesEntity = StatesEntity.builder().id(STATE_WITH_SINGLE_STATE_FLOW_ID) .name(STATE_WITH_SINGLE_STATE_FLOW_NAME).build(); StatesEntity stateEntityInStateFlowEntityList = StatesEntity.builder().id(FIRST_STATE_ID).name(FIRST_STATE_NAME).build(); StateDTO stateDTOInStateFlowDTOList = StateDTO.builder().id(FIRST_STATE_ID).name(FIRST_STATE_NAME).build(); List<StateFlowEntity> singleElementStateFlowEntityList = Collections.singletonList( StateFlowEntity.builder().fromStateEntity(statesEntity) .toStateEntity(stateEntityInStateFlowEntityList) .build() ); List<StateFlowDTO> expectedSingleElementStateFlowDtoList = Collections.singletonList( StateFlowDTO.builder().fromStateDTO(stateDTO).toStateDTO(stateDTOInStateFlowDTOList) .build() ); given(converterService.convert(stateDTO, StatesEntity.class)).willReturn(statesEntity); given(stateFlowRepository.findByFromStateEntity(statesEntity)) .willReturn(singleElementStateFlowEntityList); given(converterService.convert(singleElementStateFlowEntityList, StateFlowDTO.class)) .willReturn(expectedSingleElementStateFlowDtoList); Collection<StateFlowDTO> result = stateFlowService.getStateFlowDTOByFromStateDTO(stateDTO); assertThat(result, notNullValue()); assertThat(result, is(expectedSingleElementStateFlowDtoList)); then(converterService).should().convert(stateDTO, StatesEntity.class); then(stateFlowRepository).should().findByFromStateEntity(statesEntity); then(converterService).should().convert(singleElementStateFlowEntityList, StateFlowDTO.class); }
@Test public void getStateFlowDTOByFromStateDTOShouldReturnListWithThreeElementWhenStateDtoHasThreeStateFlow() { StateDTO stateDTO = StateDTO.builder().id(STATE_WITH_THREE_STATE_FLOW_ID).name( STATE_WITH_THREE_STATE_FLOW_NAME).build(); StatesEntity statesEntity = StatesEntity.builder().id(STATE_WITH_THREE_STATE_FLOW_ID) .name(STATE_WITH_THREE_STATE_FLOW_NAME).build(); List<StatesEntity> statesEntitiesInStateFlowEntitiesList = Arrays.asList( StatesEntity.builder().id(FIRST_STATE_ID).name(FIRST_STATE_NAME).build(), StatesEntity.builder().id(SECOND_STATE_ID).name(SECOND_STATE_NAME).build(), StatesEntity.builder().id(THIRD_STATE_ID).name(THIRD_STATE_NAME).build() ); List<StateDTO> stateDTOsInStateFlowDTOsList = Arrays.asList( StateDTO.builder().id(FIRST_STATE_ID).name(FIRST_STATE_NAME).build(), StateDTO.builder().id(SECOND_STATE_ID).name(SECOND_STATE_NAME).build(), StateDTO.builder().id(THIRD_STATE_ID).name(THIRD_STATE_NAME).build() ); List<StateFlowEntity> threeElementStateFlowEntityList = Arrays.asList( StateFlowEntity.builder().fromStateEntity(statesEntity) .toStateEntity(statesEntitiesInStateFlowEntitiesList.get(0)) .build(), StateFlowEntity.builder().fromStateEntity(statesEntity) .toStateEntity(statesEntitiesInStateFlowEntitiesList.get(1)) .build(), StateFlowEntity.builder().fromStateEntity(statesEntity) .toStateEntity(statesEntitiesInStateFlowEntitiesList.get(2)) .build() ); List<StateFlowDTO> expectedThreeElementStateFlowDtoList = Arrays.asList( StateFlowDTO.builder().fromStateDTO(stateDTO) .toStateDTO(stateDTOsInStateFlowDTOsList.get(0)).build(), StateFlowDTO.builder().fromStateDTO(stateDTO) .toStateDTO(stateDTOsInStateFlowDTOsList.get(1)).build(), StateFlowDTO.builder().fromStateDTO(stateDTO) .toStateDTO(stateDTOsInStateFlowDTOsList.get(2)).build() ); given(converterService.convert(stateDTO, StatesEntity.class)).willReturn(statesEntity); given(stateFlowRepository.findByFromStateEntity(statesEntity)) .willReturn(threeElementStateFlowEntityList); given(converterService .convert(statesEntity, StateDTO.class)) .willReturn(stateDTO); given(converterService.convert(threeElementStateFlowEntityList, StateFlowDTO.class)) .willReturn(expectedThreeElementStateFlowDtoList); Collection<StateFlowDTO> result = stateFlowService.getStateFlowDTOByFromStateDTO(stateDTO); assertThat(result, notNullValue()); assertThat(result, is(expectedThreeElementStateFlowDtoList)); then(converterService).should().convert(stateDTO, StatesEntity.class); then(stateFlowRepository).should().findByFromStateEntity(statesEntity); then(converterService).should().convert(threeElementStateFlowEntityList, StateFlowDTO.class); }