From 7095dc6b66355794290bc61397728f18f47aba61 Mon Sep 17 00:00:00 2001 From: minahYu Date: Tue, 18 Jun 2024 16:46:28 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=B9=9C=EA=B5=AC=EC=8B=A0=EC=B2=AD=20?= =?UTF-8?q?=EC=88=98=EB=9D=BD=20service=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/service/FriendServiceImplTest.kt | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/user/src/test/kotlin/kpring/user/service/FriendServiceImplTest.kt b/user/src/test/kotlin/kpring/user/service/FriendServiceImplTest.kt index e5a24eaa..bd5ae755 100644 --- a/user/src/test/kotlin/kpring/user/service/FriendServiceImplTest.kt +++ b/user/src/test/kotlin/kpring/user/service/FriendServiceImplTest.kt @@ -13,6 +13,7 @@ import kpring.user.global.CommonTest import kpring.user.repository.FriendRepository import kpring.user.repository.UserRepository import org.springframework.security.crypto.password.PasswordEncoder +import java.util.* internal class FriendServiceImplTest : FunSpec({ val userRepository: UserRepository = mockk() @@ -107,7 +108,10 @@ internal class FriendServiceImplTest : FunSpec({ val friendList = listOf(mockk(relaxed = true)) every { - friendRepository.findAllByUserIdAndRequestStatus(CommonTest.TEST_USER_ID, FriendRequestStatus.RECEIVED) + friendRepository.findAllByUserIdAndRequestStatus( + CommonTest.TEST_USER_ID, + FriendRequestStatus.RECEIVED, + ) } returns friendList val response = friendService.getFriendRequests(CommonTest.TEST_USER_ID) @@ -116,4 +120,75 @@ internal class FriendServiceImplTest : FunSpec({ request.username shouldBe friend.username } } + + test("친구신청수락_성공") { + val receivedFriend = mockk(relaxed = true) + val requestedFriend = mockk(relaxed = true) + + every { + friendRepository.findByUserIdAndFriendIdAndRequestStatus( + CommonTest.TEST_USER_ID, + CommonTest.TEST_FRIEND_ID, + FriendRequestStatus.RECEIVED, + ) + } returns Optional.of(receivedFriend) + every { + friendRepository.findByUserIdAndFriendIdAndRequestStatus( + CommonTest.TEST_FRIEND_ID, + CommonTest.TEST_USER_ID, + FriendRequestStatus.REQUESTED, + ) + } returns Optional.of(requestedFriend) + + every { receivedFriend.updateRequestStatus(any()) } just Runs + every { requestedFriend.updateRequestStatus(any()) } just Runs + + val response = + friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID) + response.friendId shouldBe CommonTest.TEST_FRIEND_ID + + verify(exactly = 2) { + friendRepository.findByUserIdAndFriendIdAndRequestStatus(any(), any(), any()) + } + } + + test("친구신청수락_실패_해당하는 친구신청이 없는 케이스") { + every { + friendRepository.findByUserIdAndFriendIdAndRequestStatus( + CommonTest.TEST_USER_ID, + CommonTest.TEST_FRIEND_ID, + FriendRequestStatus.RECEIVED, + ) + } throws ServiceException(UserErrorCode.FRIENDSHIP_ALREADY_EXISTS_OR_NOT_FOUND) + + val exception = + shouldThrow { + friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID) + } + exception.errorCode.message() shouldBe "해당하는 친구신청이 없거나 이미 친구입니다." + } + + test("친구신청수락_실패_이미 친구인 케이스") { + + every { + friendRepository.findByUserIdAndFriendIdAndRequestStatus( + CommonTest.TEST_USER_ID, + CommonTest.TEST_FRIEND_ID, + FriendRequestStatus.RECEIVED, + ) + } returns Optional.empty() + every { + friendRepository.findByUserIdAndFriendIdAndRequestStatus( + CommonTest.TEST_FRIEND_ID, + CommonTest.TEST_USER_ID, + FriendRequestStatus.REQUESTED, + ) + } returns Optional.empty() + + val exception = + shouldThrow { + friendService.acceptFriendRequest(CommonTest.TEST_USER_ID, CommonTest.TEST_FRIEND_ID) + } + exception.errorCode.message() shouldBe "해당하는 친구신청이 없거나 이미 친구입니다." + } })