From d78f5e201f4354ff264c62927cfd02269343d7b8 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 4 May 2023 14:01:21 +0900 Subject: [PATCH] =?UTF-8?q?test:=20MetaService=E3=80=81ServerInitialiseSer?= =?UTF-8?q?vice=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/service/MetaServiceImplTest.kt | 72 +++++++++++++++++++ .../ServerInitialiseServiceImplTest.kt | 56 +++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/service/MetaServiceImplTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImplTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/service/MetaServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/MetaServiceImplTest.kt new file mode 100644 index 00000000..46ab3715 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/service/MetaServiceImplTest.kt @@ -0,0 +1,72 @@ +@file:OptIn(ExperimentalCoroutinesApi::class) + +package dev.usbharu.hideout.service + +import dev.usbharu.hideout.domain.model.hideout.entity.Jwt +import dev.usbharu.hideout.domain.model.hideout.entity.Meta +import dev.usbharu.hideout.exception.NotInitException +import dev.usbharu.hideout.repository.IMetaRepository +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.mockito.kotlin.* +import java.util.* +import kotlin.test.assertEquals + +class MetaServiceImplTest { + @Test + fun `getMeta メタデータを取得できる`() = runTest { + val meta = Meta("1.0.0", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) + val metaRepository = mock { + onBlocking { get() } doReturn meta + } + val metaService = MetaServiceImpl(metaRepository) + val actual = metaService.getMeta() + assertEquals(meta, actual) + } + + @Test + fun `getMeta メタデータが無いときはNotInitExceptionがthrowされる`() = runTest { + val metaRepository = mock { + onBlocking { get() } doReturn null + } + val metaService = MetaServiceImpl(metaRepository) + assertThrows { metaService.getMeta() } + } + + @Test + fun `updateMeta メタデータを保存できる`() = runTest { + + val meta = Meta("1.0.1", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) + val metaRepository = mock { + onBlocking { save(any()) } doReturn Unit + } + val metaServiceImpl = MetaServiceImpl(metaRepository) + metaServiceImpl.updateMeta(meta) + argumentCaptor { + verify(metaRepository).save(capture()) + assertEquals(meta, firstValue) + } + } + + @Test + fun `getJwtMeta Jwtメタデータを取得できる`() = runTest { + val meta = Meta("1.0.0", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda")) + val metaRepository = mock { + onBlocking { get() } doReturn meta + } + val metaService = MetaServiceImpl(metaRepository) + val actual = metaService.getJwtMeta() + assertEquals(meta.jwt, actual) + } + + @Test + fun `getJwtMeta メタデータが無いときはNotInitExceptionがthrowされる`() = runTest { + val metaRepository = mock { + onBlocking { get() } doReturn null + } + val metaService = MetaServiceImpl(metaRepository) + assertThrows { metaService.getJwtMeta() } + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImplTest.kt new file mode 100644 index 00000000..791e349a --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImplTest.kt @@ -0,0 +1,56 @@ +@file:OptIn(ExperimentalCoroutinesApi::class) + +package dev.usbharu.hideout.service + +import dev.usbharu.hideout.domain.model.hideout.entity.Jwt +import dev.usbharu.hideout.domain.model.hideout.entity.Meta +import dev.usbharu.hideout.repository.IMetaRepository +import dev.usbharu.hideout.util.ServerUtil +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.mockito.kotlin.* +import java.util.* +import kotlin.test.assertEquals + +class ServerInitialiseServiceImplTest { + @Test + fun `init メタデータが無いときに初期化を実行する`() = runTest { + val metaRepository = mock { + onBlocking { get() } doReturn null + onBlocking { save(any()) } doReturn Unit + } + val serverInitialiseServiceImpl = ServerInitialiseServiceImpl(metaRepository) + + serverInitialiseServiceImpl.init() + verify(metaRepository,times(1)).save(any()) + } + + @Test + fun `init メタデータが存在して同じバージョンのときは何もしない`() = runTest { + val meta = Meta(ServerUtil.getImplementationVersion(), Jwt(UUID.randomUUID(),"aaafafd","afafasdf")) + val metaRepository = mock { + onBlocking { get() } doReturn meta + } + val serverInitialiseServiceImpl = ServerInitialiseServiceImpl(metaRepository) + serverInitialiseServiceImpl.init() + verify(metaRepository,times(0)).save(any()) + } + + @Test + fun `init メタデータが存在して違うバージョンのときはバージョンを変更する`() = runTest { + val meta = Meta("1.0.0", Jwt(UUID.randomUUID(),"aaafafd","afafasdf")) + val metaRepository = mock { + onBlocking { get() } doReturn meta + onBlocking { save(any()) } doReturn Unit + } + + val serverInitialiseServiceImpl = ServerInitialiseServiceImpl(metaRepository) + serverInitialiseServiceImpl.init() + verify(metaRepository,times(1)).save(any()) + argumentCaptor { + verify(metaRepository,times(1)).save(capture()) + assertEquals(ServerUtil.getImplementationVersion(),firstValue.version) + } + } +}