|
1 | 1 | package dev.zarr.zarrjava; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | | -import dev.zarr.zarrjava.store.FilesystemStore; |
5 | | -import dev.zarr.zarrjava.store.HttpStore; |
6 | | -import dev.zarr.zarrjava.store.S3Store; |
| 4 | +import dev.zarr.zarrjava.core.Attributes; |
| 5 | +import dev.zarr.zarrjava.store.*; |
7 | 6 | import dev.zarr.zarrjava.v3.*; |
8 | 7 | import org.junit.jupiter.api.Assertions; |
9 | 8 | import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
10 | 11 | import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; |
11 | 12 | import software.amazon.awssdk.regions.Region; |
12 | 13 | import software.amazon.awssdk.services.s3.S3Client; |
13 | 14 |
|
| 15 | +import java.io.File; |
| 16 | +import java.io.FileOutputStream; |
14 | 17 | import java.io.IOException; |
15 | 18 | import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.stream.Stream; |
| 22 | +import java.util.zip.ZipOutputStream; |
16 | 23 |
|
| 24 | +import static dev.zarr.zarrjava.Utils.zipFile; |
17 | 25 | import static dev.zarr.zarrjava.v3.Node.makeObjectMapper; |
18 | 26 |
|
19 | 27 | public class ZarrStoreTest extends ZarrTest { |
@@ -78,4 +86,72 @@ public void testHttpStore() throws IOException, ZarrException { |
78 | 86 |
|
79 | 87 | Assertions.assertArrayEquals(new long[]{1, 4096, 4096, 2048}, array.metadata().shape); |
80 | 88 | } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testZipStore() throws ZarrException, IOException { |
| 92 | + Path sourceDir = TESTOUTPUT.resolve("testZipStore"); |
| 93 | + Path targetDir = TESTOUTPUT.resolve("testZipStore.zip"); |
| 94 | + FilesystemStore fsStore = new FilesystemStore(sourceDir); |
| 95 | + writeTestGroupV3(fsStore, true); |
| 96 | + |
| 97 | + FileOutputStream fos = new FileOutputStream(targetDir.toFile()); |
| 98 | + ZipOutputStream zipOut = new ZipOutputStream(fos); |
| 99 | + |
| 100 | + File fileToZip = new File(sourceDir.toUri()); |
| 101 | + zipFile(fileToZip, fileToZip.getName(), zipOut); |
| 102 | + zipOut.close(); |
| 103 | + fos.close(); |
| 104 | + |
| 105 | + ZipStore zipStore = new ZipStore(targetDir); |
| 106 | + assertIsTestGroupV3(Group.open(zipStore.resolve()), true); |
| 107 | + } |
| 108 | + |
| 109 | + static Stream<Store> localStores() { |
| 110 | + return Stream.of( |
| 111 | +// new ConcurrentMemoryStore(), |
| 112 | + new FilesystemStore(TESTOUTPUT.resolve("testLocalStoresFS")), |
| 113 | + new ZipStore(TESTOUTPUT.resolve("testLocalStoresZIP.zip")) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest |
| 118 | + @MethodSource("localStores") |
| 119 | + public void testLocalStores(Store store) throws IOException, ZarrException { |
| 120 | + boolean useParallel = true; |
| 121 | + Group group = writeTestGroupV3(store, useParallel); |
| 122 | + assertIsTestGroupV3(group, useParallel); |
| 123 | + } |
| 124 | + |
| 125 | + int[] testData(){ |
| 126 | + int[] testData = new int[1024 * 1024]; |
| 127 | + Arrays.setAll(testData, p -> p); |
| 128 | + return testData; |
| 129 | + } |
| 130 | + |
| 131 | + Group writeTestGroupV3(Store store, boolean useParallel) throws ZarrException, IOException { |
| 132 | + StoreHandle storeHandle = store.resolve(); |
| 133 | + |
| 134 | + Group group = Group.create(storeHandle); |
| 135 | + Array array = group.createArray("array", b -> b |
| 136 | + .withShape(1024, 1024) |
| 137 | + .withDataType(DataType.UINT32) |
| 138 | + .withChunkShape(5, 5) |
| 139 | + ); |
| 140 | + array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{1024, 1024}, testData()), useParallel); |
| 141 | + group.createGroup("subgroup"); |
| 142 | + group.setAttributes(new Attributes(b -> b.set("some", "value"))); |
| 143 | + return group; |
| 144 | + } |
| 145 | + |
| 146 | + void assertIsTestGroupV3(Group group, boolean useParallel) throws ZarrException { |
| 147 | + Stream<dev.zarr.zarrjava.core.Node> nodes = group.list(); |
| 148 | + Assertions.assertEquals(2, nodes.count()); |
| 149 | + Array array = (Array) group.get("array"); |
| 150 | + Assertions.assertNotNull(array); |
| 151 | + ucar.ma2.Array result = array.read(useParallel); |
| 152 | + Assertions.assertArrayEquals(testData(), (int[]) result.get1DJavaArray(ucar.ma2.DataType.UINT)); |
| 153 | + Attributes attrs = group.metadata().attributes; |
| 154 | + Assertions.assertNotNull(attrs); |
| 155 | + Assertions.assertEquals("value", attrs.getString("some")); |
| 156 | + } |
81 | 157 | } |
0 commit comments