Search in sources :

Example 16 with DbxClientV2

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

the class DbxClientV2Test method testOnlineInvalidToken.

@Test
public void testOnlineInvalidToken() 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(createInvalidTokenResponse());
    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");
        assertThat(ex.getAuthError()).isEqualTo(AuthError.INVALID_ACCESS_TOKEN);
        assertThat(ex.getMessage()).isEqualTo("");
        return;
    }
    fail("API v2 call should throw exception");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 17 with DbxClientV2

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

the class DbxClientV2Test method testDontRefreshBeforeCallIfNotExpired.

@Test
public void testDontRefreshBeforeCallIfNotExpired() 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(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
    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 18 with DbxClientV2

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

the class DbxClientV2Test method testDontRefreshForInvalidGrant.

@Test
public void testDontRefreshForInvalidGrant() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    ByteArrayInputStream responseStream = new ByteArrayInputStream(("{" + "\"error_description\":\"refresh token is invalid or revoked\"" + ",\"error\":\"invalid_grant\"" + "}").getBytes("UTF-8"));
    HttpRequestor.Response finishResponse = new HttpRequestor.Response(400, responseStream, new HashMap<String, List<String>>());
    DbxCredential credential = new DbxCredential("accesstoken", 10L, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(finishResponse).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
    assertThat(credential.getExpiresAt()).isEqualTo(new Long(10));
    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) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 19 with DbxClientV2

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

the class DbxClientV2Test method testRefreshAndRetryWith503Retry.

@Test
public void testRefreshAndRetryWith503Retry() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled().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(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(4)).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 20 with DbxClientV2

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

the class DbxClientV2Test method testRefreshAndRetryAfterTokenExpiredForDownload.

@Test
public void testRefreshAndRetryAfterTokenExpiredForDownload() 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(createDownloadSuccessResponse("data".getBytes(), new String(serialize(expected))));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DbxDownloader<FileMetadata> downloader = client.files().download("/path");
    try {
        downloader.download(out);
    } finally {
        downloader.close();
    }
    assertThat(out.toString()).isEqualTo("data");
    Metadata actual = downloader.getResult();
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("new_token");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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