|
| 1 | +package com.microsoft.graph.functional; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Ignore; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.microsoft.graph.concurrency.ChunkedUploadProvider; |
| 12 | +import com.microsoft.graph.concurrency.IProgressCallback; |
| 13 | +import com.microsoft.graph.core.ClientException; |
| 14 | +import com.microsoft.graph.models.extensions.DriveItem; |
| 15 | +import com.microsoft.graph.models.extensions.DriveItemUploadableProperties; |
| 16 | +import com.microsoft.graph.models.extensions.UploadSession; |
| 17 | + |
| 18 | +@Ignore |
| 19 | +public class OneDriveTests { |
| 20 | + private TestBase testBase; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() { |
| 24 | + testBase = new TestBase(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test large file upload. |
| 29 | + * https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md |
| 30 | + * |
| 31 | + * @throws IOException if the input file is not found |
| 32 | + * @throws InterruptedException if the chunked upload fails |
| 33 | + */ |
| 34 | + @Test |
| 35 | + public void testLargeFileUpload() throws IOException, InterruptedException { |
| 36 | + String itemId = "01BQHXQL5GQVAGCFJLYRH3EAG2YHGERMQA"; //Test upload folder |
| 37 | + |
| 38 | + //Get resource file from file system |
| 39 | + InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("hamilton.jpg"); |
| 40 | + int fileSize = uploadFile.available(); |
| 41 | + |
| 42 | + IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () { |
| 43 | + @Override |
| 44 | + public void progress(final long current, final long max) { |
| 45 | + //Check progress |
| 46 | + } |
| 47 | + @Override |
| 48 | + public void success(final DriveItem result) { |
| 49 | + //Handle the successful response |
| 50 | + String finishedItemId = result.id; |
| 51 | + Assert.assertNotNull(finishedItemId); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void failure(final ClientException ex) { |
| 56 | + //Handle the failed upload |
| 57 | + Assert.fail("Upload session failed"); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + UploadSession uploadSession = testBase |
| 62 | + .graphClient |
| 63 | + .me() |
| 64 | + .drive() |
| 65 | + .items(itemId) |
| 66 | + .itemWithPath("_hamilton.jpg") |
| 67 | + .createUploadSession(new DriveItemUploadableProperties()) |
| 68 | + .buildRequest() |
| 69 | + .post(); |
| 70 | + |
| 71 | + ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>( |
| 72 | + uploadSession, |
| 73 | + testBase.graphClient, |
| 74 | + uploadFile, |
| 75 | + fileSize, |
| 76 | + DriveItem.class); |
| 77 | + |
| 78 | + chunkedUploadProvider.upload(callback); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments