AuthAwareFavoritaDataSource implements FavoritaDataSource { <T> Observable<T> errorWhenLoggedIn(Throwable throwable) { if (throwable instanceof AuthException) { return userDataSource.getCurrentUser() .flatMap(user -> Observable.error(throwable)); } else { return Observable.error(throwable); } } AuthAwareFavoritaDataSource(FavoritaDataSource authenticatedDataSource, UserDataSource userDataSource); @Override Observable<List<Favorita>> getFavoritas(); @Override Observable<Favorita> saveFavorita(Favorita favorita); @Override Observable<Favorita> getFavoritaById(Integer idParada); @Override Observable<Integer> deleteFavorita(Integer idParada); @Override Observable<List<Favorita>> saveFavoritas(List<Favorita> favoritas); @Override Observable<List<Favorita>> replaceFavoritas(List<Favorita> favoritas);  }
@Test public void return_error_when_unknown_exception() throws Exception { TestException anyError = new TestException(); TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); dataSource.errorWhenLoggedIn(anyError) .subscribe(testSubscriber); testSubscriber.assertError(TestException.class); }
@Test public void return_empty_when_authexception_and_current_user_empty() throws Exception { when(userDataSource.getCurrentUser()).thenReturn(empty()); AuthException authError = new AuthException(); TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); dataSource.errorWhenLoggedIn(authError) .subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertNoValues(); testSubscriber.assertCompleted(); }
@Test public void return_auth_error_when_authexception_and_current_user_exist() throws Exception { when(userDataSource.getCurrentUser()).thenReturn(just(new SevibusUser())); AuthException authError = new AuthException(); TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); dataSource.errorWhenLoggedIn(authError) .subscribe(testSubscriber); testSubscriber.assertError(AuthException.class); }