This commit is contained in:
usbharu 2024-08-09 10:00:40 +09:00
parent 2cfc8bc0d8
commit 7db82462fa
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
3 changed files with 103 additions and 7 deletions

View File

@ -16,6 +16,7 @@
package dev.usbharu.hideout.core.application.post
import dev.usbharu.hideout.core.application.exception.InternalServerException
import dev.usbharu.hideout.core.application.shared.LocalUserAbstractApplicationService
import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
@ -24,7 +25,6 @@ import dev.usbharu.hideout.core.domain.model.post.PostId
import dev.usbharu.hideout.core.domain.model.post.PostOverview
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.support.principal.FromApi
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import dev.usbharu.hideout.core.infrastructure.factory.PostFactoryImpl
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@ -35,17 +35,13 @@ class RegisterLocalPostApplicationService(
private val postFactory: PostFactoryImpl,
private val actorRepository: ActorRepository,
private val postRepository: PostRepository,
private val userDetailRepository: UserDetailRepository,
transaction: Transaction,
) : LocalUserAbstractApplicationService<RegisterLocalPost, Long>(transaction, Companion.logger) {
override suspend fun internalExecute(command: RegisterLocalPost, principal: FromApi): Long {
val actorId = (
userDetailRepository.findById(principal.userDetailId)
?: throw IllegalStateException("actor not found")
).actorId
val actorId = principal.actorId
val actor = actorRepository.findById(actorId)!!
val actor = actorRepository.findById(actorId) ?: throw InternalServerException("Actor $actorId not found.")
val post = postFactory.createLocal(
actor = actor,

View File

@ -0,0 +1,57 @@
package dev.usbharu.hideout.core.application.post
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.post.Visibility
import dev.usbharu.hideout.core.domain.model.support.acct.Acct
import dev.usbharu.hideout.core.domain.model.support.principal.FromApi
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
import dev.usbharu.hideout.core.infrastructure.factory.PostFactoryImpl
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever
import utils.TestTransaction
@ExtendWith(MockitoExtension::class)
class RegisterLocalPostApplicationServiceTest {
@InjectMocks
lateinit var service: RegisterLocalPostApplicationService
@Mock
lateinit var actorRepository: ActorRepository
@Mock
lateinit var postRepository: PostRepository
@Mock
lateinit var postFactoryImpl: PostFactoryImpl
@Spy
val transaction = TestTransaction
@Test
fun postを作成できる() = runTest {
val actor = TestActorFactory.create(id = 1)
whenever(actorRepository.findById(ActorId(1))) doReturn actor
whenever(
postFactoryImpl.createLocal(
eq(actor),
)
)
service.execute(
RegisterLocalPost("content test", null, Visibility.PUBLIC, null, false, emptyList<>()), FromApi(
ActorId(1), UserDetailId(1), Acct("test", "example.com")
)
)
}
}

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.application.post
import dev.usbharu.hideout.core.application.exception.InternalServerException
import dev.usbharu.hideout.core.application.exception.PermissionDeniedException
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
@ -105,5 +106,47 @@ class UpdateLocalNoteApplicationServiceTest {
}
}
@Test
fun userDetailが見つからない場合失敗() = runTest {
whenever(postRepository.findById(PostId(1))).doReturn(TestPostFactory.create(id = 1, actorId = 1))
assertThrows<InternalServerException> {
service.execute(
UpdateLocalNote(1, null, "test", false, emptyList()), FromApi(
ActorId(1),
UserDetailId(1), Acct("test", "example.com")
)
)
}
verify(userDetailRepository, times(1)).findById(UserDetailId(1))
verify(actorRepository, never()).findById(any())
}
@Test
fun actorが見つからない場合失敗() = runTest {
val post = TestPostFactory.create()
whenever(postRepository.findById(post.id)).doReturn(post)
whenever(userDetailRepository.findById(UserDetailId(1))).doReturn(
UserDetail.create(
UserDetailId(1), post.actorId,
UserDetailHashedPassword("")
)
)
assertThrows<InternalServerException> {
service.execute(
UpdateLocalNote(post.id.id, null, "test", false, emptyList()), FromApi(
post.actorId,
UserDetailId(1),
Acct("test", "example.com")
)
)
}
verify(userDetailRepository, times(1)).findById(UserDetailId(1))
verify(actorRepository, times(1)).findById(ActorId(1))
verify(postRepository, never()).save(any())
}
}