Skip to content

Commit

Permalink
RxJava2 Migration: Return single/maybe where appropriate. (nytimes#185)
Browse files Browse the repository at this point in the history
* RxJava2 Migration: Return single/maybe where appropriate.

* Remove no longer used class.

* Pull in the fix for nytimes#181 from develop.

* Re-iterated solution nytimes#181 issue.
  • Loading branch information
tairrzayev authored and digitalbuddha committed Apr 24, 2017
1 parent f009e11 commit 20445ff
Show file tree
Hide file tree
Showing 47 changed files with 444 additions and 372 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/com/nytimes/android/sample/SampleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Single;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
Expand Down Expand Up @@ -80,9 +81,9 @@ private Persister<BufferedSource, BarCode> newPersister() throws IOException {
return SourcePersisterFactory.create(getApplicationContext().getCacheDir());
}

private Observable<BufferedSource> fetcher(BarCode barCode) {
private Single<BufferedSource> fetcher(BarCode barCode) {
return provideRetrofit().fetchSubredditForPersister(barCode.getKey(), "10")
.map(responseBody -> responseBody.source());
.map(ResponseBody::source);
}

private Api provideRetrofit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void loadPosts() {

this.persistedStore
.get(awwRequest)
.flatMap(this::sanitizeData)
.flatMapObservable(this::sanitizeData)
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void loadPosts() {

this.nonPersistedStore
.get(awwRequest)
.flatMap(new Function<RedditData, ObservableSource<Post>>() {
.flatMapObservable(new Function<RedditData, ObservableSource<Post>>() {
@Override
public ObservableSource<Post> apply(@NonNull RedditData redditData) throws Exception {
return sanitizeData(redditData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.nytimes.android.sample.data.model.RedditData;

import io.reactivex.Observable;
import io.reactivex.Single;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.Path;
Expand All @@ -11,10 +11,10 @@
public interface Api {

@GET("r/{subredditName}/new/.json")
Observable<RedditData> fetchSubreddit(@Path("subredditName") String subredditName,
@Query("limit") String limit);
Single<RedditData> fetchSubreddit(@Path("subredditName") String subredditName,
@Query("limit") String limit);

@GET("r/{subredditName}/new/.json")
Observable<ResponseBody> fetchSubredditForPersister(@Path("subredditName") String subredditName,
@Query("limit") String limit);
Single<ResponseBody> fetchSubredditForPersister(@Path("subredditName") String subredditName,
@Query("limit") String limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,24 @@
import org.junit.Before;
import org.junit.Test;


import io.reactivex.Observable;
import io.reactivex.Single;

import static junit.framework.Assert.assertEquals;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class StoreIntegrationTest {

private Store<String, BarCode> testStore;

@Test
public void addition_isCorrect() throws Exception {

}


@Before
public void setUp() throws Exception {
testStore = StoreBuilder.<String>barcode()
.fetcher(barCode -> Observable.just("hello"))
.fetcher(barCode -> Single.just("hello"))
.open();

}

@Test
public void testRepeatedGet() throws Exception {
String first = testStore.get(BarCode.empty()).blockingFirst();
String first = testStore.get(BarCode.empty()).blockingGet();
assertEquals(first, "hello");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import javax.annotation.Nonnull;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Maybe;
import io.reactivex.MaybeEmitter;
import io.reactivex.MaybeOnSubscribe;
import okio.BufferedSource;

/**
Expand All @@ -20,6 +20,7 @@
* @param <T> key type
*/
public class FSReader<T> implements DiskRead<BufferedSource, T> {
private static final String ERROR_MESSAGE = "resolvedKey does not resolve to a file";
final FileSystem fileSystem;
final PathResolver<T> pathResolver;

Expand All @@ -30,23 +31,23 @@ public FSReader(FileSystem fileSystem, PathResolver<T> pathResolver) {

@Nonnull
@Override
public Observable<BufferedSource> read(@Nonnull final T key) {
return Observable.create(new ObservableOnSubscribe<BufferedSource>() {
public Maybe<BufferedSource> read(@Nonnull final T key) {
return Maybe.create(new MaybeOnSubscribe<BufferedSource>() {
@Override
public void subscribe(ObservableEmitter<BufferedSource> emitter) {
public void subscribe(MaybeEmitter<BufferedSource> emitter) {
String resolvedKey = pathResolver.resolve(key);
boolean exists = fileSystem.exists(resolvedKey);

if (exists) {
try {
BufferedSource bufferedSource = fileSystem.read(resolvedKey);
emitter.onNext(bufferedSource);
emitter.onSuccess(bufferedSource);
emitter.onComplete();
} catch (FileNotFoundException e) {
emitter.onError(e);
}
} else {
emitter.onComplete();
emitter.onError(new FileNotFoundException(ERROR_MESSAGE + resolvedKey));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import javax.annotation.Nonnull;

import io.reactivex.Observable;
import io.reactivex.Single;
import okio.BufferedSource;

/**
Expand All @@ -27,8 +27,8 @@ public FSWriter(FileSystem fileSystem, PathResolver<T> pathResolver) {

@Nonnull
@Override
public Observable<Boolean> write(@Nonnull final T key, @Nonnull final BufferedSource data) {
return Observable.fromCallable(new Callable<Boolean>() {
public Single<Boolean> write(@Nonnull final T key, @Nonnull final BufferedSource data) {
return Single.fromCallable(new Callable<Boolean>() {
@Nonnull
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import javax.annotation.Nonnull;

import io.reactivex.Observable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import okio.BufferedSource;

/**
Expand Down Expand Up @@ -34,13 +35,13 @@ public static <T> Persister<BufferedSource, T> create(FileSystem fileSystem,

@Nonnull
@Override
public Observable<BufferedSource> read(@Nonnull final T key) {
public Maybe<BufferedSource> read(@Nonnull final T key) {
return fileReader.read(key);
}

@Nonnull
@Override
public Observable<Boolean> write(@Nonnull final T key, @Nonnull final BufferedSource data) {
public Single<Boolean> write(@Nonnull final T key, @Nonnull final BufferedSource data) {
return fileWriter.write(key, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

import javax.annotation.Nonnull;

import io.reactivex.Observable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import okio.BufferedSource;

/**
Expand Down Expand Up @@ -59,13 +60,13 @@ public RecordState getRecordState(@Nonnull Key key) {

@Nonnull
@Override
public Observable<BufferedSource> read(@Nonnull Key key) {
public Maybe<BufferedSource> read(@Nonnull Key key) {
return fileReader.read(key);
}

@Nonnull
@Override
public Observable<Boolean> write(@Nonnull Key key, @Nonnull BufferedSource bufferedSource) {
public Single<Boolean> write(@Nonnull Key key, @Nonnull BufferedSource bufferedSource) {
return fileWriter.write(key, bufferedSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import javax.annotation.Nonnull;
import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import okio.BufferedSource;

/**
Expand Down Expand Up @@ -44,13 +45,13 @@ static String pathForBarcode(@Nonnull BarCode barCode) {

@Nonnull
@Override
public Observable<BufferedSource> read(@Nonnull final BarCode barCode) {
public Maybe<BufferedSource> read(@Nonnull final BarCode barCode) {
return sourceFileReader.read(barCode);
}

@Nonnull
@Override
public Observable<Boolean> write(@Nonnull final BarCode barCode, @Nonnull final BufferedSource data) {
public Single<Boolean> write(@Nonnull final BarCode barCode, @Nonnull final BufferedSource data) {
return sourceFileWriter.write(barCode, data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import com.nytimes.android.external.store2.base.impl.BarCode;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.NoSuchElementException;

import okio.BufferedSource;

Expand All @@ -23,8 +20,6 @@
import static org.mockito.Mockito.when;

public class FilePersisterTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Mock
FileSystem fileSystem;
Expand All @@ -47,35 +42,31 @@ public void readExists() throws FileNotFoundException {
.thenReturn(true);
when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);

BufferedSource returnedValue = fileSystemPersister.read(simple).blockingFirst();
BufferedSource returnedValue = fileSystemPersister.read(simple).blockingGet();
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).blockingFirst();
fileSystemPersister.read(simple).test().assertError(FileNotFoundException.class);
}

@Test
@SuppressWarnings("CheckReturnValue")
public void writeThenRead() throws IOException {
when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);
when(fileSystem.exists(resolvedPath)).thenReturn(true);
fileSystemPersister.write(simple, bufferedSource).blockingFirst();
BufferedSource source = fileSystemPersister.read(simple).blockingFirst();
fileSystemPersister.write(simple, bufferedSource).blockingGet();
BufferedSource source = fileSystemPersister.read(simple).blockingGet();
InOrder inOrder = inOrder(fileSystem);
inOrder.verify(fileSystem).write(resolvedPath, bufferedSource);
inOrder.verify(fileSystem).exists(resolvedPath);
inOrder.verify(fileSystem).read(resolvedPath);

assertThat(source).isEqualTo(bufferedSource);


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import com.nytimes.android.external.store2.base.impl.BarCode;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import okio.BufferedSource;
Expand All @@ -24,8 +21,6 @@
import static org.mockito.Mockito.when;

public class FileSystemRecordPersisterTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Mock
FileSystem fileSystem;
Expand All @@ -50,27 +45,26 @@ public void readExists() throws FileNotFoundException {
.thenReturn(true);
when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);

BufferedSource returnedValue = fileSystemPersister.read(simple).blockingFirst();
BufferedSource returnedValue = fileSystemPersister.read(simple).blockingGet();
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).blockingFirst();
fileSystemPersister.read(simple).test().assertError(FileNotFoundException.class);
}

@Test
@SuppressWarnings("CheckReturnValue")
public void writeThenRead() throws IOException {
when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);
when(fileSystem.exists(resolvedPath)).thenReturn(true);
fileSystemPersister.write(simple, bufferedSource).blockingFirst();
BufferedSource source = fileSystemPersister.read(simple).blockingFirst();
fileSystemPersister.write(simple, bufferedSource).blockingGet();
BufferedSource source = fileSystemPersister.read(simple).blockingGet();
InOrder inOrder = inOrder(fileSystem);
inOrder.verify(fileSystem).write(resolvedPath, bufferedSource);
inOrder.verify(fileSystem).exists(resolvedPath);
Expand Down
Loading

0 comments on commit 20445ff

Please sign in to comment.