CharacterRepository implements CharactersDomain.Repository { @Override public Observable<List<GoTCharacter>> getList() { Log.i(TAG, "getList()"); Observable<List<GoTCharacter>> observable; if (localDataSource.isExpired()) { observable = networkDataSource.getAll() .doOnNext(goTCharacters -> { Log.i(TAG, "will save on cache"); localDataSource.removeAll(goTCharacters); localDataSource.save(goTCharacters); Log.i(TAG, "cached network in memory"); }) .retry((attempts, error) -> error instanceof Exception && attempts < 2) .onErrorResumeNext(throwable -> { Log.i(TAG, "some network error, return cache"); return localDataSource.getAll(); }); Log.i(TAG, "fetching from network"); } else { observable = localDataSource.getAll(); Log.i(TAG, "fetching from local"); } return observable; } CharacterRepository(CharactersDomain.NetworkDataSource networkDataSource,
                             CharactersDomain.LocalDataSource localDataSource); @Override Observable<List<GoTCharacter>> getList(); @Override Observable<GoTCharacter> read(GoTCharacter entity); @Override Observable<List<GoTCharacter>> read(GoTHouse house);  }
@Test public void get_all_characters_from_network_and_save_in_cache() { TestSubscriber<List<GoTCharacter>> testSubscriber = new TestSubscriber<>(); when(localDataSource.isExpired()).thenReturn(EXPIRED); when(networkDataSource.getAll()).thenReturn(getCharactersObservable(10)); repository.getList() .subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); testSubscriber.assertValueCount(1); verify(localDataSource).save(any()); verify(networkDataSource).getAll(); }
@Test public void get_from_local_when_save_throw_an_exception() { TestSubscriber<List<GoTCharacter>> testSubscriber = new TestSubscriber<>(); when(localDataSource.isExpired()).thenReturn(EXPIRED); when(networkDataSource.getAll()).thenReturn(getCharactersObservable(10)); when(localDataSource.getAll()).thenReturn(getCharactersObservable(1)); doThrow(Exception.class).when(localDataSource).save(any(List.class)); repository.getList() .subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); verify(localDataSource).getAll(); }
@Test public void get_from_local_when_cache_is_not_expired() { TestSubscriber<List<GoTCharacter>> testSubscriber = new TestSubscriber<>(); when(localDataSource.isExpired()).thenReturn(NOT_EXPIRED); when(localDataSource.getAll()).thenReturn(getCharactersObservable(0)); repository.getList() .subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); verify(localDataSource).getAll(); verify(networkDataSource, never()).getAll(); }
@Test public void get_from_local_when_remoteDataSource_fail() { TestSubscriber<List<GoTCharacter>> testSubscriber = new TestSubscriber<>(); when(localDataSource.isExpired()).thenReturn(EXPIRED); when(networkDataSource.getAll()).thenReturn(getErrorObservable()); when(localDataSource.getAll()).thenReturn(getCharactersObservable(10)); repository.getList() .subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); verify(networkDataSource).getAll(); verify(localDataSource).getAll(); }