|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 19 | import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; |
20 | 20 |
|
| 21 | +import java.util.concurrent.CompletableFuture; |
21 | 22 | import org.junit.jupiter.api.AfterAll; |
22 | 23 | import org.junit.jupiter.api.BeforeAll; |
23 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import software.amazon.awssdk.core.ResponseInputStream; |
| 26 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 27 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
24 | 28 | import software.amazon.awssdk.core.sync.RequestBody; |
| 29 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
25 | 30 | import software.amazon.awssdk.services.s3.S3Client; |
| 31 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
26 | 32 |
|
27 | 33 | public class EmptyFileS3IntegrationTest extends UrlHttpConnectionS3IntegrationTestBase { |
28 | 34 | private static final String BUCKET = temporaryBucketName(EmptyFileS3IntegrationTest.class); |
@@ -54,4 +60,50 @@ public void s3EmptyFileContentLengthIsCorrectWithoutChecksumValidationEnabled() |
54 | 60 | assertThat(s3.getObject(r -> r.bucket(BUCKET).key("x")).response().contentLength()).isEqualTo(0); |
55 | 61 | } |
56 | 62 | } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void s3EmptyFileGetAsInputStreamReturnsEOF() throws Exception{ |
| 66 | + try (S3Client s3 = s3ClientBuilder().build()) { |
| 67 | + s3.putObject(r -> r.bucket(BUCKET).key("x"), RequestBody.empty()); |
| 68 | + |
| 69 | + ResponseInputStream<GetObjectResponse> stream = |
| 70 | + s3.getObject(r -> r.bucket(BUCKET).key("x")); |
| 71 | + |
| 72 | + assertThat(stream.read()).isEqualTo(-1); |
| 73 | + |
| 74 | + stream.close(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void syncEmptyObjectWithChecksumValidation_arrayRead_returnsEOF() throws Exception { |
| 80 | + try (S3Client s3 = s3ClientBuilder().build()) { |
| 81 | + s3.putObject(r -> r.bucket(BUCKET).key("x"), RequestBody.empty()); |
| 82 | + |
| 83 | + ResponseInputStream<GetObjectResponse> stream = |
| 84 | + s3.getObject(r -> r.bucket(BUCKET).key("x")); |
| 85 | + |
| 86 | + byte[] buffer = new byte[100]; |
| 87 | + int bytesRead = stream.read(buffer); |
| 88 | + |
| 89 | + assertThat(bytesRead).isEqualTo(-1); |
| 90 | + |
| 91 | + stream.close(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void asyncEmptyObjectWithChecksumValidation_returnsEmpty() throws Exception { |
| 97 | + try (S3AsyncClient s3Async = S3AsyncClient.builder().region(DEFAULT_REGION).build()) { |
| 98 | + s3Async.putObject(r -> r.bucket(BUCKET).key("x"),AsyncRequestBody.empty()).join(); |
| 99 | + |
| 100 | + |
| 101 | + CompletableFuture<byte[]> future = s3Async.getObject(r -> r.bucket(BUCKET).key("x"), |
| 102 | + AsyncResponseTransformer.toBytes()).thenApply( |
| 103 | + res -> res.asByteArray()); |
| 104 | + |
| 105 | + byte[] content = future.join(); |
| 106 | + assertThat(content).isEmpty(); |
| 107 | + } |
| 108 | + } |
57 | 109 | } |
0 commit comments