forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneDriveTests.java
More file actions
129 lines (111 loc) · 3.88 KB
/
Copy pathOneDriveTests.java
File metadata and controls
129 lines (111 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.microsoft.graph.functional;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.google.gson.JsonPrimitive;
import com.microsoft.graph.concurrency.ChunkedUploadProvider;
import com.microsoft.graph.concurrency.IProgressCallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.http.CoreHttpProvider;
import com.microsoft.graph.models.extensions.DriveItem;
import com.microsoft.graph.models.extensions.DriveItemUploadableProperties;
import com.microsoft.graph.models.extensions.UploadSession;
@Ignore
public class OneDriveTests {
private TestBase testBase;
@Before
public void setUp() {
testBase = new TestBase();
}
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
@Override
public void progress(final long current, final long max) {
//Check progress
}
@Override
public void success(final DriveItem result) {
//Handle the successful response
String finishedItemId = result.id;
Assert.assertNotNull(finishedItemId);
}
@Override
public void failure(final ClientException ex) {
//Handle the failed upload
Assert.fail("Upload session failed");
}
};
/**
* Test large file upload.
* https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
*
* @throws IOException if the input file is not found
* @throws InterruptedException if the chunked upload fails
*/
@Test
public void testLargeFileUpload() throws IOException, InterruptedException {
//Get resource file from file system
InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("largefile10M.blob");
long fileSize = (long) uploadFile.available();
UploadSession uploadSession = testBase
.graphClient
.me()
.drive()
.root()
.itemWithPath("largefile10M.blob")
.createUploadSession(new DriveItemUploadableProperties())
.buildRequest()
.post();
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
uploadSession,
testBase.graphClient,
uploadFile,
fileSize,
DriveItem.class);
chunkedUploadProvider.upload(callback);
}
@Test
public void testDownloadWithCustomRequest() throws IOException {
final String testDownloadFileId = "01RWFXFJG3UYRHE75RZVFYWKNUEBB53H7A";
try (final InputStream stream = testBase.graphClient.customRequest("/me/drive/items/"+testDownloadFileId+"/content", InputStream.class).buildRequest().get()) {
assertFalse("stream should not be empty", stream.read() == -1);
}
}
@Test
public void downloadJsonFileFromOneDrive() throws Exception {
final DriveItemUploadableProperties item = new DriveItemUploadableProperties();
item.name = "test.json";
item.additionalDataManager().put("@microsoft.graph.conflictBehavior", new JsonPrimitive("replace"));
final InputStream uploadFile = new ByteArrayInputStream("{\"hehe\":\"haha\"}".getBytes(StandardCharsets.UTF_8));
final long fileSize = (long) uploadFile.available();
final UploadSession session = testBase.graphClient.me()
.drive()
.root()
.itemWithPath(item.name)
.createUploadSession(item)
.buildRequest()
.post();
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
session,
testBase.graphClient,
uploadFile,
fileSize,
DriveItem.class);
chunkedUploadProvider.upload(callback);
final InputStream stream = testBase.graphClient.me()
.drive()
.root()
.itemWithPath(item.name)
.content()
.buildRequest()
.get();
final String fileContent = CoreHttpProvider.streamToString(stream);
assertTrue(fileContent.length() > 0);
}
}