Search in sources :

Example 36 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class GenericParserStoreTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    Parser<BufferedSource, Foo> parser = GsonParserFactory.createSourceParser(new Gson(), Foo.class);
    Store<Foo, BarCode> simpleStore = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(persister).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(persister.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(persister.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 : BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 37 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class GsonSourceListParserTest method testSimple.

@Test
public void testSimple() {
    MockitoAnnotations.initMocks(this);
    Parser<BufferedSource, List<Foo>> parser = GsonParserFactory.createSourceParser(new Gson(), new TypeToken<List<Foo>>() {
    }.getType());
    Store<List<Foo>, BarCode> simpleStore = StoreBuilder.<BarCode, BufferedSource, List<Foo>>parsedWithKey().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo foo = new Foo("a");
    Foo foo2 = new Foo("b");
    Foo foo3 = new Foo("c");
    List<Foo> data = Arrays.asList(foo, foo2, foo3);
    String sourceData = new Gson().toJson(data);
    BufferedSource source = source(sourceData);
    Observable<BufferedSource> value = Observable.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(persister.read(barCode)).thenReturn(Observable.<BufferedSource>empty()).thenReturn(value);
    when(persister.write(barCode, source)).thenReturn(Observable.just(true));
    List<Foo> result = simpleStore.get(barCode).toBlocking().first();
    assertThat(result.get(0).value).isEqualTo("a");
    assertThat(result.get(1).value).isEqualTo("b");
    assertThat(result.get(2).value).isEqualTo("c");
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) BarCode(com.nytimes.android.external.store.base.impl.BarCode) Gson(com.google.gson.Gson) List(java.util.List) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 38 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class SimpleTest method testDeleteWhileReading.

@Test
public void testDeleteWhileReading() throws IOException {
    fileSystem.write("/boo", source(testString1));
    BufferedSource source = fileSystem.read("/boo");
    fileSystem.delete("/boo");
    assertThat(fileSystem.exists("/boo")).isFalse();
    assertThat(source.readUtf8()).isEqualTo(testString1);
    assertThat(fileSystem.exists("/boo")).isFalse();
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 39 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class SimpleTest method deleteWhileReadingThenWrite.

@Test
public void deleteWhileReadingThenWrite() throws IOException {
    fileSystem.write("/boo", source(testString1));
    // open a source and hang onto it
    BufferedSource source1 = fileSystem.read("/boo");
    // now delete the file
    fileSystem.delete("/boo");
    // exists() should say it's gone even though
    assertThat(fileSystem.exists("/boo")).isFalse();
    // we still have a source to it
    // and now un-delete it by writing a new version
    fileSystem.write("/boo", source(testString2));
    // exists() should say it's back
    assertThat(fileSystem.exists("/boo")).isTrue();
    // open another source and hang onto it
    BufferedSource source2 = fileSystem.read("/boo");
    // now delete the file *again*
    fileSystem.delete("/boo");
    // the sources should have the correct data even though the file was deleted/re-written/deleted
    assertThat(source1.readUtf8()).isEqualTo(testString1);
    assertThat(source2.readUtf8()).isEqualTo(testString2);
    // now that the 2 sources have been fully read, you shouldn't be able to read it
    assertThat(fileSystem.exists("/boo")).isFalse();
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 40 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class JacksonSourceParserStoreTest method testCustomJsonFactorySourceParser.

@Test
public void testCustomJsonFactorySourceParser() {
    JsonFactory jsonFactory = new JsonFactory();
    Parser<BufferedSource, Foo> parser = JacksonParserFactory.createSourceParser(jsonFactory, Foo.class);
    Store<Foo, BarCode> store = StoreBuilder.<BarCode, BufferedSource, Foo>parsedWithKey().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo result = store.get(barCode).toBlocking().first();
    validateFoo(result);
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store.middleware.jackson.data.Foo) BarCode(com.nytimes.android.external.store.base.impl.BarCode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Aggregations

BufferedSource (okio.BufferedSource)72 Test (org.junit.Test)33 Buffer (okio.Buffer)21 IOException (java.io.IOException)19 Request (okhttp3.Request)8 Response (okhttp3.Response)8 BarCode (com.nytimes.android.external.store.base.impl.BarCode)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 EOFException (java.io.EOFException)6 InputStream (java.io.InputStream)6 ResponseBody (okhttp3.ResponseBody)6 Gson (com.google.gson.Gson)4 File (java.io.File)4 Charset (java.nio.charset.Charset)4 Headers (okhttp3.Headers)4 MediaType (okhttp3.MediaType)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 BufferedSink (okio.BufferedSink)4 Sink (okio.Sink)4 RequestBody (okhttp3.RequestBody)3