diff --git a/src/main/java/com/somemore/community/dto/response/CommunityBoardDetailResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityBoardDetailResponseDto.java new file mode 100644 index 000000000..22a9a28cd --- /dev/null +++ b/src/main/java/com/somemore/community/dto/response/CommunityBoardDetailResponseDto.java @@ -0,0 +1,40 @@ +package com.somemore.community.dto.response; + +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import com.somemore.community.domain.CommunityBoard; +import io.swagger.v3.oas.annotations.media.Schema; + +import java.time.LocalDateTime; +import java.util.UUID; + +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +@Schema(description = "커뮤니티 게시글 상세 조회 응답 DTO") +public record CommunityBoardDetailResponseDto( + @Schema(description = "커뮤니티 게시글 ID", example = "12") + Long id, + @Schema(description = "작성자(봉사자) ID", example = "123e4567-e89b-12d3-a456-426614174000") + UUID writerId, + @Schema(description = "커뮤니티 게시글 제목", example = "11/29 OO도서관 봉사 같이 갈 사람 모집합니다.") + String title, + @Schema(description = "커뮤니티 게시글 내용", example = "저 포함 5명이 같이 가면 좋을 거 같아요") + String content, + @Schema(description = "이미지 URL", example = "https://image.domain.com/links") + String imageUrl, + @Schema(description = "커뮤니티 게시글 생성 일시", example = "2023-12-02T11:00:00") + LocalDateTime createdAt, + @Schema(description = "커뮤니티 게시글 수정 일시", example = "2023-12-02T11:00:00") + LocalDateTime updatedAt +) { + public static CommunityBoardDetailResponseDto from(CommunityBoard board) { + return new CommunityBoardDetailResponseDto( + board.getId(), + board.getWriterId(), + board.getTitle(), + board.getContent(), + board.getImgUrl(), + board.getCreatedAt(), + board.getUpdatedAt() + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetDetailResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityBoardGetDetailResponseDto.java deleted file mode 100644 index b9a0f69f4..000000000 --- a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetDetailResponseDto.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.somemore.community.dto.response; - -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.databind.annotation.JsonNaming; -import com.somemore.community.domain.CommunityBoard; - -import java.time.LocalDateTime; -import java.util.UUID; - -@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) -public record CommunityBoardGetDetailResponseDto( - Long id, - UUID writerId, - String title, - String content, - String imageUrl, - LocalDateTime createdAt, - LocalDateTime updatedAt -) { - public static CommunityBoardGetDetailResponseDto fromEntity(CommunityBoard board) { - return new CommunityBoardGetDetailResponseDto( - board.getId(), - board.getWriterId(), - board.getTitle(), - board.getContent(), - board.getImgUrl(), - board.getCreatedAt(), - board.getUpdatedAt() - ); - } -} \ No newline at end of file diff --git a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java deleted file mode 100644 index ba15f67ee..000000000 --- a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.somemore.community.dto.response; - -import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; -import com.somemore.community.repository.mapper.CommunityBoardView; - -import java.time.LocalDateTime; - -@JsonNaming(SnakeCaseStrategy.class) -public record CommunityBoardGetResponseDto( - Long id, - String title, - String writerNickname, - LocalDateTime createdAt -) { - public static CommunityBoardGetResponseDto fromEntity(CommunityBoardView board) { - return new CommunityBoardGetResponseDto( - board.communityBoard().getId(), - board.communityBoard().getTitle(), - board.writerNickname(), - board.communityBoard().getCreatedAt() - ); - } -} - diff --git a/src/main/java/com/somemore/community/dto/response/CommunityBoardResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityBoardResponseDto.java new file mode 100644 index 000000000..c06365181 --- /dev/null +++ b/src/main/java/com/somemore/community/dto/response/CommunityBoardResponseDto.java @@ -0,0 +1,31 @@ +package com.somemore.community.dto.response; + +import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import com.somemore.community.repository.mapper.CommunityBoardView; +import io.swagger.v3.oas.annotations.media.Schema; + +import java.time.LocalDateTime; + +@JsonNaming(SnakeCaseStrategy.class) +@Schema(description = "커뮤니티 게시글 목록 조회 응답 DTO") +public record CommunityBoardResponseDto( + @Schema(description = "커뮤니티 게시글 ID", example = "12") + Long id, + @Schema(description = "커뮤니티 게시글 ID", example = "12") + String title, + @Schema(description = "작성자(봉사자) ID", example = "123e4567-e89b-12d3-a456-426614174000") + String writerNickname, + @Schema(description = "커뮤니티 게시글 생성 일시", example = "2023-12-02T11:00:00") + LocalDateTime createdAt +) { + public static CommunityBoardResponseDto from(CommunityBoardView board) { + return new CommunityBoardResponseDto( + board.communityBoard().getId(), + board.communityBoard().getTitle(), + board.writerNickname(), + board.communityBoard().getCreatedAt() + ); + } +} + diff --git a/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java index 78731c7be..19983bb24 100644 --- a/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java +++ b/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; import com.somemore.community.repository.mapper.CommunityCommentView; +import io.swagger.v3.oas.annotations.media.Schema; import java.time.LocalDateTime; import java.util.ArrayList; @@ -10,17 +11,32 @@ @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) public record CommunityCommentResponseDto( + @Schema(description = "커뮤니티 댓글 ID", example = "1234") Long id, + @Schema(description = "작성자(봉사자) 닉네임", example = "dkdudab") String writerNickname, + @Schema(description = "커뮤니티 댓글 내용", example = "저도 함께 하고 싶습니다.") String content, + @Schema(description = "커뮤니티 댓글 수정 일시", example = "2023-12-02T11:00:00") LocalDateTime updatedAt, + @Schema(description = "대댓글 목록", example = """ + [ + { + "id": 3, + "content": "첫 번째 댓글의 답글입니다.", + "writerNickname": "사용자2", + "createdAt": "2023-12-02T11:00:00", + "replies": [] + } + ] +""") List replies ) { public CommunityCommentResponseDto { replies = replies == null ? new ArrayList<>() : replies; } - public static CommunityCommentResponseDto fromView(CommunityCommentView comment) { + public static CommunityCommentResponseDto from(CommunityCommentView comment) { return new CommunityCommentResponseDto( comment.communityComment().getId(), comment.writerNickname(), diff --git a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java index 64fb8ea0e..e767e837c 100644 --- a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java +++ b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java @@ -2,16 +2,17 @@ import com.somemore.community.domain.CommunityBoard; import com.somemore.community.repository.mapper.CommunityBoardView; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; -import java.util.List; import java.util.Optional; import java.util.UUID; public interface CommunityBoardRepository { CommunityBoard save(CommunityBoard communityBoard); Optional findById(Long id); - List getCommunityBoards(); - List findByWriterId(UUID writerId); + Page findCommunityBoards(Pageable pageable); + Page findByWriterId(UUID writerId, Pageable pageable); boolean existsById(Long id); default boolean doesNotExistById(Long id) { return !existsById(id); diff --git a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java index 62dc924ba..a61627107 100644 --- a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java +++ b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java @@ -1,6 +1,7 @@ package com.somemore.community.repository.board; import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.community.domain.CommunityBoard; @@ -8,6 +9,9 @@ import com.somemore.community.domain.QCommunityBoard; import com.somemore.volunteer.domain.QVolunteer; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Repository; import java.util.List; @@ -21,6 +25,9 @@ public class CommunityBoardRepositoryImpl implements CommunityBoardRepository { private final JPAQueryFactory queryFactory; private final CommunityBoardJpaRepository communityBoardJpaRepository; + private static final QCommunityBoard communityBoard = QCommunityBoard.communityBoard; + private static final QVolunteer volunteer = QVolunteer.volunteer; + @Override public CommunityBoard save(CommunityBoard communityBoard) { return communityBoardJpaRepository.save(communityBoard); @@ -28,28 +35,40 @@ public CommunityBoard save(CommunityBoard communityBoard) { @Override public Optional findById(Long id) { - QCommunityBoard communityBoard = QCommunityBoard.communityBoard; - return Optional.ofNullable(queryFactory .selectFrom(communityBoard) .where(communityBoard.id.eq(id) - .and(communityBoard.deleted.eq(false))) + .and(isNotDeleted())) .fetchOne()); } @Override - public List getCommunityBoards() { - return getCommunityBoardsQuery() + public Page findCommunityBoards(Pageable pageable) { + List content = getCommunityBoardsQuery() .where(QCommunityBoard.communityBoard.deleted.eq(false)) .fetch(); + + JPAQuery countQuery = queryFactory + .select(communityBoard.count()) + .from(communityBoard) + .where(isNotDeleted()); + + return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @Override - public List findByWriterId(UUID writerId) { - return getCommunityBoardsQuery() + public Page findByWriterId(UUID writerId, Pageable pageable) { + List content = getCommunityBoardsQuery() .where(QCommunityBoard.communityBoard.writerId.eq(writerId) .and(QCommunityBoard.communityBoard.deleted.eq(false))) .fetch(); + + JPAQuery countQuery = queryFactory + .select(communityBoard.count()) + .from(communityBoard) + .where(isNotDeleted()); + + return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @Override @@ -58,9 +77,6 @@ public boolean existsById(Long id) { } private JPAQuery getCommunityBoardsQuery() { - QCommunityBoard communityBoard = QCommunityBoard.communityBoard; - QVolunteer volunteer = QVolunteer.volunteer; - return queryFactory .select(Projections.constructor(CommunityBoardView.class, communityBoard, @@ -75,4 +91,8 @@ private JPAQuery getCommunityBoardsQuery() { public void deleteAllInBatch() { communityBoardJpaRepository.deleteAllInBatch(); } + + private BooleanExpression isNotDeleted() { + return communityBoard.deleted.eq(false); + } } diff --git a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java index f71e5f630..8075dd0fd 100644 --- a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java +++ b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java @@ -2,14 +2,15 @@ import com.somemore.community.domain.CommunityComment; import com.somemore.community.repository.mapper.CommunityCommentView; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; -import java.util.List; import java.util.Optional; public interface CommunityCommentRepository { CommunityComment save(CommunityComment communityComment); Optional findById(Long id); - List findCommentsByBoardId(Long boardId); + Page findCommentsByBoardId(Long boardId, Pageable pageable); boolean existsById(Long id); default boolean doesNotExistById(Long id) { return !existsById(id); diff --git a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java index c3eba1fc8..dca47cef9 100644 --- a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java +++ b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java @@ -1,12 +1,16 @@ package com.somemore.community.repository.comment; import com.querydsl.core.types.Projections; +import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.community.domain.CommunityComment; import com.somemore.community.domain.QCommunityComment; import com.somemore.community.repository.mapper.CommunityCommentView; import com.somemore.volunteer.domain.QVolunteer; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Repository; import java.util.List; @@ -19,6 +23,9 @@ public class CommunityCommentRepositoryImpl implements CommunityCommentRepositor private final JPAQueryFactory queryFactory; private final CommunityCommentJpaRepository communityCommentJpaRepository; + private static final QCommunityComment communityComment = QCommunityComment.communityComment; + private static final QVolunteer volunteer = QVolunteer.volunteer; + @Override public CommunityComment save(CommunityComment communityComment) { return communityCommentJpaRepository.save(communityComment); @@ -26,8 +33,6 @@ public CommunityComment save(CommunityComment communityComment) { @Override public Optional findById(Long id) { - QCommunityComment communityComment = QCommunityComment.communityComment; - return Optional.ofNullable(queryFactory .selectFrom(communityComment) .where(communityComment.id.eq(id) @@ -35,11 +40,8 @@ public Optional findById(Long id) { .fetchOne()); } - public List findCommentsByBoardId(Long boardId) { - QCommunityComment communityComment = QCommunityComment.communityComment; - QVolunteer volunteer = QVolunteer.volunteer; - - return queryFactory + public Page findCommentsByBoardId(Long boardId, Pageable pageable) { + List content = queryFactory .select(Projections.constructor(CommunityCommentView.class, communityComment, volunteer.nickname)) @@ -48,12 +50,16 @@ public List findCommentsByBoardId(Long boardId) { .where(communityComment.communityBoardId.eq(boardId)) .orderBy(communityComment.parentCommentId.asc().nullsFirst(), communityComment.createdAt.asc()) .fetch(); + + JPAQuery countQuery = queryFactory + .select(communityComment.count()) + .from(communityComment); + + return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @Override public boolean existsById(Long id) { - QCommunityComment communityComment = QCommunityComment.communityComment; - return queryFactory .selectOne() .from(communityComment) diff --git a/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java b/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java index d9be49dda..0e516630a 100644 --- a/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java +++ b/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java @@ -2,18 +2,19 @@ import com.somemore.community.domain.CommunityBoard; import com.somemore.community.repository.mapper.CommunityBoardView; -import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto; -import com.somemore.community.dto.response.CommunityBoardGetResponseDto; +import com.somemore.community.dto.response.CommunityBoardDetailResponseDto; +import com.somemore.community.dto.response.CommunityBoardResponseDto; import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.community.usecase.board.CommunityBoardQueryUseCase; import com.somemore.global.exception.BadRequestException; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.List; import java.util.UUID; -import java.util.function.Function; import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD; @@ -23,29 +24,26 @@ public class CommunityBoardQueryService implements CommunityBoardQueryUseCase { private final CommunityBoardRepository communityBoardRepository; + private static final int PAGE_SIZE = 10; @Override - public List getCommunityBoards() { - List boards = communityBoardRepository.getCommunityBoards(); - return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity); + public Page getCommunityBoards(int page) { + Pageable pageable = PageRequest.of(page, PAGE_SIZE); + Page boards = communityBoardRepository.findCommunityBoards(pageable); + return boards.map(CommunityBoardResponseDto::from); } @Override - public List getCommunityBoardsByWriterId(UUID writerId) { - List boards = communityBoardRepository.findByWriterId(writerId); - return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity); + public Page getCommunityBoardsByWriterId(UUID writerId, int page) { + Pageable pageable = PageRequest.of(page, PAGE_SIZE); + Page boards = communityBoardRepository.findByWriterId(writerId ,pageable); + return boards.map(CommunityBoardResponseDto::from); } @Override - public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) { + public CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id) { CommunityBoard board = communityBoardRepository.findById(id) .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); - return CommunityBoardGetDetailResponseDto.fromEntity(board); - } - - private List mapEntitiesToDtos(List entities, Function mapper) { - return entities.stream() - .map(mapper) - .toList(); + return CommunityBoardDetailResponseDto.from(board); } } diff --git a/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java index 5be670611..92936df9e 100644 --- a/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java +++ b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java @@ -6,6 +6,10 @@ import com.somemore.community.repository.mapper.CommunityCommentView; import com.somemore.community.usecase.comment.CommunityCommentQueryUseCase; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -17,12 +21,16 @@ public class CommunityCommentQueryService implements CommunityCommentQueryUseCase { private final CommunityCommentRepository communityCommentRepository; + private static final int PAGE_SIZE = 4; @Override - public List getCommunityCommentsByBoardId(Long boardId) { - List allComments = communityCommentRepository.findCommentsByBoardId(boardId); - List filteredComments = filterValidComments(allComments); - return createCommentHierarchy(filteredComments); + public Page getCommunityCommentsByBoardId(Long boardId, int page) { + Pageable pageable = PageRequest.of(page, PAGE_SIZE); + Page commentPage = communityCommentRepository.findCommentsByBoardId(boardId, pageable); + List filteredComments = filterValidComments(commentPage.getContent()); + List hierarchicalComments = createCommentHierarchy(filteredComments); + + return new PageImpl<>(hierarchicalComments, pageable, filteredComments.size()); } private List filterValidComments(List comments) { @@ -60,7 +68,7 @@ private List createCommentHierarchy(List rootComments = new ArrayList<>(); for (CommunityCommentView comment : comments) { - CommunityCommentResponseDto dto = CommunityCommentResponseDto.fromView(comment); + CommunityCommentResponseDto dto = CommunityCommentResponseDto.from(comment); commentMap.put(dto.id(), dto); Long parentCommentId = comment.communityComment().getParentCommentId(); diff --git a/src/main/java/com/somemore/community/usecase/board/CommunityBoardQueryUseCase.java b/src/main/java/com/somemore/community/usecase/board/CommunityBoardQueryUseCase.java index 70987a033..6e1d4c5c9 100644 --- a/src/main/java/com/somemore/community/usecase/board/CommunityBoardQueryUseCase.java +++ b/src/main/java/com/somemore/community/usecase/board/CommunityBoardQueryUseCase.java @@ -1,13 +1,13 @@ package com.somemore.community.usecase.board; -import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto; -import com.somemore.community.dto.response.CommunityBoardGetResponseDto; +import com.somemore.community.dto.response.CommunityBoardDetailResponseDto; +import com.somemore.community.dto.response.CommunityBoardResponseDto; +import org.springframework.data.domain.Page; -import java.util.List; import java.util.UUID; public interface CommunityBoardQueryUseCase { - List getCommunityBoards(); - List getCommunityBoardsByWriterId(UUID writerId); - CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id); + Page getCommunityBoards(int page); + Page getCommunityBoardsByWriterId(UUID writerId, int page); + CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id); } diff --git a/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java b/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java index 33ab69c94..4cd3eb945 100644 --- a/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java +++ b/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java @@ -1,9 +1,8 @@ package com.somemore.community.usecase.comment; import com.somemore.community.dto.response.CommunityCommentResponseDto; - -import java.util.List; +import org.springframework.data.domain.Page; public interface CommunityCommentQueryUseCase { - List getCommunityCommentsByBoardId(Long boardId); + Page getCommunityCommentsByBoardId(Long boardId, int page); } diff --git a/src/test/java/com/somemore/common/fixture/CommunityBoardFixture.java b/src/test/java/com/somemore/common/fixture/CommunityBoardFixture.java new file mode 100644 index 000000000..a55264dc7 --- /dev/null +++ b/src/test/java/com/somemore/common/fixture/CommunityBoardFixture.java @@ -0,0 +1,42 @@ +package com.somemore.common.fixture; + +import com.somemore.community.domain.CommunityBoard; + +import java.util.UUID; + +public class CommunityBoardFixture { + + public static final String TITLE = "테스트 커뮤니티 게시글 제목"; + public static final String CONTENT = "테스트 커뮤니티 게시글 내용"; + public static final String IMG_URL = "http://community.example.com/123"; + + private CommunityBoardFixture() { + + } + + public static CommunityBoard createCommunityBoard() { + return CommunityBoard.builder() + .title(TITLE) + .content(CONTENT) + .imgUrl(IMG_URL) + .writerId(UUID.randomUUID()) + .build(); + } + + public static CommunityBoard createCommunityBoard(UUID writerId) { + return CommunityBoard.builder() + .title(TITLE) + .content(CONTENT) + .imgUrl(IMG_URL) + .writerId(writerId) + .build(); + } + public static CommunityBoard createCommunityBoard(String title, UUID writerId) { + return CommunityBoard.builder() + .title(title) + .content(CONTENT) + .imgUrl(IMG_URL) + .writerId(writerId) + .build(); + } +} diff --git a/src/test/java/com/somemore/common/fixture/CommunityCommentFixture.java b/src/test/java/com/somemore/common/fixture/CommunityCommentFixture.java new file mode 100644 index 000000000..113c5d868 --- /dev/null +++ b/src/test/java/com/somemore/common/fixture/CommunityCommentFixture.java @@ -0,0 +1,41 @@ +package com.somemore.common.fixture; + +import com.somemore.community.domain.CommunityComment; + +import java.util.UUID; + +public class CommunityCommentFixture { + + public static final String CONTENT = "커뮤니티 댓글 테스트 내용"; + + private CommunityCommentFixture() { + + } + + public static CommunityComment createCommunityComment(Long communityBoardId, UUID writerId) { + return CommunityComment.builder() + .communityBoardId(communityBoardId) + .content(CONTENT) + .writerId(writerId) + .parentCommentId(null) + .build(); + } + + public static CommunityComment createCommunityComment(Long communityBoardId, UUID wrtierId, Long parentCommentId) { + return CommunityComment.builder() + .communityBoardId(communityBoardId) + .content(CONTENT) + .writerId(wrtierId) + .parentCommentId(parentCommentId) + .build(); + } + + public static CommunityComment createCommunityComment(String content, Long communityBoardId, UUID writerId) { + return CommunityComment.builder() + .communityBoardId(communityBoardId) + .content(content) + .writerId(writerId) + .parentCommentId(null) + .build(); + } +} diff --git a/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java b/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java index bd524f811..1d7202844 100644 --- a/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java +++ b/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java @@ -7,12 +7,17 @@ import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.volunteer.domain.Volunteer; import com.somemore.volunteer.repository.VolunteerRepository; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.transaction.annotation.Transactional; -import java.util.List; +import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard; + import java.util.Optional; import java.util.UUID; @@ -26,17 +31,37 @@ class CommunityBoardRepositoryTest extends IntegrationTestSupport { @Autowired private VolunteerRepository volunteerRepository; + private UUID writerId; + + @BeforeEach + void setUp() { + String oAuthId1 = "example-oauth-id1"; + Volunteer volunteer1 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId1); + volunteerRepository.save(volunteer1); + writerId = volunteer1.getId(); + + String oAuthId2 = "example-oauth-id2"; + Volunteer volunteer2 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId2); + volunteerRepository.save(volunteer2); + + for (int i = 1; i <= 8; i++) { + String title = "제목" + i; + CommunityBoard communityBoard = createCommunityBoard(title, writerId); + communityBoardRepository.save(communityBoard); + } + + for (int i = 1; i <= 20; i++) { + String title = "제목" + i; + CommunityBoard communityBoard = createCommunityBoard(title, volunteer2.getId()); + communityBoardRepository.save(communityBoard); + } + } + @DisplayName("커뮤니티 게시글 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)") @Test void getCommunityBoardById() { //given - CommunityBoard communityBoard = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목") - .content("테스트 커뮤니티 게시글 내용") - .imgUrl("http://community.example.com/123") - .writerId(UUID.randomUUID()) - .build(); - + CommunityBoard communityBoard = createCommunityBoard(UUID.randomUUID()); communityBoardRepository.save(communityBoard); //when @@ -54,14 +79,9 @@ void getCommunityBoardById() { @Test void getCommunityBoardByDeletedId() { //given - CommunityBoard communityBoard = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목") - .content("테스트 커뮤니티 게시글 내용") - .imgUrl("http://community.example.com/123") - .writerId(UUID.randomUUID()) - .build(); - + CommunityBoard communityBoard = createCommunityBoard(UUID.randomUUID()); communityBoardRepository.save(communityBoard); + communityBoardRepository.deleteAllInBatch(); //when @@ -71,114 +91,54 @@ void getCommunityBoardByDeletedId() { assertThat(foundCommunityBoard).isEmpty(); } - @DisplayName("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)") + @DisplayName("저장된 전체 커뮤니티 게시글을 페이지로 조회할 수 있다. (Repository)") @Test void getCommunityBoards() { + //given - String oAuthId = "example-oauth-id"; - Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId); - volunteerRepository.save(volunteer); - - CommunityBoard communityBoard1 = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목1") - .content("테스트 커뮤니티 게시글 내용1") - .imgUrl("http://community.example.com/123") - .writerId(volunteer.getId()) - .build(); - - CommunityBoard communityBoard2 = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목2") - .content("테스트 커뮤니티 게시글 내용2") - .imgUrl("http://community.example.com/12") - .writerId(volunteer.getId()) - .build(); - - communityBoardRepository.save(communityBoard1); - communityBoardRepository.save(communityBoard2); + Pageable pageable = getPageable(); //when - List communityBoards = communityBoardRepository.getCommunityBoards(); + Page communityBoards = communityBoardRepository.findCommunityBoards(pageable); //then - assertThat(communityBoards).hasSize(2); - assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId()); - assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle()); - assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname()); - assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt()); - assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId()); - assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle()); - assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname()); - assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt()); + assertThat(communityBoards).isNotNull(); + assertThat(communityBoards.getTotalElements()).isEqualTo(28); + assertThat(communityBoards.getSize()).isEqualTo(10); + assertThat(communityBoards.getNumber()).isZero(); } @DisplayName("저장된 커뮤니티 게시글 리스트를 작성자별로 조회할 수 있다. (Repository)") @Test void getCommunityBoardsByWriterId() { //given - String oAuthId = "example-oauth-id"; - Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId); - volunteerRepository.save(volunteer); - - CommunityBoard communityBoard1 = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목1") - .content("테스트 커뮤니티 게시글 내용1") - .imgUrl("http://community.example.com/123") - .writerId(volunteer.getId()) - .build(); - - CommunityBoard communityBoard2 = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목2") - .content("테스트 커뮤니티 게시글 내용2") - .imgUrl("http://community.example.com/12") - .writerId(volunteer.getId()) - .build(); - - CommunityBoard communityBoard3 = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목3") - .content("테스트 커뮤니티 게시글 내용3") - .imgUrl("http://community.example.com/1") - .writerId(UUID.randomUUID()) - .build(); - - communityBoardRepository.save(communityBoard1); - communityBoardRepository.save(communityBoard2); - communityBoardRepository.save(communityBoard3); + Pageable pageable = getPageable(); //when - List communityBoards = communityBoardRepository.findByWriterId(volunteer.getId()); + Page communityBoards = communityBoardRepository.findByWriterId(writerId, pageable); //then - assertThat(communityBoards).hasSize(2); - assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId()); - assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle()); - assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname()); - assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt()); - assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId()); - assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle()); - assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname()); - assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt()); + assertThat(communityBoards).isNotNull(); + assertThat(communityBoards.getTotalElements()).isEqualTo(8); + assertThat(communityBoards.getSize()).isEqualTo(10); + assertThat(communityBoards.getNumber()).isZero(); } @DisplayName("게시글 id로 게시글이 존재하는지 확인할 수 있다.") @Test void existsById() { - //given - UUID writerId = UUID.randomUUID(); - - CommunityBoard communityBoard = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목") - .content("테스트 커뮤니티 게시글 내용") - .imgUrl("http://community.example.com/123") - .writerId(writerId) - .build(); - - CommunityBoard savedComment = communityBoardRepository.save(communityBoard); + CommunityBoard communityBoard = createCommunityBoard(); + communityBoardRepository.save(communityBoard); //when - boolean isExist = communityBoardRepository.existsById(savedComment.getId()); + boolean isExist = communityBoardRepository.existsById(communityBoard.getId()); //then assertThat(isExist).isTrue(); } + + private Pageable getPageable() { + return PageRequest.of(0, 10); + } } diff --git a/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java b/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java index 0a84492a6..e32c5f2c4 100644 --- a/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java +++ b/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java @@ -13,13 +13,17 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.transaction.annotation.Transactional; -import java.util.List; import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; +import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard; +import static com.somemore.common.fixture.CommunityCommentFixture.createCommunityComment; @Transactional class CommunityCommentRepositoryTest extends IntegrationTestSupport { @@ -40,34 +44,19 @@ void setUp() { String oAuthId = "example-oauth-id"; Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId); volunteerRepository.save(volunteer); - writerId = volunteer.getId(); - CommunityBoard communityBoard = CommunityBoard.builder() - .title("테스트 커뮤니티 게시글 제목") - .content("테스트 커뮤니티 게시글 내용") - .imgUrl("http://community.example.com/123") - .writerId(writerId) - .build(); - + CommunityBoard communityBoard = createCommunityBoard(writerId); communityBoardRepository.save(communityBoard); - boardId = communityBoard.getId(); - CommunityComment communityComment = CommunityComment.builder() - .communityBoardId(boardId) - .writerId(writerId) - .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(null) - .build(); - + CommunityComment communityComment = createCommunityComment(boardId, writerId); savedComment = communityCommentRepository.save(communityComment); } @DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)") @Test - void createCommunityComment() { - + void createParentCommunityComment() { //given //when //then @@ -79,14 +68,8 @@ void createCommunityComment() { @DisplayName("댓글에 대댓글을 생성할 수 있다. (Repository)") @Test void createCommunityCommentReply() { - //given - CommunityComment communityCommentReply = CommunityComment.builder() - .communityBoardId(boardId) - .writerId(writerId) - .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(savedComment.getId()) - .build(); + CommunityComment communityCommentReply = createCommunityComment(boardId, writerId, savedComment.getId()); //when CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply); @@ -100,7 +83,6 @@ void createCommunityCommentReply() { @DisplayName("댓글을 id로 조회할 수 있다. (Repository)") @Test void findCommunityCommentById() { - //given //when Optional comment = communityCommentRepository.findById(savedComment.getId()); @@ -115,7 +97,6 @@ void findCommunityCommentById() { @DisplayName("댓글 id로 댓글이 존재하는지 확인할 수 있다.") @Test void existsById() { - //given //when boolean isExist = communityCommentRepository.existsById(savedComment.getId()); @@ -124,26 +105,29 @@ void existsById() { assertThat(isExist).isTrue(); } - @DisplayName("게시글 id로 게시글에 달린 댓글을 조회할 수 있다. (Repository)") + @DisplayName("게시글 id로 게시글에 달린 댓글을 페이지로 조회할 수 있다. (Repository)") @Test void findCommunityCommentByBoardId() { - //given - CommunityComment communityCommentReply = CommunityComment.builder() - .communityBoardId(boardId) - .writerId(writerId) - .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(savedComment.getId()) - .build(); + for (int i = 1; i <= 8; i++) { + String content = "제목" + i; + CommunityComment communityComment = createCommunityComment(content, boardId, writerId); + communityCommentRepository.save(communityComment); + } - CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply); + Pageable pageable = getPageable(); //when - List comments = communityCommentRepository.findCommentsByBoardId(boardId); + Page comments = communityCommentRepository.findCommentsByBoardId(boardId, pageable); //then - assertThat(comments).hasSize(2); - assertThat(comments.getFirst().communityComment().getId()).isEqualTo(savedComment.getId()); - assertThat(comments.getLast().communityComment().getId()).isEqualTo(savedCommentReply.getId()); + assertThat(comments).isNotNull(); + assertThat(comments.getTotalElements()).isEqualTo(9); + assertThat(comments.getSize()).isEqualTo(4); + assertThat(comments.getNumber()).isZero(); + } + + private Pageable getPageable() { + return PageRequest.of(0, 4); } } diff --git a/src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java b/src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java index e8c72be10..199a473f0 100644 --- a/src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java @@ -4,9 +4,8 @@ import com.somemore.auth.oauth.OAuthProvider; import com.somemore.center.repository.CenterRepository; import com.somemore.community.domain.CommunityBoard; -import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; -import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto; -import com.somemore.community.dto.response.CommunityBoardGetResponseDto; +import com.somemore.community.dto.response.CommunityBoardDetailResponseDto; +import com.somemore.community.dto.response.CommunityBoardResponseDto; import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.community.usecase.board.CreateCommunityBoardUseCase; import com.somemore.community.usecase.board.DeleteCommunityBoardUseCase; @@ -20,14 +19,16 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; -import java.util.List; -import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard; + + class CommunityBoardQueryServiceTest extends IntegrationTestSupport { @Autowired @@ -44,40 +45,26 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport { CommunityBoardQueryService communityBoardQueryService; private UUID writerId1; - private Long communityId1, communityId2; - private String imgUrl, nickName1, nickName2; + private Long communityId1; @BeforeEach void setUp() { - CommunityBoardCreateRequestDto dto1 = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목1") - .content("커뮤니티 테스트 내용1") - .build(); - - CommunityBoardCreateRequestDto dto2 = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목2") - .content("커뮤니티 테스트 내용2") - .build(); - String oAuthId = "example-oauth-id"; Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId); - volunteerRepository.save(volunteer); String oAuthId2 = "example-oauth-id"; Volunteer volunteer2 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId2); - volunteerRepository.save(volunteer2); writerId1 = volunteer.getId(); UUID writerId2 = volunteer2.getId(); - nickName1 = volunteer.getNickname(); - nickName2 = volunteer2.getNickname(); - imgUrl = "https://image.test.url/123"; - communityId1 = createCommunityBoardUseCase.createCommunityBoard(dto1, writerId1, imgUrl); - communityId2 = createCommunityBoardUseCase.createCommunityBoard(dto2, writerId2, null); + CommunityBoard communityBoard1 = createCommunityBoard(writerId1); + communityBoardRepository.save(communityBoard1); + communityBoardRepository.save(createCommunityBoard(writerId2)); + communityId1 = communityBoard1.getId(); } @AfterEach @@ -85,55 +72,34 @@ void tearDown() { communityBoardRepository.deleteAllInBatch(); } - @DisplayName("저장된 커뮤니티 게시글 리스트를 조회한다.") + @DisplayName("저장된 커뮤니티 게시글 리스트를 페이지로 조회한다.") @Test void getAllCommunityBoards() { //given - // when - List dtos = communityBoardQueryService.getCommunityBoards(); - - // then - Optional communityBoard1 = communityBoardRepository.findById(communityId1); - Optional communityBoard2 = communityBoardRepository.findById(communityId2); - - assertThat(dtos) - .isNotNull() - .hasSize(2); - - CommunityBoardGetResponseDto board1 = dtos.getLast(); - assertThat(board1.id()).isEqualTo(communityId1); - assertThat(board1.title()).isEqualTo(communityBoard1.get().getTitle()); - assertThat(board1.writerNickname()).isEqualTo(nickName1); - assertThat(board1.createdAt()).isEqualTo(communityBoard1.get().getCreatedAt()); - - CommunityBoardGetResponseDto board2 = dtos.getFirst(); - assertThat(board2.id()).isEqualTo(communityId2); - assertThat(board2.title()).isEqualTo(communityBoard2.get().getTitle()); - assertThat(board2.writerNickname()).isEqualTo(nickName2); - assertThat(board2.createdAt()).isEqualTo(communityBoard2.get().getCreatedAt()); + //when + Page dtos = communityBoardQueryService.getCommunityBoards(0); + + //then + assertThat(dtos).isNotNull(); + assertThat(dtos.getTotalElements()).isEqualTo(2); + assertThat(dtos.getSize()).isEqualTo(10); + assertThat(dtos.getNumber()).isZero(); } - @DisplayName("저장된 커뮤니티 게시글 리스트를 작성자자별로 조회한다.") + @DisplayName("저장된 커뮤니티 게시글 리스트를 작성자자별로 페이지 조회한다.") @Test void getCommunityBoardsByWriter() { //given //when - List dtos = communityBoardQueryService.getCommunityBoardsByWriterId(writerId1); + Page dtos = communityBoardQueryService.getCommunityBoardsByWriterId(writerId1, 0); //then - Optional communityBoard1 = communityBoardRepository.findById(communityId1); - - assertThat(dtos) - .isNotNull() - .hasSize(1); - - CommunityBoardGetResponseDto board1 = dtos.getFirst(); - assertThat(board1.id()).isEqualTo(communityId1); - assertThat(board1.title()).isEqualTo(communityBoard1.get().getTitle()); - assertThat(board1.writerNickname()).isEqualTo(nickName1); - assertThat(board1.createdAt()).isEqualTo(communityBoard1.get().getCreatedAt()); + assertThat(dtos).isNotNull(); + assertThat(dtos.getTotalElements()).isEqualTo(1); + assertThat(dtos.getSize()).isEqualTo(10); + assertThat(dtos.getNumber()).isZero(); } @DisplayName("커뮤니티 게시글의 상세 정보를 조회한다.") @@ -142,15 +108,14 @@ void getCommunityBoardDetail() { //given //when - CommunityBoardGetDetailResponseDto communityBoard = communityBoardQueryService.getCommunityBoardDetail(communityId1); + CommunityBoardDetailResponseDto communityBoard = communityBoardQueryService.getCommunityBoardDetail(communityId1); //then assertThat(communityBoard).isNotNull(); assertThat(communityBoard.id()).isEqualTo(communityId1); - assertThat(communityBoard.title()).isEqualTo("커뮤니티 테스트 제목1"); - assertThat(communityBoard.content()).isEqualTo("커뮤니티 테스트 내용1"); + assertThat(communityBoard.title()).isEqualTo("테스트 커뮤니티 게시글 제목"); + assertThat(communityBoard.content()).isEqualTo("테스트 커뮤니티 게시글 내용"); assertThat(communityBoard.writerId()).isEqualTo(writerId1); - assertThat(communityBoard.imageUrl()).isEqualTo(imgUrl); } @DisplayName("삭제된 커뮤니티 게시글의 상세 정보를 조회할 때 예외를 던진다.") @@ -167,7 +132,6 @@ void getCommunityBoardDetailWithDeletedId() { assertThatExceptionOfType(BadRequestException.class) .isThrownBy(callable) .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage()); - } } diff --git a/src/test/java/com/somemore/community/service/board/DeleteCommunityBoardServiceTest.java b/src/test/java/com/somemore/community/service/board/DeleteCommunityBoardServiceTest.java index 8cc6a566e..e09ef643c 100644 --- a/src/test/java/com/somemore/community/service/board/DeleteCommunityBoardServiceTest.java +++ b/src/test/java/com/somemore/community/service/board/DeleteCommunityBoardServiceTest.java @@ -59,7 +59,7 @@ void deleteCommunityBoardWithId() { deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); //then - assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty(); + assertThat(communityBoardQueryUseCase.getCommunityBoards(0)).isEmpty(); } @DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.") diff --git a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java index a41b33119..184fc665b 100644 --- a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java @@ -4,8 +4,6 @@ import com.somemore.auth.oauth.OAuthProvider; import com.somemore.community.domain.CommunityBoard; import com.somemore.community.domain.CommunityComment; -import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; -import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; import com.somemore.community.dto.response.CommunityCommentResponseDto; import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.community.repository.comment.CommunityCommentRepository; @@ -17,12 +15,13 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; -import java.util.List; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; - +import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard; +import static com.somemore.common.fixture.CommunityCommentFixture.createCommunityComment; class CommunityCommentQueryServiceTest extends IntegrationTestSupport { @Autowired @@ -37,41 +36,31 @@ class CommunityCommentQueryServiceTest extends IntegrationTestSupport { private DeleteCommunityCommentUseCase deleteCommunityCommentUseCase; private Long boardId, commentId, replyId; - UUID writerId1, writerId2; + UUID writerId; @BeforeEach void setUp() { String oAuthId1 = "example-oauth-id"; Volunteer volunteer1 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId1); volunteerRepository.save(volunteer1); - writerId1 = volunteer1.getId(); - - String oAuthId2 = "example-oauth-id"; - Volunteer volunteer2 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId2); - volunteerRepository.save(volunteer2); - writerId2 = volunteer2.getId(); - - CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목") - .content("커뮤니티 테스트 내용") - .build(); - CommunityBoard communityBoard = communityBoardRepository.save(boardDto.toEntity(writerId1, "https://test.image/123")); + writerId = volunteer1.getId(); + + CommunityBoard communityBoard = createCommunityBoard(writerId); + communityBoardRepository.save(communityBoard); boardId = communityBoard.getId(); - CommunityCommentCreateRequestDto dto1 = CommunityCommentCreateRequestDto.builder() - .communityBoardId(boardId) - .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(null) - .build(); - CommunityComment communityComment = communityCommentRepository.save(dto1.toEntity(writerId2)); - commentId = communityComment.getId(); - - CommunityCommentCreateRequestDto dto2 = CommunityCommentCreateRequestDto.builder() - .communityBoardId(boardId) - .content("커뮤니티 대댓글 테스트 내용") - .parentCommentId(commentId) - .build(); - CommunityComment communityReply = communityCommentRepository.save(dto2.toEntity(writerId1)); + CommunityComment communityComment1 = createCommunityComment(boardId, writerId); + communityCommentRepository.save(communityComment1); + commentId = communityComment1.getId(); + + for (int i = 1; i <= 8; i++) { + String content = "제목" + i; + CommunityComment communityComment = createCommunityComment(content, boardId, writerId); + communityCommentRepository.save(communityComment); + } + + CommunityComment communityReply = createCommunityComment(boardId, writerId, commentId); + communityCommentRepository.save(communityReply); replyId = communityReply.getId(); } @@ -86,37 +75,29 @@ void getCommentsByCommunityBoardId() { //given //when - List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + Page comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId, 0); //then - assertThat(comments).hasSize(1); - assertThat(comments.getFirst().id()).isEqualTo(commentId); - assertThat(comments.getFirst().replies()).hasSize(1); - assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); + assertThat(comments).isNotNull(); + assertThat(comments.getTotalElements()).isEqualTo(10); + assertThat(comments.getSize()).isEqualTo(4); + assertThat(comments.getNumber()).isZero(); } @DisplayName("삭제된 댓글의 경우 조회할 수 없다.") @Test void doesNotFind() { - //given - CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder() - .communityBoardId(boardId) - .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(null) - .build(); - CommunityComment communityComment = communityCommentRepository.save(dto.toEntity(writerId2)); - - deleteCommunityCommentUseCase.deleteCommunityComment(writerId2, communityComment.getId()); - deleteCommunityCommentUseCase.deleteCommunityComment(writerId1, replyId); + deleteCommunityCommentUseCase.deleteCommunityComment(writerId, replyId); //when - List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + Page comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId, 0); //then - assertThat(comments).hasSize(1); - assertThat(comments.getFirst().id()).isEqualTo(commentId); - assertThat(comments.getFirst().replies()).isEmpty(); + assertThat(comments).isNotNull(); + assertThat(comments.getTotalElements()).isEqualTo(9); + assertThat(comments.getSize()).isEqualTo(4); + assertThat(comments.getNumber()).isZero(); } @DisplayName("대댓글이 있는 댓글의 경우 삭제된 댓글로 조회할 수 있다.") @@ -124,15 +105,14 @@ void doesNotFind() { void getCommentsByCommunityBoardIdWithDeletedComment() { //given - deleteCommunityCommentUseCase.deleteCommunityComment(writerId2, commentId); + deleteCommunityCommentUseCase.deleteCommunityComment(writerId, commentId); //when - List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + Page comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId, 0); //then - assertThat(comments).hasSize(1); - assertThat(comments.getFirst().content()).isEqualTo("삭제된 댓글입니다"); - assertThat(comments.getFirst().writerNickname()).isEmpty(); - assertThat(comments.getFirst().replies()).hasSize(1); - assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); + assertThat(comments).isNotNull(); + assertThat(comments.getTotalElements()).isEqualTo(10); + assertThat(comments.getSize()).isEqualTo(4); + assertThat(comments.getNumber()).isZero(); } }