From 2f337056567a8289b273d6ca5a012e24b4206880 Mon Sep 17 00:00:00 2001 From: kong-hana01 <79015120+kong-hana01@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:05:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=EA=B3=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=EB=A5=BC=20=EB=B6=84=EB=A6=AC=20(#460)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: comment 내에서 member의 객체 참조 -> id 참조로 변경 * test: 커버링 인덱스 성능 테스트를 위한 쿼리 케이스 생성 - 2번: 댓글에 커버링 인덱스 적용 - 3번: 서브쿼리를 활용해 댓글에 커버링 인덱스 적용 - 4번: 댓글에 커버링 인덱스 적용 후 쓰레드 풀을 사용해 댓글, 멤버에 조회 * refactor: 사용하지 않는 쿼리 및 기능 삭제 * chore: 인덱스 추가 * test: 사용하지 않는 테스트 삭제 * refactor: comment 패키지 생성 및 의존성 분리 * test: comment 필드 변경으로 불필요한 테스트 로직 제거 * refactor: dto를 interface로 프로젝션하도록 변경 --- .../controller/CommentController.java | 11 ++--- .../comment => comment/domain}/Comment.java | 15 +++---- .../repository/CommentRepository.java | 18 ++++---- .../comment/repository/dto/CommentMember.java | 13 ++++++ .../{ => comment}/service/CommentService.java | 43 +++++++++---------- .../service/dto/CommentRequest.java | 2 +- .../comment/service/dto/CommentResponse.java | 38 ++++++++++++++++ .../service/dto/CommentsResponse.java | 2 +- .../repository/MemberRepository.java | 7 +++ .../repository/dto/MemberNickname.java | 9 ++++ .../service/dto/CommentResponse.java | 25 ----------- backend/src/main/resources/application.yml | 8 ++++ ...__create_index_room_id_id_from_comment.sql | 1 + .../digginroom/digginroom/TestFixture.java | 2 +- .../controller/CommentControllerTest.java | 34 +++++---------- .../digginroom/controller/ControllerTest.java | 1 - .../controller/RoomControllerTest.java | 8 +--- .../repository/CommentRepositoryTest.java | 24 +++++------ .../service/CommentServiceTest.java | 30 ++++++------- 19 files changed, 157 insertions(+), 134 deletions(-) rename backend/src/main/java/com/digginroom/digginroom/{ => comment}/controller/CommentController.java (87%) rename backend/src/main/java/com/digginroom/digginroom/{domain/comment => comment/domain}/Comment.java (62%) rename backend/src/main/java/com/digginroom/digginroom/{ => comment}/repository/CommentRepository.java (61%) create mode 100644 backend/src/main/java/com/digginroom/digginroom/comment/repository/dto/CommentMember.java rename backend/src/main/java/com/digginroom/digginroom/{ => comment}/service/CommentService.java (71%) rename backend/src/main/java/com/digginroom/digginroom/{ => comment}/service/dto/CommentRequest.java (84%) create mode 100644 backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentResponse.java rename backend/src/main/java/com/digginroom/digginroom/{ => comment}/service/dto/CommentsResponse.java (62%) create mode 100644 backend/src/main/java/com/digginroom/digginroom/repository/dto/MemberNickname.java delete mode 100644 backend/src/main/java/com/digginroom/digginroom/service/dto/CommentResponse.java create mode 100644 backend/src/main/resources/db/migration/V6.4__create_index_room_id_id_from_comment.sql diff --git a/backend/src/main/java/com/digginroom/digginroom/controller/CommentController.java b/backend/src/main/java/com/digginroom/digginroom/comment/controller/CommentController.java similarity index 87% rename from backend/src/main/java/com/digginroom/digginroom/controller/CommentController.java rename to backend/src/main/java/com/digginroom/digginroom/comment/controller/CommentController.java index c66460dca..db862d8ae 100644 --- a/backend/src/main/java/com/digginroom/digginroom/controller/CommentController.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/controller/CommentController.java @@ -1,12 +1,13 @@ -package com.digginroom.digginroom.controller; +package com.digginroom.digginroom.comment.controller; import static org.springframework.http.HttpStatus.CREATED; import static org.springframework.http.HttpStatus.OK; -import com.digginroom.digginroom.service.CommentService; -import com.digginroom.digginroom.service.dto.CommentRequest; -import com.digginroom.digginroom.service.dto.CommentResponse; -import com.digginroom.digginroom.service.dto.CommentsResponse; +import com.digginroom.digginroom.controller.Auth; +import com.digginroom.digginroom.comment.service.CommentService; +import com.digginroom.digginroom.comment.service.dto.CommentRequest; +import com.digginroom.digginroom.comment.service.dto.CommentResponse; +import com.digginroom.digginroom.comment.service.dto.CommentsResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; diff --git a/backend/src/main/java/com/digginroom/digginroom/domain/comment/Comment.java b/backend/src/main/java/com/digginroom/digginroom/comment/domain/Comment.java similarity index 62% rename from backend/src/main/java/com/digginroom/digginroom/domain/comment/Comment.java rename to backend/src/main/java/com/digginroom/digginroom/comment/domain/Comment.java index 708c0a6ac..c9999d650 100644 --- a/backend/src/main/java/com/digginroom/digginroom/domain/comment/Comment.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/domain/Comment.java @@ -1,11 +1,9 @@ -package com.digginroom.digginroom.domain.comment; +package com.digginroom.digginroom.comment.domain; import com.digginroom.digginroom.domain.BaseEntity; -import com.digginroom.digginroom.domain.member.Member; import com.digginroom.digginroom.exception.CommentException.NotOwnerException; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; @@ -20,17 +18,16 @@ public class Comment extends BaseEntity { private Long roomId; @Column(length = 500) private String comment; - @ManyToOne - private Member member; + private Long memberId; - public void updateComment(final String comment, final Member member) { - if (!isOwner(member)) { + public void updateComment(final String comment, final Long memberId) { + if (!isOwner(memberId)) { throw new NotOwnerException(); } this.comment = comment; } - public boolean isOwner(final Member member) { - return this.member.equals(member); + public boolean isOwner(final Long memberId) { + return this.memberId.equals(memberId); } } diff --git a/backend/src/main/java/com/digginroom/digginroom/repository/CommentRepository.java b/backend/src/main/java/com/digginroom/digginroom/comment/repository/CommentRepository.java similarity index 61% rename from backend/src/main/java/com/digginroom/digginroom/repository/CommentRepository.java rename to backend/src/main/java/com/digginroom/digginroom/comment/repository/CommentRepository.java index c3013d604..ca26150fd 100644 --- a/backend/src/main/java/com/digginroom/digginroom/repository/CommentRepository.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/repository/CommentRepository.java @@ -1,6 +1,7 @@ -package com.digginroom.digginroom.repository; +package com.digginroom.digginroom.comment.repository; -import com.digginroom.digginroom.domain.comment.Comment; +import com.digginroom.digginroom.comment.domain.Comment; +import com.digginroom.digginroom.comment.repository.dto.CommentMember; import com.digginroom.digginroom.exception.CommentException.NotFoundException; import java.util.Optional; import org.springframework.data.domain.Pageable; @@ -17,14 +18,15 @@ default Comment getCommentById(final Long id) { return findCommentById(id).orElseThrow(NotFoundException::new); } - @Query("SELECT c " - + "FROM Comment c " - + "JOIN FETCH c.member " + @Query("SELECT new com.digginroom.digginroom.comment.repository.dto.CommentMember(" + + "c.id, c.comment, c.createdAt, c.updatedAt, m.id, m.nickname.nickname" + + ") " + + "FROM Comment c JOIN Member m " + + "ON c.memberId = m.id " + "WHERE c.roomId = :roomId " + "AND c.id < :lastCommentId " - + "ORDER BY c.id DESC " - ) - Slice getCommentsByCursor( + + "ORDER BY c.id DESC ") + Slice getCommentsByCursor( @Param("roomId") final Long roomId, @Param("lastCommentId") final Long lastCommentId, final Pageable pageable diff --git a/backend/src/main/java/com/digginroom/digginroom/comment/repository/dto/CommentMember.java b/backend/src/main/java/com/digginroom/digginroom/comment/repository/dto/CommentMember.java new file mode 100644 index 000000000..e85bb43e3 --- /dev/null +++ b/backend/src/main/java/com/digginroom/digginroom/comment/repository/dto/CommentMember.java @@ -0,0 +1,13 @@ +package com.digginroom.digginroom.comment.repository.dto; + +import java.time.LocalDateTime; + +public record CommentMember( + Long id, + String comment, + LocalDateTime createdAt, + LocalDateTime updatedAt, + Long memberId, + String nickname +) { +} diff --git a/backend/src/main/java/com/digginroom/digginroom/service/CommentService.java b/backend/src/main/java/com/digginroom/digginroom/comment/service/CommentService.java similarity index 71% rename from backend/src/main/java/com/digginroom/digginroom/service/CommentService.java rename to backend/src/main/java/com/digginroom/digginroom/comment/service/CommentService.java index bfa106705..e0be7b1d2 100644 --- a/backend/src/main/java/com/digginroom/digginroom/service/CommentService.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/service/CommentService.java @@ -1,18 +1,19 @@ -package com.digginroom.digginroom.service; +package com.digginroom.digginroom.comment.service; import static com.digginroom.digginroom.exception.CommentException.InvalidCommentSizeException; import static com.digginroom.digginroom.exception.CommentException.NotOwnerException; -import com.digginroom.digginroom.domain.comment.Comment; -import com.digginroom.digginroom.domain.member.Member; +import com.digginroom.digginroom.comment.domain.Comment; +import com.digginroom.digginroom.comment.repository.CommentRepository; +import com.digginroom.digginroom.comment.repository.dto.CommentMember; +import com.digginroom.digginroom.comment.service.dto.CommentRequest; +import com.digginroom.digginroom.comment.service.dto.CommentResponse; +import com.digginroom.digginroom.comment.service.dto.CommentsResponse; import com.digginroom.digginroom.exception.CommentException.InvalidLastCommentIdException; import com.digginroom.digginroom.exception.RoomException.NotFoundException; -import com.digginroom.digginroom.repository.CommentRepository; import com.digginroom.digginroom.repository.MemberRepository; import com.digginroom.digginroom.repository.RoomRepository; -import com.digginroom.digginroom.service.dto.CommentRequest; -import com.digginroom.digginroom.service.dto.CommentResponse; -import com.digginroom.digginroom.service.dto.CommentsResponse; +import com.digginroom.digginroom.repository.dto.MemberNickname; import java.util.Objects; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; @@ -40,16 +41,13 @@ public CommentsResponse getRoomComments( Long resolvedLastCommentId = getLastCommentId(lastCommentId); validateLastCommentId(resolvedLastCommentId); validateCommentSize(size); - - Member member = memberRepository.getMemberById(memberId); - Slice comments = commentRepository.getCommentsByCursor( + Slice commentMembers = commentRepository.getCommentsByCursor( roomId, resolvedLastCommentId, PageRequest.of(DEFAULT_PAGE_SIZE, size) ); - - return new CommentsResponse(comments.getContent().stream() - .map(comment -> CommentResponse.of(comment, comment.isOwner(member))) + return new CommentsResponse(commentMembers.getContent().stream() + .map(commentMember -> CommentResponse.of(commentMember, memberId)) .toList()); } @@ -74,12 +72,12 @@ private Long getLastCommentId(final Long lastCommentId) { public CommentResponse comment(final Long roomId, final Long memberId, final CommentRequest request) { validateExistRoom(roomId); - Member member = memberRepository.getMemberById(memberId); + MemberNickname memberNickname = memberRepository.getMemberNickname(memberId); - Comment comment = new Comment(roomId, request.comment(), member); + Comment comment = new Comment(roomId, request.comment(), memberId); commentRepository.save(comment); - return CommentResponse.of(comment, comment.isOwner(member)); + return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname()); } public void validateExistRoom(final Long roomId) { @@ -89,15 +87,14 @@ public void validateExistRoom(final Long roomId) { } public void delete(final Long memberId, final Long commentId) { - Member member = memberRepository.getMemberById(memberId); Comment comment = commentRepository.getCommentById(commentId); - validateSameOwner(member, comment); + validateSameOwner(memberId, comment); commentRepository.delete(comment); } - private void validateSameOwner(final Member member, final Comment comment) { - if (!comment.isOwner(member)) { + private void validateSameOwner(final Long memberId, final Comment comment) { + if (!comment.isOwner(memberId)) { throw new NotOwnerException(); } } @@ -107,9 +104,9 @@ public CommentResponse update( final Long commentId, final CommentRequest request ) { - Member member = memberRepository.getMemberById(memberId); + MemberNickname memberNickname = memberRepository.getMemberNickname(memberId); Comment comment = commentRepository.getCommentById(commentId); - comment.updateComment(request.comment(), member); - return CommentResponse.of(comment, comment.isOwner(member)); + comment.updateComment(request.comment(), memberId); + return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname()); } } diff --git a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentRequest.java b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentRequest.java similarity index 84% rename from backend/src/main/java/com/digginroom/digginroom/service/dto/CommentRequest.java rename to backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentRequest.java index f1d59014f..0b403274d 100644 --- a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentRequest.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentRequest.java @@ -1,4 +1,4 @@ -package com.digginroom.digginroom.service.dto; +package com.digginroom.digginroom.comment.service.dto; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; diff --git a/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentResponse.java b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentResponse.java new file mode 100644 index 000000000..71a2e9e07 --- /dev/null +++ b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentResponse.java @@ -0,0 +1,38 @@ +package com.digginroom.digginroom.comment.service.dto; + +import com.digginroom.digginroom.comment.domain.Comment; +import com.digginroom.digginroom.comment.repository.dto.CommentMember; +import java.time.LocalDateTime; +import java.util.Objects; + +public record CommentResponse( + Long id, + String writer, + String comment, + LocalDateTime createdAt, + LocalDateTime updatedAt, + boolean isOwner +) { + + public static CommentResponse of(final CommentMember commentMember, final Long memberId) { + return new CommentResponse( + commentMember.id(), + commentMember.nickname(), + commentMember.comment(), + commentMember.createdAt(), + commentMember.updatedAt(), + Objects.equals(commentMember.memberId(), memberId) + ); + } + + public static CommentResponse of(final Comment comment, boolean isOwner, String nickname) { + return new CommentResponse( + comment.getId(), + nickname, + comment.getComment(), + comment.getCreatedAt(), + comment.getUpdatedAt(), + isOwner + ); + } +} diff --git a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentsResponse.java b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentsResponse.java similarity index 62% rename from backend/src/main/java/com/digginroom/digginroom/service/dto/CommentsResponse.java rename to backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentsResponse.java index d9d19aa4c..3ddf62e9b 100644 --- a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentsResponse.java +++ b/backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentsResponse.java @@ -1,4 +1,4 @@ -package com.digginroom.digginroom.service.dto; +package com.digginroom.digginroom.comment.service.dto; import java.util.List; diff --git a/backend/src/main/java/com/digginroom/digginroom/repository/MemberRepository.java b/backend/src/main/java/com/digginroom/digginroom/repository/MemberRepository.java index ed6b97438..0c6a5f76e 100644 --- a/backend/src/main/java/com/digginroom/digginroom/repository/MemberRepository.java +++ b/backend/src/main/java/com/digginroom/digginroom/repository/MemberRepository.java @@ -2,6 +2,7 @@ import com.digginroom.digginroom.domain.member.Member; import com.digginroom.digginroom.exception.MemberException.NotFoundException; +import com.digginroom.digginroom.repository.dto.MemberNickname; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; @@ -18,4 +19,10 @@ default Member getMemberByUsername(final String username) { default Member getMemberById(final Long id) { return findById(id).orElseThrow(NotFoundException::new); } + + Optional findMemberNicknameById(final Long id); + + default MemberNickname getMemberNickname(final Long id) { + return findMemberNicknameById(id).orElseThrow(NotFoundException::new); + } } diff --git a/backend/src/main/java/com/digginroom/digginroom/repository/dto/MemberNickname.java b/backend/src/main/java/com/digginroom/digginroom/repository/dto/MemberNickname.java new file mode 100644 index 000000000..884d2ef3c --- /dev/null +++ b/backend/src/main/java/com/digginroom/digginroom/repository/dto/MemberNickname.java @@ -0,0 +1,9 @@ +package com.digginroom.digginroom.repository.dto; + +import org.springframework.beans.factory.annotation.Value; + +public interface MemberNickname { + + @Value("#{target.nickname}") + String getNickname(); +} diff --git a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentResponse.java b/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentResponse.java deleted file mode 100644 index 7efbce8c9..000000000 --- a/backend/src/main/java/com/digginroom/digginroom/service/dto/CommentResponse.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.digginroom.digginroom.service.dto; - -import com.digginroom.digginroom.domain.comment.Comment; -import java.time.LocalDateTime; - -public record CommentResponse( - Long id, - String writer, - String comment, - LocalDateTime createdAt, - LocalDateTime updatedAt, - boolean isOwner -) { - - public static CommentResponse of(final Comment comment, boolean isOwner) { - return new CommentResponse( - comment.getId(), - comment.getMember().getNickname(), - comment.getComment(), - comment.getCreatedAt(), - comment.getUpdatedAt(), - isOwner - ); - } -} diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index b22a0b4f0..490048a53 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -14,6 +14,9 @@ spring: enabled: true baseline-on-migrate: true baseline-version: 5 + datasource: + hikari: + maximum-pool-size: 44 springdoc: api-docs: path: /docs @@ -38,3 +41,8 @@ management: base-path: /manage info: version: '1.0.0' +server: + tomcat: + threads: + max: 16 + min-spare: 16 diff --git a/backend/src/main/resources/db/migration/V6.4__create_index_room_id_id_from_comment.sql b/backend/src/main/resources/db/migration/V6.4__create_index_room_id_id_from_comment.sql new file mode 100644 index 000000000..36e6a08d0 --- /dev/null +++ b/backend/src/main/resources/db/migration/V6.4__create_index_room_id_id_from_comment.sql @@ -0,0 +1 @@ +CREATE INDEX COMMENT_ROOM_ID_COMMENT_ID_IDX ON comment (room_id, id desc); diff --git a/backend/src/test/java/com/digginroom/digginroom/TestFixture.java b/backend/src/test/java/com/digginroom/digginroom/TestFixture.java index c35eb294c..8801f7ccd 100644 --- a/backend/src/test/java/com/digginroom/digginroom/TestFixture.java +++ b/backend/src/test/java/com/digginroom/digginroom/TestFixture.java @@ -11,7 +11,7 @@ import com.digginroom.digginroom.oauth.jwk.KakaoJwkProvider; import com.digginroom.digginroom.oauth.jwk.ThirdPartyJwkProviders; import com.digginroom.digginroom.oauth.payload.IdTokenPayload; -import com.digginroom.digginroom.service.dto.CommentRequest; +import com.digginroom.digginroom.comment.service.dto.CommentRequest; import com.digginroom.digginroom.service.dto.MemberLoginRequest; import com.digginroom.digginroom.service.dto.MemberSaveRequest; import com.digginroom.digginroom.util.DigginRoomPasswordEncoder; diff --git a/backend/src/test/java/com/digginroom/digginroom/controller/CommentControllerTest.java b/backend/src/test/java/com/digginroom/digginroom/controller/CommentControllerTest.java index 67a8884a6..321d2084e 100644 --- a/backend/src/test/java/com/digginroom/digginroom/controller/CommentControllerTest.java +++ b/backend/src/test/java/com/digginroom/digginroom/controller/CommentControllerTest.java @@ -2,20 +2,17 @@ import static com.digginroom.digginroom.TestFixture.COMMENT_UPDATE_REQUEST; import static com.digginroom.digginroom.TestFixture.나무; -import static com.digginroom.digginroom.TestFixture.파워; import static org.assertj.core.api.Assertions.assertThat; import com.digginroom.digginroom.TestFixture; +import com.digginroom.digginroom.comment.domain.Comment; +import com.digginroom.digginroom.comment.repository.CommentRepository; +import com.digginroom.digginroom.comment.service.dto.CommentRequest; +import com.digginroom.digginroom.comment.service.dto.CommentResponse; +import com.digginroom.digginroom.comment.service.dto.CommentsResponse; import com.digginroom.digginroom.controller.mock.MockLoginServer; -import com.digginroom.digginroom.domain.comment.Comment; -import com.digginroom.digginroom.domain.member.Member; import com.digginroom.digginroom.domain.room.Room; -import com.digginroom.digginroom.repository.CommentRepository; -import com.digginroom.digginroom.repository.MemberRepository; import com.digginroom.digginroom.repository.RoomRepository; -import com.digginroom.digginroom.service.dto.CommentRequest; -import com.digginroom.digginroom.service.dto.CommentResponse; -import com.digginroom.digginroom.service.dto.CommentsResponse; import io.restassured.RestAssured; import io.restassured.http.ContentType; import java.util.List; @@ -32,19 +29,16 @@ @SuppressWarnings("NonAsciiCharacters") class CommentControllerTest extends ControllerTest { - private MemberRepository memberRepository; private RoomRepository roomRepository; private CommentRepository commentRepository; private MockLoginServer mockLoginServer; private Room 나무; @Autowired - public CommentControllerTest(MemberRepository memberRepository, - RoomRepository roomRepository, + public CommentControllerTest(RoomRepository roomRepository, CommentRepository commentRepository, ObjectProvider mockLoginServerObjectProvider ) { - this.memberRepository = memberRepository; this.roomRepository = roomRepository; this.commentRepository = commentRepository; this.mockLoginServer = mockLoginServerObjectProvider.getObject(); @@ -143,10 +137,8 @@ void setUp() { @Test void 유저가_마지막으로_본_댓글과_볼_댓글_개수를_전달하지_않으면_최신_댓글_10개의_댓글들을_볼_수_있다() { - Member 파워 = memberRepository.save(파워()); - for (int i = 1; i <= 15; i++) { - commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워)); + commentRepository.save(new Comment(나무.getId(), "댓글" + i, 1L)); } String cookie = login(port); @@ -170,10 +162,8 @@ void setUp() { @Test void 유저가_마지막으로_본_댓글을_전달하면_더_이전에_작성한_룸의_댓글들을_볼_수_있다() { - Member 파워 = memberRepository.save(파워()); - for (int i = 1; i <= 15; i++) { - commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워)); + commentRepository.save(new Comment(나무.getId(), "댓글" + i, 1L)); } String cookie = login(port); @@ -195,10 +185,8 @@ void setUp() { @Test void 유저가_볼_댓글_개수를_전달하면_최대_전달한_개수만큼의_룸의_댓글들을_볼_수_있다() { - Member 파워 = memberRepository.save(파워()); - for (int i = 1; i <= 15; i++) { - commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워)); + commentRepository.save(new Comment(나무.getId(), "댓글" + i, 1L)); } String cookie = login(port); @@ -220,10 +208,8 @@ void setUp() { @Test void 유저가_마지막으로_본_댓글과_볼_댓글_개수를_전달하면_더_이전에_작성한_댓글들을_최대_전달한_개수만큼_볼_수_있다() { - Member 파워 = memberRepository.save(파워()); - for (int i = 1; i <= 15; i++) { - commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워)); + commentRepository.save(new Comment(나무.getId(), "댓글" + i, 1L)); } String cookie = login(port); diff --git a/backend/src/test/java/com/digginroom/digginroom/controller/ControllerTest.java b/backend/src/test/java/com/digginroom/digginroom/controller/ControllerTest.java index ae06e5360..8c791c767 100644 --- a/backend/src/test/java/com/digginroom/digginroom/controller/ControllerTest.java +++ b/backend/src/test/java/com/digginroom/digginroom/controller/ControllerTest.java @@ -32,4 +32,3 @@ void setUp() { RestAssured.port = port; } } - diff --git a/backend/src/test/java/com/digginroom/digginroom/controller/RoomControllerTest.java b/backend/src/test/java/com/digginroom/digginroom/controller/RoomControllerTest.java index 48e85f0ab..00754ec3c 100644 --- a/backend/src/test/java/com/digginroom/digginroom/controller/RoomControllerTest.java +++ b/backend/src/test/java/com/digginroom/digginroom/controller/RoomControllerTest.java @@ -2,14 +2,12 @@ import static com.digginroom.digginroom.TestFixture.나무; import static com.digginroom.digginroom.TestFixture.차이; -import static com.digginroom.digginroom.TestFixture.파워; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.emptyCollectionOf; import static org.hamcrest.Matchers.hasSize; import com.digginroom.digginroom.controller.mock.MockLoginServer; import com.digginroom.digginroom.domain.room.Room; -import com.digginroom.digginroom.repository.MemberRepository; import com.digginroom.digginroom.repository.RoomRepository; import com.digginroom.digginroom.service.dto.RoomRequest; import com.digginroom.digginroom.service.dto.RoomResponse; @@ -28,18 +26,15 @@ @SuppressWarnings("NonAsciiCharacters") class RoomControllerTest extends ControllerTest { - private MemberRepository memberRepository; private RoomRepository roomRepository; private MockLoginServer mockLoginServer; private Room room1; private Room room2; @Autowired - public RoomControllerTest(MemberRepository memberRepository, - RoomRepository roomRepository, + public RoomControllerTest(RoomRepository roomRepository, ObjectProvider mockLoginServerObjectProvider ) { - this.memberRepository = memberRepository; this.roomRepository = roomRepository; this.mockLoginServer = mockLoginServerObjectProvider.getObject(); } @@ -48,7 +43,6 @@ public RoomControllerTest(MemberRepository memberRepository, @BeforeEach void setUp() { super.setUp(); - memberRepository.save(파워()); room1 = roomRepository.save(나무()); room2 = roomRepository.save(차이()); } diff --git a/backend/src/test/java/com/digginroom/digginroom/repository/CommentRepositoryTest.java b/backend/src/test/java/com/digginroom/digginroom/repository/CommentRepositoryTest.java index 88617d071..0d545fb86 100644 --- a/backend/src/test/java/com/digginroom/digginroom/repository/CommentRepositoryTest.java +++ b/backend/src/test/java/com/digginroom/digginroom/repository/CommentRepositoryTest.java @@ -5,18 +5,16 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import com.digginroom.digginroom.domain.BaseEntity; -import com.digginroom.digginroom.domain.comment.Comment; +import com.digginroom.digginroom.comment.domain.Comment; +import com.digginroom.digginroom.comment.repository.CommentRepository; import com.digginroom.digginroom.domain.member.Member; import com.digginroom.digginroom.exception.CommentException; +import com.digginroom.digginroom.comment.repository.dto.CommentMember; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import java.util.List; import java.util.stream.LongStream; import org.junit.jupiter.api.BeforeEach; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import com.digginroom.digginroom.exception.CommentException; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -59,7 +57,7 @@ void init() { private void 멤버가_룸에_댓글을_여러_개를_저장_한다(final long 룸ID, final Member 멤버, final int 저장개수) { for (int index = 0; index < 저장개수; index++) { - Comment comment = new Comment(룸ID, "댓글입니다.", 멤버); + Comment comment = new Comment(룸ID, "댓글입니다.", 멤버.getId()); em.persist(comment); } } @@ -74,11 +72,11 @@ void init() { void 마지막으로_본_댓글이_주어지고_size_가_10인_경우_해당_룸의_댓글_ID_부터_최신_댓글_부터_10개를_반환한다() { PageRequest pageRequest = PageRequest.of(0, 10); - Slice comments = commentRepository.getCommentsByCursor(1L, 15L, pageRequest); + Slice comments = commentRepository.getCommentsByCursor(1L, 15L, pageRequest); assertThat(comments.getContent()) .hasSize(10) - .map(BaseEntity::getId) + .map(CommentMember::id) .usingRecursiveComparison() .isEqualTo(getReversedList(5, 14)); } @@ -94,11 +92,11 @@ private List getReversedList(final int startIndex, final int endIndex) { void 마지막으로_본_댓글이_주어지고_size_가_2인_경우_해당_룸의_댓글_ID_부터_최신_댓글_부터_2개를_반환한다() { PageRequest pageRequest = PageRequest.of(0, 2); - Slice comments = commentRepository.getCommentsByCursor(1L, 15L, pageRequest); + Slice comments = commentRepository.getCommentsByCursor(1L, 15L, pageRequest); assertThat(comments.getContent()) .hasSize(2) - .map(BaseEntity::getId) + .map(CommentMember::id) .usingRecursiveComparison() .isEqualTo(getReversedList(13, 14)); } @@ -107,11 +105,11 @@ private List getReversedList(final int startIndex, final int endIndex) { void 마지막으로_본_댓글이_주어졌을_때_size_가_10이고_남은_댓글이_10개_이하인_경우_남은_댓글만_반환한다() { PageRequest pageRequest = PageRequest.of(0, 10); - Slice comments = commentRepository.getCommentsByCursor(1L, 5L, pageRequest); + Slice comments = commentRepository.getCommentsByCursor(1L, 5L, pageRequest); assertThat(comments.getContent()) .hasSize(4) - .map(BaseEntity::getId) + .map(CommentMember::id) .usingRecursiveComparison() .isEqualTo(getReversedList(1, 4)); } @@ -120,7 +118,7 @@ private List getReversedList(final int startIndex, final int endIndex) { void 마지막으로_본_댓글이_주어지고_룸에_그_이하의_댓글이_없는_경우_빈_결과를_반환한다() { PageRequest pageRequest = PageRequest.of(0, 10); - Slice comments = commentRepository.getCommentsByCursor(2L, 10L, pageRequest); + Slice comments = commentRepository.getCommentsByCursor(2L, 10L, pageRequest); assertThat(comments.getContent()).isEmpty(); } diff --git a/backend/src/test/java/com/digginroom/digginroom/service/CommentServiceTest.java b/backend/src/test/java/com/digginroom/digginroom/service/CommentServiceTest.java index 8b97398d9..35247f189 100644 --- a/backend/src/test/java/com/digginroom/digginroom/service/CommentServiceTest.java +++ b/backend/src/test/java/com/digginroom/digginroom/service/CommentServiceTest.java @@ -1,17 +1,17 @@ package com.digginroom.digginroom.service; -import static com.digginroom.digginroom.TestFixture.블랙캣; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.when; -import com.digginroom.digginroom.domain.comment.Comment; +import com.digginroom.digginroom.comment.service.CommentService; import com.digginroom.digginroom.exception.CommentException.InvalidCommentSizeException; import com.digginroom.digginroom.exception.CommentException.InvalidLastCommentIdException; -import com.digginroom.digginroom.repository.CommentRepository; -import com.digginroom.digginroom.repository.MemberRepository; -import com.digginroom.digginroom.service.dto.CommentResponse; -import com.digginroom.digginroom.service.dto.CommentsResponse; +import com.digginroom.digginroom.comment.repository.CommentRepository; +import com.digginroom.digginroom.comment.repository.dto.CommentMember; +import com.digginroom.digginroom.comment.service.dto.CommentResponse; +import com.digginroom.digginroom.comment.service.dto.CommentsResponse; +import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -30,18 +30,16 @@ class CommentServiceTest { @Mock private CommentRepository commentRepository; - @Mock - private MemberRepository memberRepository; @InjectMocks private CommentService commentService; @Test void 댓글을_무한_스크롤로_조회할_수_있다() { - Comment kong = new Comment(1L, "kong", 블랙캣()); - Comment blackCat = new Comment(1L, "blackCat", 블랙캣()); - List comments = List.of(blackCat, kong); + LocalDateTime now = LocalDateTime.now(); + CommentMember kong = new CommentMember(1L, "kong", now, now, 1L, "kong1"); + CommentMember blackCat = new CommentMember(1L, "blackCat", now, now, 1L, "blackCat"); + List comments = List.of(blackCat, kong); - when(memberRepository.getMemberById(1L)).thenReturn(블랙캣()); when(commentRepository.getCommentsByCursor(1L, 10L, PageRequest.of(0, 10))) .thenReturn(new PageImpl<>(comments)); @@ -55,11 +53,11 @@ class CommentServiceTest { @Test void 마지막으로_본_댓글이_없는_경우_Long_MAX_VALUE로_검색한다() { - Comment kong = new Comment(1L, "kong", 블랙캣()); - Comment blackCat = new Comment(1L, "blackCat", 블랙캣()); - List comments = List.of(blackCat, kong); + LocalDateTime now = LocalDateTime.now(); + CommentMember kong = new CommentMember(1L, "kong", now, now, 1L, "kong1"); + CommentMember blackCat = new CommentMember(1L, "blackCat", now, now, 1L, "blackCat"); + List comments = List.of(blackCat, kong); - when(memberRepository.getMemberById(1L)).thenReturn(블랙캣()); when(commentRepository.getCommentsByCursor(1L, Long.MAX_VALUE, PageRequest.of(0, 10))) .thenReturn(new PageImpl<>(comments));