diff --git a/app/build.gradle b/app/build.gradle index 5eed9f9..06b411c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,6 +28,10 @@ android { abortOnError false disable 'InvalidPackage' } + + packagingOptions { + exclude 'META-INF/rxjava.properties' + } } dependencies { @@ -40,21 +44,20 @@ dependencies { compile libraries.supportDesign compile libraries.retrofit compile libraries.retrofitGsonConverter - compile libraries.retrofitRx + compile libraries.retrofitRx2 compile libraries.picasso compile libraries.guava annotationProcessor libraries.immutablesValue // <-- for annotation processor provided libraries.immutablesValue // <-- for annotation API provided libraries.immutablesGson // for annotations - compile 'com.nytimes.android:store:2.0.2' - compile'com.nytimes.android:cache:2.0.2' - compile'com.nytimes.android:middleware:2.0.2' - compile 'com.nytimes.android:filesystem:2.0.2' - //compile project(path: ':store') - //compile project(path: ':cache') - //compile project(path: ':middleware') - //compile project(path: ':filesystem') + //compile 'com.nytimes.android:store:2.0.2' + //compile'com.nytimes.android:cache:2.0.2' + //compile'com.nytimes.android:middleware:2.0.2' + //compile 'com.nytimes.android:filesystem:2.0.2' + compile project(path: ':store') + compile project(path: ':cache') + compile project(path: ':middleware') + compile project(path: ':filesystem') retrolambdaConfig libraries.retrolambda - compile libraries.rxAndroid - + compile libraries.rxAndroid2 } diff --git a/app/src/main/java/com/nytimes/android/sample/SampleApp.java b/app/src/main/java/com/nytimes/android/sample/SampleApp.java index c50a9dd..a3618b0 100644 --- a/app/src/main/java/com/nytimes/android/sample/SampleApp.java +++ b/app/src/main/java/com/nytimes/android/sample/SampleApp.java @@ -1,12 +1,10 @@ package com.nytimes.android.sample; import android.app.Application; -import android.util.Log; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.nytimes.android.external.fs.SourcePersisterFactory; -import com.nytimes.android.external.store.base.Fetcher; import com.nytimes.android.external.store.base.Persister; import com.nytimes.android.external.store.base.impl.BarCode; import com.nytimes.android.external.store.base.impl.MemoryPolicy; @@ -20,13 +18,11 @@ import com.nytimes.android.sample.data.remote.Api; import java.io.IOException; import java.util.concurrent.TimeUnit; -import javax.annotation.Nonnull; - +import io.reactivex.Observable; import okio.BufferedSource; -import retrofit2.GsonConverterFactory; import retrofit2.Retrofit; -import retrofit2.RxJavaCallAdapterFactory; -import rx.Observable; +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; +import retrofit2.converter.gson.GsonConverterFactory; public class SampleApp extends Application { @@ -63,11 +59,11 @@ public class SampleApp extends Application { return StoreBuilder.barcode() .fetcher(barCode -> provideRetrofit().fetchSubreddit(barCode.getKey(), "10")) .memoryPolicy( - MemoryPolicy - .builder() - .setExpireAfter(10) - .setExpireAfterTimeUnit(TimeUnit.SECONDS) - .build() + MemoryPolicy + .builder() + .setExpireAfter(10) + .setExpireAfterTimeUnit(TimeUnit.SECONDS) + .build() ) .open(); } @@ -93,7 +89,7 @@ public class SampleApp extends Application { return new Retrofit.Builder() .baseUrl("http://reddit.com/") .addConverterFactory(GsonConverterFactory.create(provideGson())) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .validateEagerly(BuildConfig.DEBUG) // Fail early: check Retrofit configuration at creation time in Debug build. .build() .create(Api.class); diff --git a/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java b/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java index 74e1bf2..ab26458 100644 --- a/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java +++ b/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java @@ -18,9 +18,9 @@ import com.nytimes.android.sample.reddit.PostAdapter; import java.util.List; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; import static android.widget.Toast.makeText; @@ -52,6 +52,7 @@ public class PersistingStoreActivity extends AppCompatActivity { } } + @SuppressWarnings("CheckReturnValue") public void loadPosts() { BarCode awwRequest = new BarCode(RedditData.class.getSimpleName(), "aww"); @@ -74,7 +75,7 @@ public class PersistingStoreActivity extends AppCompatActivity { } private Observable sanitizeData(RedditData redditData) { - return Observable.from(redditData.data().children()) + return Observable.fromIterable(redditData.data().children()) .map(Children::data); } diff --git a/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java b/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java index 9d4ace9..fbe295e 100644 --- a/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java +++ b/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java @@ -19,9 +19,12 @@ import com.nytimes.android.sample.reddit.PostAdapter; import java.util.List; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Function; +import io.reactivex.schedulers.Schedulers; import static android.widget.Toast.makeText; @@ -51,12 +54,18 @@ public class StoreActivity extends AppCompatActivity { } } + @SuppressWarnings("CheckReturnValue") public void loadPosts() { BarCode awwRequest = new BarCode(RedditData.class.getSimpleName(), "aww"); this.nonPersistedStore .get(awwRequest) - .flatMap(this::sanitizeData) + .flatMap(new Function>() { + @Override + public ObservableSource apply(@NonNull RedditData redditData) throws Exception { + return sanitizeData(redditData); + } + }) .toList() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -74,7 +83,7 @@ public class StoreActivity extends AppCompatActivity { } private Observable sanitizeData(RedditData redditData) { - return Observable.from(redditData.data().children()) + return Observable.fromIterable(redditData.data().children()) .map(Children::data); } diff --git a/app/src/main/java/com/nytimes/android/sample/data/remote/Api.java b/app/src/main/java/com/nytimes/android/sample/data/remote/Api.java index a10be87..9c8e172 100644 --- a/app/src/main/java/com/nytimes/android/sample/data/remote/Api.java +++ b/app/src/main/java/com/nytimes/android/sample/data/remote/Api.java @@ -2,12 +2,11 @@ package com.nytimes.android.sample.data.remote; import com.nytimes.android.sample.data.model.RedditData; -import okhttp3.Response; +import io.reactivex.Observable; import okhttp3.ResponseBody; import retrofit2.http.GET; import retrofit2.http.Path; import retrofit2.http.Query; -import rx.Observable; public interface Api { diff --git a/app/src/test/java/com/nytimes/android/sample/StoreIntegrationTest.java b/app/src/test/java/com/nytimes/android/sample/StoreIntegrationTest.java index 3da9890..96dd6a0 100644 --- a/app/src/test/java/com/nytimes/android/sample/StoreIntegrationTest.java +++ b/app/src/test/java/com/nytimes/android/sample/StoreIntegrationTest.java @@ -7,7 +7,8 @@ import com.nytimes.android.external.store.base.impl.StoreBuilder; import org.junit.Before; import org.junit.Test; -import rx.Observable; + +import io.reactivex.Observable; import static junit.framework.Assert.assertEquals; @@ -36,7 +37,7 @@ public class StoreIntegrationTest { @Test public void testRepeatedGet() throws Exception { - String first = testStore.get(BarCode.empty()).toBlocking().first(); + String first = testStore.get(BarCode.empty()).blockingFirst(); assertEquals(first, "hello"); } diff --git a/buildsystem/dependencies.gradle b/buildsystem/dependencies.gradle index 8b693c9..8d5349c 100644 --- a/buildsystem/dependencies.gradle +++ b/buildsystem/dependencies.gradle @@ -26,12 +26,14 @@ ext.versions = [ // Reactive. rxJava : '1.2.6', + rxJava2 : '2.0.7', rxJavaProGuardRules : '1.1.6.0', rxJavaAsyncUtil : '0.21.0', rxAndroid : '1.2.1', + rxAndroid2 : '2.0.1', // Others. - retrofit : '2.0.0-beta3', + retrofit : '2.2.0', retrolambda : '2.3.0', dagger : '2.9', jsr305 : '3.0.1', @@ -80,14 +82,16 @@ ext.libraries = [ // Reactive. rxJava : "io.reactivex:rxjava:$versions.rxJava", + rxJava2 : "io.reactivex.rxjava2:rxjava:$versions.rxJava2", rxJavaAsyncUtil : "io.reactivex:rxjava-async-util:$versions.rxJavaAsyncUtil", rxJavaProGuardRules : "com.artemzin.rxjava:proguard-rules:$versions.rxJavaProGuardRules", rxAndroid : "io.reactivex:rxandroid:$versions.rxAndroid", + rxAndroid2 : "io.reactivex.rxjava2:rxandroid:$versions.rxAndroid2", // Others. retrofit : "com.squareup.retrofit2:retrofit:$versions.retrofit", retrofitGsonConverter : "com.squareup.retrofit2:converter-gson:$versions.retrofit", - retrofitRx : "com.squareup.retrofit2:adapter-rxjava:$versions.retrofit", + retrofitRx2 : "com.squareup.retrofit2:adapter-rxjava2:$versions.retrofit", retrolambda : "net.orfjackal.retrolambda:retrolambda:$versions.retrolambda", dagger : "com.google.dagger:dagger:$versions.dagger", daggerCompiler : "com.google.dagger:dagger-compiler:$versions.dagger", diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/FSReader.java b/filesystem/src/main/java/com/nytimes/android/external/fs/FSReader.java index 7f4eab9..bd3acdc 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/FSReader.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/FSReader.java @@ -7,10 +7,10 @@ import java.io.FileNotFoundException; import javax.annotation.Nonnull; +import io.reactivex.Observable; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; import okio.BufferedSource; -import rx.Emitter; -import rx.Observable; -import rx.functions.Action1; /** * FSReader is used when persisting from file system @@ -31,24 +31,24 @@ public class FSReader implements DiskRead { @Nonnull @Override public Observable read(@Nonnull final T key) { - return Observable.fromEmitter(new Action1>() { + return Observable.create(new ObservableOnSubscribe() { @Override - public void call(Emitter emitter) { + public void subscribe(ObservableEmitter emitter) { String resolvedKey = pathResolver.resolve(key); boolean exists = fileSystem.exists(resolvedKey); - + if (exists) { try { BufferedSource bufferedSource = fileSystem.read(resolvedKey); emitter.onNext(bufferedSource); - emitter.onCompleted(); + emitter.onComplete(); } catch (FileNotFoundException e) { emitter.onError(e); } } else { - emitter.onCompleted(); + emitter.onComplete(); } } - }, Emitter.BackpressureMode.NONE); + }); } } diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/FSWriter.java b/filesystem/src/main/java/com/nytimes/android/external/fs/FSWriter.java index 67bf9e7..e860ae3 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/FSWriter.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/FSWriter.java @@ -7,8 +7,9 @@ import java.util.concurrent.Callable; import javax.annotation.Nonnull; +import io.reactivex.Observable; import okio.BufferedSource; -import rx.Observable; + /** * FSReader is used when persisting to file system * PathResolver will be used in creating file system paths based on cache keys. diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemPersister.java b/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemPersister.java index 31a8ca7..0cb1595 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemPersister.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemPersister.java @@ -5,8 +5,9 @@ import com.nytimes.android.external.store.base.Persister; import javax.annotation.Nonnull; +import io.reactivex.Observable; import okio.BufferedSource; -import rx.Observable; + /** * FileSystemPersister is used when persisting to/from file system * PathResolver will be used in creating file system paths based on cache keys. diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemRecordPersister.java b/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemRecordPersister.java index 7ebebcc..1dce978 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemRecordPersister.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/FileSystemRecordPersister.java @@ -9,8 +9,8 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; +import io.reactivex.Observable; import okio.BufferedSource; -import rx.Observable; /** * FileSystemRecordPersister is used when persisting to/from file system while being stale aware diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersister.java b/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersister.java index 5888e2a..02e9c12 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersister.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersister.java @@ -8,8 +8,8 @@ import com.nytimes.android.external.store.base.impl.BarCode; import javax.annotation.Nonnull; import javax.inject.Inject; +import io.reactivex.Observable; import okio.BufferedSource; -import rx.Observable; /** * Persister to be used when storing something to persister from a BufferedSource diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/FilePersisterTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/FilePersisterTest.java index 8c275f1..47f2d76 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/FilePersisterTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/FilePersisterTest.java @@ -47,25 +47,27 @@ public class FilePersisterTest { .thenReturn(true); when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource); - BufferedSource returnedValue = fileSystemPersister.read(simple).toBlocking().single(); + BufferedSource returnedValue = fileSystemPersister.read(simple).blockingFirst(); assertThat(returnedValue).isEqualTo(bufferedSource); } @Test + @SuppressWarnings("CheckReturnValue") public void readDoesNotExist() throws FileNotFoundException { expectedException.expect(NoSuchElementException.class); when(fileSystem.exists(resolvedPath)) .thenReturn(false); - fileSystemPersister.read(simple).toBlocking().single(); + fileSystemPersister.read(simple).blockingFirst(); } @Test + @SuppressWarnings("CheckReturnValue") public void writeThenRead() throws IOException { when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource); when(fileSystem.exists(resolvedPath)).thenReturn(true); - fileSystemPersister.write(simple, bufferedSource).toBlocking().single(); - BufferedSource source = fileSystemPersister.read(simple).toBlocking().first(); + fileSystemPersister.write(simple, bufferedSource).blockingFirst(); + BufferedSource source = fileSystemPersister.read(simple).blockingFirst(); InOrder inOrder = inOrder(fileSystem); inOrder.verify(fileSystem).write(resolvedPath, bufferedSource); inOrder.verify(fileSystem).exists(resolvedPath); diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/FileSystemRecordPersisterTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/FileSystemRecordPersisterTest.java index 8d86bc3..3ed2f30 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/FileSystemRecordPersisterTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/FileSystemRecordPersisterTest.java @@ -50,25 +50,27 @@ public class FileSystemRecordPersisterTest { .thenReturn(true); when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource); - BufferedSource returnedValue = fileSystemPersister.read(simple).toBlocking().single(); + BufferedSource returnedValue = fileSystemPersister.read(simple).blockingFirst(); assertThat(returnedValue).isEqualTo(bufferedSource); } @Test + @SuppressWarnings("CheckReturnValue") public void readDoesNotExist() throws FileNotFoundException { expectedException.expect(NoSuchElementException.class); when(fileSystem.exists(resolvedPath)) .thenReturn(false); - fileSystemPersister.read(simple).toBlocking().single(); + fileSystemPersister.read(simple).blockingFirst(); } @Test + @SuppressWarnings("CheckReturnValue") public void writeThenRead() throws IOException { when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource); when(fileSystem.exists(resolvedPath)).thenReturn(true); - fileSystemPersister.write(simple, bufferedSource).toBlocking().single(); - BufferedSource source = fileSystemPersister.read(simple).toBlocking().first(); + fileSystemPersister.write(simple, bufferedSource).blockingFirst(); + BufferedSource source = fileSystemPersister.read(simple).blockingFirst(); InOrder inOrder = inOrder(fileSystem); inOrder.verify(fileSystem).write(resolvedPath, bufferedSource); inOrder.verify(fileSystem).exists(resolvedPath); diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/RecordPersisterTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/RecordPersisterTest.java index d2a0a36..5dd94b1 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/RecordPersisterTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/RecordPersisterTest.java @@ -45,7 +45,7 @@ public class RecordPersisterTest { .thenReturn(true); when(fileSystem.read(simple.toString())).thenReturn(bufferedSource); - BufferedSource returnedValue = sourcePersister.read(simple).toBlocking().single(); + BufferedSource returnedValue = sourcePersister.read(simple).blockingFirst(); assertThat(returnedValue).isEqualTo(bufferedSource); } @@ -74,17 +74,18 @@ public class RecordPersisterTest { } @Test + @SuppressWarnings("CheckReturnValue") public void readDoesNotExist() throws FileNotFoundException { expectedException.expect(NoSuchElementException.class); when(fileSystem.exists(SourcePersister.pathForBarcode(simple))) .thenReturn(false); - sourcePersister.read(simple).toBlocking().single(); + sourcePersister.read(simple).blockingFirst(); } @Test public void write() throws IOException { - assertThat(sourcePersister.write(simple, bufferedSource).toBlocking().single()).isTrue(); + assertThat(sourcePersister.write(simple, bufferedSource).blockingSingle()).isTrue(); } @Test diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/SourceDiskDaoStoreTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/SourceDiskDaoStoreTest.java index 68f5bfb..84f7f0e 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/SourceDiskDaoStoreTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/SourceDiskDaoStoreTest.java @@ -14,9 +14,9 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayInputStream; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static com.google.common.base.Charsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -65,9 +65,9 @@ public class SourceDiskDaoStoreTest { when(diskDAO.write(barCode, source)) .thenReturn(Observable.just(true)); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); - result = store.get(barCode).toBlocking().first(); + result = store.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); verify(fetcher, times(1)).fetch(barCode); } diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/SourceFilerReaderWriterStoreTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/SourceFilerReaderWriterStoreTest.java index 800990a..9df3ec0 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/SourceFilerReaderWriterStoreTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/SourceFilerReaderWriterStoreTest.java @@ -13,9 +13,9 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayInputStream; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static com.google.common.base.Charsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -65,9 +65,9 @@ public class SourceFilerReaderWriterStoreTest { when(fileWriter.write(barCode, source)) .thenReturn(Observable.just(true)); - Foo result = simpleStore.get(barCode).toBlocking().first(); + Foo result = simpleStore.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); - result = simpleStore.get(barCode).toBlocking().first(); + result = simpleStore.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); verify(fetcher, times(1)).fetch(barCode); } diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java index befc8b2..073722f 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java @@ -44,22 +44,23 @@ public class SourcePersisterTest { .thenReturn(true); when(fileSystem.read(simple.toString())).thenReturn(bufferedSource); - BufferedSource returnedValue = sourcePersister.read(simple).toBlocking().single(); + BufferedSource returnedValue = sourcePersister.read(simple).blockingSingle(); assertThat(returnedValue).isEqualTo(bufferedSource); } @Test + @SuppressWarnings("CheckReturnValue") public void readDoesNotExist() throws FileNotFoundException { expectedException.expect(NoSuchElementException.class); when(fileSystem.exists(SourcePersister.pathForBarcode(simple))) .thenReturn(false); - sourcePersister.read(simple).toBlocking().single(); + sourcePersister.read(simple).blockingSingle(); } @Test public void write() throws IOException { - assertThat(sourcePersister.write(simple, bufferedSource).toBlocking().single()).isTrue(); + assertThat(sourcePersister.write(simple, bufferedSource).blockingSingle()).isTrue(); } @Test diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/StoreNetworkBeforeStaleTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/StoreNetworkBeforeStaleTest.java index bbfc9f5..c769197 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/StoreNetworkBeforeStaleTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/StoreNetworkBeforeStaleTest.java @@ -14,8 +14,8 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; +import io.reactivex.Observable; import okio.BufferedSource; -import rx.Observable; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.never; diff --git a/filesystem/src/test/java/com/nytimes/android/external/fs/StoreRefreshWhenStaleTest.java b/filesystem/src/test/java/com/nytimes/android/external/fs/StoreRefreshWhenStaleTest.java index 57cb80b..a548324 100644 --- a/filesystem/src/test/java/com/nytimes/android/external/fs/StoreRefreshWhenStaleTest.java +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/StoreRefreshWhenStaleTest.java @@ -13,10 +13,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; import okio.BufferedSource; -import rx.Observable; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -81,20 +81,22 @@ public class StoreRefreshWhenStaleTest { when(persister.write(barCode, network1)) .thenReturn(Observable.just(true)); - BufferedSource result = store.get(barCode) - .test() - .awaitTerminalEvent() - .getOnNextEvents() - .get(0); - assertThat(result).isEqualTo(disk1); + TestObserver testObserver = store + .get(barCode) + .test(); + testObserver.awaitTerminalEvent(); + testObserver.assertNoErrors(); + testObserver.assertResult(disk1); verify(fetcher, times(0)).fetch(barCode); verify(persister, times(1)).getRecordState(barCode); store.clear(barCode); - result = store.get(barCode).test().awaitTerminalEvent().getOnNextEvents().get(0); - assertThat(result).isEqualTo(disk2); + testObserver = store + .get(barCode) + .test(); + testObserver.awaitTerminalEvent(); + testObserver.assertResult(disk2); verify(fetcher, times(0)).fetch(barCode); verify(persister, times(2)).getRecordState(barCode); - } } diff --git a/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParser.java b/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParser.java index a43f476..a43eab4 100644 --- a/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParser.java +++ b/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParser.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import java.io.IOException; import java.io.Reader; @@ -13,6 +14,8 @@ import java.lang.reflect.Type; import javax.annotation.Nonnull; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; + public class JacksonReaderParser implements Parser { private final ObjectMapper objectMapper; @@ -30,11 +33,11 @@ public class JacksonReaderParser implements Parser { } @Override - public Parsed call(@Nonnull Reader reader) { + public Parsed apply(@NonNull Reader reader) throws ParserException { try { return objectMapper.readValue(reader, parsedType); } catch (IOException e) { - return null; + throw new ParserException(e.getMessage(), e); } } } diff --git a/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParser.java b/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParser.java index 262fb67..c797ac7 100644 --- a/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParser.java +++ b/middleware-jackson/src/main/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParser.java @@ -4,15 +4,16 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; import okio.BufferedSource; public class JacksonSourceParser implements Parser { @@ -32,14 +33,13 @@ public class JacksonSourceParser implements Parser implements Parser { private final ObjectMapper objectMapper; @@ -29,12 +31,11 @@ public class JacksonStringParser implements Parser { } @Override - @Nullable - public Parsed call(@Nonnull String source) { + public Parsed apply(@NonNull String s) throws ParserException { try { - return objectMapper.readValue(source, parsedType); + return objectMapper.readValue(s, parsedType); } catch (IOException e) { - return null; + throw new ParserException(e.getMessage(), e); } } } diff --git a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParserStoreTest.java b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParserStoreTest.java index cf0ee2a..cc2d8c7 100644 --- a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParserStoreTest.java +++ b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonReaderParserStoreTest.java @@ -20,7 +20,8 @@ import org.mockito.MockitoAnnotations; import java.io.Reader; import java.io.StringReader; -import rx.Observable; + +import io.reactivex.Observable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -66,7 +67,7 @@ public class JacksonReaderParserStoreTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); @@ -85,7 +86,7 @@ public class JacksonReaderParserStoreTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); diff --git a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParserStoreTest.java b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParserStoreTest.java index f19c46d..ecbee48 100644 --- a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParserStoreTest.java +++ b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonSourceParserStoreTest.java @@ -20,9 +20,9 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayInputStream; import java.nio.charset.Charset; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -74,7 +74,7 @@ public class JacksonSourceParserStoreTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); @@ -93,7 +93,7 @@ public class JacksonSourceParserStoreTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); diff --git a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonStringParserStoreTest.java b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonStringParserStoreTest.java index 382cae8..efca977 100644 --- a/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonStringParserStoreTest.java +++ b/middleware-jackson/src/test/java/com/nytimes/android/external/store/middleware/jackson/JacksonStringParserStoreTest.java @@ -17,7 +17,8 @@ import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import rx.Observable; + +import io.reactivex.Observable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -61,7 +62,7 @@ public class JacksonStringParserStoreTest { .parser(JacksonParserFactory.createStringParser(Foo.class)) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); @@ -80,7 +81,7 @@ public class JacksonStringParserStoreTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); validateFoo(result); diff --git a/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParser.java b/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParser.java index 2104bd7..085ae31 100644 --- a/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParser.java +++ b/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParser.java @@ -1,6 +1,7 @@ package com.nytimes.android.external.store.middleware.moshi; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; @@ -8,9 +9,9 @@ import java.io.IOException; import java.lang.reflect.Type; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; import okio.BufferedSource; public class MoshiSourceParser implements Parser { @@ -23,12 +24,11 @@ public class MoshiSourceParser implements Parser } @Override - @Nullable - public Parsed call(BufferedSource source) { + public Parsed apply(@NonNull BufferedSource bufferedSource) throws ParserException { try { - return jsonAdapter.fromJson(source); + return jsonAdapter.fromJson(bufferedSource); } catch (IOException e) { - return null; + throw new ParserException(e.getMessage(), e); } } } diff --git a/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParser.java b/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParser.java index 2320108..18b6af2 100644 --- a/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParser.java +++ b/middleware-moshi/src/main/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParser.java @@ -1,6 +1,7 @@ package com.nytimes.android.external.store.middleware.moshi; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; @@ -8,9 +9,10 @@ import java.io.IOException; import java.lang.reflect.Type; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; + public class MoshiStringParser implements Parser { private final JsonAdapter jsonAdapter; @@ -20,13 +22,13 @@ public class MoshiStringParser implements Parser { jsonAdapter = moshi.adapter(type); } + @Override - @Nullable - public Parsed call(@Nonnull String source) { + public Parsed apply(@NonNull String s) throws ParserException { try { - return jsonAdapter.fromJson(source); + return jsonAdapter.fromJson(s); } catch (IOException e) { - return null; + throw new ParserException(e.getMessage(), e); } } } diff --git a/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParserTest.java b/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParserTest.java index 013a5c1..d88781d 100644 --- a/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParserTest.java +++ b/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiSourceParserTest.java @@ -18,9 +18,9 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayInputStream; import java.nio.charset.Charset; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -74,7 +74,7 @@ public class MoshiSourceParserTest { .parser(parser) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); assertEquals(result.number, 123); assertEquals(result.string, "abc"); diff --git a/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParserStoreTest.java b/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParserStoreTest.java index 278d7fc..4a8b2b5 100644 --- a/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParserStoreTest.java +++ b/middleware-moshi/src/test/java/com/nytimes/android/external/store/middleware/moshi/MoshiStringParserStoreTest.java @@ -15,7 +15,8 @@ import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import rx.Observable; + +import io.reactivex.Observable; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.times; @@ -58,7 +59,7 @@ public class MoshiStringParserStoreTest { .parser(MoshiParserFactory.createStringParser(Foo.class)) .open(); - Foo result = store.get(barCode).toBlocking().first(); + Foo result = store.get(barCode).blockingFirst(); assertEquals(result.number, 123); assertEquals(result.string, "abc"); diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java index 22aad93..4aea30c 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java @@ -2,13 +2,15 @@ package com.nytimes.android.external.store.middleware; import com.google.gson.Gson; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import java.io.Reader; import java.lang.reflect.Type; -import javax.annotation.Nonnull; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; + import static com.nytimes.android.external.cache.Preconditions.checkNotNull; public class GsonReaderParser implements Parser { @@ -25,7 +27,7 @@ public class GsonReaderParser implements Parser { } @Override - public Parsed call(@Nonnull Reader reader) { + public Parsed apply(@NonNull Reader reader) throws ParserException { return gson.fromJson(reader, type); } } diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java index bfb1d79..5345468 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java @@ -3,15 +3,16 @@ package com.nytimes.android.external.store.middleware; import com.google.gson.Gson; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.nio.charset.Charset; -import javax.annotation.Nonnull; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; import okio.BufferedSource; import static com.nytimes.android.external.cache.Preconditions.checkNotNull; @@ -42,11 +43,11 @@ public class GsonSourceParser implements Parser } @Override - public Parsed call(@Nonnull BufferedSource source) { - try (InputStreamReader reader = new InputStreamReader(source.inputStream(), Charset.forName("UTF-8"))) { + public Parsed apply(@NonNull BufferedSource bufferedSource) throws ParserException { + try (InputStreamReader reader = new InputStreamReader(bufferedSource.inputStream(), Charset.forName("UTF-8"))) { return gson.fromJson(reader, type); } catch (IOException e) { - throw new RuntimeException(e); + throw new ParserException(e.getMessage(), e); } } } diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java index 311ae4f..3bb7676 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java @@ -2,10 +2,14 @@ package com.nytimes.android.external.store.middleware; import com.google.gson.Gson; import com.nytimes.android.external.store.base.Parser; +import com.nytimes.android.external.store.util.ParserException; + import java.lang.reflect.Type; import javax.inject.Inject; +import io.reactivex.annotations.NonNull; + import static com.nytimes.android.external.cache.Preconditions.checkNotNull; public class GsonStringParser implements Parser { @@ -22,7 +26,7 @@ public class GsonStringParser implements Parser { } @Override - public Parsed call(String source) { - return gson.fromJson(source, type); + public Parsed apply(@NonNull String s) throws ParserException { + return gson.fromJson(s, type); } } diff --git a/middleware/src/test/java/com/nytimes/android/external/store/GenericParserStoreTest.java b/middleware/src/test/java/com/nytimes/android/external/store/GenericParserStoreTest.java index 783c6b6..57539f5 100644 --- a/middleware/src/test/java/com/nytimes/android/external/store/GenericParserStoreTest.java +++ b/middleware/src/test/java/com/nytimes/android/external/store/GenericParserStoreTest.java @@ -15,9 +15,9 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayInputStream; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static com.google.common.base.Charsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -67,9 +67,9 @@ public class GenericParserStoreTest { when(persister.write(barCode, source)) .thenReturn(Observable.just(true)); - Foo result = simpleStore.get(barCode).toBlocking().first(); + Foo result = simpleStore.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); - result = simpleStore.get(barCode).toBlocking().first(); + result = simpleStore.get(barCode).blockingFirst(); assertThat(result.bar).isEqualTo(KEY); verify(fetcher, times(1)).fetch(barCode); } diff --git a/middleware/src/test/java/com/nytimes/android/external/store/GsonSourceListParserTest.java b/middleware/src/test/java/com/nytimes/android/external/store/GsonSourceListParserTest.java index 637447d..0af9b6c 100644 --- a/middleware/src/test/java/com/nytimes/android/external/store/GsonSourceListParserTest.java +++ b/middleware/src/test/java/com/nytimes/android/external/store/GsonSourceListParserTest.java @@ -18,9 +18,9 @@ import java.io.ByteArrayInputStream; import java.util.Arrays; import java.util.List; +import io.reactivex.Observable; import okio.BufferedSource; import okio.Okio; -import rx.Observable; import static com.google.common.base.Charsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -76,7 +76,7 @@ public class GsonSourceListParserTest { when(persister.write(barCode, source)) .thenReturn(Observable.just(true)); - List result = simpleStore.get(barCode).toBlocking().first(); + List result = simpleStore.get(barCode).blockingFirst(); assertThat(result.get(0).value).isEqualTo("a"); assertThat(result.get(1).value).isEqualTo("b"); assertThat(result.get(2).value).isEqualTo("c"); diff --git a/store/build.gradle b/store/build.gradle index aadc215..dd8ce14 100644 --- a/store/build.gradle +++ b/store/build.gradle @@ -5,7 +5,7 @@ version = VERSION_NAME dependencies { compile project(path: ':cache') - compile libraries.rxJava + compile libraries.rxJava2 compile libraries.jsr305 testCompile libraries.mockito diff --git a/store/src/main/java/com/nytimes/android/external/store/base/DiskRead.java b/store/src/main/java/com/nytimes/android/external/store/base/DiskRead.java index aae574d..5f2ffe9 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/DiskRead.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/DiskRead.java @@ -2,7 +2,8 @@ package com.nytimes.android.external.store.base; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; + public interface DiskRead { @Nonnull diff --git a/store/src/main/java/com/nytimes/android/external/store/base/DiskWrite.java b/store/src/main/java/com/nytimes/android/external/store/base/DiskWrite.java index 0321de5..6e92877 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/DiskWrite.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/DiskWrite.java @@ -2,7 +2,7 @@ package com.nytimes.android.external.store.base; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; public interface DiskWrite { /** diff --git a/store/src/main/java/com/nytimes/android/external/store/base/Fetcher.java b/store/src/main/java/com/nytimes/android/external/store/base/Fetcher.java index 4fb5325..586af41 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/Fetcher.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/Fetcher.java @@ -2,7 +2,8 @@ package com.nytimes.android.external.store.base; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; + /** * Interface for fetching new data for a Store diff --git a/store/src/main/java/com/nytimes/android/external/store/base/InternalStore.java b/store/src/main/java/com/nytimes/android/external/store/base/InternalStore.java index 4faf4fc..f0bce43 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/InternalStore.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/InternalStore.java @@ -4,7 +4,7 @@ import com.nytimes.android.external.store.base.impl.Store; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; /** * this interface allows us to mark a {@link Store} as "internal", exposing methods for retrieving data diff --git a/store/src/main/java/com/nytimes/android/external/store/base/Parser.java b/store/src/main/java/com/nytimes/android/external/store/base/Parser.java index 7c514a7..0a45fc4 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/Parser.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/Parser.java @@ -1,7 +1,14 @@ package com.nytimes.android.external.store.base; -import rx.functions.Func1; +import com.nytimes.android.external.store.util.ParserException; + +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Function; //just a marker interface allowing for a reimplementation of how the parser is implemented -public interface Parser extends Func1 { +public interface Parser extends Function { + + @Override + Parsed apply(@NonNull Raw raw) throws ParserException; + } diff --git a/store/src/main/java/com/nytimes/android/external/store/base/Persister.java b/store/src/main/java/com/nytimes/android/external/store/base/Persister.java index 8846d50..8a44d98 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/Persister.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/Persister.java @@ -2,7 +2,7 @@ package com.nytimes.android.external.store.base; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; /** * Interface for fetching data from persister diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/MultiParser.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/MultiParser.java index 26ba559..ac8c351 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/MultiParser.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/MultiParser.java @@ -6,6 +6,8 @@ import com.nytimes.android.external.store.util.ParserException; import java.util.ArrayList; import java.util.List; +import io.reactivex.annotations.NonNull; + import static com.nytimes.android.external.cache.Preconditions.checkArgument; import static com.nytimes.android.external.cache.Preconditions.checkNotNull; @@ -28,12 +30,13 @@ public class MultiParser implements KeyParser implements InternalStore { Cache> inFlightRequests; Cache> memCache; + StalePolicy stalePolicy; Persister persister; KeyParser parser; - StalePolicy stalePolicy; private final PublishSubject refreshSubject = PublishSubject.create(); private Fetcher fetcher; @@ -69,11 +70,11 @@ final class RealInternalStore implements InternalStore implements InternalStore maximumInFlightRequestsDuration) { inFlightRequests = CacheBuilder - .newBuilder() - .expireAfterWrite(maximumInFlightRequestsDuration, TimeUnit.SECONDS) - .build(); + .newBuilder() + .expireAfterWrite(maximumInFlightRequestsDuration, TimeUnit.SECONDS) + .build(); } else { inFlightRequests = CacheBuilder.newBuilder() - .expireAfterWrite(memoryPolicy.getExpireAfter(), memoryPolicy.getExpireAfterTimeUnit()) - .build(); + .expireAfterWrite(memoryPolicy.getExpireAfter(), memoryPolicy.getExpireAfterTimeUnit()) + .build(); } } private void initMemCache(MemoryPolicy memoryPolicy) { memCache = CacheBuilder - .newBuilder() - .maximumSize(memoryPolicy.getMaxSize()) - .expireAfterWrite(memoryPolicy.getExpireAfter(), memoryPolicy.getExpireAfterTimeUnit()) - .build(); + .newBuilder() + .maximumSize(memoryPolicy.getMaxSize()) + .expireAfterWrite(memoryPolicy.getExpireAfter(), memoryPolicy.getExpireAfterTimeUnit()) + .build(); } /** @@ -114,8 +115,8 @@ final class RealInternalStore implements InternalStore get(@Nonnull final Key key) { return Observable.concat( - lazyCache(key), - fetch(key) + lazyCache(key), + fetch(key) ).take(1); } @@ -124,7 +125,7 @@ final class RealInternalStore implements InternalStore getRefreshing(@Nonnull final Key key) { return get(key) - .compose(StoreUtil.repeatWhenCacheEvicted(refreshSubject, key)); + .compose(StoreUtil.repeatWhenCacheEvicted(refreshSubject, key)); } @@ -133,13 +134,13 @@ final class RealInternalStore implements InternalStore lazyCache(@Nonnull final Key key) { return Observable - .defer(new Func0>() { - @Override - public Observable call() { - return cache(key); - } - }) - .onErrorResumeNext(new OnErrorResumeWithEmpty()); + .defer(new Callable>() { + @Override + public ObservableSource call() { + return cache(key); + } + }) + .onErrorResumeNext(new OnErrorResumeWithEmpty()); } Observable cache(@Nonnull final Key key) { @@ -147,8 +148,7 @@ final class RealInternalStore implements InternalStore>() { @Nonnull @Override - @SuppressWarnings("PMD.SignatureDeclareThrowsException") - public Observable call() throws Exception { + public Observable call() { return disk(key); } }); @@ -184,35 +184,36 @@ final class RealInternalStore implements InternalStore readDisk(@Nonnull final Key key) { return persister().read(key) - .onErrorResumeNext(new OnErrorResumeWithEmpty()) - .map(new Func1() { - @Override - public Parsed call(Raw raw) { - return parser.call(key, raw); - } - }) - .doOnNext(new Action1() { - @Override - public void call(Parsed parsed) { - updateMemory(key, parsed); - if (stalePolicy == StalePolicy.REFRESH_ON_STALE - && persisterIsStale(key, persister)) { - backfillCache(key); + .onErrorResumeNext(new OnErrorResumeWithEmpty()) + .map(new Function() { + @Override + public Parsed apply(@NonNull Raw raw) { + return parser.apply(key, raw); } - } - }).cache(); + }) + .doOnNext(new Consumer() { + @Override + public void accept(@NonNull Parsed parsed) { + updateMemory(key, parsed); + if (stalePolicy == StalePolicy.REFRESH_ON_STALE + && persisterIsStale(key, persister)) { + backfillCache(key); + } + } + }).cache(); } + @SuppressWarnings("CheckReturnValue") void backfillCache(@Nonnull Key key) { - fetch(key).subscribe(new Action1() { + fetch(key).subscribe(new Consumer() { @Override - public void call(Parsed parsed) { - //do Nothing we are just backfilling cache + public void accept(@NonNull Parsed parsed) { + // do Nothing we are just backfilling cache } - }, new Action1() { + }, new Consumer() { @Override - public void call(Throwable throwable) { - //do nothing as we are just backfilling cache + public void accept(@NonNull Throwable throwable) { + // do nothing as we are just backfilling cache } }); } @@ -227,10 +228,9 @@ final class RealInternalStore implements InternalStore fetch(@Nonnull final Key key) { - return Observable.defer(new Func0>() { - @Nullable + return Observable.defer(new Callable>() { @Override - public Observable call() { + public ObservableSource call() { return fetchAndPersist(key); } }); @@ -264,41 +264,41 @@ final class RealInternalStore implements InternalStore response(@Nonnull final Key key) { return fetcher() - .fetch(key) - .flatMap(new Func1>() { - @Override - public Observable call(Raw raw) { - return persister().write(key, raw) - .flatMap(new Func1>() { - @Override - public Observable call(Boolean aBoolean) { - return readDisk(key); - } - }); - } - }) - .onErrorResumeNext(new Func1>() { - @Override - public Observable call(Throwable throwable) { - if (stalePolicy == StalePolicy.NETWORK_BEFORE_STALE) { - return readDisk(key); + .fetch(key) + .flatMap(new Function>() { + @Override + public ObservableSource apply(@NonNull Raw raw) { + return persister().write(key, raw) + .flatMap(new Function>() { + @Override + public ObservableSource apply(@NonNull Boolean aBoolean) { + return readDisk(key); + } + }); } - return Observable.error(throwable); - } - }) - .doOnNext(new Action1() { - @Override - public void call(Parsed data) { - notifySubscribers(data); - } - }) - .doOnTerminate(new Action0() { - @Override - public void call() { - inFlightRequests.invalidate(key); - } - }) - .cache(); + }) + .onErrorResumeNext(new Function>() { + @Override + public ObservableSource apply(@NonNull Throwable throwable) { + if (stalePolicy == StalePolicy.NETWORK_BEFORE_STALE) { + return readDisk(key); + } + return Observable.error(throwable); + } + }) + .doOnNext(new Consumer() { + @Override + public void accept(@NonNull Parsed parsed) { + notifySubscribers(parsed); + } + }) + .doOnTerminate(new Action() { + @Override + public void run() { + inFlightRequests.invalidate(key); + } + }) + .cache(); } void notifySubscribers(Parsed data) { @@ -314,7 +314,7 @@ final class RealInternalStore implements InternalStore stream(@Nonnull Key key) { - Observable stream = subject.asObservable(); + Observable stream = subject.hide(); //If nothing was emitted through the subject yet, start stream with get() value if (!subject.hasValue()) { @@ -327,7 +327,7 @@ final class RealInternalStore implements InternalStore stream() { - return subject.asObservable(); + return subject.hide(); } /** diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStore.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStore.java index b5de574..e28b2e4 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStore.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStore.java @@ -12,7 +12,8 @@ import com.nytimes.android.external.store.util.NoopPersister; import javax.annotation.Nonnull; -import rx.Observable; + +import io.reactivex.Observable; import static com.nytimes.android.external.store.base.impl.StalePolicy.UNSPECIFIED; diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStoreBuilder.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStoreBuilder.java index 0714e63..4a1b364 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStoreBuilder.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealStoreBuilder.java @@ -16,7 +16,8 @@ import java.util.List; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; + /** * Builder where there parser is used. diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmits.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmits.java index 0a018e2..4a2caa2 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmits.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmits.java @@ -2,8 +2,11 @@ package com.nytimes.android.external.store.base.impl; import javax.annotation.Nonnull; -import rx.Observable; -import rx.functions.Func1; +import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.ObservableTransformer; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Function; import static com.nytimes.android.external.cache.Preconditions.checkNotNull; @@ -11,7 +14,7 @@ import static com.nytimes.android.external.cache.Preconditions.checkNotNull; * A Transformer that takes a source observable and re-subscribes to the upstream Observable when * it emits. */ -final class RepeatWhenEmits implements Observable.Transformer { +final class RepeatWhenEmits implements ObservableTransformer { private final Observable source; @@ -25,13 +28,13 @@ final class RepeatWhenEmits implements Observable.Transformer { } @Override - public Observable call(Observable upstream) { - return upstream.repeatWhen(new Func1, Observable>() { + public ObservableSource apply(Observable upstream) { + return upstream.repeatWhen(new Function, ObservableSource>() { @Override - public Observable call(Observable events) { - return events.switchMap(new Func1>() { + public ObservableSource apply(@NonNull Observable objectObservable) { + return objectObservable.switchMap(new Function>() { @Override - public Observable call(Void aVoid) { + public ObservableSource apply(@NonNull Object o) { return source; } }); diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/Store.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/Store.java index 9a2eb52..3e1f71e 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/Store.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/Store.java @@ -3,8 +3,9 @@ package com.nytimes.android.external.store.base.impl; import javax.annotation.Nonnull; -import rx.Observable; -import rx.annotations.Experimental; +import io.reactivex.Observable; +import io.reactivex.annotations.Experimental; + /** * a {@link com.nytimes.android.external.store.base.impl.StoreBuilder StoreBuilder} diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreBuilder.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreBuilder.java index 1b2f511..3c11795 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreBuilder.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreBuilder.java @@ -2,7 +2,7 @@ package com.nytimes.android.external.store.base.impl; import javax.annotation.Nonnull; -import rx.annotations.Beta; +import io.reactivex.annotations.Beta; /** diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreUtil.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreUtil.java index 9d761c1..7ccfa83 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreUtil.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/StoreUtil.java @@ -6,9 +6,12 @@ import com.nytimes.android.external.store.base.RecordState; import javax.annotation.Nonnull; -import rx.Observable; -import rx.functions.Func1; -import rx.subjects.PublishSubject; +import io.reactivex.Observable; +import io.reactivex.ObservableTransformer; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Predicate; +import io.reactivex.subjects.PublishSubject; + import static com.nytimes.android.external.store.base.RecordState.STALE; @@ -17,11 +20,11 @@ final class StoreUtil { } @Nonnull - static Observable.Transformer + static ObservableTransformer repeatWhenCacheEvicted(PublishSubject refreshSubject, @Nonnull final Key keyForRepeat) { - Observable filter = refreshSubject.filter(new Func1() { + Observable filter = refreshSubject.filter(new Predicate() { @Override - public Boolean call(Key key) { + public boolean test(@NonNull Key key) throws Exception { return key.equals(keyForRepeat); } }); diff --git a/store/src/main/java/com/nytimes/android/external/store/util/KeyParser.java b/store/src/main/java/com/nytimes/android/external/store/util/KeyParser.java index a3a8b28..6b3a198 100644 --- a/store/src/main/java/com/nytimes/android/external/store/util/KeyParser.java +++ b/store/src/main/java/com/nytimes/android/external/store/util/KeyParser.java @@ -1,13 +1,11 @@ package com.nytimes.android.external.store.util; -import javax.annotation.Nonnull; - -import rx.functions.Func2; - -public interface KeyParser extends Func2 { +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.BiFunction; +public interface KeyParser extends BiFunction { @Override - @Nonnull - Parsed call(@Nonnull Key key, @Nonnull Raw raw); + Parsed apply(@NonNull Key key, @NonNull Raw raw) throws ParserException; + } diff --git a/store/src/main/java/com/nytimes/android/external/store/util/NoKeyParser.java b/store/src/main/java/com/nytimes/android/external/store/util/NoKeyParser.java index 6b30132..49c139d 100644 --- a/store/src/main/java/com/nytimes/android/external/store/util/NoKeyParser.java +++ b/store/src/main/java/com/nytimes/android/external/store/util/NoKeyParser.java @@ -4,6 +4,8 @@ import com.nytimes.android.external.store.base.Parser; import javax.annotation.Nonnull; +import io.reactivex.annotations.NonNull; + public class NoKeyParser implements KeyParser { private final Parser parser; @@ -12,8 +14,7 @@ public class NoKeyParser implements KeyParser implements Parser { + @Override - public Object call(Object object) { - return object; + public Parsed apply(@NonNull Raw raw) throws ParserException { + return (Parsed) raw; } } diff --git a/store/src/main/java/com/nytimes/android/external/store/util/NoopPersister.java b/store/src/main/java/com/nytimes/android/external/store/util/NoopPersister.java index 0bd6021..6615dc9 100644 --- a/store/src/main/java/com/nytimes/android/external/store/util/NoopPersister.java +++ b/store/src/main/java/com/nytimes/android/external/store/util/NoopPersister.java @@ -11,7 +11,8 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; + /** * Pass-through diskdao for stores that don't want to use persister diff --git a/store/src/main/java/com/nytimes/android/external/store/util/OnErrorResumeWithEmpty.java b/store/src/main/java/com/nytimes/android/external/store/util/OnErrorResumeWithEmpty.java index e1025af..a42589a 100644 --- a/store/src/main/java/com/nytimes/android/external/store/util/OnErrorResumeWithEmpty.java +++ b/store/src/main/java/com/nytimes/android/external/store/util/OnErrorResumeWithEmpty.java @@ -1,15 +1,17 @@ package com.nytimes.android.external.store.util; -import rx.Observable; -import rx.functions.Func1; + +import io.reactivex.Observable; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Function; /** * Resume with empty observable on error */ -public class OnErrorResumeWithEmpty implements Func1> { +public class OnErrorResumeWithEmpty implements Function> { @Override - public Observable call(Throwable throwable) { + public Observable apply(@NonNull Throwable throwable) { return Observable.empty(); } } diff --git a/store/src/main/java/com/nytimes/android/external/store/util/ParserException.java b/store/src/main/java/com/nytimes/android/external/store/util/ParserException.java index d3ea229..4b7099b 100644 --- a/store/src/main/java/com/nytimes/android/external/store/util/ParserException.java +++ b/store/src/main/java/com/nytimes/android/external/store/util/ParserException.java @@ -5,7 +5,15 @@ package com.nytimes.android.external.store.util; */ public class ParserException extends RuntimeException { + public ParserException(Throwable cause) { + super(cause); + } + public ParserException(String message) { super(message); } + + public ParserException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java b/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java index 93b9472..ff70c96 100644 --- a/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java @@ -12,7 +12,8 @@ import java.util.concurrent.Callable; import javax.annotation.Nonnull; -import rx.Observable; + +import io.reactivex.Observable; import static org.assertj.core.api.Assertions.assertThat; diff --git a/store/src/test/java/com/nytimes/android/external/store/ClearStoreTest.java b/store/src/test/java/com/nytimes/android/external/store/ClearStoreTest.java index 951faa9..426cb9f 100644 --- a/store/src/test/java/com/nytimes/android/external/store/ClearStoreTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/ClearStoreTest.java @@ -16,7 +16,8 @@ import java.util.concurrent.atomic.AtomicInteger; import javax.annotation.Nonnull; -import rx.Observable; + +import io.reactivex.Observable; import static com.nytimes.android.external.store.GetRefreshingTest.ClearingPersister; import static org.assertj.core.api.Assertions.assertThat; diff --git a/store/src/test/java/com/nytimes/android/external/store/DontCacheErrorsTest.java b/store/src/test/java/com/nytimes/android/external/store/DontCacheErrorsTest.java index 89d3b19..bc327d0 100644 --- a/store/src/test/java/com/nytimes/android/external/store/DontCacheErrorsTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/DontCacheErrorsTest.java @@ -12,7 +12,8 @@ import java.util.concurrent.Callable; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; + public class DontCacheErrorsTest { @@ -47,12 +48,13 @@ public class DontCacheErrorsTest { shouldThrow = true; store.get(barcode).test() - .awaitTerminalEvent() - .assertError(Exception.class); + .assertTerminated() + .assertError(Exception.class) + .awaitTerminalEvent(); shouldThrow = false; store.get(barcode).test() - .awaitTerminalEvent() - .assertNoErrors(); + .assertNoErrors() + .awaitTerminalEvent(); } } diff --git a/store/src/test/java/com/nytimes/android/external/store/GetRefreshingTest.java b/store/src/test/java/com/nytimes/android/external/store/GetRefreshingTest.java index 48353c4..e2a9b27 100644 --- a/store/src/test/java/com/nytimes/android/external/store/GetRefreshingTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/GetRefreshingTest.java @@ -18,8 +18,8 @@ import java.util.concurrent.atomic.AtomicInteger; import javax.annotation.Nonnull; -import rx.Observable; -import rx.observers.AssertableSubscriber; +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -63,16 +63,16 @@ public class GetRefreshingTest { when(persister.write(barcode, 2)).thenReturn(Observable.just(true)); - AssertableSubscriber refreshingObservable = store.getRefreshing(barcode).test(); - assertThat(refreshingObservable.getValueCount()).isEqualTo(1); + TestObserver refreshingObservable = store.getRefreshing(barcode).test(); + refreshingObservable.assertValueCount(1); assertThat(networkCalls.intValue()).isEqualTo(1); //clearing the store should produce another network call store.clear(barcode); - assertThat(refreshingObservable.getValueCount()).isEqualTo(2); + refreshingObservable.assertValueCount(2); assertThat(networkCalls.intValue()).isEqualTo(2); store.get(barcode).test().awaitTerminalEvent(); - assertThat(refreshingObservable.getValueCount()).isEqualTo(2); + refreshingObservable.assertValueCount(2); assertThat(networkCalls.intValue()).isEqualTo(2); } @@ -98,10 +98,10 @@ public class GetRefreshingTest { when(persister.write(barcode2, 1)).thenReturn(Observable.just(true)); when(persister.write(barcode2, 2)).thenReturn(Observable.just(true)); - AssertableSubscriber testObservable1 = store.getRefreshing(barcode1).test(); - AssertableSubscriber testObservable2 = store.getRefreshing(barcode2).test(); - assertThat(testObservable1.getValueCount()).isEqualTo(1); - assertThat(testObservable2.getValueCount()).isEqualTo(1); + TestObserver testObservable1 = store.getRefreshing(barcode1).test(); + TestObserver testObservable2 = store.getRefreshing(barcode2).test(); + testObservable1.assertValueCount(1); + testObservable2.assertValueCount(1); assertThat(networkCalls.intValue()).isEqualTo(2); diff --git a/store/src/test/java/com/nytimes/android/external/store/KeyParserTest.java b/store/src/test/java/com/nytimes/android/external/store/KeyParserTest.java index 925b04f..495f6d8 100644 --- a/store/src/test/java/com/nytimes/android/external/store/KeyParserTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/KeyParserTest.java @@ -12,8 +12,10 @@ import org.mockito.runners.MockitoJUnitRunner; import javax.annotation.Nonnull; -import rx.Observable; -import rx.observers.AssertableSubscriber; +import io.reactivex.Observable; +import io.reactivex.annotations.NonNull; +import io.reactivex.observers.TestObserver; + @RunWith(MockitoJUnitRunner.class) public class KeyParserTest { @@ -27,7 +29,7 @@ public class KeyParserTest { store = StoreBuilder.parsedWithKey() .parser(new KeyParser() { @Override - public String call(Integer integer, String s) { + public String apply(@NonNull Integer integer, @NonNull String s) { return s + integer; } }) @@ -42,12 +44,11 @@ public class KeyParserTest { } @Test + @SuppressWarnings("PMD.SignatureDeclareThrowsException") public void testStoreWithKeyParserFuncNoPersister() throws Exception { - AssertableSubscriber testObservable = store.get(KEY).test().awaitTerminalEvent(); + TestObserver testObservable = store.get(KEY).test().await(); testObservable.assertNoErrors() .assertValues(NETWORK + KEY) - .assertUnsubscribed(); - - + .awaitTerminalEvent(); } } diff --git a/store/src/test/java/com/nytimes/android/external/store/SequentialTest.java b/store/src/test/java/com/nytimes/android/external/store/SequentialTest.java index 135b042..ccaa85a 100644 --- a/store/src/test/java/com/nytimes/android/external/store/SequentialTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/SequentialTest.java @@ -12,7 +12,8 @@ import java.util.concurrent.Callable; import javax.annotation.Nonnull; -import rx.Observable; + +import io.reactivex.Observable; import static org.assertj.core.api.Assertions.assertThat; diff --git a/store/src/test/java/com/nytimes/android/external/store/StoreBuilderTest.java b/store/src/test/java/com/nytimes/android/external/store/StoreBuilderTest.java index 8e045e3..fdf83b0 100644 --- a/store/src/test/java/com/nytimes/android/external/store/StoreBuilderTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/StoreBuilderTest.java @@ -14,7 +14,8 @@ import java.util.Date; import javax.annotation.Nonnull; -import rx.Observable; +import io.reactivex.Observable; +import io.reactivex.annotations.NonNull; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +49,7 @@ public class StoreBuilderTest { }) .parser(new Parser() { @Override - public Date call(String s) { + public Date apply(@NonNull String s) { return DATE; } }) @@ -73,9 +74,9 @@ public class StoreBuilderTest { } }) .open(); - Date result = store.get(5).toBlocking().first(); - result = barCodeStore.get(new BarCode("test", "5")).toBlocking().first(); - result = keyStore.get(5).toBlocking().first(); + Date result = store.get(5).blockingFirst(); + result = barCodeStore.get(new BarCode("test", "5")).blockingFirst(); + result = keyStore.get(5).blockingFirst(); assertThat(result).isNotNull(); } diff --git a/store/src/test/java/com/nytimes/android/external/store/StoreTest.java b/store/src/test/java/com/nytimes/android/external/store/StoreTest.java index a17ce93..876ccfc 100644 --- a/store/src/test/java/com/nytimes/android/external/store/StoreTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/StoreTest.java @@ -4,9 +4,9 @@ import com.nytimes.android.external.cache.Cache; import com.nytimes.android.external.cache.CacheBuilder; import com.nytimes.android.external.store.base.Fetcher; import com.nytimes.android.external.store.base.Persister; -import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.external.store.base.impl.BarCode; import com.nytimes.android.external.store.base.impl.RealStore; +import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.external.store.base.impl.StoreBuilder; import com.nytimes.android.external.store.util.NoopPersister; @@ -18,10 +18,11 @@ import org.mockito.MockitoAnnotations; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import rx.Emitter; -import rx.Observable; -import rx.functions.Action1; -import rx.functions.Func2; +import io.reactivex.Observable; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.BiFunction; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.spy; @@ -65,10 +66,10 @@ public class StoreTest { when(persister.write(barCode, NETWORK)) .thenReturn(Observable.just(true)); - String value = simpleStore.get(barCode).toBlocking().first(); + String value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(DISK); - value = simpleStore.get(barCode).toBlocking().first(); + value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(DISK); verify(fetcher, times(1)).fetch(barCode); } @@ -83,9 +84,9 @@ public class StoreTest { .open(); Observable networkObservable = - Observable.fromEmitter(new Action1>() { + Observable.create(new ObservableOnSubscribe() { @Override - public void call(Emitter emitter) { + public void subscribe(ObservableEmitter emitter) { if (counter.incrementAndGet() == 1) { emitter.onNext(NETWORK); @@ -93,7 +94,9 @@ public class StoreTest { emitter.onError(new RuntimeException("Yo Dawg your inflight is broken")); } } - }, Emitter.BackpressureMode.NONE); + }); + + when(fetcher.fetch(barCode)) .thenReturn(networkObservable); @@ -106,14 +109,13 @@ public class StoreTest { String response = simpleStore.get(barCode).zipWith(simpleStore.get(barCode), - new Func2() { + new BiFunction() { @Override - public String call(String s, String s2) { + public String apply(@NonNull String s, @NonNull String s2) { return "hello"; } }) - .toBlocking() - .first(); + .blockingFirst(); assertThat(response).isEqualTo("hello"); verify(fetcher, times(1)).fetch(barCode); } @@ -133,9 +135,9 @@ public class StoreTest { .thenReturn(Observable.just(DISK)); when(persister.write(barCode, NETWORK)).thenReturn(Observable.just(true)); - String value = simpleStore.get(barCode).toBlocking().first(); + String value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(DISK); - value = simpleStore.get(barCode).toBlocking().first(); + value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(DISK); verify(fetcher, times(1)).fetch(barCode); } @@ -151,14 +153,14 @@ public class StoreTest { when(fetcher.fetch(barCode)) .thenReturn(Observable.just(NETWORK)); - String value = simpleStore.get(barCode).toBlocking().first(); + String value = simpleStore.get(barCode).blockingFirst(); verify(fetcher, times(1)).fetch(barCode); verify(persister, times(1)).write(barCode, NETWORK); verify(persister, times(2)).read(barCode); assertThat(value).isEqualTo(NETWORK); - value = simpleStore.get(barCode).toBlocking().first(); + value = simpleStore.get(barCode).blockingFirst(); verify(persister, times(2)).read(barCode); verify(persister, times(1)).write(barCode, NETWORK); verify(fetcher, times(1)).fetch(barCode); diff --git a/store/src/test/java/com/nytimes/android/external/store/StoreWithParserTest.java b/store/src/test/java/com/nytimes/android/external/store/StoreWithParserTest.java index cab9500..54207b3 100644 --- a/store/src/test/java/com/nytimes/android/external/store/StoreWithParserTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/StoreWithParserTest.java @@ -11,7 +11,8 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import rx.Observable; + +import io.reactivex.Observable; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.times; @@ -32,7 +33,7 @@ public class StoreWithParserTest { private final BarCode barCode = new BarCode("key", "value"); @Test - public void testSimple() { + public void testSimple() throws Exception { MockitoAnnotations.initMocks(this); @@ -52,17 +53,17 @@ public class StoreWithParserTest { when(persister.write(barCode, NETWORK)) .thenReturn(Observable.just(true)); - when(parser.call(DISK)).thenReturn(barCode.getKey()); + when(parser.apply(DISK)).thenReturn(barCode.getKey()); - String value = simpleStore.get(barCode).toBlocking().first(); + String value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(barCode.getKey()); - value = simpleStore.get(barCode).toBlocking().first(); + value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(barCode.getKey()); verify(fetcher, times(1)).fetch(barCode); } @Test - public void testSubclass() { + public void testSubclass() throws Exception { MockitoAnnotations.initMocks(this); Store simpleStore = new SampleParsingStore(fetcher, persister, parser); @@ -77,11 +78,11 @@ public class StoreWithParserTest { when(persister.write(barCode, NETWORK)) .thenReturn(Observable.just(true)); - when(parser.call(DISK)).thenReturn(barCode.getKey()); + when(parser.apply(DISK)).thenReturn(barCode.getKey()); - String value = simpleStore.get(barCode).toBlocking().first(); + String value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(barCode.getKey()); - value = simpleStore.get(barCode).toBlocking().first(); + value = simpleStore.get(barCode).blockingFirst(); assertThat(value).isEqualTo(barCode.getKey()); verify(fetcher, times(1)).fetch(barCode); } diff --git a/store/src/test/java/com/nytimes/android/external/store/base/impl/MultiParserTest.java b/store/src/test/java/com/nytimes/android/external/store/base/impl/MultiParserTest.java index ef5a7f2..a9b6557 100644 --- a/store/src/test/java/com/nytimes/android/external/store/base/impl/MultiParserTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/base/impl/MultiParserTest.java @@ -19,21 +19,21 @@ public class MultiParserTest { private static final Parser PARSER_1 = new Parser() { @Override - public String call(Integer value) { + public String apply(Integer value) { return String.valueOf(value); } }; private static final Parser PARSER_2 = new Parser() { @Override - public BarCode call(String value) { + public BarCode apply(String value) { return new BarCode(value, "KEY"); } }; private static final Parser PARSER_3 = new Parser() { @Override - public UUID call(BarCode barCode) { + public UUID apply(BarCode barCode) { return UUID.randomUUID(); } }; @@ -49,7 +49,7 @@ public class MultiParserTest { parsersChain.add(new NoKeyParser<>(PARSER_3)); KeyParser parser = new MultiParser<>(parsersChain); - UUID parsed = parser.call(new Object(), 100); + UUID parsed = parser.apply(new Object(), 100); assertNotNull(parsed); } @@ -64,7 +64,7 @@ public class MultiParserTest { parsersChain.add(new NoKeyParser<>(PARSER_2)); KeyParser parser = new MultiParser<>(parsersChain); - UUID parsed = parser.call(new Object(), 100); + UUID parsed = parser.apply(new Object(), 100); assertNotNull(parsed); } diff --git a/store/src/test/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmitsTest.java b/store/src/test/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmitsTest.java index e9e83ba..729647b 100644 --- a/store/src/test/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmitsTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/base/impl/RepeatWhenEmitsTest.java @@ -4,9 +4,9 @@ import org.junit.Test; import java.util.concurrent.Callable; -import rx.Observable; -import rx.observers.AssertableSubscriber; -import rx.subjects.PublishSubject; +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; +import io.reactivex.subjects.PublishSubject; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -25,7 +25,7 @@ public class RepeatWhenEmitsTest { // create an observable and apply the transformer to test PublishSubject source = PublishSubject.create(); - AssertableSubscriber testSubscriber = Observable.fromCallable(mockCallable) + TestObserver testSubscriber = Observable.fromCallable(mockCallable) .compose(RepeatWhenEmits.from(source)) .test(); diff --git a/store/src/test/java/com/nytimes/android/external/store/util/NoopPersisterTest.java b/store/src/test/java/com/nytimes/android/external/store/util/NoopPersisterTest.java index 6e374db..9e45724 100644 --- a/store/src/test/java/com/nytimes/android/external/store/util/NoopPersisterTest.java +++ b/store/src/test/java/com/nytimes/android/external/store/util/NoopPersisterTest.java @@ -12,11 +12,10 @@ public class NoopPersisterTest { @Test public void writeReadTest() { - NoopPersister persister = NoopPersister.create(); - boolean success = persister.write(barCode, "foo").toBlocking().first(); + boolean success = persister.write(barCode, "foo").blockingFirst(); assertThat(success).isTrue(); - String rawValue = persister.read(barCode).toBlocking().first(); + String rawValue = persister.read(barCode).blockingFirst(); assertThat(rawValue).isEqualTo("foo"); } @@ -24,7 +23,7 @@ public class NoopPersisterTest { public void noopParserFuncTest() { NoopParserFunc noopParserFunc = new NoopParserFunc<>(); String input = "foo"; - String output = (String) noopParserFunc.call(input); + String output = (String) noopParserFunc.apply(input); assertThat(input).isEqualTo(output); //intended object ref comparison assertThat(input).isSameAs(output);