PlacesViewModel implements IPlacesViewModel { @Override public Observable<Boolean> fetchAllPlaces() { return mPlacesApi.placesResult() .map(googleSearchResult -> { allPlaces = googleSearchResult.results; mPlaces.onNext(googleSearchResult.results); return true; }) .timeout(TIME_OUT, TimeUnit.SECONDS) .retry(RETRY); } PlacesViewModel(@NonNull IPlacesApi placesApi); Observable<List<Place>> currentPlaces(); @Override Observable<Boolean> fetchAllPlaces(); @Override void filterPlacesByType(String type);  }
@Test public void fetchAllPlacesSuccess() { placesViewModel.fetchAllPlaces().subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); testSubscriber.assertReceivedOnNext(Collections.singletonList(true)); }
@Test public void fetchAllPlaces() { placesViewModel.fetchAllPlaces().subscribe(); placesViewModel.currentPlaces().subscribe(testSubscriberPlaces); testSubscriberPlaces.assertNoErrors(); List<List<Place>> lists = testSubscriberPlaces.getOnNextEvents(); assertEquals(lists.size(), 1); assertEquals(lists.get(0).size(), 10); }
@Test public void fetchTimeout() throws Exception { when(placesApi.placesResult()).thenReturn( Observable.create(subscriber -> { try { Thread.sleep(5100); subscriber.onNext(TestDataUtils.nearByData()); subscriber.onCompleted(); } catch (InterruptedException e) { subscriber.onError(e); } }) ); try { boolean success = placesViewModel.fetchAllPlaces().toBlocking().first(); if (success) fail("Should be timeout"); } catch (Exception ignored) { } }
@Test public void fetchOnTime() throws Exception { when(placesApi.placesResult()).thenReturn( Observable.create(subscriber -> { try { Thread.sleep(4900); subscriber.onNext(TestDataUtils.nearByData()); subscriber.onCompleted(); } catch (InterruptedException e) { subscriber.onError(e); } }) ); try { boolean success = placesViewModel.fetchAllPlaces().toBlocking().first(); assertTrue(success); } catch (Exception ignored) { fail("Should be on time"); } }
@Test public void fetchRetry() throws Exception { AtomicInteger atomicInteger = new AtomicInteger(0); when(placesApi.placesResult()).thenReturn( Observable.create(subscriber -> { if (atomicInteger.getAndIncrement() < 3) { subscriber.onError(new Exception()); } else { subscriber.onNext(TestDataUtils.nearByData()); subscriber.onCompleted(); } }) ); try { boolean success = placesViewModel.fetchAllPlaces().toBlocking().first(); verify(placesApi).placesResult(); assertTrue(success); } catch (Exception ignored) { fail("Have to retry three times"); } }
@Test public void fetchExceedRetry() throws Exception { AtomicInteger atomicInteger = new AtomicInteger(0); when(placesApi.placesResult()).thenReturn( Observable.create(subscriber -> { if (atomicInteger.getAndIncrement() < 4) { subscriber.onError(new Exception()); } else { subscriber.onNext(TestDataUtils.nearByData()); subscriber.onCompleted(); } }) ); try { placesViewModel.fetchAllPlaces().toBlocking().first(); fail("Should be out of retry"); } catch (Exception ignored) { } }