NetworkDataProvider implements NetworkActionProvider { @Override public Observable<Server> searchServer() { return serverSearchManager.searchServer().map(serverEntityDataMapper::transform); } @Inject  NetworkDataProvider(ServerSearchManager serverSearchManager,
      ServerEntityDataMapper serverEntityDataMapper,
      NetworkAddressEntityDataMapper networkAddressEntityDataMapper); @Override Observable<Server> searchServer(); @Override Observable<Server> searchServer(NetworkAddress networkAddress);  }
@Test public void shouldReturnValidServer() throws Exception { ServerEntity serverEntity = getServerEntity(); Server server = getServer(); Observable<ServerEntity> observable = Observable.create(subscriber -> { subscriber.onNext(serverEntity); subscriber.onComplete(); }); when(mockServerSearchManager.searchServer()).thenReturn(observable); when(mockServerEntityDataMapper.transform(serverEntity)).thenReturn(server); Observable<Server> serverObservable = networkActionProvider.searchServer(); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertNoErrors(); testObserver.assertComplete(); testObserver.assertResult(server); verify(mockServerSearchManager, times(1)).searchServer(); verify(mockServerEntityDataMapper, times(1)).transform(serverEntity); verifyNoMoreInteractions(mockServerSearchManager, mockServerEntityDataMapper); }
@Test public void shouldNotReturnAnyServer() throws Exception { when(mockServerSearchManager.searchServer()).thenReturn(Observable.never()); Observable<Server> serverObservable = networkActionProvider.searchServer(); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertEmpty(); testObserver.assertNotComplete(); verify(mockServerSearchManager, times(1)).searchServer(); verifyZeroInteractions(mockServerEntityDataMapper); verifyNoMoreInteractions(mockServerSearchManager); }
@Test public void shouldReturnServerForAGivenNetworkAddress() throws Exception { NetworkAddress networkAddress = NetworkAddress.builder() .ip("192.168.1.1") .port(9998) .build(); NetworkAddressEntity networkAddressEntity = NetworkAddressEntity.builder() .ip("192.168.1.1") .port(9998) .build(); ServerEntity serverEntity = getServerEntity(); Server server = getServer(); Observable<ServerEntity> observable = Observable.create(subscriber -> { subscriber.onNext(serverEntity); subscriber.onComplete(); }); when(mockNetworkAddressEntityDataMapper.transformInverse(networkAddress)).thenReturn( networkAddressEntity); when(mockServerSearchManager.searchServer(networkAddressEntity)).thenReturn(observable); when(mockServerEntityDataMapper.transform(serverEntity)).thenReturn(server); Observable<Server> serverObservable = networkActionProvider.searchServer(networkAddress); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertNoErrors(); testObserver.assertComplete(); testObserver.assertResult(server); verify(mockServerSearchManager, times(1)).searchServer(networkAddressEntity); verify(mockServerEntityDataMapper, times(1)).transform(serverEntity); verifyNoMoreInteractions(mockServerSearchManager, mockServerEntityDataMapper); }
@Test public void shouldNotReturnAnyServerForACertainNetworkAddress() throws Exception { NetworkAddress networkAddress = NetworkAddress.builder() .ip("192.168.1.435") .port(9998) .build(); NetworkAddressEntity networkAddressEntity = NetworkAddressEntity.builder() .ip("192.168.1.435") .port(9998) .build(); when(mockNetworkAddressEntityDataMapper.transformInverse(networkAddress)).thenReturn( networkAddressEntity); when(mockServerSearchManager.searchServer(networkAddressEntity)).thenReturn(Observable.never()); Observable<Server> serverObservable = networkActionProvider.searchServer(networkAddress); TestObserver<Server> testObserver = serverObservable.test(); testObserver.assertEmpty(); testObserver.assertNotComplete(); verify(mockServerSearchManager, times(1)).searchServer(networkAddressEntity); verifyZeroInteractions(mockServerEntityDataMapper); verifyNoMoreInteractions(mockServerSearchManager); }