From 234a9f4d7b42aef7aca2c37908758662e6ef1ccf Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 28 Jan 2024 17:17:15 +0900 Subject: [PATCH] =?UTF-8?q?test:=20NotificationRequest=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 --- .../FollowNotificationRequestTest.kt | 28 ++++++ .../FollowRequestNotificationRequestTest.kt | 28 ++++++ .../MentionNotificationRequestTest.kt | 28 ++++++ .../NotificationServiceImplTest.kt | 97 +++++++++++++++++++ .../PostNotificationRequestTest.kt | 28 ++++++ .../ReactionNotificationRequestTest.kt | 28 ++++++ .../RepostNotificationRequestTest.kt | 28 ++++++ 7 files changed, 265 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowNotificationRequestTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowRequestNotificationRequestTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/MentionNotificationRequestTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImplTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/PostNotificationRequestTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/ReactionNotificationRequestTest.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/core/service/notification/RepostNotificationRequestTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowNotificationRequestTest.kt new file mode 100644 index 00000000..1e886b32 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import java.time.Instant + +class FollowNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = FollowNotificationRequest(1, 2).buildNotification(1, createdAt) + + Assertions.assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "follow", + userId = 1, + sourceActorId = 2, + postId = null, + text = null, + reactionId = null, + createdAt = createdAt + ) + ) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowRequestNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowRequestNotificationRequestTest.kt new file mode 100644 index 00000000..370e24be --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/FollowRequestNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import java.time.Instant + +class FollowRequestNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = FollowRequestNotificationRequest(1, 2).buildNotification(1, createdAt) + + Assertions.assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "follow-request", + userId = 1, + sourceActorId = 2, + postId = null, + text = null, + reactionId = null, + createdAt = createdAt + ) + ) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/MentionNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/MentionNotificationRequestTest.kt new file mode 100644 index 00000000..05172802 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/MentionNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.Instant + +class MentionNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = MentionNotificationRequest(1, 2, 3).buildNotification(1, createdAt) + + assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "mention", + userId = 1, + sourceActorId = 2, + postId = 3, + text = null, + reactionId = null, + createdAt = createdAt + ) + ) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImplTest.kt new file mode 100644 index 00000000..7ac0bacd --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/NotificationServiceImplTest.kt @@ -0,0 +1,97 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.application.config.ApplicationConfig +import dev.usbharu.hideout.application.service.id.TwitterSnowflakeIdGenerateService +import dev.usbharu.hideout.core.domain.model.actor.ActorRepository +import dev.usbharu.hideout.core.domain.model.notification.Notification +import dev.usbharu.hideout.core.domain.model.notification.NotificationRepository +import dev.usbharu.hideout.core.domain.model.post.PostRepository +import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository +import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository +import kotlinx.coroutines.test.runTest +import org.assertj.core.api.Assertions.assertThat +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.* +import utils.UserBuilder +import java.net.URL + +@ExtendWith(MockitoExtension::class) +class NotificationServiceImplTest { + + + @Mock + private lateinit var relationshipNotificationManagementService: RelationshipNotificationManagementService + + @Mock + private lateinit var relationshipRepository: RelationshipRepository + + @Spy + private val notificationStoreList: MutableList = mutableListOf() + + @Mock + private lateinit var notificationRepository: NotificationRepository + + @Mock + private lateinit var actorRepository: ActorRepository + + @Mock + private lateinit var postRepository: PostRepository + + @Mock + private lateinit var reactionRepository: ReactionRepository + + @Spy + private val applicationConfig = ApplicationConfig(URL("https://example.com")) + + @InjectMocks + private lateinit var notificationServiceImpl: NotificationServiceImpl + + @Test + fun `publishNotifi ローカルユーザーへの通知を発行する`() = runTest { + + val actor = UserBuilder.localUserOf(domain = "example.com") + + whenever(actorRepository.findById(eq(1))).doReturn(actor) + + val id = TwitterSnowflakeIdGenerateService.generateId() + + whenever(notificationRepository.generateId()).doReturn(id) + + whenever(notificationRepository.save(any())).doAnswer { it.arguments[0] as Notification } + + + val actual = notificationServiceImpl.publishNotify(PostNotificationRequest(1, 2, 3)) + + assertThat(actual).isNotNull() + + verify(notificationRepository, times(1)).save(any()) + } + + @Test + fun `publishNotify ユーザーが存在しないときは発行しない`() = runTest { + val actual = notificationServiceImpl.publishNotify(PostNotificationRequest(1, 2, 3)) + + assertThat(actual).isNull() + } + + @Test + fun `publishNotify ユーザーがリモートユーザーの場合は発行しない`() = runTest { + val actor = UserBuilder.remoteUserOf(domain = "remote.example.com") + + whenever(actorRepository.findById(eq(1))).doReturn(actor) + + val actual = notificationServiceImpl.publishNotify(PostNotificationRequest(1, 2, 3)) + + assertThat(actual).isNull() + } + + @Test + fun unpublishNotify() = runTest { + notificationServiceImpl.unpublishNotify(1) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/PostNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/PostNotificationRequestTest.kt new file mode 100644 index 00000000..d4ce9bb7 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/PostNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import java.time.Instant + +class PostNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = PostNotificationRequest(1, 2, 3).buildNotification(1, createdAt) + + Assertions.assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "post", + userId = 1, + sourceActorId = 2, + postId = 3, + text = null, + reactionId = null, + createdAt = createdAt + ) + ) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/ReactionNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/ReactionNotificationRequestTest.kt new file mode 100644 index 00000000..1d662bc8 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/ReactionNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import java.time.Instant + +class ReactionNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = ReactionNotificationRequest(1, 2, 3, 4).buildNotification(1, createdAt) + + Assertions.assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "reaction", + userId = 1, + sourceActorId = 2, + postId = 3, + text = null, + reactionId = 4, + createdAt = createdAt + ) + ) + } +} diff --git a/src/test/kotlin/dev/usbharu/hideout/core/service/notification/RepostNotificationRequestTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/RepostNotificationRequestTest.kt new file mode 100644 index 00000000..f81f8786 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/core/service/notification/RepostNotificationRequestTest.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.service.notification + +import dev.usbharu.hideout.core.domain.model.notification.Notification +import org.assertj.core.api.Assertions +import org.junit.jupiter.api.Test +import java.time.Instant + +class RepostNotificationRequestTest { + + @Test + fun buildNotification() { + val createdAt = Instant.now() + val actual = RepostNotificationRequest(1, 2, 3).buildNotification(1, createdAt) + + Assertions.assertThat(actual).isEqualTo( + Notification( + id = 1, + type = "repost", + userId = 1, + sourceActorId = 2, + postId = 3, + text = null, + reactionId = null, + createdAt = createdAt + ) + ) + } +}