diff --git a/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java b/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java index 3754388..50e76ef 100644 --- a/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java +++ b/src/test/java/com/jisungin/application/talkroom/TalkRoomServiceTest.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -914,6 +915,23 @@ void createTalkRoomWithImage() throws Exception { } + @Test + @DisplayName("최근 의견이 달린 토론방을 조회 할때 의견이 없다면 빈 값을 보낸다.") + void fetchRecentCommentedDiscussionsWithCommentEmpty() throws Exception { + // given + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent_comment") + .build(); + + // when + TalkRoomPageResponse response = talkRoomService.findAllTalkRoom(request, null); + + // then + Assertions.assertThat(response.getResponse().getQueryResponse()).isEmpty(); + } + private static List listUsers() { return IntStream.range(0, 10) .mapToObj(i -> User.builder() diff --git a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java index bbf4f5b..893fec2 100644 --- a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java +++ b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryTest.java @@ -521,6 +521,110 @@ public void getTalkRoomsRelatedBookTotalSize() { assertThat(anotherTotalCount).isEqualTo(10L); } + @Test + @DisplayName("최근 의견이 달린 토론방을 가져온다.") + void fetchRecentCommentedDiscussions() throws Exception { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + List talkRooms = IntStream.range(1, 11) + .mapToObj(i -> TalkRoom.builder() + .user(user) + .book(book) + .title("토론방" + i) + .content("내용" + i) + .build()) + .toList(); + talkRoomRepository.saveAll(talkRooms); + + List comments = IntStream.range(3, 6) + .mapToObj(i -> Comment.builder() + .user(user) + .talkRoom(talkRooms.get(i)) + .content("의견") + .build()) + .toList(); + commentRepository.saveAll(comments); + + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent-comment") + .build(); + + // when + List response = talkRoomRepository.findAllTalkRoom(request.getOffset(), + request.getSize(), request.getOrder(), request.getQuery()); + + // then + assertThat("토론방6").isEqualTo(response.get(0).getTitle()); + assertThat("토론방5").isEqualTo(response.get(1).getTitle()); + assertThat("토론방4").isEqualTo(response.get(2).getTitle()); + } + + @Test + @DisplayName("최근 의견이 달린 토론방을 가져온다.") + void fetchRecentCommentedDiscussions2() throws Exception { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + List talkRooms = IntStream.range(1, 11) + .mapToObj(i -> TalkRoom.builder() + .user(user) + .book(book) + .title("토론방" + i) + .content("내용" + i) + .build()) + .toList(); + talkRoomRepository.saveAll(talkRooms); + + Comment comment1 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(3)) + .build(); + Comment comment2 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(4)) + .build(); + Comment comment3 = Comment.builder() + .content("의견") + .user(user) + .talkRoom(talkRooms.get(5)) + .build(); + Comment comment4 = Comment.builder() + .content("의견 추가") + .user(user) + .talkRoom(talkRooms.get(5)) + .build(); + + commentRepository.saveAll(List.of(comment1, comment2, comment3, comment4)); + + SearchServiceRequest request = SearchServiceRequest.builder() + .size(3) + .page(1) + .order("recent-comment") + .build(); + + // when + List response = talkRoomRepository.findAllTalkRoom(request.getOffset(), + request.getSize(), request.getOrder(), request.getQuery()); + + // then + assertThat("토론방6").isEqualTo(response.get(0).getTitle()); + assertThat("토론방5").isEqualTo(response.get(1).getTitle()); + assertThat("토론방4").isEqualTo(response.get(2).getTitle()); + } + private static Comment createComment(TalkRoom talkRoom, User user) { return Comment.builder() .talkRoom(talkRoom)