Skip to content

Commit d32a129

Browse files
committed
- arranges unit test to upload file before testing
1 parent 0af1708 commit d32a129

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

src/test/java/com/microsoft/graph/functional/OneDriveTests.java

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertTrue;
55

6+
import java.io.ByteArrayInputStream;
67
import java.io.IOException;
78
import java.io.InputStream;
9+
import java.nio.charset.StandardCharsets;
810

911
import org.junit.Assert;
1012
import org.junit.Before;
1113
import org.junit.Ignore;
1214
import org.junit.Test;
1315

16+
import com.google.gson.JsonPrimitive;
1417
import com.microsoft.graph.concurrency.ChunkedUploadProvider;
1518
import com.microsoft.graph.concurrency.IProgressCallback;
1619
import com.microsoft.graph.core.ClientException;
@@ -27,7 +30,25 @@ public class OneDriveTests {
2730
public void setUp() {
2831
testBase = new TestBase();
2932
}
30-
33+
34+
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
35+
@Override
36+
public void progress(final long current, final long max) {
37+
//Check progress
38+
}
39+
@Override
40+
public void success(final DriveItem result) {
41+
//Handle the successful response
42+
String finishedItemId = result.id;
43+
Assert.assertNotNull(finishedItemId);
44+
}
45+
46+
@Override
47+
public void failure(final ClientException ex) {
48+
//Handle the failed upload
49+
Assert.fail("Upload session failed");
50+
}
51+
};
3152
/**
3253
* Test large file upload.
3354
* https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
@@ -43,25 +64,6 @@ public void testLargeFileUpload() throws IOException, InterruptedException {
4364
InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("hamilton.jpg");
4465
long fileSize = (long) uploadFile.available();
4566

46-
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
47-
@Override
48-
public void progress(final long current, final long max) {
49-
//Check progress
50-
}
51-
@Override
52-
public void success(final DriveItem result) {
53-
//Handle the successful response
54-
String finishedItemId = result.id;
55-
Assert.assertNotNull(finishedItemId);
56-
}
57-
58-
@Override
59-
public void failure(final ClientException ex) {
60-
//Handle the failed upload
61-
Assert.fail("Upload session failed");
62-
}
63-
};
64-
6567
UploadSession uploadSession = testBase
6668
.graphClient
6769
.me()
@@ -89,13 +91,38 @@ public void testDownloadWithCustomRequest() throws IOException {
8991
}
9092
@Test
9193
public void downloadJsonFileFromOneDrive() throws Exception {
94+
final DriveItemUploadableProperties item = new DriveItemUploadableProperties();
95+
item.name = "test.json";
96+
item.additionalDataManager().put("@microsoft.graph.conflictBehavior", new JsonPrimitive("replace"));
97+
98+
final InputStream uploadFile = new ByteArrayInputStream("{\"hehe\":\"haha\"}".getBytes(StandardCharsets.UTF_8));
99+
100+
final long fileSize = (long) uploadFile.available();
101+
102+
final UploadSession session = testBase.graphClient.me()
103+
.drive()
104+
.root()
105+
.itemWithPath(item.name)
106+
.createUploadSession(item)
107+
.buildRequest()
108+
.post();
109+
110+
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
111+
session,
112+
testBase.graphClient,
113+
uploadFile,
114+
fileSize,
115+
DriveItem.class);
116+
117+
chunkedUploadProvider.upload(callback);
118+
92119
final InputStream stream = testBase.graphClient.me()
93-
.drive()
94-
.root()
95-
.itemWithPath("test.json")
96-
.content()
97-
.buildRequest()
98-
.get();
120+
.drive()
121+
.root()
122+
.itemWithPath(item.name)
123+
.content()
124+
.buildRequest()
125+
.get();
99126

100127
final String fileContent = CoreHttpProvider.streamToString(stream);
101128

0 commit comments

Comments
 (0)