Search in sources :

Example 41 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testOnlineWontRefresh.

@Test
public void testOnlineWontRefresh() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    DbxCredential credential = new DbxCredential("accesstoken");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse());
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.files().getMetadata(expected.getId());
    } catch (InvalidAccessTokenException ex) {
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
        assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
        AuthError authError = DbxRequestUtil.readJsonFromErrorMessage(AuthError.Serializer.INSTANCE, ex.getMessage(), ex.getRequestId());
        assertThat(authError).isEqualTo(AuthError.EXPIRED_ACCESS_TOKEN);
        return;
    }
    fail("API v2 call should throw exception");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) AuthError(com.dropbox.core.v2.auth.AuthError) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 42 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRefreshAndRetryAfterTokenExpired.

@Test
public void testRefreshAndRetryAfterTokenExpired() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    long now = System.currentTimeMillis();
    DbxCredential credential = new DbxCredential("accesstoken", now + 2 * DbxCredential.EXPIRE_MARGIN, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse()).thenReturn(createSuccessRefreshResponse("new_token", 14400L)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("new_token");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 43 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetrySuccessWithBackoff.

@Test
public void testRetrySuccessWithBackoff() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = constructFileMetadate();
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(// no backoff
    createEmptyResponse(503)).thenReturn(// backoff 1 sec
    createRateLimitResponse(1)).thenReturn(// backoff 2 sec
    createRateLimitResponse(2)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    long start = System.currentTimeMillis();
    Metadata actual = client.files().getMetadata(expected.getId());
    long end = System.currentTimeMillis();
    // no way easy way to properly test this, but request should
    // have taken AT LEAST 3 seconds due to backoff.
    assertWithMessage("duration: " + (end - start) + " millis").that(end - start >= 3000L).isTrue();
    // should have been called 4 times: initial call + 3 retries
    verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Test(org.testng.annotations.Test)

Example 44 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.

the class DbxClientV2IT method testError409.

@Test(expectedExceptions = { GetMetadataErrorException.class })
public void testError409() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    String path = ITUtil.path(getClass(), "/testError409/" + ITUtil.format(new Date()));
    try {
        client.files().getMetadata(path);
    } catch (GetMetadataErrorException ex) {
        assertThat(ex.getRequestId()).isNotNull();
        if (ex.getUserMessage() != null) {
            assertThat(ex.getUserMessage().getLocale()).isNotNull();
            assertThat(ex.getUserMessage().getText()).isNotNull();
            assertThat(ex.getUserMessage().toString()).isNotNull();
        }
        GetMetadataError err = ex.errorValue;
        assertThat(err).isNotNull();
        assertThat(err.tag()).isEqualTo(GetMetadataError.Tag.PATH);
        assertThat(err.isPath()).isTrue();
        LookupError lookup = err.getPathValue();
        assertThat(lookup).isNotNull();
        assertThat(lookup.tag()).isEqualTo(LookupError.Tag.NOT_FOUND);
        assertThat(lookup.isNotFound()).isTrue();
        assertThat(lookup.isNotFile()).isFalse();
        assertThat(lookup.isOther()).isFalse();
        assertThat(lookup.isMalformedPath()).isFalse();
        // raise so test can confirm an exception was thrown
        throw ex;
    }
}
Also used : GetMetadataError(com.dropbox.core.v2.files.GetMetadataError) Date(java.util.Date) LookupError(com.dropbox.core.v2.files.LookupError) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException) Test(org.testng.annotations.Test)

Example 45 with DbxClientV2

use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.

the class DbxClientV2IT method testDownloadBuilder.

@Test
public void testDownloadBuilder() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    String now = ITUtil.format(new Date());
    byte[] rtfV1 = ITUtil.toBytes("{\rtf1 sample {\b v1} (" + now + ")}");
    byte[] rtfV2 = ITUtil.toBytes("{\rtf1 sample {\b v2} (" + now + ")}");
    String path = ITUtil.path(getClass(), "/testDownloadBuilder/" + now + ".rtf");
    FileMetadata metadataV1 = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.ADD).withMute(true).uploadAndFinish(new ByteArrayInputStream(rtfV1));
    assertThat(metadataV1.getPathLower()).isEqualTo(path.toLowerCase());
    FileMetadata metadataV2 = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.OVERWRITE).withMute(true).uploadAndFinish(new ByteArrayInputStream(rtfV2));
    assertThat(metadataV2.getPathLower()).isEqualTo(path.toLowerCase());
    // ensure we have separate revisions
    assertThat(metadataV1.getRev()).isNotEqualTo(metadataV2.getRev());
    // now use download builder to set revision and make sure it works properly
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path).withRev(metadataV1.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV1);
    out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path).withRev(metadataV2.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
    // ensure we still keep the non-builder optional route in our generator (for
    // backwards-compatibility)
    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV1.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV1);
    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV2.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
    // and ensure we keep the required-only route
    out = new ByteArrayOutputStream();
    client.files().download(path).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

DbxClientV2 (com.dropbox.core.v2.DbxClientV2)25 FileMetadata (com.dropbox.core.v2.files.FileMetadata)24 Test (org.testng.annotations.Test)19 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)18 HttpRequestor (com.dropbox.core.http.HttpRequestor)15 Metadata (com.dropbox.core.v2.files.Metadata)15 DbxException (com.dropbox.core.DbxException)10 DbxCredential (com.dropbox.core.oauth.DbxCredential)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 JsonReader (com.dropbox.core.json.JsonReader)5 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)5 FullAccount (com.dropbox.core.v2.users.FullAccount)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)4 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 IOException (java.io.IOException)4 Date (java.util.Date)4 NetworkIOException (com.dropbox.core.NetworkIOException)3 ProgressListener (com.dropbox.core.util.IOUtil.ProgressListener)3