Skip to content

Commit 493e63f

Browse files
committed
test(community): communityComment 수정 기능 테스트 작성
1 parent be132e1 commit 493e63f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.somemore.community.service.comment;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
6+
import com.somemore.IntegrationTestSupport;
7+
import com.somemore.community.domain.CommunityComment;
8+
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
9+
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
10+
import com.somemore.community.repository.comment.CommunityCommentRepository;
11+
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
12+
import com.somemore.global.exception.BadRequestException;
13+
import com.somemore.global.exception.ExceptionMessage;
14+
import org.assertj.core.api.ThrowableAssert;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
21+
import java.util.Optional;
22+
import java.util.UUID;
23+
24+
class UpdateCommunityCommentServiceTest extends IntegrationTestSupport {
25+
26+
@Autowired
27+
private UpdateCommunityCommentService updateCommunityCommentService;
28+
@Autowired
29+
private CreateCommunityCommentUseCase createCommunityCommentUseCase;
30+
@Autowired
31+
private CommunityCommentRepository communityCommentRepository;
32+
33+
private UUID writerId;
34+
private Long commentId;
35+
36+
@BeforeEach
37+
void setUp() {
38+
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
39+
.content("커뮤니티 댓글 테스트 내용")
40+
.parentCommentId(null)
41+
.build();
42+
43+
writerId = UUID.randomUUID();
44+
45+
commentId = createCommunityCommentUseCase.createCommunityComment(dto, writerId);
46+
}
47+
48+
@AfterEach
49+
void tearDown() {
50+
communityCommentRepository.deleteAllInBatch();
51+
}
52+
53+
@DisplayName("댓글을 수정한다.")
54+
@Test
55+
void updateCommunityComment() {
56+
//given
57+
CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto.builder()
58+
.content("수정한 커뮤니티 댓글 내용")
59+
.build();
60+
61+
//when
62+
updateCommunityCommentService.updateCommunityComment(dto, commentId, writerId);
63+
64+
//then
65+
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
66+
assertThat(communityComment).isPresent();
67+
assertThat(communityComment.get().getId()).isEqualTo(commentId);
68+
assertThat(communityComment.get().getContent()).isEqualTo("수정한 커뮤니티 댓글 내용");
69+
}
70+
71+
@DisplayName("작성자가 아닌 id로 댓글을 수정하고자 할 때 예외를 던진다.")
72+
@Test
73+
void updateCommunityCommentWithNotWriterId() {
74+
//given
75+
CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto.builder()
76+
.content("수정한 커뮤니티 댓글 내용")
77+
.build();
78+
79+
//when
80+
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(dto, commentId, UUID.randomUUID());
81+
82+
//then
83+
assertThatExceptionOfType(BadRequestException.class)
84+
.isThrownBy(callable)
85+
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
86+
}
87+
}

0 commit comments

Comments
 (0)