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 20cb9dd..a542416 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 @@ -29,7 +29,7 @@ public class SourcePersister implements Persister { @Inject public SourcePersister(FileSystem fileSystem) { - this.sourceFileReader = new SourceFileReader(fileSystem); + sourceFileReader = new SourceFileReader(fileSystem); sourceFileWriter = new SourceFileWriter(fileSystem); } diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java b/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java index 5f74a04..819a184 100644 --- a/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java +++ b/filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java @@ -2,6 +2,7 @@ package com.nytimes.android.external.fs; import android.support.annotation.NonNull; +import com.nytimes.android.external.fs.filesystem.FileSystem; import com.nytimes.android.external.fs.filesystem.FileSystemFactory; import com.nytimes.android.external.store.base.Persister; @@ -31,4 +32,18 @@ public final class SourcePersisterFactory { } return new SourcePersister(FileSystemFactory.create(root)); } + + /** + * Returns a new {@link BufferedSource} persister with the provided fileSystem as the root of the + * persistence {@link com.nytimes.android.external.fs.filesystem.FileSystem}. + * + * @throws IOException + */ + @NonNull + public static Persister create(@NonNull FileSystem fileSystem) throws IOException { + if (fileSystem == null) { + throw new IllegalArgumentException("fileSystem cannot be null."); + } + return new SourcePersister(fileSystem); + } } 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 new file mode 100644 index 0000000..291b449 --- /dev/null +++ b/filesystem/src/test/java/com/nytimes/android/external/fs/SourcePersisterTest.java @@ -0,0 +1,69 @@ +package com.nytimes.android.external.fs; + +import com.nytimes.android.external.fs.filesystem.FileSystem; +import com.nytimes.android.external.store.base.impl.BarCode; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.NoSuchElementException; + +import okio.BufferedSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +public class SourcePersisterTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Mock + FileSystem fileSystem; + @Mock + BufferedSource bufferedSource; + + private SourcePersister sourcePersister; + private final BarCode simple = new BarCode("type", "key"); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + sourcePersister = new SourcePersister(fileSystem); + } + + @Test + public void readExists() throws FileNotFoundException { + when(fileSystem.exists(SourcePersister.pathForBarcode(simple))) + .thenReturn(true); + when(fileSystem.read(SourcePersister.pathForBarcode(simple))).thenReturn(bufferedSource); + + BufferedSource returnedValue = sourcePersister.read(simple).toBlocking().single(); + assertThat(returnedValue).isEqualTo(bufferedSource); + } + + @Test + public void readDoesNotExist() throws FileNotFoundException { + expectedException.expect(NoSuchElementException.class); + when(fileSystem.exists(SourcePersister.pathForBarcode(simple))) + .thenReturn(false); + + sourcePersister.read(simple).toBlocking().single(); + } + + @Test + public void write() throws IOException { + assertThat(sourcePersister.write(simple, bufferedSource).toBlocking().single()).isTrue(); + } + + @Test + public void pathForBarcode() { + assertThat(SourcePersister.pathForBarcode(simple)).isEqualTo("typekey"); + } +}