use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DbxClientV2Test method testRetrySuccess.
@Test
public void testRetrySuccess() throws DbxException, IOException {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
FileMetadata expected = new FileMetadata("bar.txt", "id:1HkLjqifwMAAAAAAAAAAAQ", new Date(1456169040985L), new Date(1456169040985L), "2e0c38735597", 2091603);
// 503 twice, then return result
HttpRequestor.Uploader mockUploader = mockUploader();
when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
Metadata actual = client.files().getMetadata(expected.getId());
// should have only been called 3 times: initial call + 2 retries
verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
assertEquals(actual.getName(), expected.getName());
assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DbxClientV2Test method serialize.
private static byte[] serialize(Metadata metadata) {
assertNotNull(metadata);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
serializer(Metadata.class).serialize(metadata, out);
} catch (Exception ex) {
fail("unserializable type: " + metadata.getClass(), ex);
return null;
}
return out.toByteArray();
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DbxClientV2IT method testRangeDownload.
@Test
public void testRangeDownload() throws Exception {
DbxClientV2 client = ITUtil.newClientV2();
byte[] contents = ITUtil.randomBytes(500);
String path = ITUtil.path(getClass(), "/testRangeDownload.dat");
FileMetadata metadata = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.OVERWRITE).withMute(true).uploadAndFinish(new ByteArrayInputStream(contents));
assertEquals(metadata.getSize(), contents.length);
assertRangeDownload(client, path, contents, 0, contents.length);
assertRangeDownload(client, path, contents, 0, 200);
assertRangeDownload(client, path, contents, 300, 200);
assertRangeDownload(client, path, contents, 499, 1);
assertRangeDownload(client, path, contents, 0, 600);
assertRangeDownload(client, path, contents, 250, null);
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DbxClientV2IT method testUploadAndDownload.
private void testUploadAndDownload(DbxClientV2 client) throws Exception {
byte[] contents = ITUtil.randomBytes(1024);
String filename = "testUploadAndDownload.dat";
String path = ITUtil.path(getClass(), "/" + filename);
FileMetadata metadata = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.ADD).withMute(true).uploadAndFinish(new ByteArrayInputStream(contents));
assertEquals(metadata.getName(), filename);
assertEquals(metadata.getPathLower(), path.toLowerCase());
assertEquals(metadata.getSize(), contents.length);
Metadata actual = client.files().getMetadata(path);
assertTrue(actual instanceof FileMetadata, actual.getClass().getCanonicalName());
assertEquals(actual, metadata);
DbxDownloader<FileMetadata> downloader = client.files().download(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
downloader.download(out);
byte[] actualContents = out.toByteArray();
FileMetadata actualResult = downloader.getResult();
assertEquals(actualResult, metadata);
assertEquals(actualContents, contents);
Metadata deleted = client.files().delete(path);
assertEquals(deleted, metadata);
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class Main method main.
public static void main(String[] args) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Instantiate factory and set shared factory
DbxRequestUtil.sharedCallbackFactory = new DbxExampleGlobalCallbackFactory();
try {
// Get files and folder metadata from Dropbox root directory
client.files().listFolder("/does/not/exist/folder/");
} catch (ListFolderErrorException ex) {
System.err.println("STANDARD ROUTE ERROR HANDLER: " + ex.errorValue + "\n");
} catch (DbxException ex) {
System.err.println("STANDARD NETWORK ERROR HANDLER: " + ex + "\n");
}
try {
// Get files and folder metadata from Dropbox root directory
client.auth().tokenRevoke();
client.files().listFolder("/does/not/exist");
} catch (ListFolderErrorException ex) {
System.err.println("STANDARD ROUTE ERROR HANDLER2: " + ex.errorValue + "\n");
} catch (DbxException ex) {
System.err.println("STANDARD NETWORK ERROR HANDLER2: " + ex + "\n");
}
}
Aggregations