|
18 | 18 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
19 | 19 | import static com.github.tomakehurst.wiremock.client.WireMock.any;
|
20 | 20 | import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
|
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.head; |
21 | 22 | import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
22 | 24 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
23 | 25 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
24 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
|
33 | 35 | import java.util.concurrent.CancellationException;
|
34 | 36 | import java.util.concurrent.CompletionException;
|
35 | 37 | import org.junit.jupiter.api.BeforeAll;
|
| 38 | +import org.junit.jupiter.api.Test; |
36 | 39 | import org.junit.jupiter.params.ParameterizedTest;
|
37 | 40 | import org.junit.jupiter.params.provider.ValueSource;
|
38 | 41 | import org.mockito.ArgumentMatchers;
|
|
42 | 45 | import software.amazon.awssdk.regions.Region;
|
43 | 46 | import software.amazon.awssdk.services.s3.S3AsyncClient;
|
44 | 47 | import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
|
| 48 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
45 | 49 | import software.amazon.awssdk.services.s3.model.S3Exception;
|
46 | 50 | import software.amazon.awssdk.testutils.RandomTempFile;
|
47 | 51 | import software.amazon.awssdk.transfer.s3.CaptureTransferListener;
|
48 | 52 | import software.amazon.awssdk.transfer.s3.S3TransferManager;
|
| 53 | +import software.amazon.awssdk.transfer.s3.model.Copy; |
49 | 54 | import software.amazon.awssdk.transfer.s3.model.FileUpload;
|
50 | 55 | import software.amazon.awssdk.transfer.s3.progress.LoggingTransferListener;
|
51 | 56 | import software.amazon.awssdk.transfer.s3.progress.TransferListener;
|
@@ -173,7 +178,6 @@ void listeners_reports_ErrorsWhenCancelled(boolean multipartEnabled) throws Inte
|
173 | 178 | assertThat(transferListener.isTransferComplete()).isFalse();
|
174 | 179 | assertThat(transferListener.isTransferInitiated()).isTrue();
|
175 | 180 | assertMockOnFailure(transferListenerMock);
|
176 |
| - |
177 | 181 | }
|
178 | 182 |
|
179 | 183 | @ParameterizedTest
|
@@ -209,4 +213,141 @@ void listeners_reports_ProgressWhenSuccess(boolean multipartEnabled) throws Inte
|
209 | 213 | int numTimesBytesTransferred = multipartEnabled ? 2 : 1;
|
210 | 214 | Mockito.verify(transferListenerMock, times(numTimesBytesTransferred)).bytesTransferred(ArgumentMatchers.any());
|
211 | 215 | }
|
| 216 | + |
| 217 | + @Test |
| 218 | + void copyWithJavaBasedClient_listeners_reports_ErrorsWithValidPayload() throws InterruptedException { |
| 219 | + S3AsyncClient s3Async = s3AsyncClient(true); |
| 220 | + |
| 221 | + TransferListener transferListenerMock = mock(TransferListener.class); |
| 222 | + stubFor(any(anyUrl()).willReturn(aResponse().withStatus(404).withBody(ERROR_BODY))); |
| 223 | + S3TransferManager tm = new GenericS3TransferManager(s3Async, mock(UploadDirectoryHelper.class), |
| 224 | + mock(TransferManagerConfiguration.class), |
| 225 | + mock(DownloadDirectoryHelper.class)); |
| 226 | + CaptureTransferListener transferListener = new CaptureTransferListener(); |
| 227 | + |
| 228 | + Copy copy = |
| 229 | + tm.copy(u -> u.copyObjectRequest(p -> p |
| 230 | + .sourceBucket(EXAMPLE_BUCKET) |
| 231 | + .sourceKey(TEST_KEY) |
| 232 | + .destinationBucket(EXAMPLE_BUCKET) |
| 233 | + .destinationKey("copiedObj")) |
| 234 | + .addTransferListener(LoggingTransferListener.create()) |
| 235 | + .addTransferListener(transferListener) |
| 236 | + .addTransferListener(transferListenerMock) |
| 237 | + .build()); |
| 238 | + |
| 239 | + assertThatExceptionOfType(CompletionException.class).isThrownBy(() -> copy.completionFuture().join()); |
| 240 | + Thread.sleep(500); |
| 241 | + assertThat(transferListener.getExceptionCaught()).isInstanceOf(NoSuchKeyException.class); |
| 242 | + assertThat(transferListener.isTransferComplete()).isFalse(); |
| 243 | + assertThat(transferListener.isTransferInitiated()).isTrue(); |
| 244 | + assertMockOnFailure(transferListenerMock); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + void copyWithJavaBasedClient_listeners_reports_ErrorsWithValidInValidPayload() throws InterruptedException { |
| 249 | + S3AsyncClient s3Async = s3AsyncClient(true); |
| 250 | + |
| 251 | + TransferListener transferListenerMock = mock(TransferListener.class); |
| 252 | + stubFor(any(anyUrl()).willReturn(aResponse().withStatus(404).withBody("?"))); |
| 253 | + S3TransferManager tm = new GenericS3TransferManager(s3Async, mock(UploadDirectoryHelper.class), |
| 254 | + mock(TransferManagerConfiguration.class), |
| 255 | + mock(DownloadDirectoryHelper.class)); |
| 256 | + CaptureTransferListener transferListener = new CaptureTransferListener(); |
| 257 | + |
| 258 | + Copy copy = |
| 259 | + tm.copy(u -> u.copyObjectRequest(p -> p |
| 260 | + .sourceBucket(EXAMPLE_BUCKET) |
| 261 | + .sourceKey(TEST_KEY) |
| 262 | + .destinationBucket(EXAMPLE_BUCKET) |
| 263 | + .destinationKey("copiedObj")) |
| 264 | + .addTransferListener(LoggingTransferListener.create()) |
| 265 | + .addTransferListener(transferListener) |
| 266 | + .addTransferListener(transferListenerMock) |
| 267 | + .build()); |
| 268 | + |
| 269 | + assertThatExceptionOfType(CompletionException.class).isThrownBy(() -> copy.completionFuture().join()); |
| 270 | + Thread.sleep(500); |
| 271 | + assertThat(transferListener.getExceptionCaught()).isInstanceOf(S3Exception.class); |
| 272 | + assertThat(transferListener.isTransferComplete()).isFalse(); |
| 273 | + assertThat(transferListener.isTransferInitiated()).isTrue(); |
| 274 | + assertMockOnFailure(transferListenerMock); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + void copyWithJavaBasedClient_listeners_reports_ErrorsWhenCancelled() throws InterruptedException { |
| 279 | + S3AsyncClient s3Async = s3AsyncClient(true); |
| 280 | + |
| 281 | + TransferListener transferListenerMock = mock(TransferListener.class); |
| 282 | + stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody("{}"))); |
| 283 | + S3TransferManager tm = new GenericS3TransferManager(s3Async, mock(UploadDirectoryHelper.class), |
| 284 | + mock(TransferManagerConfiguration.class), |
| 285 | + mock(DownloadDirectoryHelper.class)); |
| 286 | + CaptureTransferListener transferListener = new CaptureTransferListener(); |
| 287 | + |
| 288 | + tm.copy(u -> u.copyObjectRequest(p -> p |
| 289 | + .sourceBucket(EXAMPLE_BUCKET) |
| 290 | + .sourceKey(TEST_KEY) |
| 291 | + .destinationBucket(EXAMPLE_BUCKET) |
| 292 | + .destinationKey("copiedObj")) |
| 293 | + .addTransferListener(LoggingTransferListener.create()) |
| 294 | + .addTransferListener(transferListener) |
| 295 | + .addTransferListener(transferListenerMock) |
| 296 | + .build()).completionFuture().cancel(true); |
| 297 | + |
| 298 | + Thread.sleep(500); |
| 299 | + assertThat(transferListener.getExceptionCaught()).isInstanceOf(CancellationException.class); |
| 300 | + assertThat(transferListener.isTransferComplete()).isFalse(); |
| 301 | + assertThat(transferListener.isTransferInitiated()).isTrue(); |
| 302 | + assertMockOnFailure(transferListenerMock); |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + void copyWithJavaBasedClient_listeners_reports_ProgressWhenSuccess_copy() throws InterruptedException { |
| 307 | + String destinationKey = "copiedObj"; |
| 308 | + S3AsyncClient s3Async = s3AsyncClient(true); |
| 309 | + |
| 310 | + TransferListener transferListenerMock = mock(TransferListener.class); |
| 311 | + |
| 312 | + stubFor(head(anyUrl()).willReturn(aResponse().withStatus(200).withHeader("Content-Length", "16777216"))); |
| 313 | + |
| 314 | + String createMpuUrl = "/" + EXAMPLE_BUCKET + "/" + destinationKey + "?uploads"; |
| 315 | + String createMpuResponse = "<CreateMultipartUploadResult><UploadId>1234</UploadId></CreateMultipartUploadResult>"; |
| 316 | + stubFor(post(urlEqualTo(createMpuUrl)).willReturn(aResponse().withStatus(200).withBody(createMpuResponse))); |
| 317 | + |
| 318 | + String copyObjectUrl = "/" + EXAMPLE_BUCKET + "/" + destinationKey + "?uploadId=1234"; |
| 319 | + String copyObjectUrl1 = "/" + EXAMPLE_BUCKET + "/" + destinationKey + "?partNumber=1&uploadId=1234"; |
| 320 | + String copyObjectUrl2 = "/" + EXAMPLE_BUCKET + "/" + destinationKey + "?partNumber=2&uploadId=1234"; |
| 321 | + |
| 322 | + String copyObjectResponse = "<CopyPartResult><ETag>test-etag</ETag></CopyPartResult>"; |
| 323 | + stubFor(post(copyObjectUrl).willReturn(aResponse().withStatus(200).withBody(copyObjectResponse))); |
| 324 | + stubFor(put(copyObjectUrl1).willReturn(aResponse().withStatus(200).withBody(copyObjectResponse))); |
| 325 | + stubFor(put(copyObjectUrl2).willReturn(aResponse().withStatus(200).withBody(copyObjectResponse))); |
| 326 | + |
| 327 | + S3TransferManager tm = new GenericS3TransferManager(s3Async, mock(UploadDirectoryHelper.class), |
| 328 | + mock(TransferManagerConfiguration.class), |
| 329 | + mock(DownloadDirectoryHelper.class)); |
| 330 | + CaptureTransferListener transferListener = new CaptureTransferListener(); |
| 331 | + |
| 332 | + tm.copy(u -> u.copyObjectRequest(p -> p |
| 333 | + .sourceBucket(EXAMPLE_BUCKET) |
| 334 | + .sourceKey(TEST_KEY) |
| 335 | + .destinationBucket(EXAMPLE_BUCKET) |
| 336 | + .destinationKey(destinationKey)) |
| 337 | + .addTransferListener(LoggingTransferListener.create()) |
| 338 | + .addTransferListener(transferListener) |
| 339 | + .addTransferListener(transferListenerMock) |
| 340 | + .build()); |
| 341 | + |
| 342 | + Thread.sleep(500); |
| 343 | + assertThat(transferListener.getExceptionCaught()).isNull(); |
| 344 | + assertThat(transferListener.isTransferComplete()).isTrue(); |
| 345 | + assertThat(transferListener.isTransferInitiated()).isTrue(); |
| 346 | + Mockito.verify(transferListenerMock, times(0)).transferFailed(ArgumentMatchers.any()); |
| 347 | + Mockito.verify(transferListenerMock, times(1)).transferInitiated(ArgumentMatchers.any()); |
| 348 | + Mockito.verify(transferListenerMock, times(1)).transferComplete(ArgumentMatchers.any()); |
| 349 | + |
| 350 | + int numTimesBytesTransferred = 2; |
| 351 | + Mockito.verify(transferListenerMock, times(numTimesBytesTransferred)).bytesTransferred(ArgumentMatchers.any()); |
| 352 | + } |
212 | 353 | }
|
0 commit comments