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 "해당하는 친구신청이 없거나 이미 친구입니다." + } })