|
1 | 1 | package com.example.log4u.domain.follow; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.*; |
4 | | - |
5 | | -import java.sql.Connection; |
6 | | -import java.sql.DriverManager; |
7 | | -import java.sql.SQLException; |
8 | | - |
9 | 3 | import org.junit.jupiter.api.DisplayName; |
10 | | -import org.junit.jupiter.api.Test; |
11 | | -import org.springframework.beans.factory.annotation.Autowired; |
12 | 4 | import org.springframework.boot.test.context.SpringBootTest; |
13 | 5 |
|
14 | | -import com.example.log4u.domain.follow.entitiy.Follow; |
15 | | -import com.example.log4u.domain.follow.exception.FollowNotFoundException; |
16 | | -import com.example.log4u.domain.follow.repository.FollowRepository; |
17 | | -import com.example.log4u.domain.follow.service.FollowService; |
18 | | -import com.example.log4u.domain.user.entity.User; |
19 | | -import com.example.log4u.domain.user.exception.UserNotFoundException; |
20 | | -import com.example.log4u.domain.user.repository.UserRepository; |
21 | | -import com.example.log4u.fixture.UserFixture; |
22 | | - |
23 | | -import jakarta.transaction.Transactional; |
24 | | - |
25 | 6 | @DisplayName("팔로우 통합 테스트(시큐리티 제외)") |
26 | 7 | @SpringBootTest |
27 | 8 | class FollowTest { |
28 | 9 |
|
29 | | - @Autowired |
30 | | - private FollowService followService; |
31 | | - |
32 | | - @Autowired |
33 | | - private FollowRepository followRepository; |
34 | | - |
35 | | - @Autowired |
36 | | - private UserRepository userRepository; |
37 | | - |
38 | | - private static final String WRONG_TARGET = "nonExistUser"; |
39 | | - private static final String TARGET = "targetUser"; |
40 | | - |
41 | | - @Test |
42 | | - @DisplayName("테스트용 DB 연결 확인") |
43 | | - void checkDatabaseConnection() { |
44 | | - try (Connection connection = DriverManager.getConnection( |
45 | | - "jdbc:mysql://localhost:3307/log4u", |
46 | | - "dev", |
47 | | - "devcos4-team08")) { |
48 | | - assertFalse(connection.isClosed()); |
49 | | - } catch (SQLException e) { |
50 | | - fail("데이터베이스 연결 실패: " + e.getMessage()); |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - @Test |
55 | | - @Transactional |
56 | | - @DisplayName("팔로우 시 타겟 유저가 없어 USER NOT FOUND 예외 발생") |
57 | | - void createFollowFailureWithUserNotFound() { |
58 | | - User initiator = UserFixture.createUserFixture(); |
59 | | - final Long initiatorId = initiator.getUserId(); |
60 | | - |
61 | | - assertThrows(UserNotFoundException.class, |
62 | | - () -> followService.createFollow(initiatorId, WRONG_TARGET)); |
63 | | - } |
64 | | - |
65 | | - @Test |
66 | | - @Transactional |
67 | | - @DisplayName("팔로우가 되어야 한다.") |
68 | | - void createFollowSuccess() { |
69 | | - Long[] ids = saveOneFollow(); |
70 | | - final Long initiatorId = ids[0]; |
71 | | - final Long targetId = ids[1]; |
72 | | - |
73 | | - assertTrue(followRepository.existsByInitiatorIdAndTargetId(initiatorId, targetId)); |
74 | | - } |
75 | | - |
76 | | - @Test |
77 | | - @Transactional |
78 | | - @DisplayName("팔로우 취소가 되어야한다.") |
79 | | - void deleteFollowSuccess() { |
80 | | - Long[] ids = saveOneFollow(); |
81 | | - final Long initiatorId = ids[0]; |
82 | | - |
83 | | - followService.deleteFollow(initiatorId, TARGET); |
84 | | - |
85 | | - assertThrows(FollowNotFoundException.class, |
86 | | - () -> followService.deleteFollow(initiatorId, TARGET)); |
87 | | - } |
88 | | - |
89 | | - @Test |
90 | | - @Transactional |
91 | | - @DisplayName("팔로우한 정보가 없어 FollowNotFound 발생") |
92 | | - void deleteFollowFailureWithFollowNotFound() { |
93 | | - User initiator = UserFixture.createUserFixture(); |
94 | | - User target = UserFixture.createUserFixtureWithNickname(TARGET); |
95 | | - |
96 | | - final Long initiatorId = initiator.getUserId(); |
97 | | - userRepository.save(target); |
98 | | - |
99 | | - assertThrows(FollowNotFoundException.class, |
100 | | - () -> followService.deleteFollow(initiatorId, TARGET)); |
101 | | - } |
102 | | - |
103 | | - private Long[] saveOneFollow() { |
104 | | - User initiator = UserFixture.createUserFixture(); |
105 | | - User target = UserFixture.createUserFixtureWithNickname(TARGET); |
106 | | - |
107 | | - initiator = userRepository.save(initiator); |
108 | | - target = userRepository.save(target); |
109 | | - |
110 | | - Follow follow = Follow.of( |
111 | | - initiator.getUserId(), |
112 | | - target.getUserId() |
113 | | - ); |
114 | | - |
115 | | - followRepository.save(follow); |
116 | | - |
117 | | - return new Long[] {initiator.getUserId(), target.getUserId()}; |
118 | | - } |
| 10 | + // @Autowired |
| 11 | + // private FollowService followService; |
| 12 | + // |
| 13 | + // @Autowired |
| 14 | + // private FollowRepository followRepository; |
| 15 | + // |
| 16 | + // @Autowired |
| 17 | + // private UserRepository userRepository; |
| 18 | + // |
| 19 | + // private static final String WRONG_TARGET = "nonExistUser"; |
| 20 | + // private static final String TARGET = "targetUser"; |
| 21 | + // |
| 22 | + // @Test |
| 23 | + // @DisplayName("테스트용 DB 연결 확인") |
| 24 | + // void checkDatabaseConnection() { |
| 25 | + // try (Connection connection = DriverManager.getConnection( |
| 26 | + // "jdbc:mysql://localhost:3307/log4u", |
| 27 | + // "dev", |
| 28 | + // "devcos4-team08")) { |
| 29 | + // assertFalse(connection.isClosed()); |
| 30 | + // } catch (SQLException e) { |
| 31 | + // fail("데이터베이스 연결 실패: " + e.getMessage()); |
| 32 | + // } |
| 33 | + // } |
| 34 | + // |
| 35 | + // @Test |
| 36 | + // @Transactional |
| 37 | + // @DisplayName("팔로우 시 타겟 유저가 없어 USER NOT FOUND 예외 발생") |
| 38 | + // void createFollowFailureWithUserNotFound() { |
| 39 | + // User initiator = UserFixture.createUserFixture(); |
| 40 | + // final Long initiatorId = initiator.getUserId(); |
| 41 | + // |
| 42 | + // assertThrows(UserNotFoundException.class, |
| 43 | + // () -> followService.createFollow(initiatorId, WRONG_TARGET)); |
| 44 | + // } |
| 45 | + // |
| 46 | + // @Test |
| 47 | + // @Transactional |
| 48 | + // @DisplayName("팔로우가 되어야 한다.") |
| 49 | + // void createFollowSuccess() { |
| 50 | + // Long[] ids = saveOneFollow(); |
| 51 | + // final Long initiatorId = ids[0]; |
| 52 | + // final Long targetId = ids[1]; |
| 53 | + // |
| 54 | + // assertTrue(followRepository.existsByInitiatorIdAndTargetId(initiatorId, targetId)); |
| 55 | + // } |
| 56 | + // |
| 57 | + // @Test |
| 58 | + // @Transactional |
| 59 | + // @DisplayName("팔로우 취소가 되어야한다.") |
| 60 | + // void deleteFollowSuccess() { |
| 61 | + // Long[] ids = saveOneFollow(); |
| 62 | + // final Long initiatorId = ids[0]; |
| 63 | + // |
| 64 | + // followService.deleteFollow(initiatorId, TARGET); |
| 65 | + // |
| 66 | + // assertThrows(FollowNotFoundException.class, |
| 67 | + // () -> followService.deleteFollow(initiatorId, TARGET)); |
| 68 | + // } |
| 69 | + // |
| 70 | + // @Test |
| 71 | + // @Transactional |
| 72 | + // @DisplayName("팔로우한 정보가 없어 FollowNotFound 발생") |
| 73 | + // void deleteFollowFailureWithFollowNotFound() { |
| 74 | + // User initiator = UserFixture.createUserFixture(); |
| 75 | + // User target = UserFixture.createUserFixtureWithNickname(TARGET); |
| 76 | + // |
| 77 | + // final Long initiatorId = initiator.getUserId(); |
| 78 | + // userRepository.save(target); |
| 79 | + // |
| 80 | + // assertThrows(FollowNotFoundException.class, |
| 81 | + // () -> followService.deleteFollow(initiatorId, TARGET)); |
| 82 | + // } |
| 83 | + // |
| 84 | + // private Long[] saveOneFollow() { |
| 85 | + // User initiator = UserFixture.createUserFixture(); |
| 86 | + // User target = UserFixture.createUserFixtureWithNickname(TARGET); |
| 87 | + // |
| 88 | + // initiator = userRepository.save(initiator); |
| 89 | + // target = userRepository.save(target); |
| 90 | + // |
| 91 | + // Follow follow = Follow.of( |
| 92 | + // initiator.getUserId(), |
| 93 | + // target.getUserId() |
| 94 | + // ); |
| 95 | + // |
| 96 | + // followRepository.save(follow); |
| 97 | + // |
| 98 | + // return new Long[] {initiator.getUserId(), target.getUserId()}; |
| 99 | + // } |
119 | 100 | } |
0 commit comments