11package com .somemore .community .service .comment ;
22
3+ import static com .somemore .global .exception .ExceptionMessage .NOT_EXISTS_COMMUNITY_BOARD ;
4+ import static com .somemore .global .exception .ExceptionMessage .UNAUTHORIZED_COMMUNITY_COMMENT ;
35import static org .assertj .core .api .Assertions .assertThat ;
46import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
57
68import com .somemore .IntegrationTestSupport ;
9+ import com .somemore .community .domain .CommunityBoard ;
710import com .somemore .community .domain .CommunityComment ;
11+ import com .somemore .community .dto .request .CommunityBoardCreateRequestDto ;
812import com .somemore .community .dto .request .CommunityCommentCreateRequestDto ;
913import com .somemore .community .dto .request .CommunityCommentUpdateRequestDto ;
14+ import com .somemore .community .repository .board .CommunityBoardRepository ;
1015import com .somemore .community .repository .comment .CommunityCommentRepository ;
11- import com .somemore .community .usecase .comment .CreateCommunityCommentUseCase ;
1216import com .somemore .global .exception .BadRequestException ;
13- import com .somemore .global .exception .ExceptionMessage ;
1417import org .assertj .core .api .ThrowableAssert ;
1518import org .junit .jupiter .api .AfterEach ;
1619import org .junit .jupiter .api .BeforeEach ;
@@ -26,23 +29,38 @@ class UpdateCommunityCommentServiceTest extends IntegrationTestSupport {
2629 @ Autowired
2730 private UpdateCommunityCommentService updateCommunityCommentService ;
2831 @ Autowired
29- private CreateCommunityCommentUseCase createCommunityCommentUseCase ;
30- @ Autowired
3132 private CommunityCommentRepository communityCommentRepository ;
33+ @ Autowired
34+ private CommunityBoardRepository communityBoardRepository ;
3235
3336 private UUID writerId ;
3437 private Long commentId ;
38+ private CommunityCommentUpdateRequestDto updateRequestDto ;
3539
3640 @ BeforeEach
3741 void setUp () {
38- CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto .builder ()
42+ CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto .builder ()
43+ .title ("커뮤니티 테스트 제목" )
44+ .content ("커뮤니티 테스트 내용" )
45+ .build ();
46+
47+ writerId = UUID .randomUUID ();
48+
49+ CommunityBoard communityBoard = communityBoardRepository .save (boardDto .toEntity (writerId , "https://test.image/123" ));
50+
51+ CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto .builder ()
52+ .communityBoardId (communityBoard .getId ())
3953 .content ("커뮤니티 댓글 테스트 내용" )
4054 .parentCommentId (null )
4155 .build ();
4256
43- writerId = UUID .randomUUID ();
57+ CommunityComment communityComment = communityCommentRepository .save (commentDto .toEntity (writerId ));
58+
59+ commentId = communityComment .getId ();
4460
45- commentId = createCommunityCommentUseCase .createCommunityComment (dto , writerId );
61+ updateRequestDto = CommunityCommentUpdateRequestDto .builder ()
62+ .content ("수정한 커뮤니티 댓글 내용" )
63+ .build ();
4664 }
4765
4866 @ AfterEach
@@ -53,13 +71,10 @@ void tearDown() {
5371 @ DisplayName ("댓글을 수정한다." )
5472 @ Test
5573 void updateCommunityComment () {
56- //given
57- CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto .builder ()
58- .content ("수정한 커뮤니티 댓글 내용" )
59- .build ();
6074
75+ //given
6176 //when
62- updateCommunityCommentService .updateCommunityComment (dto , commentId , writerId );
77+ updateCommunityCommentService .updateCommunityComment (updateRequestDto , commentId , writerId );
6378
6479 //then
6580 Optional <CommunityComment > communityComment = communityCommentRepository .findById (commentId );
@@ -71,17 +86,29 @@ void updateCommunityComment() {
7186 @ DisplayName ("작성자가 아닌 id로 댓글을 수정하고자 할 때 예외를 던진다." )
7287 @ Test
7388 void updateCommunityCommentWithNotWriterId () {
89+
7490 //given
75- CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto .builder ()
76- .content ("수정한 커뮤니티 댓글 내용" )
77- .build ();
91+ //when
92+ ThrowableAssert .ThrowingCallable callable = () -> updateCommunityCommentService .updateCommunityComment (updateRequestDto , commentId , UUID .randomUUID ());
7893
94+ //then
95+ assertThatExceptionOfType (BadRequestException .class )
96+ .isThrownBy (callable )
97+ .withMessage (UNAUTHORIZED_COMMUNITY_COMMENT .getMessage ());
98+ }
99+
100+ @ DisplayName ("삭제된 게시글의 댓글을 수정하고자 할 때 예외를 던진다." )
101+ @ Test
102+ void updateCommunityCommentWithDeletedBoardId () {
103+
104+ //given
105+ communityBoardRepository .deleteAllInBatch ();
79106 //when
80- ThrowableAssert .ThrowingCallable callable = () -> updateCommunityCommentService .updateCommunityComment (dto , commentId , UUID . randomUUID () );
107+ ThrowableAssert .ThrowingCallable callable = () -> updateCommunityCommentService .updateCommunityComment (updateRequestDto , commentId , writerId );
81108
82109 //then
83110 assertThatExceptionOfType (BadRequestException .class )
84111 .isThrownBy (callable )
85- .withMessage (ExceptionMessage . UNAUTHORIZED_COMMUNITY_COMMENT .getMessage ());
112+ .withMessage (NOT_EXISTS_COMMUNITY_BOARD .getMessage ());
86113 }
87114}
0 commit comments