test: 無効化していたAPResourceResolveServiceImplのテストを有効化

This commit is contained in:
usbharu 2023-10-31 18:50:40 +09:00
parent 3ad36109e0
commit 23db387054
1 changed files with 78 additions and 102 deletions

View File

@ -6,25 +6,19 @@ import dev.usbharu.hideout.domain.model.ap.Object
import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import io.ktor.client.* import dev.usbharu.hideout.service.ap.APRequestService
import io.ktor.client.engine.mock.*
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.junit.jupiter.MockitoExtension import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any import org.mockito.kotlin.*
import org.mockito.kotlin.doReturn import utils.UserBuilder
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import java.net.URL import java.net.URL
import java.time.Instant
import kotlin.test.assertEquals
@ExtendWith(MockitoExtension::class) @ExtendWith(MockitoExtension::class)
@Disabled
class APResourceResolveServiceImplTest { class APResourceResolveServiceImplTest {
val userBuilder = User.UserBuilder(CharacterLimit(), ApplicationConfig(URL("https://example.com"))) val userBuilder = User.UserBuilder(CharacterLimit(), ApplicationConfig(URL("https://example.com")))
@ -33,109 +27,89 @@ class APResourceResolveServiceImplTest {
@Test @Test
fun `単純な一回のリクエスト`() = runTest { fun `単純な一回のリクエスト`() = runTest {
var count = 0
val httpClient = HttpClient(MockEngine { request ->
count++
respondOk("{}")
})
val userRepository = mock<UserRepository>() val userRepository = mock<UserRepository>()
whenever(userRepository.findById(any())).doReturn( val user = UserBuilder.localUserOf()
userBuilder.of( whenever(userRepository.findById(any())) doReturn user
2L,
"follower",
"follower.example.com",
"followerUser",
"test follower user",
"https://follower.example.com/inbox",
"https://follower.example.com/outbox",
"https://follower.example.com",
"https://follower.example.com",
publicKey = "",
createdAt = Instant.now(),
keyId = ""
)
)
val apRequestService = mock<APRequestService> {
onBlocking {
apGet(
eq("https"),
eq(user),
eq(Object::class.java)
)
} doReturn dev.usbharu.hideout.domain.model.ap.Object(
emptyList()
)
}
val apResourceResolveService = val apResourceResolveService =
APResourceResolveServiceImpl(mock(), userRepository, InMemoryCacheManager()) APResourceResolveServiceImpl(apRequestService, userRepository, InMemoryCacheManager())
apResourceResolveService.resolve<Object>("https", 0) apResourceResolveService.resolve<Object>("https", 0)
assertEquals(1, count) verify(apRequestService, times(1)).apGet(eq("https"), eq(user), eq(Object::class.java))
} }
@Test @Test
fun 複数回の同じリクエストが重複して発行されない() = runTest { fun 複数回の同じリクエストが重複して発行されない() = runTest {
var count = 0
val httpClient = HttpClient(MockEngine { request ->
count++
respondOk("{}")
})
val userRepository = mock<UserRepository>() val userRepository = mock<UserRepository>()
whenever(userRepository.findById(any())).doReturn( val user = UserBuilder.localUserOf()
userBuilder.of( whenever(userRepository.findById(any())) doReturn user
2L,
"follower", val apRequestService = mock<APRequestService> {
"follower.example.com", onBlocking {
"followerUser", apGet(
"test follower user", eq("https"),
"https://follower.example.com/inbox", eq(user),
"https://follower.example.com/outbox", eq(Object::class.java)
"https://follower.example.com", )
"https://follower.example.com", } doReturn dev.usbharu.hideout.domain.model.ap.Object(
publicKey = "", emptyList()
createdAt = Instant.now(),
keyId = ""
) )
) }
val apResourceResolveService = val apResourceResolveService =
APResourceResolveServiceImpl(mock(), userRepository, InMemoryCacheManager()) APResourceResolveServiceImpl(apRequestService, userRepository, InMemoryCacheManager())
apResourceResolveService.resolve<Object>("https", 0) apResourceResolveService.resolve<Object>("https", 0)
apResourceResolveService.resolve<Object>("https", 0) apResourceResolveService.resolve<Object>("https", 0)
apResourceResolveService.resolve<Object>("https", 0) apResourceResolveService.resolve<Object>("https", 0)
apResourceResolveService.resolve<Object>("https", 0) apResourceResolveService.resolve<Object>("https", 0)
assertEquals(1, count) verify(apRequestService, times(1)).apGet(
eq("https"),
eq(user),
eq(Object::class.java)
)
} }
@Test @Test
fun 複数回の同じリクエストが同時に発行されても重複して発行されない() = runTest { fun 複数回の同じリクエストが同時に発行されても重複して発行されない() = runTest {
var count = 0
val httpClient = HttpClient(MockEngine { request ->
count++
respondOk("{}")
})
val userRepository = mock<UserRepository>() val userRepository = mock<UserRepository>()
val user = UserBuilder.localUserOf()
whenever(userRepository.findById(any())).doReturn( whenever(userRepository.findById(any())) doReturn user
userBuilder.of(
2L,
"follower", val apRequestService = mock<APRequestService> {
"follower.example.com", onBlocking {
"followerUser", apGet(
"test follower user", eq("https"),
"https://follower.example.com/inbox", eq(user),
"https://follower.example.com/outbox", eq(Object::class.java)
"https://follower.example.com", )
"https://follower.example.com", } doReturn dev.usbharu.hideout.domain.model.ap.Object(
publicKey = "", emptyList()
createdAt = Instant.now(),
keyId = ""
) )
) }
val apResourceResolveService = val apResourceResolveService =
APResourceResolveServiceImpl(mock(), userRepository, InMemoryCacheManager()) APResourceResolveServiceImpl(apRequestService, userRepository, InMemoryCacheManager())
repeat(10) { repeat(10) {
awaitAll( awaitAll(
@ -153,45 +127,47 @@ class APResourceResolveServiceImplTest {
) )
} }
assertEquals(1, count) verify(apRequestService, times(1)).apGet(
eq("https"),
eq(user),
eq(Object::class.java)
)
} }
@Test @Test
fun 関係のないリクエストは発行する() = runTest { fun 関係のないリクエストは発行する() = runTest {
var count = 0
val httpClient = HttpClient(MockEngine { request ->
count++
respondOk("{}")
})
val userRepository = mock<UserRepository>() val userRepository = mock<UserRepository>()
val user = UserBuilder.localUserOf()
whenever(userRepository.findById(any())).doReturn( whenever(userRepository.findById(any())).doReturn(
userBuilder.of( user
2L,
"follower",
"follower.example.com",
"followerUser",
"test follower user",
"https://follower.example.com/inbox",
"https://follower.example.com/outbox",
"https://follower.example.com",
"https://follower.example.com",
publicKey = "",
createdAt = Instant.now(),
keyId = ""
)
) )
val apRequestService = mock<APRequestService> {
onBlocking {
apGet(
any(),
eq(user),
eq(Object::class.java)
)
} doReturn dev.usbharu.hideout.domain.model.ap.Object(
emptyList()
)
}
val apResourceResolveService = val apResourceResolveService =
APResourceResolveServiceImpl(mock(), userRepository, InMemoryCacheManager()) APResourceResolveServiceImpl(apRequestService, userRepository, InMemoryCacheManager())
apResourceResolveService.resolve<Object>("abcd", 0) apResourceResolveService.resolve<Object>("abcd", 0)
apResourceResolveService.resolve<Object>("1234", 0) apResourceResolveService.resolve<Object>("1234", 0)
apResourceResolveService.resolve<Object>("aaaa", 0) apResourceResolveService.resolve<Object>("aaaa", 0)
assertEquals(3, count) verify(apRequestService, times(3)).apGet(
any(),
eq(user),
eq(Object::class.java)
)
} }