|
4 | 4 | import static org.junit.jupiter.api.Assertions.*; |
5 | 5 | import static org.mockito.BDDMockito.*; |
6 | 6 |
|
| 7 | +import java.util.List; |
7 | 8 | import java.util.Optional; |
8 | 9 |
|
9 | 10 | import org.junit.jupiter.api.DisplayName; |
|
12 | 13 | import org.mockito.InjectMocks; |
13 | 14 | import org.mockito.Mock; |
14 | 15 | import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | +import org.springframework.data.domain.PageRequest; |
| 17 | +import org.springframework.data.domain.Pageable; |
| 18 | +import org.springframework.data.domain.Slice; |
| 19 | +import org.springframework.data.domain.SliceImpl; |
15 | 20 |
|
| 21 | +import com.example.log4u.common.dto.PageResponse; |
16 | 22 | import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto; |
| 23 | +import com.example.log4u.domain.comment.dto.response.CommentListResponseDto; |
| 24 | +import com.example.log4u.domain.comment.dto.response.CommentResponseDto; |
17 | 25 | import com.example.log4u.domain.comment.entity.Comment; |
18 | 26 | import com.example.log4u.domain.comment.exception.NotFoundCommentException; |
19 | 27 | import com.example.log4u.domain.comment.exception.UnauthorizedAccessException; |
@@ -134,6 +142,34 @@ void commentDelete_Fail_Unauthorized() { |
134 | 142 | verify(commentRepository, never()).delete(any()); |
135 | 143 | } |
136 | 144 |
|
| 145 | + @DisplayName("성공 테스트: 특정 다이어리 댓글 전체 조회 (커서 기반)") |
| 146 | + @Test |
| 147 | + void getCommentsList_In_DiaryDetail_Success() { |
| 148 | + // given |
| 149 | + Long diaryId = 1L; |
| 150 | + int size = 5; |
| 151 | + Long cursorCommentId = null; |
| 152 | + |
| 153 | + List<Comment> commentList = CommentFixture.createCommentsListFixture(size + 1); // hasNext 판별 위해 +1 |
| 154 | + Pageable pageable = PageRequest.of(0, size); |
| 155 | + boolean hasNext = commentList.size() > size; |
| 156 | + |
| 157 | + List<Comment> sliced = hasNext ? commentList.subList(0, size) : commentList; |
| 158 | + |
| 159 | + Slice<Comment> slice = new SliceImpl<>(sliced, pageable, hasNext); |
| 160 | + given(commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable)) |
| 161 | + .willReturn(slice); |
| 162 | + |
| 163 | + // when |
| 164 | + PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId, size); |
| 165 | + |
| 166 | + // then |
| 167 | + assertThat(response.content()).hasSize(sliced.size()); |
| 168 | + assertThat(response.pageInfo().hasNext()).isEqualTo(hasNext); |
| 169 | + assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().getCommentId() : null); |
| 170 | + |
| 171 | + verify(commentRepository).findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable); |
| 172 | + } |
137 | 173 |
|
138 | 174 |
|
139 | 175 | } |
0 commit comments