Search in sources :

Example 1 with Downloader

use of com.dropbox.core.v1.DbxClientV1.Downloader in project dropbox-sdk-java by dropbox.

the class DbxClientV1Test method testRetryDownload.

@Test
public void testRetryDownload() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // load File metadata json
    InputStream in = getClass().getResourceAsStream("/file-with-photo-info.json");
    assertNotNull(in);
    String metadataJson = IOUtil.toUtf8String(in);
    byte[] expected = new byte[] { 1, 2, 3, 4 };
    // 503 once, then return 200
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expected, "X-Dropbox-Metadata", metadataJson));
    Downloader downloader = client.startGetThumbnail(DbxThumbnailSize.w64h64, DbxThumbnailFormat.JPEG, "/foo/bar.jpg", null);
    // should have been attempted twice
    verify(mockRequestor, times(2)).doGet(anyString(), anyHeaders());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtil.copyStreamToStream(downloader.body, bout);
    byte[] actual = bout.toByteArray();
    assertEquals(actual, expected);
    assertEquals(downloader.metadata.path, "/Photos/Sample Album/Boston City Flow.jpg");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Downloader(com.dropbox.core.v1.DbxClientV1.Downloader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Aggregations

DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 HttpRequestor (com.dropbox.core.http.HttpRequestor)1 Downloader (com.dropbox.core.v1.DbxClientV1.Downloader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Test (org.testng.annotations.Test)1