@Test public void testGetItemForceCacheUnsatisfiable() { when(TestRestServiceFactory.hnRestService.cachedItemRx(eq("1"))) .thenReturn(Observable.error(new IOException())); when(TestRestServiceFactory.hnRestService.itemRx(eq("1"))) .thenReturn(Observable.just(mock(HackerNewsItem.class))); client.getItem("1", ItemManager.MODE_CACHE, itemListener); verify(TestRestServiceFactory.hnRestService).cachedItemRx(eq("1")); verify(TestRestServiceFactory.hnRestService).itemRx(eq("1")); }
@Override public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener) { if (listener == null) { return; } Observable<HackerNewsItem> itemObservable; switch (cacheMode) { case MODE_DEFAULT: default: itemObservable = mRestService.itemRx(itemId); break; case MODE_NETWORK: itemObservable = mRestService.networkItemRx(itemId); break; case MODE_CACHE: itemObservable = mRestService.cachedItemRx(itemId) .onErrorResumeNext(mRestService.itemRx(itemId)); break; } Observable.defer(() -> Observable.zip( mSessionManager.isViewed(itemId), mFavoriteManager.check(itemId), itemObservable, (isViewed, favorite, hackerNewsItem) -> { if (hackerNewsItem != null) { hackerNewsItem.preload(); hackerNewsItem.setIsViewed(isViewed); hackerNewsItem.setFavorite(favorite); } return hackerNewsItem; })) .subscribeOn(mIoScheduler) .observeOn(mMainThreadScheduler) .subscribe(listener::onResponse, t -> listener.onError(t != null ? t.getMessage() : "")); }
HackerNewsClient implements ItemManager, UserManager { @Override public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener) { if (listener == null) { return; } Observable<HackerNewsItem> itemObservable; switch (cacheMode) { case MODE_DEFAULT: default: itemObservable = mRestService.itemRx(itemId); break; case MODE_NETWORK: itemObservable = mRestService.networkItemRx(itemId); break; case MODE_CACHE: itemObservable = mRestService.cachedItemRx(itemId) .onErrorResumeNext(mRestService.itemRx(itemId)); break; } Observable.defer(() -> Observable.zip( mSessionManager.isViewed(itemId), mFavoriteManager.check(itemId), itemObservable, (isViewed, favorite, hackerNewsItem) -> { if (hackerNewsItem != null) { hackerNewsItem.preload(); hackerNewsItem.setIsViewed(isViewed); hackerNewsItem.setFavorite(favorite); } return hackerNewsItem; })) .subscribeOn(mIoScheduler) .observeOn(mMainThreadScheduler) .subscribe(listener::onResponse, t -> listener.onError(t != null ? t.getMessage() : "")); } }
HackerNewsClient implements ItemManager, UserManager { @Override public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener) { if (listener == null) { return; } Observable<HackerNewsItem> itemObservable; switch (cacheMode) { case MODE_DEFAULT: default: itemObservable = mRestService.itemRx(itemId); break; case MODE_NETWORK: itemObservable = mRestService.networkItemRx(itemId); break; case MODE_CACHE: itemObservable = mRestService.cachedItemRx(itemId) .onErrorResumeNext(mRestService.itemRx(itemId)); break; } Observable.defer(() -> Observable.zip( mSessionManager.isViewed(itemId), mFavoriteManager.check(itemId), itemObservable, (isViewed, favorite, hackerNewsItem) -> { if (hackerNewsItem != null) { hackerNewsItem.preload(); hackerNewsItem.setIsViewed(isViewed); hackerNewsItem.setFavorite(favorite); } return hackerNewsItem; })) .subscribeOn(mIoScheduler) .observeOn(mMainThreadScheduler) .subscribe(listener::onResponse, t -> listener.onError(t != null ? t.getMessage() : "")); } @Inject  HackerNewsClient(RestServiceFactory factory,
                            SessionManager sessionManager,
                            FavoriteManager favoriteManager); }
HackerNewsClient implements ItemManager, UserManager { @Override public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener) { if (listener == null) { return; } Observable<HackerNewsItem> itemObservable; switch (cacheMode) { case MODE_DEFAULT: default: itemObservable = mRestService.itemRx(itemId); break; case MODE_NETWORK: itemObservable = mRestService.networkItemRx(itemId); break; case MODE_CACHE: itemObservable = mRestService.cachedItemRx(itemId) .onErrorResumeNext(mRestService.itemRx(itemId)); break; } Observable.defer(() -> Observable.zip( mSessionManager.isViewed(itemId), mFavoriteManager.check(itemId), itemObservable, (isViewed, favorite, hackerNewsItem) -> { if (hackerNewsItem != null) { hackerNewsItem.preload(); hackerNewsItem.setIsViewed(isViewed); hackerNewsItem.setFavorite(favorite); } return hackerNewsItem; })) .subscribeOn(mIoScheduler) .observeOn(mMainThreadScheduler) .subscribe(listener::onResponse, t -> listener.onError(t != null ? t.getMessage() : "")); } @Inject  HackerNewsClient(RestServiceFactory factory,
                            SessionManager sessionManager,
                            FavoriteManager favoriteManager); @Override void getStories(@FetchMode String filter, @CacheMode int cacheMode,
                           final ResponseListener<Item[]> listener); @Override void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener); @Override Item[] getStories(String filter, @CacheMode int cacheMode); @Override Item getItem(String itemId, @CacheMode int cacheMode); @Override void getUser(String username, final ResponseListener<User> listener); }
HackerNewsClient implements ItemManager, UserManager { @Override public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener) { if (listener == null) { return; } Observable<HackerNewsItem> itemObservable; switch (cacheMode) { case MODE_DEFAULT: default: itemObservable = mRestService.itemRx(itemId); break; case MODE_NETWORK: itemObservable = mRestService.networkItemRx(itemId); break; case MODE_CACHE: itemObservable = mRestService.cachedItemRx(itemId) .onErrorResumeNext(mRestService.itemRx(itemId)); break; } Observable.defer(() -> Observable.zip( mSessionManager.isViewed(itemId), mFavoriteManager.check(itemId), itemObservable, (isViewed, favorite, hackerNewsItem) -> { if (hackerNewsItem != null) { hackerNewsItem.preload(); hackerNewsItem.setIsViewed(isViewed); hackerNewsItem.setFavorite(favorite); } return hackerNewsItem; })) .subscribeOn(mIoScheduler) .observeOn(mMainThreadScheduler) .subscribe(listener::onResponse, t -> listener.onError(t != null ? t.getMessage() : "")); } @Inject  HackerNewsClient(RestServiceFactory factory,
                            SessionManager sessionManager,
                            FavoriteManager favoriteManager); @Override void getStories(@FetchMode String filter, @CacheMode int cacheMode,
                           final ResponseListener<Item[]> listener); @Override void getItem(final String itemId, @CacheMode int cacheMode, ResponseListener<Item> listener); @Override Item[] getStories(String filter, @CacheMode int cacheMode); @Override Item getItem(String itemId, @CacheMode int cacheMode); @Override void getUser(String username, final ResponseListener<User> listener); static final String HOST; static final String BASE_WEB_URL; static final String WEB_ITEM_PATH; }