From 23db38705466398f327d591b1de5f6648d6ca5a9 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:50:40 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E7=84=A1=E5=8A=B9=E5=8C=96=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9FAPResourceResolveServiceImpl?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E6=9C=89=E5=8A=B9?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APResourceResolveServiceImplTest.kt | 180 ++++++++---------- 1 file changed, 78 insertions(+), 102 deletions(-) diff --git a/src/test/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImplTest.kt index 87f5a553..2450480b 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImplTest.kt @@ -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() - 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 { + 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("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() - 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 { + 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("https", 0) apResourceResolveService.resolve("https", 0) apResourceResolveService.resolve("https", 0) apResourceResolveService.resolve("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() + 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 { + 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() + 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 { + 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("abcd", 0) apResourceResolveService.resolve("1234", 0) apResourceResolveService.resolve("aaaa", 0) - assertEquals(3, count) + verify(apRequestService, times(3)).apGet( + any(), + eq(user), + eq(Object::class.java) + ) }