From f2fe87088864be73f0c132111a093bfc0db972de Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:32:04 +0900 Subject: [PATCH] =?UTF-8?q?test:=20APLikeServiceImpl=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ap/APLikeServiceImplTest.kt | 110 ++++++++++++++++++ src/test/kotlin/utils/PostBuilder.kt | 39 +++++++ 2 files changed, 149 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/service/ap/APLikeServiceImplTest.kt create mode 100644 src/test/kotlin/utils/PostBuilder.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/service/ap/APLikeServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/ap/APLikeServiceImplTest.kt new file mode 100644 index 00000000..88140695 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/service/ap/APLikeServiceImplTest.kt @@ -0,0 +1,110 @@ +package dev.usbharu.hideout.service.ap + +import dev.usbharu.hideout.domain.model.ActivityPubStringResponse +import dev.usbharu.hideout.domain.model.ap.Like +import dev.usbharu.hideout.domain.model.ap.Note +import dev.usbharu.hideout.domain.model.ap.Person +import dev.usbharu.hideout.exception.ap.FailedToGetActivityPubResourceException +import dev.usbharu.hideout.query.PostQueryService +import dev.usbharu.hideout.service.reaction.ReactionService +import io.ktor.http.* +import kotlinx.coroutines.async +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.mockito.kotlin.* +import utils.PostBuilder +import utils.TestTransaction +import utils.UserBuilder + + +class APLikeServiceImplTest { + @Test + fun `receiveLike 正常なLikeを処理できる`() = runTest { + val actor = "https://example.com/actor" + val note = "https://example.com/note" + val like = Like( + name = "Like", actor = actor, id = "htps://example.com", `object` = note, content = "aaa" + ) + + val user = UserBuilder.localUserOf() + val apUserService = mock { + onBlocking { fetchPersonWithEntity(eq(actor), anyOrNull()) } doReturn (Person( + name = "TestUser", + id = "https://example.com", + preferredUsername = "Test user", + summary = "test user", + inbox = "https://example.com/inbox", + outbox = "https://example.com/outbox", + url = "https://example.com/", + icon = null, + publicKey = null, + followers = null, + following = null + ) to user) + } + val apNoteService = mock { + on { fetchNoteAsync(eq(note), anyOrNull()) } doReturn async { + Note( + name = "Note", + id = "https://example.com/note", + attributedTo = "https://example.com/actor", + content = "Hello World", + published = "Date: Wed, 21 Oct 2015 07:28:00 GMT", + ) + } + } + val post = PostBuilder.of() + val postQueryService = mock { + onBlocking { findByUrl(eq(note)) } doReturn post + } + val reactionService = mock() + val apLikeServiceImpl = APLikeServiceImpl( + reactionService, apUserService, apNoteService, postQueryService, TestTransaction + ) + + val actual = apLikeServiceImpl.receiveLike(like) + + verify(reactionService, times(1)).receiveReaction(eq("aaa"), eq("example.com"), eq(user.id), eq(post.id)) + assertEquals(ActivityPubStringResponse(HttpStatusCode.OK, ""), actual) + } + + @Test + fun `recieveLike Likeのobjectのurlが取得できないとき何もしない`() = runTest { + val actor = "https://example.com/actor" + val note = "https://example.com/note" + val like = Like( + name = "Like", actor = actor, id = "htps://example.com", `object` = note, content = "aaa" + ) + + val user = UserBuilder.localUserOf() + val apUserService = mock { + onBlocking { fetchPersonWithEntity(eq(actor), anyOrNull()) } doReturn (Person( + name = "TestUser", + id = "https://example.com", + preferredUsername = "Test user", + summary = "test user", + inbox = "https://example.com/inbox", + outbox = "https://example.com/outbox", + url = "https://example.com/", + icon = null, + publicKey = null, + followers = null, + following = null + ) to user) + } + val apNoteService = mock { + on { fetchNoteAsync(eq(note), anyOrNull()) } doThrow FailedToGetActivityPubResourceException() + } + + val reactionService = mock() + val apLikeServiceImpl = APLikeServiceImpl( + reactionService, apUserService, apNoteService, mock(), TestTransaction + ) + + val actual = apLikeServiceImpl.receiveLike(like) + + verify(reactionService, times(0)).receiveReaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull()) + assertEquals(ActivityPubStringResponse(HttpStatusCode.OK, ""), actual) + } +} diff --git a/src/test/kotlin/utils/PostBuilder.kt b/src/test/kotlin/utils/PostBuilder.kt new file mode 100644 index 00000000..630d006e --- /dev/null +++ b/src/test/kotlin/utils/PostBuilder.kt @@ -0,0 +1,39 @@ +package utils + +import dev.usbharu.hideout.config.CharacterLimit +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility +import dev.usbharu.hideout.service.core.TwitterSnowflakeIdGenerateService +import kotlinx.coroutines.runBlocking +import java.time.Instant + +object PostBuilder { + + private val postBuilder = Post.PostBuilder(CharacterLimit()) + + private val idGenerator = TwitterSnowflakeIdGenerateService + + fun of( + id: Long = generateId(), + userId: Long = generateId(), + overview: String? = null, + text: String = "Hello World", + createdAt: Long = Instant.now().toEpochMilli(), + visibility: Visibility = Visibility.PUBLIC, + url: String = "https://example.com/users/$userId/posts/$id" + ): Post { + return postBuilder.of( + id = id, + userId = userId, + overview = overview, + text = text, + createdAt = createdAt, + visibility = visibility, + url = url, + ) + } + + private fun generateId(): Long = runBlocking { + idGenerator.generateId() + } +}