@Test public void getPhoto_shouldCacheAfterFirstRemoteCall() { final Photo photo = new Photo(10, 20, "abc"); repository.getPhoto(1, loadPhotoCallback); verify(localDataSource, times(1)).getPhoto(eq(1), photoCallbackCaptor.capture()); photoCallbackCaptor.getValue().onPhotoNotAvailable(); verify(remoteDataSource, times(1)).getPhoto(eq(1), photoCallbackCaptor.capture()); photoCallbackCaptor.getValue().onPhotoLoaded(photo); assertThat(repository.cachedPhotos.size(), is(1)); repository.getPhoto(1, loadPhotoCallback); verify(remoteDataSource, times(1)) .getPhoto(anyInt(), any(PhotoDataSource.LoadPhotoCallback.class)); }
@Override public void getPhoto(int photoId, @NonNull LoadPhotoCallback callback) { Photo cachedPhoto = getPhotoFromCache(photoId); if (cachedPhoto != null) { callback.onPhotoLoaded(cachedPhoto); return; } photoLocalDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { photoRemoteDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { callback.onPhotoNotAvailable(); } }); } }); }
PhotoRepository implements PhotoDataSource { @Override public void getPhoto(int photoId, @NonNull LoadPhotoCallback callback) { Photo cachedPhoto = getPhotoFromCache(photoId); if (cachedPhoto != null) { callback.onPhotoLoaded(cachedPhoto); return; } photoLocalDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { photoRemoteDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { callback.onPhotoNotAvailable(); } }); } }); } }
PhotoRepository implements PhotoDataSource { @Override public void getPhoto(int photoId, @NonNull LoadPhotoCallback callback) { Photo cachedPhoto = getPhotoFromCache(photoId); if (cachedPhoto != null) { callback.onPhotoLoaded(cachedPhoto); return; } photoLocalDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { photoRemoteDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { callback.onPhotoNotAvailable(); } }); } }); } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); }
PhotoRepository implements PhotoDataSource { @Override public void getPhoto(int photoId, @NonNull LoadPhotoCallback callback) { Photo cachedPhoto = getPhotoFromCache(photoId); if (cachedPhoto != null) { callback.onPhotoLoaded(cachedPhoto); return; } photoLocalDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { photoRemoteDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { callback.onPhotoNotAvailable(); } }); } }); } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); @Override void getPhotos(@NonNull LoadPhotosCallback callback); @Override void getPhoto(int photoId, @NonNull LoadPhotoCallback callback); @Override void savePhoto(@NonNull Photo photo); @Override void deletePhoto(int photoId); @Override void deleteAllPhotos(); @Override void refreshPhotos(); }
PhotoRepository implements PhotoDataSource { @Override public void getPhoto(int photoId, @NonNull LoadPhotoCallback callback) { Photo cachedPhoto = getPhotoFromCache(photoId); if (cachedPhoto != null) { callback.onPhotoLoaded(cachedPhoto); return; } photoLocalDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { photoRemoteDataSource.getPhoto(photoId, new LoadPhotoCallback() { @Override public void onPhotoLoaded(Photo photo) { cachedPhotos.put(photo.getPhotoId(), photo); callback.onPhotoLoaded(photo); } @Override public void onPhotoNotAvailable() { callback.onPhotoNotAvailable(); } }); } }); } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); @Override void getPhotos(@NonNull LoadPhotosCallback callback); @Override void getPhoto(int photoId, @NonNull LoadPhotoCallback callback); @Override void savePhoto(@NonNull Photo photo); @Override void deletePhoto(int photoId); @Override void deleteAllPhotos(); @Override void refreshPhotos();  }