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) + } + } +}