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");
}
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());
}
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();
}
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;
}
}
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);
}
Aggregations