From 39841b9d88ff7e8fa8bcc376fbf6598f56923113 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:32:15 +0900 Subject: [PATCH] =?UTF-8?q?test:=20LocalFileSystemMediaDataStore.kt?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + .../LocalFileSystemMediaDataStoreTest.kt | 121 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStoreTest.kt diff --git a/.gitignore b/.gitignore index 0d7dbff3..1f876f7f 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,5 @@ out/ /tomcat/ /tomcat-e2e/ /e2eTest.log +/files/test-media-29599b059-06e1-49eb-91b8-1405244e57ce.png +/files/thumbnail-test-media-29599b059-06e1-49eb-91b8-1405244e57ce.png diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStoreTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStoreTest.kt new file mode 100644 index 00000000..e2854912 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStoreTest.kt @@ -0,0 +1,121 @@ +package dev.usbharu.hideout.core.service.media + +import dev.usbharu.hideout.application.config.ApplicationConfig +import dev.usbharu.hideout.application.config.LocalStorageConfig +import kotlinx.coroutines.test.runTest +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.net.URL +import java.nio.file.Path +import java.util.* +import kotlin.io.path.readBytes +import kotlin.io.path.toPath + +class LocalFileSystemMediaDataStoreTest { + + private val path = String.javaClass.classLoader.getResource("400x400.png")?.toURI()?.toPath()!! + + @Test + fun `save inputStreamを使用して正常に保存できる`() = runTest { + val applicationConfig = ApplicationConfig(URL("https://example.com")) + val storageConfig = LocalStorageConfig("files", null) + + val localFileSystemMediaDataStore = LocalFileSystemMediaDataStore(applicationConfig, storageConfig) + + val fileInputStream = path.readBytes() + + assertThat(fileInputStream.size).isNotEqualTo(0) + + val mediaSave = MediaSave( + "test-media-1${UUID.randomUUID()}.png", + "", + fileInputStream, + fileInputStream + ) + + val save = localFileSystemMediaDataStore.save(mediaSave) + + assertThat(save).isInstanceOf(SuccessSavedMedia::class.java) + + save as SuccessSavedMedia + + assertThat(Path.of("files").toAbsolutePath().resolve(save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + assertThat(Path.of("files").toAbsolutePath().resolve("thumbnail-" + save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + } + + @Test + fun 一時ファイルを使用して正常に保存できる() = runTest { + val applicationConfig = ApplicationConfig(URL("https://example.com")) + val storageConfig = LocalStorageConfig("files", null) + + val localFileSystemMediaDataStore = LocalFileSystemMediaDataStore(applicationConfig, storageConfig) + + val fileInputStream = path.readBytes() + + assertThat(fileInputStream.size).isNotEqualTo(0) + + val saveRequest = MediaSaveRequest( + "test-media-2${UUID.randomUUID()}.png", + "", + path, + path + ) + + val save = localFileSystemMediaDataStore.save(saveRequest) + + assertThat(save).isInstanceOf(SuccessSavedMedia::class.java) + + save as SuccessSavedMedia + + assertThat(Path.of("files").toAbsolutePath().resolve(save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + assertThat(Path.of("files").toAbsolutePath().resolve("thumbnail-" + save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + } + + @Test + fun idを使用して削除できる() = runTest { + val applicationConfig = ApplicationConfig(URL("https://example.com")) + val storageConfig = LocalStorageConfig("files", null) + + val localFileSystemMediaDataStore = LocalFileSystemMediaDataStore(applicationConfig, storageConfig) + + val fileInputStream = path.readBytes() + + assertThat(fileInputStream.size).isNotEqualTo(0) + + val saveRequest = MediaSaveRequest( + "test-media-2${UUID.randomUUID()}.png", + "", + path, + path + ) + + val save = localFileSystemMediaDataStore.save(saveRequest) + + assertThat(save).isInstanceOf(SuccessSavedMedia::class.java) + + save as SuccessSavedMedia + + assertThat(Path.of("files").toAbsolutePath().resolve(save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + assertThat(Path.of("files").toAbsolutePath().resolve("thumbnail-" + save.name)) + .exists() + .hasSize(fileInputStream.size.toLong()) + + + localFileSystemMediaDataStore.delete(save.name) + + assertThat(Path.of("files").toAbsolutePath().resolve(save.name)) + .doesNotExist() + assertThat(Path.of("files").toAbsolutePath().resolve("thumbnail-" + save.name)) + .doesNotExist() + } +}