diff --git a/src/test/java/sirius/biz/storage/s3/ObjectStoresSpec.groovy b/src/test/java/sirius/biz/storage/s3/ObjectStoresSpec.groovy deleted file mode 100644 index cc685a3ab..000000000 --- a/src/test/java/sirius/biz/storage/s3/ObjectStoresSpec.groovy +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Made with all the love in the world - * by scireum in Remshalden, Germany - * - * Copyright by scireum GmbH - * http://www.scireum.de - info@scireum.de - */ - -package sirius.biz.storage.s3 - -import sirius.kernel.BaseSpecification -import sirius.kernel.commons.Files -import sirius.kernel.commons.Streams -import sirius.kernel.commons.Tuple -import sirius.kernel.di.std.Part - -import java.nio.charset.StandardCharsets - -class ObjectStoresSpec extends BaseSpecification { - - @Part - private static ObjectStores stores - - def "createBucketWorks"() { - File download - when: - File file = File.createTempFile("test", "") - FileOutputStream fout = new FileOutputStream(file) - for (int i = 0; i < 10024; i++) { - fout.write("This is a test.".getBytes(StandardCharsets.UTF_8)) - } - fout.close() - and: - stores.store().upload(stores.store().getBucketName("test"), "test", file, null) - and: - download = stores.store().download(stores.store().getBucketName("test"), "test") - and: - def expectedContents = com.google.common.io.Files.toString(file, StandardCharsets.UTF_8) - def downloadedContents = com.google.common.io.Files.toString(download, StandardCharsets.UTF_8) - then: - expectedContents == downloadedContents - cleanup: - Files.delete(file) - Files.delete(download) - } - - def "PUT and GET works"() { - File download - when: - File file = File.createTempFile("test", "") - FileOutputStream fout = new FileOutputStream(file) - for (int i = 0; i < 10024; i++) { - fout.write("This is a test.".getBytes(StandardCharsets.UTF_8)) - } - fout.close() - and: - stores.store().upload(stores.store().getBucketName("test"), "test", file, null) - and: - download = stores.store().download(stores.store().getBucketName("test"), "test") - and: - URLConnection c = new URL(stores.store(). - objectUrl(stores.store().getBucketName("test"), "test")).openConnection() - and: - def expectedContents = com.google.common.io.Files.toString(file, StandardCharsets.UTF_8) - def downloadedContents = com.google.common.io.Files.toString(download, StandardCharsets.UTF_8) - def downloadedData = new String(Streams.toByteArray(c.getInputStream()), StandardCharsets.UTF_8) - then: - expectedContents == downloadedData - and: - expectedContents == downloadedContents - cleanup: - Files.delete(file) - Files.delete((File) download) - } - - def "ensureBucketExists"() { - when: - stores.store().ensureBucketExists(stores.store().getBucketName("exists")) - then: - stores.store().doesBucketExist(stores.store().getBucketName("exists")) - stores.bucketCache.get(Tuple.create(stores.store().name, - stores.store().getBucketName("exists").getName())) - !stores.store().doesBucketExist(stores.store().getBucketName("not-exists")) - stores.bucketCache.get(Tuple.create(stores.store().name, - stores.store().getBucketName("not-exists").getName())) == null - } - - def "deleteBucket"() { - when: - stores.store().ensureBucketExists(stores.store().getBucketName("deleted")) - then: - stores.store().doesBucketExist(stores.store().getBucketName("deleted")) - when: - stores.store().deleteBucket(stores.store().getBucketName("deleted")) - then: - !stores.store().doesBucketExist(stores.store().getBucketName("deleted")) - stores.bucketCache.get(Tuple.create(stores.store().name, - stores.store().getBucketName("deleted").getName())) == null - } - -} diff --git a/src/test/kotlin/sirius/biz/storage/s3/ObjectStoresTest.kt b/src/test/kotlin/sirius/biz/storage/s3/ObjectStoresTest.kt new file mode 100644 index 000000000..4fb8b7c7b --- /dev/null +++ b/src/test/kotlin/sirius/biz/storage/s3/ObjectStoresTest.kt @@ -0,0 +1,114 @@ +/* + * Made with all the love in the world + * by scireum in Remshalden, Germany + * + * Copyright by scireum GmbH + * http://www.scireum.de - info@scireum.de + */ + +package sirius.biz.storage.s3 + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import sirius.kernel.SiriusExtension +import sirius.kernel.commons.Files +import sirius.kernel.commons.Tuple +import sirius.kernel.di.std.Part +import java.io.File +import java.io.FileOutputStream +import java.net.URL +import java.nio.file.Files as files_ +import java.nio.charset.StandardCharsets +import kotlin.test.assertEquals +import kotlin.test.assertFalse +/** + * Tests the [ObjectStores]. + */ +@ExtendWith(SiriusExtension::class) +class ObjectStoresTest { + + @Test + fun `create bucket works`() { + val file = File.createTempFile("test", "") + val fout = FileOutputStream(file) + repeat(10024) { + fout.write("This is a test.".toByteArray(StandardCharsets.UTF_8)) + } + fout.close() + stores.store().upload(stores.store().getBucketName("test"), "test", file, null) + val download = stores.store().download(stores.store().getBucketName("test"), "test") + val expectedContents = files_.readString(file.toPath(), StandardCharsets.UTF_8) + val downloadedContents = files_.readString(download.toPath(), StandardCharsets.UTF_8) + assertEquals(expectedContents, downloadedContents) + Files.delete(file) + Files.delete(download) + } + + @Test + fun `PUT and GET works`() { + val file = File.createTempFile("test", "") + val fout = FileOutputStream(file) + repeat(10024) { + fout.write("This is a test.".toByteArray(StandardCharsets.UTF_8)) + } + fout.close() + stores.store().upload(stores.store().getBucketName("test"), "test", file, null) + val download = stores.store().download(stores.store().getBucketName("test"), "test") + val c = URL( + stores.store().objectUrl(stores.store().getBucketName("test"), "test") + ).openConnection() + + val expectedContents = files_.readString(file.toPath(), StandardCharsets.UTF_8) + val downloadedContents = files_.readString(download.toPath(), StandardCharsets.UTF_8) + val downloadedData = String(c.getInputStream().readAllBytes(), StandardCharsets.UTF_8) + + assertEquals(expectedContents, downloadedData) + assertEquals(expectedContents, downloadedContents) + Files.delete(file) + Files.delete(download) + } + + @Test + fun `ensure bucket exists`() { + stores.store().ensureBucketExists(stores.store().getBucketName("exists")) + stores.store().doesBucketExist(stores.store().getBucketName("exists")) + stores.bucketCache.get( + Tuple.create( + stores.store().name, + stores.store().getBucketName("exists").getName() + ) + ) + !stores.store().doesBucketExist(stores.store().getBucketName("not-exists")) + assertEquals( + null, stores.bucketCache.get( + Tuple.create( + stores.store().name, + stores.store().getBucketName("not-exists").getName() + ) + ) + ) + } + + @Test + fun `delete Bucket works`() { + stores.store().ensureBucketExists(stores.store().getBucketName("deleted")) + stores.store().doesBucketExist(stores.store().getBucketName("deleted")) + stores.store().deleteBucket(stores.store().getBucketName("deleted")) + assertFalse { stores.store().doesBucketExist(stores.store().getBucketName("deleted")) } + assertEquals( + null, stores.bucketCache.get( + Tuple.create( + stores.store().name, + stores.store().getBucketName("deleted").getName() + ) + ) + ) + } + + companion object { + @Part + @JvmStatic + private lateinit var stores: ObjectStores + + } +}