Search in sources :

Example 56 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class SourceDiskDaoStoreTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    GsonSourceParser<Foo> parser = new GsonSourceParser<>(new Gson(), Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(diskDAO).fetcher(fetcher).parser(parser).open();
    Foo foo = new Foo();
    foo.bar = barCode.getKey();
    String sourceData = new Gson().toJson(foo);
    BufferedSource source = source(sourceData);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(diskDAO.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(diskDAO.write(barCode, source)).thenReturn(Observable.just(true));
    Foo result = store.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    result = store.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : GsonSourceParser(com.nytimes.android.external.store.middleware.GsonSourceParser) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 57 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class SourceFilerReaderWriterStoreTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    GsonSourceParser<Foo> parser = new GsonSourceParser<>(new Gson(), Foo.class);
    Store<Foo, BarCode> simpleStore = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(fileReader, fileWriter).fetcher(fetcher).parser(parser).open();
    Foo foo = new Foo();
    foo.bar = barCode.getKey();
    String sourceData = new Gson().toJson(foo);
    BufferedSource source = source(sourceData);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(fileReader.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(fileWriter.write(barCode, source)).thenReturn(Observable.just(true));
    Foo result = simpleStore.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    result = simpleStore.get(barCode).toBlocking().first();
    assertThat(result.bar).isEqualTo(KEY);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : GsonSourceParser(com.nytimes.android.external.store.middleware.GsonSourceParser) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 58 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class FilePersisterTest method readExists.

@Test
public void readExists() throws FileNotFoundException {
    when(fileSystem.exists(resolvedPath)).thenReturn(true);
    when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);
    BufferedSource returnedValue = fileSystemPersister.read(simple).toBlocking().single();
    assertThat(returnedValue).isEqualTo(bufferedSource);
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 59 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class MultiTest method createAndPopulateTestFileSystem.

private FileSystem createAndPopulateTestFileSystem() throws IOException {
    File baseDir = createTempDir();
    FileSystem fileSystem = FileSystemFactory.create(baseDir);
    for (String path : fileData.keySet()) {
        for (String data : fileData.get(path)) {
            BufferedSource source = source(data);
            fileSystem.write(path, source);
            source.close();
        }
    }
    assertThat(fileSystem.list("/").size()).isEqualTo(fileData.size());
    return fileSystem;
}
Also used : FileSystem(com.nytimes.android.external.fs.filesystem.FileSystem) File(java.io.File) BufferedSource(okio.BufferedSource)

Example 60 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class RecordPersisterTest method readExists.

@Test
public void readExists() throws FileNotFoundException {
    when(fileSystem.exists(simple.toString())).thenReturn(true);
    when(fileSystem.read(simple.toString())).thenReturn(bufferedSource);
    BufferedSource returnedValue = sourcePersister.read(simple).toBlocking().single();
    assertThat(returnedValue).isEqualTo(bufferedSource);
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Aggregations

BufferedSource (okio.BufferedSource)97 Test (org.junit.Test)51 IOException (java.io.IOException)23 Buffer (okio.Buffer)23 Request (okhttp3.Request)10 Response (okhttp3.Response)10 Gson (com.google.gson.Gson)8 EOFException (java.io.EOFException)8 ResponseBody (okhttp3.ResponseBody)8 BarCode (com.nytimes.android.external.store.base.impl.BarCode)7 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)7 Charset (java.nio.charset.Charset)7 MediaType (okhttp3.MediaType)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 File (java.io.File)6 InputStream (java.io.InputStream)6 Headers (okhttp3.Headers)6 RequestBody (okhttp3.RequestBody)6 BufferedSink (okio.BufferedSink)6 List (java.util.List)4