|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.s3.internal.multipart; |
| 17 | + |
| 18 | + |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.MockitoAnnotations; |
| 33 | +import org.reactivestreams.Subscription; |
| 34 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 35 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 36 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 37 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 38 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 39 | + |
| 40 | +public class MultipartDownloaderSubscriberMockClientTest { |
| 41 | + @Mock |
| 42 | + private S3AsyncClient s3Client; |
| 43 | + |
| 44 | + @Mock |
| 45 | + private Subscription subscription; |
| 46 | + |
| 47 | + @Mock |
| 48 | + private AsyncResponseTransformer<GetObjectResponse, GetObjectResponse> responseTransformer; |
| 49 | + |
| 50 | + private GetObjectRequest getObjectRequest; |
| 51 | + private MultipartDownloaderSubscriber subscriber; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setUp() { |
| 55 | + MockitoAnnotations.openMocks(this); |
| 56 | + getObjectRequest = GetObjectRequest.builder() |
| 57 | + .bucket("test-bucket") |
| 58 | + .key("test-key") |
| 59 | + .build(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testValidationPassesWhenCallCountMatchesTotalParts() throws InterruptedException { |
| 64 | + subscriber = new MultipartDownloaderSubscriber(s3Client, getObjectRequest); |
| 65 | + GetObjectResponse response1 = createMockResponse(3, "etag1"); |
| 66 | + GetObjectResponse response2 = createMockResponse(3, "etag2"); |
| 67 | + GetObjectResponse response3 = createMockResponse(3, "etag3"); |
| 68 | + |
| 69 | + CompletableFuture<GetObjectResponse> future1 = CompletableFuture.completedFuture(response1); |
| 70 | + CompletableFuture<GetObjectResponse> future2 = CompletableFuture.completedFuture(response2); |
| 71 | + CompletableFuture<GetObjectResponse> future3 = CompletableFuture.completedFuture(response3); |
| 72 | + |
| 73 | + when(s3Client.getObject(any(GetObjectRequest.class), eq(responseTransformer))) |
| 74 | + .thenReturn(future1, future2, future3); |
| 75 | + |
| 76 | + subscriber.onSubscribe(subscription); |
| 77 | + subscriber.onNext(responseTransformer); |
| 78 | + subscriber.onNext(responseTransformer); |
| 79 | + subscriber.onNext(responseTransformer); |
| 80 | + Thread.sleep(100); |
| 81 | + |
| 82 | + subscriber.onComplete(); |
| 83 | + |
| 84 | + assertDoesNotThrow(() -> subscriber.future().get(1, TimeUnit.SECONDS)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testValidationFailsWhenCallCountExceedsTotalParts() throws InterruptedException { |
| 89 | + subscriber = new MultipartDownloaderSubscriber(s3Client, getObjectRequest); |
| 90 | + GetObjectResponse response1 = createMockResponse(3, "etag1"); |
| 91 | + GetObjectResponse response2 = createMockResponse(3, "etag2"); |
| 92 | + |
| 93 | + CompletableFuture<GetObjectResponse> future1 = CompletableFuture.completedFuture(response1); |
| 94 | + CompletableFuture<GetObjectResponse> future2 = CompletableFuture.completedFuture(response2); |
| 95 | + |
| 96 | + when(s3Client.getObject(any(GetObjectRequest.class), eq(responseTransformer))) |
| 97 | + .thenReturn(future1, future2); |
| 98 | + |
| 99 | + subscriber.onSubscribe(subscription); |
| 100 | + subscriber.onNext(responseTransformer); |
| 101 | + subscriber.onNext(responseTransformer); |
| 102 | + Thread.sleep(100); |
| 103 | + |
| 104 | + subscriber.onComplete(); |
| 105 | + |
| 106 | + ExecutionException exception = assertThrows(ExecutionException.class, |
| 107 | + () -> subscriber.future().get(1, TimeUnit.SECONDS)); |
| 108 | + assertTrue(exception.getCause() instanceof SdkClientException); |
| 109 | + assertTrue(exception.getCause().getMessage().contains("PartsCount validation failed")); |
| 110 | + assertTrue(exception.getCause().getMessage().contains("Expected 3, downloaded 2 parts")); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + private GetObjectResponse createMockResponse(int partsCount, String etag) { |
| 115 | + GetObjectResponse.Builder builder = GetObjectResponse.builder() |
| 116 | + .eTag(etag) |
| 117 | + .contentLength(1024L); |
| 118 | + |
| 119 | + builder.partsCount(partsCount); |
| 120 | + return builder.build(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments