fix issue #76, add additional create(FileSystem) within SourcePersisterFactory, some unit tests for SourcePersister (#77)

This commit is contained in:
Brian Plummer 2017-01-23 21:57:25 -05:00 committed by GitHub
parent 2ad00e4968
commit d31b8c5f44
3 changed files with 85 additions and 1 deletions

View file

@ -29,7 +29,7 @@ public class SourcePersister implements Persister<BufferedSource> {
@Inject
public SourcePersister(FileSystem fileSystem) {
this.sourceFileReader = new SourceFileReader(fileSystem);
sourceFileReader = new SourceFileReader(fileSystem);
sourceFileWriter = new SourceFileWriter(fileSystem);
}

View file

@ -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<BufferedSource> create(@NonNull FileSystem fileSystem) throws IOException {
if (fileSystem == null) {
throw new IllegalArgumentException("fileSystem cannot be null.");
}
return new SourcePersister(fileSystem);
}
}

View file

@ -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");
}
}