|
| 1 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 2 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 3 | +import com.amazonaws.client.builder.AwsClientBuilder; |
| 4 | +import com.amazonaws.services.s3.AmazonS3; |
| 5 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 6 | +import me.aneeshneelam.lib.aws.s3.v1.S3ObjectSummaryIterator; |
| 7 | +import me.aneeshneelam.lib.aws.s3.v2.AsyncS3ObjectIterator; |
| 8 | +import me.aneeshneelam.lib.aws.s3.v2.S3ObjectIterator; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.parallel.Execution; |
| 14 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 15 | +import org.testcontainers.containers.localstack.LocalStackContainer; |
| 16 | +import org.testcontainers.junit.jupiter.Container; |
| 17 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 18 | +import org.testcontainers.utility.DockerImageName; |
| 19 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 20 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 21 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 22 | +import software.amazon.awssdk.regions.Region; |
| 23 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 24 | +import software.amazon.awssdk.services.s3.S3Client; |
| 25 | +import software.amazon.awssdk.services.s3.model.*; |
| 26 | + |
| 27 | +import java.net.URI; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.Spliterator; |
| 31 | +import java.util.Spliterators; |
| 32 | +import java.util.stream.LongStream; |
| 33 | +import java.util.stream.StreamSupport; |
| 34 | + |
| 35 | + |
| 36 | +@Execution(ExecutionMode.SAME_THREAD) |
| 37 | +@Testcontainers(disabledWithoutDocker = true, parallel = true) |
| 38 | +public class S3ObjectIteratorContainerTest { |
| 39 | + private static final long s3ObjectCount = 3000; |
| 40 | + private static final String s3BucketName = "aneesh-test-bucket"; |
| 41 | + private static final String s3KeyPrefix = "files/"; |
| 42 | + private static final String fileNameFormat = "file-%d.txt"; |
| 43 | + private static final String fileContentFormat = "This is file %d"; |
| 44 | + |
| 45 | + public static final String localStackImageName = "localstack/localstack"; |
| 46 | + public static final String localStackImageVersion = "3.6.0"; |
| 47 | + public static final String localStackFullImageName = String.format("%s:%s", localStackImageName, localStackImageVersion); |
| 48 | + public static final DockerImageName localStackDockerImageName = DockerImageName.parse(localStackFullImageName); |
| 49 | + |
| 50 | + @Container |
| 51 | + public static LocalStackContainer localStackContainer = new LocalStackContainer(localStackDockerImageName) |
| 52 | + .withServices(LocalStackContainer.Service.S3); |
| 53 | + |
| 54 | + private static boolean createS3Bucket; |
| 55 | + private static boolean loadS3Objects; |
| 56 | + |
| 57 | + private AmazonS3 amazonS3; |
| 58 | + private S3Client s3Client; |
| 59 | + private S3AsyncClient s3AsyncClient; |
| 60 | + |
| 61 | + @BeforeAll |
| 62 | + public static void beforeAll() { |
| 63 | + createS3Bucket = true; |
| 64 | + loadS3Objects = true; |
| 65 | + } |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + public void setUp() { |
| 69 | + String awsAccessKey = localStackContainer.getAccessKey(); |
| 70 | + String awsSecretKey = localStackContainer.getSecretKey(); |
| 71 | + String awsRegion = localStackContainer.getRegion(); |
| 72 | + URI awsS3Uri = localStackContainer.getEndpointOverride(LocalStackContainer.Service.S3); |
| 73 | + |
| 74 | + this.amazonS3 = AmazonS3ClientBuilder.standard() |
| 75 | + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(awsS3Uri.toString(), awsRegion)) |
| 76 | + .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey))) |
| 77 | + .build(); |
| 78 | + this.s3Client = S3Client.builder() |
| 79 | + .endpointOverride(awsS3Uri) |
| 80 | + .region(Region.of(awsRegion)) |
| 81 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(awsAccessKey, awsSecretKey))) |
| 82 | + .build(); |
| 83 | + this.s3AsyncClient = S3AsyncClient.builder() |
| 84 | + .endpointOverride(awsS3Uri) |
| 85 | + .region(Region.of(awsRegion)) |
| 86 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(awsAccessKey, awsSecretKey))) |
| 87 | + .build(); |
| 88 | + |
| 89 | + if (createS3Bucket) { |
| 90 | + CreateBucketResponse response = this.s3Client.createBucket(r -> r.bucket(s3BucketName)); |
| 91 | + System.out.println("Created S3 Bucket, Response: " + response.toString()); |
| 92 | + createS3Bucket = false; |
| 93 | + } |
| 94 | + |
| 95 | + if (loadS3Objects) { |
| 96 | + System.out.println("Uploading " + s3ObjectCount + " S3 Objects to Bucket: " + s3BucketName); |
| 97 | + LongStream.rangeClosed(1L, s3ObjectCount).forEach(i -> { |
| 98 | + String key = s3KeyPrefix + String.format(fileNameFormat, i); |
| 99 | + String content = String.format(fileContentFormat, i); |
| 100 | + PutObjectRequest request = PutObjectRequest.builder() |
| 101 | + .bucket(s3BucketName) |
| 102 | + .key(key) |
| 103 | + .build(); |
| 104 | + PutObjectResponse response = this.s3Client.putObject(request, RequestBody.fromString(content, StandardCharsets.UTF_8)); |
| 105 | + System.out.println("Uploaded S3 Object to Bucket: " + s3BucketName + ", Key: " + key + ", Content: " + content + ", Response: " + response.toString()); |
| 106 | + }); |
| 107 | + loadS3Objects = false; |
| 108 | + System.out.println("Uploaded " + s3ObjectCount + " S3 Objects to Bucket: " + s3BucketName); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testS3ObjectIterator() { |
| 114 | + System.out.println("Testing: " + S3ObjectIterator.class.getCanonicalName()); |
| 115 | + ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() |
| 116 | + .bucket(s3BucketName) |
| 117 | + .prefix(s3KeyPrefix) |
| 118 | + .build(); |
| 119 | + S3ObjectIterator s3ObjectIterator = new S3ObjectIterator(this.s3Client, listObjectsV2Request); |
| 120 | + long count = StreamSupport.stream(Spliterators.spliteratorUnknownSize(s3ObjectIterator, Spliterator.ORDERED), false) |
| 121 | + .count(); |
| 122 | + Assertions.assertEquals(s3ObjectCount, count); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testS3ObjectIteratorAsync() { |
| 127 | + System.out.println("Testing: " + AsyncS3ObjectIterator.class.getCanonicalName()); |
| 128 | + ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() |
| 129 | + .bucket(s3BucketName) |
| 130 | + .prefix(s3KeyPrefix) |
| 131 | + .build(); |
| 132 | + AsyncS3ObjectIterator asyncS3ObjectIterator = new AsyncS3ObjectIterator(this.s3AsyncClient, listObjectsV2Request, Duration.ofSeconds(30)); |
| 133 | + long count = StreamSupport.stream(Spliterators.spliteratorUnknownSize(asyncS3ObjectIterator, Spliterator.ORDERED), false) |
| 134 | + .count(); |
| 135 | + Assertions.assertEquals(s3ObjectCount, count); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testS3ObjectSummaryIterator() { |
| 140 | + System.out.println("Testing: " + S3ObjectSummaryIterator.class.getCanonicalName()); |
| 141 | + com.amazonaws.services.s3.model.ListObjectsV2Request listObjectsV2Request = new com.amazonaws.services.s3.model.ListObjectsV2Request() |
| 142 | + .withBucketName(s3BucketName) |
| 143 | + .withPrefix(s3KeyPrefix); |
| 144 | + S3ObjectSummaryIterator s3ObjectSummaryIterator = new S3ObjectSummaryIterator(this.amazonS3, listObjectsV2Request); |
| 145 | + long count = StreamSupport.stream(Spliterators.spliteratorUnknownSize(s3ObjectSummaryIterator, Spliterator.ORDERED), false) |
| 146 | + .count(); |
| 147 | + Assertions.assertEquals(s3ObjectCount, count); |
| 148 | + } |
| 149 | +} |
0 commit comments