test: モックの作成をアノテーションベースに

This commit is contained in:
usbharu 2024-05-18 14:08:36 +09:00
parent 0090dee2de
commit f760bace4b
1 changed files with 68 additions and 77 deletions

View File

@ -18,18 +18,31 @@
package dev.usbharu.hideout.core.service.user package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.activitypub.service.activity.delete.APSendDeleteService
import dev.usbharu.hideout.application.config.ApplicationConfig import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.config.CharacterLimit import dev.usbharu.hideout.application.config.CharacterLimit
import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.deletedActor.DeletedActorRepository
import dev.usbharu.hideout.core.domain.model.instance.Instance import dev.usbharu.hideout.core.domain.model.instance.Instance
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import dev.usbharu.hideout.core.service.instance.InstanceService import dev.usbharu.hideout.core.service.instance.InstanceService
import dev.usbharu.hideout.core.service.post.PostService
import dev.usbharu.owl.producer.api.OwlProducer
import jakarta.validation.Validation import jakarta.validation.Validation
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentMatchers.anyString import org.mockito.ArgumentMatchers.anyString
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.* import org.mockito.kotlin.*
import utils.TestApplicationConfig.testApplicationConfig import utils.TestApplicationConfig.testApplicationConfig
import java.net.URL import java.net.URL
@ -38,40 +51,65 @@ import java.time.Instant
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ExtendWith(MockitoExtension::class)
class ActorServiceTest { class ActorServiceTest {
val actorBuilder = Actor.UserBuilder(
@Mock
private lateinit var actorRepository: ActorRepository
@Mock
private lateinit var userAuthService: UserAuthService
@Spy
private val actorBuilder = Actor.UserBuilder(
CharacterLimit(), CharacterLimit(),
ApplicationConfig(URL("https://example.com")), ApplicationConfig(URL("https://example.com")),
Validation.buildDefaultValidatorFactory().validator Validation.buildDefaultValidatorFactory().validator
) )
@Spy
private val applicationConfig: ApplicationConfig = testApplicationConfig.copy(private = false)
@Mock
private lateinit var instanceService: InstanceService
@Mock
private lateinit var userDetailRepository: UserDetailRepository
@Mock
private lateinit var deletedActorRepository: DeletedActorRepository
@Mock
private lateinit var reactionRepository: ReactionRepository
@Mock
private lateinit var relationshipRepository: RelationshipRepository
@Mock
private lateinit var postService: PostService
@Mock
private lateinit var apSendDeleteService: APSendDeleteService
@Mock
private lateinit var postRepository: PostRepository
@Mock
private lateinit var owlProducer: OwlProducer
@InjectMocks
private lateinit var userService: UserServiceImpl
@Test @Test
fun `createLocalUser ローカルユーザーを作成できる`() = runTest { fun `createLocalUser ローカルユーザーを作成できる`() = runTest {
val actorRepository = mock<ActorRepository> {
onBlocking { nextId() } doReturn 110001L
}
val generateKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair() val generateKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair()
val userAuthService = mock<UserAuthService> { whenever(actorRepository.nextId()).doReturn(110001L)
onBlocking { hash(anyString()) } doReturn "hashedPassword" whenever(userAuthService.hash(anyString())).doReturn("hashedPassword")
onBlocking { generateKeyPair() } doReturn generateKeyPair whenever(userAuthService.generateKeyPair()).doReturn(generateKeyPair)
}
val userService =
UserServiceImpl(
actorRepository = actorRepository,
userAuthService = userAuthService,
actorBuilder = actorBuilder,
applicationConfig = testApplicationConfig.copy(private = false),
instanceService = mock(),
userDetailRepository = mock(),
deletedActorRepository = mock(),
reactionRepository = mock(),
relationshipRepository = mock(),
postService = mock(),
apSendDeleteService = mock(),
postRepository = mock(),
owlProducer = mock()
)
userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test")) userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test"))
verify(actorRepository, times(1)).save(any()) verify(actorRepository, times(1)).save(any())
argumentCaptor<Actor> { argumentCaptor<Actor> {
@ -91,31 +129,7 @@ class ActorServiceTest {
@Test @Test
fun `createLocalUser applicationconfig privateがtrueのときアカウントを作成できない`() = runTest { fun `createLocalUser applicationconfig privateがtrueのときアカウントを作成できない`() = runTest {
whenever(applicationConfig.private).thenReturn(true)
val actorRepository = mock<ActorRepository> {
onBlocking { nextId() } doReturn 110001L
}
val generateKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair()
val userAuthService = mock<UserAuthService> {
onBlocking { hash(anyString()) } doReturn "hashedPassword"
onBlocking { generateKeyPair() } doReturn generateKeyPair
}
val userService =
UserServiceImpl(
actorRepository = actorRepository,
userAuthService = userAuthService,
actorBuilder = actorBuilder,
applicationConfig = testApplicationConfig.copy(private = true),
instanceService = mock(),
userDetailRepository = mock(),
deletedActorRepository = mock(),
reactionRepository = mock(),
relationshipRepository = mock(),
postService = mock(),
apSendDeleteService = mock(),
postRepository = mock(),
owlProducer = mock()
)
assertThrows<IllegalStateException> { assertThrows<IllegalStateException> {
userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test")) userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test"))
@ -126,17 +140,9 @@ class ActorServiceTest {
@Test @Test
fun `createRemoteUser リモートユーザーを作成できる`() = runTest { fun `createRemoteUser リモートユーザーを作成できる`() = runTest {
val actorRepository = mock<ActorRepository> { whenever(actorRepository.nextId()).doReturn(113345L)
onBlocking { nextId() } doReturn 113345L whenever(instanceService.fetchInstance(eq("https://remote.example.com"), isNull())).doReturn(
} Instance(
val instanceService = mock<InstanceService> {
onBlocking {
fetchInstance(
eq("https://remote.example.com"),
isNull()
)
} doReturn Instance(
12345L, 12345L,
"", "",
"", "",
@ -150,23 +156,8 @@ class ActorServiceTest {
"", "",
Instant.now() Instant.now()
) )
}
val userService =
UserServiceImpl(
actorRepository = actorRepository,
userAuthService = mock(),
actorBuilder = actorBuilder,
applicationConfig = testApplicationConfig,
instanceService = instanceService,
userDetailRepository = mock(),
deletedActorRepository = mock(),
reactionRepository = mock(),
relationshipRepository = mock(),
postService = mock(),
apSendDeleteService = mock(),
postRepository = mock(),
owlProducer = mock()
) )
val user = RemoteUserCreateDto( val user = RemoteUserCreateDto(
name = "test", name = "test",
domain = "remote.example.com", domain = "remote.example.com",