|
5 | 5 | import com.microsoft.graph.core.models.UploadSession; |
6 | 6 | import com.microsoft.kiota.authentication.AuthenticationProvider; |
7 | 7 | import com.microsoft.kiota.http.OkHttpRequestAdapter; |
| 8 | + |
8 | 9 | import org.junit.jupiter.api.Test; |
9 | 10 | import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.spy; |
| 12 | +import static org.mockito.Mockito.doReturn; |
10 | 13 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | +import org.mockito.ArgumentCaptor; |
11 | 16 |
|
12 | 17 | import java.io.ByteArrayInputStream; |
13 | 18 | import java.io.IOException; |
|
17 | 22 | import java.util.ArrayList; |
18 | 23 | import java.util.Arrays; |
19 | 24 |
|
| 25 | +import org.mockito.internal.matchers.Any; |
| 26 | + |
| 27 | +import com.microsoft.graph.core.models.UploadResult; |
| 28 | + |
20 | 29 | class LargeFileUploadTest { |
21 | 30 |
|
22 | 31 | final OkHttpRequestAdapter adapter = new OkHttpRequestAdapter(mock(AuthenticationProvider.class)); |
@@ -106,4 +115,59 @@ void BreakStreamIntoCorrectRanges() throws IOException, NoSuchFieldException, Il |
106 | 115 | assertEquals(size%maxSliceSize, lastSlice.getRangeLength()); |
107 | 116 | assertEquals(size-1, lastSlice.getRangeEnd()); |
108 | 117 | } |
| 118 | + // Test for chunkInputStream method with a 5MB file |
| 119 | + @Test |
| 120 | + void uploads5MBFileSuccessfully() throws Exception { |
| 121 | + // Arrange |
| 122 | + UploadSession session = new UploadSession(); |
| 123 | + session.setNextExpectedRanges(Arrays.asList("0-")); |
| 124 | + session.setUploadUrl("http://localhost"); |
| 125 | + session.setExpirationDateTime(OffsetDateTime.now().plusHours(1)); |
| 126 | + |
| 127 | + // 5MB file |
| 128 | + byte[] data = new byte[5 * 1024 * 1024]; |
| 129 | + for (int i = 0; i < data.length; i++) { |
| 130 | + data[i] = (byte)(i % 256); |
| 131 | + } |
| 132 | + ByteArrayInputStream stream = new ByteArrayInputStream(data); |
| 133 | + int size = stream.available(); |
| 134 | + |
| 135 | + // Create a real task to get the real builder(s) |
| 136 | + LargeFileUploadTask<TestDriveItem> realTask = new LargeFileUploadTask<>(adapter, session, stream, size, TestDriveItem::createFromDiscriminatorValue); |
| 137 | + var realBuilders = realTask.getUploadSliceRequests(); |
| 138 | + |
| 139 | + // Spy the builder(s) and mock put() |
| 140 | + ArrayList<UploadSliceRequestBuilder<TestDriveItem>> spyBuilders = new ArrayList<>(); |
| 141 | + ArgumentCaptor<ByteArrayInputStream> captor = ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 142 | + |
| 143 | + for (UploadSliceRequestBuilder<TestDriveItem> builder : realBuilders) { |
| 144 | + UploadSliceRequestBuilder<TestDriveItem> spyBuilder = spy(builder); |
| 145 | + UploadResult<TestDriveItem> mockResult = new UploadResult<>(); |
| 146 | + TestDriveItem item = new TestDriveItem(); |
| 147 | + item.size = data.length; |
| 148 | + mockResult.itemResponse = item; |
| 149 | + doReturn(mockResult).when(spyBuilder).put(captor.capture()); |
| 150 | + spyBuilders.add(spyBuilder); |
| 151 | + } |
| 152 | + |
| 153 | + // Subclass LargeFileUploadTask to inject our spy builders |
| 154 | + LargeFileUploadTask<TestDriveItem> task = new LargeFileUploadTask<>(adapter, session, stream, size, TestDriveItem::createFromDiscriminatorValue) { |
| 155 | + @Override |
| 156 | + protected java.util.List<UploadSliceRequestBuilder<TestDriveItem>> getUploadSliceRequests() { |
| 157 | + return spyBuilders; |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + // Act |
| 162 | + task.upload(3, null); |
| 163 | + |
| 164 | + // Verify the chunkStream content |
| 165 | + ByteArrayInputStream capturedStream = captor.getValue(); |
| 166 | + byte[] capturedBytes = new byte[data.length]; |
| 167 | + int read = capturedStream.read(capturedBytes); |
| 168 | + assertEquals(data.length, read, "Should read all bytes from chunkStream"); |
| 169 | + for (int i = 0; i < data.length; i++) { |
| 170 | + assertEquals(data[i], capturedBytes[i], "Byte at position " + i + " should match original data"); |
| 171 | + } |
| 172 | + } |
109 | 173 | } |
0 commit comments