Skip to content

Commit 370911c

Browse files
committed
test(community): CommunityComment 삭제 기능 테스트 작성
1 parent 9ccd6ac commit 370911c

File tree

1 file changed

+88
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)