Search in sources :

Example 91 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class MoshiSourceParserTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    BufferedSource bufferedSource = source(sourceString);
    assertNotNull(bufferedSource);
    when(fetcher.fetch(barCode)).thenReturn(Single.just(bufferedSource));
    when(persister.read(barCode)).thenReturn(Maybe.<BufferedSource>empty()).thenReturn(Maybe.just(bufferedSource));
    when(persister.write(barCode, bufferedSource)).thenReturn(Single.just(true));
}
Also used : BufferedSource(okio.BufferedSource) Before(org.junit.Before)

Example 92 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class MoshiSourceParserTest method testSourceParser.

@Test
public void testSourceParser() throws Exception {
    Parser<BufferedSource, Foo> parser = MoshiParserFactory.createSourceParser(Foo.class);
    Store<Foo, BarCode> store = ParsingStoreBuilder.<BufferedSource, Foo>builder().persister(persister).fetcher(fetcher).parser(parser).open();
    Foo result = store.get(barCode).blockingGet();
    assertEquals(result.number, 123);
    assertEquals(result.string, "abc");
    assertEquals(result.bars.size(), 2);
    assertEquals(result.bars.get(0).string, "def");
    assertEquals(result.bars.get(1).string, "ghi");
    verify(fetcher, times(1)).fetch(barCode);
}
Also used : Foo(com.nytimes.android.external.store3.middleware.moshi.data.Foo) BarCode(com.nytimes.android.external.store3.base.impl.BarCode) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 93 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);
    Single<BufferedSource> value = Single.just(source);
    when(fetcher.fetch(barCode)).thenReturn(value);
    when(persister.read(barCode)).thenReturn(Maybe.<BufferedSource>empty()).thenReturn(value.toMaybe());
    when(persister.write(barCode, source)).thenReturn(Single.just(true));
    List<Foo> result = simpleStore.get(barCode).blockingGet();
    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.store3.base.impl.BarCode) Gson(com.google.gson.Gson) List(java.util.List) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 94 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class FileSystemRecordPersisterTest 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).blockingGet();
    assertThat(returnedValue).isEqualTo(bufferedSource);
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 95 with BufferedSource

use of okio.BufferedSource in project Store by NYTimes.

the class FileSystemRecordPersisterTest method writeThenRead.

@Test
@SuppressWarnings("CheckReturnValue")
public void writeThenRead() throws IOException {
    when(fileSystem.read(resolvedPath)).thenReturn(bufferedSource);
    when(fileSystem.exists(resolvedPath)).thenReturn(true);
    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);
}
Also used : InOrder(org.mockito.InOrder) 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