From c7ac781d6bba2d596f16c0248e9676d4ea0d01d2 Mon Sep 17 00:00:00 2001 From: ybchar Date: Sun, 20 Oct 2024 18:23:35 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EB=B6=80=EB=AA=A8=20=EB=8C=93=EA=B8=80?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/CommentServiceTest.java | 95 ++++++++++++++++++- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/depromeet/stonebed/domain/comment/application/CommentServiceTest.java b/src/test/java/com/depromeet/stonebed/domain/comment/application/CommentServiceTest.java index d65d224..5f6040b 100644 --- a/src/test/java/com/depromeet/stonebed/domain/comment/application/CommentServiceTest.java +++ b/src/test/java/com/depromeet/stonebed/domain/comment/application/CommentServiceTest.java @@ -8,6 +8,8 @@ import com.depromeet.stonebed.domain.comment.domain.Comment; import com.depromeet.stonebed.domain.comment.dto.request.CommentCreateRequest; import com.depromeet.stonebed.domain.comment.dto.response.CommentCreateResponse; +import com.depromeet.stonebed.domain.comment.dto.response.CommentFindOneResponse; +import com.depromeet.stonebed.domain.comment.dto.response.CommentFindResponse; import com.depromeet.stonebed.domain.fcm.application.FcmNotificationService; import com.depromeet.stonebed.domain.fcm.dao.FcmTokenRepository; import com.depromeet.stonebed.domain.member.domain.Member; @@ -16,6 +18,7 @@ import com.depromeet.stonebed.global.util.MemberUtil; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -44,8 +47,9 @@ class CommentServiceTest extends FixtureMonkeySetUp { private static final int CHILD_COMMENT_COUNT = 5; // 자식 댓글 생성 횟수 + // 생성 @Test - void 부모_댓글_생성_성공() { + void 부모_댓글_생성합니다() { // given Long recordId = 1L; String content = "너무 이쁘자나~"; @@ -68,7 +72,7 @@ class CommentServiceTest extends FixtureMonkeySetUp { } @Test - void 자식_댓글_생성_성공() { + void 자식_댓글_생성합니다() { // given String content = "너무 이쁘자나~"; Member member = fixtureMonkey.giveMeOne(Member.class); @@ -103,7 +107,7 @@ class CommentServiceTest extends FixtureMonkeySetUp { } @Test - void 부모_댓글에_여러_자식_댓글_생성_성공() { + void 부모_댓글에_여러_자식_댓글을_생성합니다() { // given String content = "부모 댓글 내용"; Member member = fixtureMonkey.giveMeOne(Member.class); @@ -197,4 +201,89 @@ private Comment createMockComment( .set("parent", parent) .sample(); } + + // 조회 + @Test + void 부모_댓글을_조회합니다() { + // given + Long recordId = 1L; + String content = "너무 이쁘자나~"; + + Member member = fixtureMonkey.giveMeOne(Member.class); + MissionRecord missionRecord = fixtureMonkey.giveMeOne(MissionRecord.class); + CommentCreateRequest request = CommentCreateRequest.of(content, recordId, null); + Comment comment = createMockComment(member, missionRecord, content); // 부모 댓글 생성 + + mockCommonDependencies(member, missionRecord, recordId, comment); // 공통 모의 설정 + + // when: 부모 댓글 생성 + CommentCreateResponse response = commentService.createComment(request); + + // 부모 댓글 조회 + when(commentRepository.findById(response.commentId())).thenReturn(Optional.of(comment)); + + // 부모 댓글을 포함한 댓글 리스트 생성 + Comment parentComment = commentRepository.findById(response.commentId()).orElse(null); + List comments = List.of(Objects.requireNonNull(parentComment)); + + // 부모 댓글을 CommentFindOneResponse로 변환 + List commentResponses = + comments.stream() + .map( + comment1 -> + CommentFindOneResponse.of( + comment1.getParent() != null + ? comment1.getParent().getId() + : null, + comment1.getId(), + comment1.getContent(), + comment1.getWriter().getId(), + comment1.getWriter().getProfile().getNickname(), + comment1.getWriter() + .getProfile() + .getProfileImageUrl(), + comment1.getCreatedAt().toString(), + comment1.getReplyComments().stream() + .map( + reply -> + CommentFindOneResponse.of( + reply.getParent() + != null + ? reply.getParent() + .getId() + : null, + reply.getId(), + reply.getContent(), + reply.getWriter() + .getId(), + reply.getWriter() + .getProfile() + .getNickname(), + reply.getWriter() + .getProfile() + .getProfileImageUrl(), + reply.getCreatedAt() + .toString(), + List.of())) + .collect(Collectors.toList()))) + .toList(); + + // 모의 객체 설정 + when(missionRecordRepository.findById(recordId)).thenReturn(Optional.of(missionRecord)); + when(commentRepository.findAllCommentsByMissionRecord(missionRecord)).thenReturn(comments); + + // when: 댓글 조회 + CommentFindResponse result = commentService.findCommentsByRecordId(recordId); + + // then: 검증 + assertNotNull(result); + assertEquals(commentResponses.size(), result.comments().size()); // 댓글 개수 비교 + assertEquals( + commentResponses.get(0).commentId(), + result.comments().get(0).commentId()); // 첫 번째 댓글 ID 비교 + + assertEquals(commentResponses.get(0).content(), content); // 첫 번째 댓글 내용 비교 + assertEquals(result.comments().get(0).content(), content); + assertEquals(commentResponses.get(0).content(), result.comments().get(0).content()); + } }