|
1 | 1 | package dev.zarr.zarrjava; |
2 | 2 |
|
| 3 | +import com.adobe.testing.s3mock.testcontainers.S3MockContainer; |
3 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import dev.zarr.zarrjava.core.*; |
5 | 6 | import dev.zarr.zarrjava.store.*; |
6 | 7 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
7 | 8 | import org.apache.commons.compress.archivers.zip.ZipFile; |
8 | | -import org.junit.jupiter.api.Assertions; |
9 | | -import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.*; |
10 | 10 | import org.junit.jupiter.params.ParameterizedTest; |
11 | 11 | import org.junit.jupiter.params.provider.CsvSource; |
12 | 12 | import org.junit.jupiter.params.provider.MethodSource; |
| 13 | +import org.testcontainers.junit.jupiter.Container; |
| 14 | +import org.testcontainers.junit.jupiter.Testcontainers; |
13 | 15 | import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; |
| 16 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 17 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 18 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 19 | +import software.amazon.awssdk.http.apache.ApacheHttpClient; |
14 | 20 | import software.amazon.awssdk.regions.Region; |
15 | 21 | import software.amazon.awssdk.services.s3.S3Client; |
16 | 22 | import software.amazon.awssdk.services.s3.S3Configuration; |
| 23 | +import software.amazon.awssdk.utils.AttributeMap; |
17 | 24 |
|
18 | 25 | import java.io.IOException; |
19 | 26 | import java.io.InputStream; |
|
32 | 39 | import static dev.zarr.zarrjava.Utils.unzipFile; |
33 | 40 | import static dev.zarr.zarrjava.Utils.zipFile; |
34 | 41 | import static dev.zarr.zarrjava.v3.Node.makeObjectMapper; |
| 42 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES; |
| 43 | + |
| 44 | + |
| 45 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 46 | +@Testcontainers |
| 47 | +class S3StoreTest { |
| 48 | + private final String TEST_BUCKET = "test-bucket"; |
| 49 | + @Container |
| 50 | + private S3MockContainer s3Mock; |
| 51 | + private S3Client s3Client; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + void setUp() { |
| 55 | + s3Mock = new S3MockContainer("latest").withInitialBuckets(TEST_BUCKET); |
| 56 | + s3Mock.start(); |
| 57 | + SdkHttpClient httpClient = ApacheHttpClient.builder().buildWithDefaults(AttributeMap.builder().put(TRUST_ALL_CERTIFICATES, Boolean.TRUE).build()); |
| 58 | + s3Client = S3Client.builder().endpointOverride(URI.create(s3Mock.getHttpsEndpoint())).serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build()).credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))).region(Region.US_EAST_1) // required, but ignored |
| 59 | + .httpClient(httpClient).build(); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + void tearDown() { |
| 64 | + if (s3Mock.isRunning()) { |
| 65 | + s3Mock.stop(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testS3Mock() { |
| 71 | + Assertions.assertTrue(s3Mock.isRunning()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testReadWriteS3Store() { |
| 76 | + S3Store s3Store = new S3Store(s3Client, TEST_BUCKET, ""); |
| 77 | + |
| 78 | + StoreHandle storeHandle = s3Store.resolve("testfile"); |
| 79 | + byte[] testData = new byte[100]; |
| 80 | + for (int i = 0; i < testData.length; i++) { |
| 81 | + testData[i] = (byte) i; |
| 82 | + } |
| 83 | + storeHandle.set(ByteBuffer.wrap(testData)); |
| 84 | + ByteBuffer retrievedData = storeHandle.read(); |
| 85 | + byte[] retrievedBytes = new byte[retrievedData.remaining()]; |
| 86 | + retrievedData.get(retrievedBytes); |
| 87 | + Assertions.assertArrayEquals(testData, retrievedBytes); |
| 88 | + } |
| 89 | + |
| 90 | +} |
35 | 91 |
|
36 | 92 | public class ZarrStoreTest extends ZarrTest { |
37 | 93 | static StoreHandle createS3StoreHandle() { |
|
0 commit comments