33import static org .junit .Assert .assertFalse ;
44import static org .junit .Assert .assertTrue ;
55
6+ import java .io .ByteArrayInputStream ;
67import java .io .IOException ;
78import java .io .InputStream ;
9+ import java .nio .charset .StandardCharsets ;
810
911import org .junit .Assert ;
1012import org .junit .Before ;
1113import org .junit .Ignore ;
1214import org .junit .Test ;
1315
16+ import com .google .gson .JsonPrimitive ;
1417import com .microsoft .graph .concurrency .ChunkedUploadProvider ;
1518import com .microsoft .graph .concurrency .IProgressCallback ;
1619import 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