|
| 1 | +package com.example.log4u.domain.follow; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | + |
| 10 | +import com.example.log4u.domain.follow.entitiy.Follow; |
| 11 | +import com.example.log4u.domain.follow.exception.FollowNotFoundException; |
| 12 | +import com.example.log4u.domain.follow.repository.FollowRepository; |
| 13 | +import com.example.log4u.domain.follow.service.FollowService; |
| 14 | +import com.example.log4u.domain.user.entity.User; |
| 15 | +import com.example.log4u.domain.user.exception.UserNotFoundException; |
| 16 | +import com.example.log4u.domain.user.repository.UserRepository; |
| 17 | +import com.example.log4u.fixture.UserFixture; |
| 18 | + |
| 19 | +import jakarta.transaction.Transactional; |
| 20 | + |
| 21 | +@DisplayName("팔로우 통합 테스트(시큐리티 제외)") |
| 22 | +@SpringBootTest |
| 23 | +class FollowTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private FollowService followService; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private FollowRepository followRepository; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private UserRepository userRepository; |
| 33 | + |
| 34 | + private static final String WRONG_TARGET = "nonexistuser"; |
| 35 | + private static final String TARGET = "targetUser"; |
| 36 | + |
| 37 | + @Test |
| 38 | + @Transactional |
| 39 | + @DisplayName("팔로우 시 유저가 없어 USER NOT FOUND 예외 발생") |
| 40 | + void createFollowFailureWithUserNotFound() { |
| 41 | + User user = UserFixture.createUserFixture(); |
| 42 | + userRepository.save(user); |
| 43 | + |
| 44 | + assertThrows(UserNotFoundException.class, |
| 45 | + () -> followService.createFollow(user.getUserId(), WRONG_TARGET)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @Transactional |
| 50 | + @DisplayName("팔로우가 되어야 한다.") |
| 51 | + void createFollowSuccess() { |
| 52 | + String targetUserNickname = "targetUser"; |
| 53 | + User initiator = UserFixture.createUserFixture(); |
| 54 | + User target = UserFixture.createUserFixtureWithNickname(targetUserNickname); |
| 55 | + |
| 56 | + initiator = userRepository.save(initiator); |
| 57 | + target = userRepository.save(target); |
| 58 | + |
| 59 | + followService.createFollow(initiator.getUserId(), targetUserNickname); |
| 60 | + |
| 61 | + followRepository.existsByInitiatorIdAndTargetId(initiator.getUserId(), target.getUserId()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @Transactional |
| 66 | + @DisplayName("팔로우 취소가 되어야한다.") |
| 67 | + void deleteFollowSuccess() { |
| 68 | + Long userId = saveOneFollow(); |
| 69 | + |
| 70 | + followService.deleteFollow(userId, TARGET); |
| 71 | + |
| 72 | + assertThrows(FollowNotFoundException.class, |
| 73 | + () -> followService.deleteFollow(userId, TARGET)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @Transactional |
| 78 | + @DisplayName("팔로우한 정보가 없어 FollowNotFound 발생") |
| 79 | + void deleteFollowFailureWithFollowNotFound() { |
| 80 | + User user = UserFixture.createUserFixture(); |
| 81 | + user = userRepository.save(user); |
| 82 | + final Long userId = user.getUserId(); |
| 83 | + |
| 84 | + assertThrows(FollowNotFoundException.class, |
| 85 | + () -> followService.deleteFollow(userId, WRONG_TARGET)); |
| 86 | + } |
| 87 | + |
| 88 | + private Long saveOneFollow() { |
| 89 | + String targetUserNickname = "targetUser"; |
| 90 | + User initiator = UserFixture.createUserFixture(); |
| 91 | + User target = UserFixture.createUserFixtureWithNickname(targetUserNickname); |
| 92 | + |
| 93 | + initiator = userRepository.save(initiator); |
| 94 | + target = userRepository.save(target); |
| 95 | + |
| 96 | + Follow follow = Follow.of( |
| 97 | + initiator.getUserId(), |
| 98 | + target.getUserId() |
| 99 | + ); |
| 100 | + |
| 101 | + followRepository.save(follow); |
| 102 | + |
| 103 | + return initiator.getUserId(); |
| 104 | + } |
| 105 | +} |
0 commit comments