55import java .sql .Connection ;
66import java .sql .DriverManager ;
77import java .sql .SQLException ;
8- import java .time .LocalDateTime ;
9- import java .util .UUID ;
108
119import org .junit .jupiter .api .DisplayName ;
1210import org .junit .jupiter .api .Test ;
1715import com .example .log4u .domain .follow .exception .FollowNotFoundException ;
1816import com .example .log4u .domain .follow .repository .FollowRepository ;
1917import com .example .log4u .domain .follow .service .FollowService ;
20- import com .example .log4u .domain .user .entity .SocialType ;
2118import com .example .log4u .domain .user .entity .User ;
2219import com .example .log4u .domain .user .exception .UserNotFoundException ;
2320import com .example .log4u .domain .user .repository .UserRepository ;
@@ -56,94 +53,33 @@ void checkDatabaseConnection() {
5653
5754 @ Test
5855 @ Transactional
59- @ DisplayName ("팔로우 시 유저가 없어 USER NOT FOUND 예외 발생" )
56+ @ DisplayName ("팔로우 시 타겟 유저가 없어 USER NOT FOUND 예외 발생" )
6057 void createFollowFailureWithUserNotFound () {
61- User user = User .builder ()
62- .name ("test" + UUID .randomUUID ())
63- .nickname ("testUser" + UUID .randomUUID ())
64- .providerId ("123" + UUID .randomUUID ())
65- .email ("test" + UUID .randomUUID () + "@example.com" )
66- .socialType (SocialType .KAKAO )
67- .role ("ROLE_USER" )
68- .statusMessage (LocalDateTime .now ().toString ())
69- .isPremium (false )
70- .build ();
71-
72- final Long userId = userRepository .save (user ).getUserId ();
58+ User initiator = UserFixture .createUserFixture ();
59+ final Long initiatorId = initiator .getUserId ();
7360
7461 assertThrows (UserNotFoundException .class ,
75- () -> followService .createFollow (userId , WRONG_TARGET ));
62+ () -> followService .createFollow (initiatorId , WRONG_TARGET ));
7663 }
7764
7865 @ Test
7966 @ Transactional
8067 @ DisplayName ("팔로우가 되어야 한다." )
8168 void createFollowSuccess () {
82- User initiator = User .builder ()
83- .name ("test" + UUID .randomUUID ())
84- .nickname ("testUser" + UUID .randomUUID ())
85- .providerId ("123" + UUID .randomUUID ())
86- .email ("test" + UUID .randomUUID () + "@example.com" )
87- .socialType (SocialType .KAKAO )
88- .role ("ROLE_USER" )
89- .statusMessage (LocalDateTime .now ().toString ())
90- .isPremium (false )
91- .build ();
92-
93- User target = User .builder ()
94- .name ("test" + UUID .randomUUID ())
95- .nickname (TARGET )
96- .providerId ("123" + UUID .randomUUID ())
97- .email ("test" + UUID .randomUUID () + "@example.com" )
98- .socialType (SocialType .KAKAO )
99- .role ("ROLE_USER" )
100- .statusMessage (LocalDateTime .now ().toString ())
101- .isPremium (false )
102- .build ();
103-
104- initiator = userRepository .save (initiator );
105- target = userRepository .save (target );
69+ Long [] ids = saveOneFollow ();
70+ final Long initiatorId = ids [0 ];
71+ final Long targetId = ids [1 ];
10672
107- followService .createFollow (initiator .getUserId (), TARGET );
108-
109- assertTrue (followRepository .existsByInitiatorIdAndTargetId (initiator .getUserId (), target .getUserId ()));
73+ assertTrue (followRepository .existsByInitiatorIdAndTargetId (initiatorId , targetId ));
11074 }
11175
11276 @ Test
11377 @ Transactional
11478 @ DisplayName ("팔로우 취소가 되어야한다." )
11579 void deleteFollowSuccess () {
116- User initiator = User .builder ()
117- .name ("test" + UUID .randomUUID ())
118- .nickname ("testUser" + UUID .randomUUID ())
119- .providerId ("123" + UUID .randomUUID ())
120- .email ("test" + UUID .randomUUID () + "@example.com" )
121- .socialType (SocialType .KAKAO )
122- .role ("ROLE_USER" )
123- .statusMessage (LocalDateTime .now ().toString ())
124- .isPremium (false )
125- .build ();
126-
127- User target = User .builder ()
128- .name ("test" + UUID .randomUUID ())
129- .nickname (TARGET )
130- .providerId ("123" + UUID .randomUUID ())
131- .email ("test" + UUID .randomUUID () + "@example.com" )
132- .socialType (SocialType .KAKAO )
133- .role ("ROLE_USER" )
134- .statusMessage (LocalDateTime .now ().toString ())
135- .isPremium (false )
136- .build ();
137-
138- final Long initiatorId = userRepository .save (initiator ).getUserId ();
139- target = userRepository .save (target );
80+ Long [] ids = saveOneFollow ();
81+ final Long initiatorId = ids [0 ];
14082
141- Follow follow = Follow .of (
142- initiatorId ,
143- target .getUserId ()
144- );
145-
146- followRepository .save (follow );
14783 followService .deleteFollow (initiatorId , TARGET );
14884
14985 assertThrows (FollowNotFoundException .class ,
@@ -154,38 +90,17 @@ void deleteFollowSuccess() {
15490 @ Transactional
15591 @ DisplayName ("팔로우한 정보가 없어 FollowNotFound 발생" )
15692 void deleteFollowFailureWithFollowNotFound () {
157- User initiator = User .builder ()
158- .name ("test" + UUID .randomUUID ())
159- .nickname ("testUser" + UUID .randomUUID ())
160- .providerId ("123" + UUID .randomUUID ())
161- .email ("test" + UUID .randomUUID () + "@example.com" )
162- .socialType (SocialType .KAKAO )
163- .role ("ROLE_USER" )
164- .statusMessage (LocalDateTime .now ().toString ())
165- .isPremium (false )
166- .build ();
93+ User initiator = UserFixture .createUserFixture ();
94+ User target = UserFixture .createUserFixtureWithNickname (TARGET );
16795
168- initiator = userRepository .save (initiator );
16996 final Long initiatorId = initiator .getUserId ();
170-
171- User target = User .builder ()
172- .name ("test" + UUID .randomUUID ())
173- .nickname (TARGET )
174- .providerId ("123" + UUID .randomUUID ())
175- .email ("test" + UUID .randomUUID () + "@example.com" )
176- .socialType (SocialType .KAKAO )
177- .role ("ROLE_USER" )
178- .statusMessage (LocalDateTime .now ().toString ())
179- .isPremium (false )
180- .build ();
181-
18297 userRepository .save (target );
18398
18499 assertThrows (FollowNotFoundException .class ,
185100 () -> followService .deleteFollow (initiatorId , TARGET ));
186101 }
187102
188- private Long saveOneFollow () {
103+ private Long [] saveOneFollow () {
189104 User initiator = UserFixture .createUserFixture ();
190105 User target = UserFixture .createUserFixtureWithNickname (TARGET );
191106
@@ -199,6 +114,6 @@ private Long saveOneFollow() {
199114
200115 followRepository .save (follow );
201116
202- return initiator .getUserId ();
117+ return new Long [] { initiator .getUserId (), target . getUserId ()} ;
203118 }
204119}
0 commit comments