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