test: MetaService、ServerInitialiseServiceのテストを追加

This commit is contained in:
usbharu 2023-05-04 14:01:21 +09:00
parent 4014c128de
commit d78f5e201f
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 128 additions and 0 deletions

View File

@ -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<IMetaRepository> {
onBlocking { get() } doReturn meta
}
val metaService = MetaServiceImpl(metaRepository)
val actual = metaService.getMeta()
assertEquals(meta, actual)
}
@Test
fun `getMeta メタデータが無いときはNotInitExceptionがthrowされる`() = runTest {
val metaRepository = mock<IMetaRepository> {
onBlocking { get() } doReturn null
}
val metaService = MetaServiceImpl(metaRepository)
assertThrows<NotInitException> { metaService.getMeta() }
}
@Test
fun `updateMeta メタデータを保存できる`() = runTest {
val meta = Meta("1.0.1", Jwt(UUID.randomUUID(), "sdfsdjk", "adafda"))
val metaRepository = mock<IMetaRepository> {
onBlocking { save(any()) } doReturn Unit
}
val metaServiceImpl = MetaServiceImpl(metaRepository)
metaServiceImpl.updateMeta(meta)
argumentCaptor<Meta> {
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<IMetaRepository> {
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<IMetaRepository> {
onBlocking { get() } doReturn null
}
val metaService = MetaServiceImpl(metaRepository)
assertThrows<NotInitException> { metaService.getJwtMeta() }
}
}

View File

@ -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<IMetaRepository> {
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<IMetaRepository> {
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<IMetaRepository> {
onBlocking { get() } doReturn meta
onBlocking { save(any()) } doReturn Unit
}
val serverInitialiseServiceImpl = ServerInitialiseServiceImpl(metaRepository)
serverInitialiseServiceImpl.init()
verify(metaRepository,times(1)).save(any())
argumentCaptor<Meta> {
verify(metaRepository,times(1)).save(capture())
assertEquals(ServerUtil.getImplementationVersion(),firstValue.version)
}
}
}