SearchServerInteractor extends ObservableInteractor<Server, SearchServerInteractor.Params> { @Override protected Observable<Server> buildReactiveStream(Params params) { if (params == null) { return Observable.error(new IllegalArgumentException("Params must be provided")); } NetworkAddress networkAddress = params.networkAddress; if (networkAddress != null && !networkAddressValidator.isValid(networkAddress)) { return Observable.error(new IllegalArgumentException("Invalid IP Address")); } return networkAddress != null ? searchServerService.searchServer(networkAddress) : searchServerService.searchServer(); } SearchServerInteractor(SearchServerService searchServerService,
      NotifyServerFoundService notifyServerFoundService,
      Validator<NetworkAddress> networkAddressValidator, ThreadExecutor threadExecutor,
      PostExecutionThread postExecutionThread); @SuppressWarnings("unchecked") @Override void execute(DisposableObserver observer, Params params); @Override void dispose();  }
@Test public void shouldBuildReactiveStreamForEmptyParams() throws Exception { SearchServerInteractor.Params emptyParams = SearchServerInteractor.Params.empty(); searchServerInteractor.buildReactiveStream(emptyParams); verify(searchServerService).searchServer(); verifyNoMoreInteractions(searchServerService); verifyZeroInteractions(mockThreadExecutor); verifyZeroInteractions(mockPostExecutionThread); }
@Test public void shouldBuildReactiveStreamForValidNetworkAddress() throws Exception { NetworkAddress networkAddress = NetworkAddress.builder() .ip("192.168.1.44") .port(9998) .build(); SearchServerInteractor.Params params = SearchServerInteractor.Params.forNetworkAddress(networkAddress); when(mockNetworkAddressValidator.isValid(networkAddress)).thenReturn(true); searchServerInteractor.buildReactiveStream(params); verify(searchServerService, times(1)).searchServer(networkAddress); verifyNoMoreInteractions(searchServerService); }
@Test public void shouldNotBuildReactiveStreamGivenNullParams() throws Exception { Observable<Server> serverObservable = searchServerInteractor.buildReactiveStream(null); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertErrorMessage("Params must be provided"); }
@Test public void shouldNotBuildReactiveStreamForInvalidNetworkAddress() throws Exception { NetworkAddress networkAddress = NetworkAddress.builder() .ip("") .port(-1) .build(); SearchServerInteractor.Params params = SearchServerInteractor.Params.forNetworkAddress(networkAddress); when(mockNetworkAddressValidator.isValid(networkAddress)).thenReturn(false); Observable<Server> serverObservable = searchServerInteractor.buildReactiveStream(params); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertErrorMessage("Invalid IP Address"); verifyZeroInteractions(searchServerService); }
@Test public void shoulNotReturnInvalidReactiveStreamWhenValidWorldFound() throws Exception { SearchServerInteractor.Params emptyParams = SearchServerInteractor.Params.empty(); when(searchServerService.searchServer()).thenReturn(getValidWorldReactiveStream()); Observable reactiveStream = searchServerInteractor.buildReactiveStream(emptyParams); assertThat(reactiveStream).isNotNull(); }
@Test public void shouldNotReturnInvalidReactiveStreamWhenNoWorldFound() throws Exception { SearchServerInteractor.Params emptyParams = SearchServerInteractor.Params.empty(); when(searchServerService.searchServer()).thenReturn(Observable.empty()); Observable reactiveStream = searchServerInteractor.buildReactiveStream(emptyParams); assertThat(reactiveStream).isNotNull(); }
@Test public void shouldReturnEmptyReactiveStreamWhenNoWorldFound() throws Exception { SearchServerInteractor.Params emptyParams = SearchServerInteractor.Params.empty(); when(searchServerService.searchServer()).thenReturn(Observable.empty()); Observable reactiveStream = searchServerInteractor.buildReactiveStream(emptyParams); assertThat(getEmptyReactiveStream()).isEqualTo(reactiveStream); }