|
23 | 23 | package com.onedrive.sdk.http; |
24 | 24 |
|
25 | 25 | import com.onedrive.sdk.concurrency.AsyncMonitorLocation; |
| 26 | +import com.onedrive.sdk.concurrency.AsyncMonitorResponseHandler; |
| 27 | +import com.onedrive.sdk.concurrency.ChunkedUploadResponseHandler; |
26 | 28 | import com.onedrive.sdk.concurrency.IProgressCallback; |
27 | 29 | import com.onedrive.sdk.concurrency.MockExecutors; |
28 | 30 | import com.onedrive.sdk.core.ClientException; |
29 | 31 | import com.onedrive.sdk.core.OneDriveErrorCodes; |
30 | 32 | import com.onedrive.sdk.extensions.AsyncOperationStatus; |
| 33 | +import com.onedrive.sdk.extensions.ChunkedUploadResult; |
31 | 34 | import com.onedrive.sdk.extensions.Drive; |
32 | 35 | import com.onedrive.sdk.extensions.Item; |
| 36 | +import com.onedrive.sdk.extensions.UploadSession; |
33 | 37 | import com.onedrive.sdk.logger.MockLogger; |
34 | 38 | import com.onedrive.sdk.serializer.MockSerializer; |
35 | 39 |
|
|
40 | 44 | import java.io.IOException; |
41 | 45 | import java.io.InputStream; |
42 | 46 | import java.io.OutputStream; |
| 47 | +import java.util.Arrays; |
43 | 48 | import java.util.HashMap; |
44 | 49 | import java.util.Map; |
45 | 50 | import java.util.concurrent.atomic.AtomicBoolean; |
@@ -76,7 +81,7 @@ public Map<String, String> getHeaders() { |
76 | 81 | setDefaultHttpProvider(new AsyncOperationStatus()); |
77 | 82 | mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data))); |
78 | 83 |
|
79 | | - AsyncOperationStatus response = mProvider.send(new MockRequest(), AsyncOperationStatus.class, null); |
| 84 | + AsyncOperationStatus response = mProvider.send(new MockRequest(), AsyncOperationStatus.class, null, new AsyncMonitorResponseHandler()); |
80 | 85 |
|
81 | 86 | assertEquals(expectedLocation, response.seeOther); |
82 | 87 | assertEquals(1, mInterceptor.getInterceptionCount()); |
@@ -105,7 +110,7 @@ public Map<String, String> getHeaders() { |
105 | 110 | setDefaultHttpProvider(new AsyncOperationStatus()); |
106 | 111 | mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data))); |
107 | 112 |
|
108 | | - AsyncOperationStatus response = mProvider.send(new MockRequest(), AsyncOperationStatus.class, null); |
| 113 | + AsyncOperationStatus response = mProvider.send(new MockRequest(), AsyncOperationStatus.class, null, new AsyncMonitorResponseHandler()); |
109 | 114 |
|
110 | 115 | assertEquals(expectedLocation, response.seeOther); |
111 | 116 | assertEquals("Completed", response.status); |
@@ -382,6 +387,113 @@ public Map<String, String> getHeaders() { |
382 | 387 | assertEquals(1, mInterceptor.getInterceptionCount()); |
383 | 388 | } |
384 | 389 |
|
| 390 | + public void testUploadReturnNextSession() throws Exception { |
| 391 | + final String expectedLocation = "http://localhost/up/uploadlocation"; |
| 392 | + final byte[] chunk = new byte[100]; |
| 393 | + final UploadSession<Item> toSerialize = new UploadSession<Item>(); |
| 394 | + toSerialize.uploadUrl = expectedLocation; |
| 395 | + toSerialize.nextExpectedRanges = Arrays.asList("100-199"); |
| 396 | + setDefaultHttpProvider(toSerialize); |
| 397 | + |
| 398 | + final ChunkedUploadResponseHandler<Item> handler = new ChunkedUploadResponseHandler(Item.class); |
| 399 | + |
| 400 | + final ITestData data = new ITestData() { |
| 401 | + @Override |
| 402 | + public int getRequestCode() { |
| 403 | + return 202; |
| 404 | + } |
| 405 | + |
| 406 | + @Override |
| 407 | + public String getJsonResponse() { |
| 408 | + return "{ }"; |
| 409 | + } |
| 410 | + |
| 411 | + @Override |
| 412 | + public Map<String, String> getHeaders() { |
| 413 | + final HashMap<String, String> headers = new HashMap<>(); |
| 414 | + headers.put("Content-Type", "application/json"); |
| 415 | + return headers; |
| 416 | + } |
| 417 | + }; |
| 418 | + |
| 419 | + mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data))); |
| 420 | + ChunkedUploadResult result = mProvider.send(new MockRequest(), ChunkedUploadResult.class, chunk, handler); |
| 421 | + |
| 422 | + assertTrue(result.chunkCompleted()); |
| 423 | + assertEquals(result.getSession(), toSerialize); |
| 424 | + } |
| 425 | + |
| 426 | + public void testUploadReturnUploadedItem() throws Exception { |
| 427 | + final String expectedLocation = "http://localhost/up/uploadlocation"; |
| 428 | + final byte[] chunk = new byte[30]; |
| 429 | + final Item toSerialize = new Item(); |
| 430 | + toSerialize.id = "abc!123"; |
| 431 | + setDefaultHttpProvider(toSerialize); |
| 432 | + |
| 433 | + final ChunkedUploadResponseHandler<Item> handler = new ChunkedUploadResponseHandler(Item.class); |
| 434 | + |
| 435 | + final ITestData data = new ITestData() { |
| 436 | + @Override |
| 437 | + public int getRequestCode() { |
| 438 | + return 201; |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + public String getJsonResponse() { |
| 443 | + return "{ }"; |
| 444 | + } |
| 445 | + |
| 446 | + @Override |
| 447 | + public Map<String, String> getHeaders() { |
| 448 | + final HashMap<String, String> headers = new HashMap<>(); |
| 449 | + headers.put("Content-Type", "application/json"); |
| 450 | + return headers; |
| 451 | + } |
| 452 | + }; |
| 453 | + |
| 454 | + mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data))); |
| 455 | + ChunkedUploadResult result = mProvider.send(new MockRequest(), ChunkedUploadResult.class, chunk, handler); |
| 456 | + |
| 457 | + assertTrue(result.uploadCompleted()); |
| 458 | + assertEquals(toSerialize, result.getItem()); |
| 459 | + } |
| 460 | + |
| 461 | + public void testUploadReturnError() throws Exception { |
| 462 | + final String expectedLocation = "http://localhost/up/uploadlocation"; |
| 463 | + final byte[] chunk = new byte[30]; |
| 464 | + final OneDriveErrorCodes errorCode = OneDriveErrorCodes.UploadSessionFailed; |
| 465 | + final OneDriveError toSerialize = new OneDriveError(); |
| 466 | + toSerialize.code = errorCode.toString(); |
| 467 | + setDefaultHttpProvider(toSerialize); |
| 468 | + |
| 469 | + final ChunkedUploadResponseHandler<Item> handler = new ChunkedUploadResponseHandler(Item.class); |
| 470 | + |
| 471 | + final ITestData data = new ITestData() { |
| 472 | + @Override |
| 473 | + public int getRequestCode() { |
| 474 | + return 500; |
| 475 | + } |
| 476 | + |
| 477 | + @Override |
| 478 | + public String getJsonResponse() { |
| 479 | + return "{ }"; |
| 480 | + } |
| 481 | + |
| 482 | + @Override |
| 483 | + public Map<String, String> getHeaders() { |
| 484 | + final HashMap<String, String> headers = new HashMap<>(); |
| 485 | + headers.put("Content-Type", "application/json"); |
| 486 | + return headers; |
| 487 | + } |
| 488 | + }; |
| 489 | + |
| 490 | + mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data))); |
| 491 | + ChunkedUploadResult result = mProvider.send(new MockRequest(), ChunkedUploadResult.class, chunk, handler); |
| 492 | + |
| 493 | + assertFalse(result.chunkCompleted()); |
| 494 | + assertTrue(result.getError().isError(errorCode)); |
| 495 | + } |
| 496 | + |
385 | 497 | /** |
386 | 498 | * Mock {@see IConnection} backed with test data |
387 | 499 | */ |
|
0 commit comments