44import com .back .domain .comment .entity .Comment ;
55import com .back .domain .comment .repository .CommentRepository ;
66import com .back .domain .post .entity .Post ;
7+ import com .back .domain .post .fixture .PostFixture ;
78import com .back .domain .post .repository .PostRepository ;
89import com .back .domain .user .entity .Gender ;
910import com .back .domain .user .entity .Mbti ;
2324
2425import java .lang .reflect .Field ;
2526import java .time .LocalDateTime ;
27+ import java .util .Optional ;
2628import java .util .UUID ;
2729
28- import static org .springframework . test . web . servlet . request . MockMvcRequestBuilders . get ;
29- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
30+ import static org .assertj . core . api . AssertionsForClassTypes . assertThat ;
31+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .* ;
3032import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
3133import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
3234import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -53,23 +55,14 @@ class CommentControllerTest {
5355 private ObjectMapper objectMapper ;
5456
5557 private User testUser ;
58+ private User anotherUser ;
5659 private Post testPost ;
60+ private Comment testComment ;
5761
5862 @ BeforeEach
5963 void setUp () {
60- String uid = UUID .randomUUID ().toString ().substring (0 , 5 );
61-
62- User user = User .builder ()
63- .email ("test" + uid + "@example.com" )
64- .password ("password" )
65- .username ("tester_" )
66- .nickname ("tester_" + uid )
67- .beliefs ("도전" )
68- .gender (Gender .M )
69- .role (Role .USER )
70- .mbti (Mbti .ISFJ )
71- .birthdayAt (LocalDateTime .of (2000 , 1 , 1 , 0 , 0 ))
72- .build ();
64+ PostFixture postFixture = new PostFixture (userRepository , postRepository );
65+ User user = postFixture .createTestUser ();
7366 testUser = userRepository .save (user );
7467
7568 Post post = Post .builder ()
@@ -78,6 +71,9 @@ void setUp() {
7871 .user (user )
7972 .build ();
8073 testPost = postRepository .save (post );
74+
75+ User another = postFixture .createAnotherUser ();
76+ anotherUser = userRepository .save (another );
8177 }
8278
8379 @ Nested
@@ -187,6 +183,136 @@ void failWithNonExistentPostId() throws Exception {
187183
188184 }
189185
186+ @ Nested
187+ @ DisplayName ("댓글 수정" )
188+ class UpdateCommentTest {
189+
190+ @ Test
191+ @ DisplayName ("성공 - 유효한 요청으로 댓글 수정" )
192+ void updateComment_Success () throws Exception {
193+ // Given
194+ testComment = Comment .builder ()
195+ .content ("테스트 댓글" )
196+ .user (testUser )
197+ .post (testPost )
198+ .build ();
199+ testComment = commentRepository .save (testComment );
200+ CommentRequest request = new CommentRequest ("수정된 댓글입니다." , false );
201+
202+ // When & Then
203+ mockMvc .perform (put ("/api/v1/posts/{postId}/comments/{commentId}" ,
204+ testPost .getId (),
205+ testComment .getId ())
206+ .param ("userId" , testUser .getId ().toString ())
207+ .contentType (MediaType .APPLICATION_JSON )
208+ .content (objectMapper .writeValueAsString (request )))
209+ .andExpect (status ().isOk ())
210+ .andExpect (jsonPath ("$.message" ).value ("성공적으로 수정되었습니다." ))
211+ .andExpect (jsonPath ("$.data" ).value (testComment .getId ()))
212+ .andDo (print ());
213+
214+ Comment updatedComment = commentRepository .findById (testComment .getId ()).orElseThrow ();
215+ assertThat (updatedComment .getContent ()).isEqualTo (request .content ());
216+ assertThat (updatedComment .getUpdatedAt ()).isNotNull ();
217+ }
218+
219+ @ Test
220+ @ DisplayName ("실패 - 빈 내용으로 댓글 수정 시도" )
221+ void updateComment_EmptyContent_Fail () throws Exception {
222+ // Given
223+ testComment = Comment .builder ()
224+ .content ("테스트 댓글" )
225+ .user (testUser )
226+ .post (testPost )
227+ .build ();
228+ testComment = commentRepository .save (testComment );
229+ CommentRequest request = new CommentRequest ("" , false );
230+
231+ // When & Then
232+ mockMvc .perform (put ("/api/v1/posts/{postId}/comments/{commentId}" ,
233+ testPost .getId (),
234+ testComment .getId ())
235+ .param ("userId" , testUser .getId ().toString ())
236+ .contentType (MediaType .APPLICATION_JSON )
237+ .content (objectMapper .writeValueAsString (request )))
238+ .andExpect (status ().isBadRequest ())
239+ .andDo (print ());
240+
241+ Comment unchangedComment = commentRepository .findById (testComment .getId ()).orElseThrow ();
242+ assertThat (unchangedComment .getContent ()).isEqualTo ("테스트 댓글" );
243+ }
244+ }
245+
246+ @ Nested
247+ @ DisplayName ("댓글 삭제" )
248+ class DeleteCommentTest {
249+
250+ @ Test
251+ @ DisplayName ("성공 - 유효한 요청으로 댓글 삭제" )
252+ void deleteComment_Success () throws Exception {
253+ testComment = Comment .builder ()
254+ .content ("테스트 댓글" )
255+ .user (testUser )
256+ .post (testPost )
257+ .build ();
258+ testComment = commentRepository .save (testComment );
259+
260+ // When & Then
261+ mockMvc .perform (delete ("/api/v1/posts/{postId}/comments/{commentId}" ,
262+ testPost .getId (),
263+ testComment .getId ())
264+ .param ("userId" , testUser .getId ().toString ()))
265+ .andExpect (status ().isOk ())
266+ .andExpect (jsonPath ("$.message" ).value ("성공적으로 삭제되었습니다." ))
267+ .andDo (print ());
268+
269+ // 실제 DB에서 삭제 확인
270+ Optional <Comment > deletedComment = commentRepository .findById (testComment .getId ());
271+ assertThat (deletedComment ).isEmpty ();
272+ }
273+
274+ @ Test
275+ @ DisplayName ("실패 - 다른 사용자의 댓글 삭제 시도" )
276+ void deleteComment_Unauthorized_Fail () throws Exception {
277+ testComment = Comment .builder ()
278+ .content ("테스트 댓글" )
279+ .user (testUser )
280+ .post (testPost )
281+ .build ();
282+ testComment = commentRepository .save (testComment );
283+
284+ // When & Then
285+ mockMvc .perform (delete ("/api/v1/posts/{postId}/comments/{commentId}" ,
286+ testPost .getId (),
287+ testComment .getId ())
288+ .param ("userId" , anotherUser .getId ().toString ()))
289+ .andExpect (status ().isUnauthorized ())
290+ .andDo (print ());
291+
292+ Optional <Comment > existingComment = commentRepository .findById (testComment .getId ());
293+ assertThat (existingComment ).isPresent ();
294+ }
295+
296+ @ Test
297+ @ DisplayName ("실패 - 존재하지 않는 댓글 삭제 시도" )
298+ void deleteComment_CommentNotFound_Fail () throws Exception {
299+ testComment = Comment .builder ()
300+ .content ("테스트 댓글" )
301+ .user (testUser )
302+ .post (testPost )
303+ .build ();
304+ testComment = commentRepository .save (testComment );
305+
306+ // When & Then
307+ mockMvc .perform (delete ("/api/v1/posts/{postId}/comments/{commentId}" ,
308+ testPost .getId (),
309+ 999L )
310+ .param ("userId" , testUser .getId ().toString ()))
311+ .andExpect (status ().isNotFound ())
312+ .andDo (print ());
313+ }
314+ }
315+
190316 private void createTestComments () {
191317 for (int i = 1 ; i <= 3 ; i ++) {
192318 Comment comment = Comment .builder ()
@@ -263,16 +389,4 @@ private void createTestCommentsWithDifferentLikes() {
263389 .build ();
264390 commentRepository .save (comment3 );
265391 }
266-
267- private void createManyTestComments (int count ) {
268- for (int i = 1 ; i <= count ; i ++) {
269- Comment comment = Comment .builder ()
270- .content ("댓글 " + i )
271- .post (testPost )
272- .user (testUser )
273- .hide (false )
274- .build ();
275- commentRepository .save (comment );
276- }
277- }
278392}
0 commit comments