diff --git a/src/test/java/com/jisungin/ControllerTestSupport.java b/src/test/java/com/jisungin/ControllerTestSupport.java index 5928924..9f12774 100644 --- a/src/test/java/com/jisungin/ControllerTestSupport.java +++ b/src/test/java/com/jisungin/ControllerTestSupport.java @@ -5,9 +5,11 @@ import com.jisungin.api.oauth.AuthContext; import com.jisungin.api.review.ReviewController; import com.jisungin.api.talkroom.TalkRoomController; +import com.jisungin.api.talkroomlike.TalkRoomLikeController; import com.jisungin.application.comment.CommentService; import com.jisungin.application.review.ReviewService; import com.jisungin.application.talkroom.TalkRoomService; +import com.jisungin.application.talkroomlike.TalkRoomLikeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -16,7 +18,8 @@ @WebMvcTest(controllers = { TalkRoomController.class, CommentController.class, - ReviewController.class + ReviewController.class, + TalkRoomLikeController.class }) public abstract class ControllerTestSupport { @@ -38,4 +41,7 @@ public abstract class ControllerTestSupport { @MockBean protected ReviewService reviewService; + @MockBean + protected TalkRoomLikeService talkRoomLikeService; + } diff --git a/src/test/java/com/jisungin/api/talkroomlike/TalkRoomLikeControllerTest.java b/src/test/java/com/jisungin/api/talkroomlike/TalkRoomLikeControllerTest.java new file mode 100644 index 0000000..c0c8872 --- /dev/null +++ b/src/test/java/com/jisungin/api/talkroomlike/TalkRoomLikeControllerTest.java @@ -0,0 +1,44 @@ +package com.jisungin.api.talkroomlike; + +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.jisungin.ControllerTestSupport; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class TalkRoomLikeControllerTest extends ControllerTestSupport { + + @Test + @DisplayName("유저가 토크방에 좋아요를 누를 수 있다.") + void likeTalkRoom() throws Exception { + // when // then + mockMvc.perform(post("/v1/talk-rooms/1/likes") + .contentType(APPLICATION_JSON) + ) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value("200")) + .andExpect(jsonPath("$.status").value("OK")) + .andExpect(jsonPath("$.message").value("좋아요 성공")); + } + + @Test + @DisplayName("유저가 토크방 좋아요를 취소 할 수 있다.") + void unLikeTalkRoom() throws Exception { + // when // then + mockMvc.perform(delete("/v1/talk-rooms/1/likes") + .contentType(APPLICATION_JSON) + ) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value("200")) + .andExpect(jsonPath("$.status").value("OK")) + .andExpect(jsonPath("$.message").value("좋아요 취소")); + } + +} \ No newline at end of file diff --git a/src/test/java/com/jisungin/application/service/talkroom/TalkRoomServiceTest.java b/src/test/java/com/jisungin/application/service/talkroom/TalkRoomServiceTest.java index 383e5f7..8d368aa 100644 --- a/src/test/java/com/jisungin/application/service/talkroom/TalkRoomServiceTest.java +++ b/src/test/java/com/jisungin/application/service/talkroom/TalkRoomServiceTest.java @@ -23,12 +23,15 @@ import com.jisungin.domain.talkroom.TalkRoomRole; import com.jisungin.domain.talkroom.repository.TalkRoomRepository; import com.jisungin.domain.talkroom.repository.TalkRoomRoleRepository; +import com.jisungin.domain.talkroomlike.TalkRoomLike; +import com.jisungin.domain.talkroomlike.repository.TalkRoomLikeRepository; import com.jisungin.domain.user.User; import com.jisungin.domain.user.repository.UserRepository; import com.jisungin.exception.BusinessException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; @@ -55,8 +58,12 @@ class TalkRoomServiceTest extends ServiceTestSupport { @Autowired CommentRepository commentRepository; + @Autowired + TalkRoomLikeRepository talkRoomLikeRepository; + @AfterEach void tearDown() { + talkRoomLikeRepository.deleteAllInBatch(); commentRepository.deleteAllInBatch(); talkRoomRoleRepository.deleteAllInBatch(); talkRoomRepository.deleteAllInBatch(); @@ -610,6 +617,293 @@ void deleteTalkRoomWithComment() { assertThat(0).isEqualTo(talkRoomRepository.findAll().size()); } + @Test + @DisplayName("토크방을 페이징 조회 시 토크방에는 좋아요 총 개수가 표시되어야 한다.") + void findAllTalkRoomWithLikeCount() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + talkRoomLikeRepository.saveAll(likes); + + TalkRoomSearchServiceRequest search = TalkRoomSearchServiceRequest.builder() + .page(2) + .size(10) + .order(null) + .build(); + + // when + PageResponse response = talkRoomService.findAllTalkRoom(search); + + // then + assertThat(5L).isEqualTo(response.getQueryResponse().get(9).getLikeCounts().get(0).getLikeCount()); + } + + @Test + @DisplayName("토크방 페이지 조회 시 토크방에 좋아요 누른 사용자 ID들이 프론트에 전송되어야 한다.") + void findAllTalkRoomWithLikeUserId() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes1 = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + List likes2 = IntStream.range(5, 10).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(i)) + .build()) + .toList(); + + List likes = new ArrayList<>(); + likes.addAll(likes1); + likes.addAll(likes2); + + talkRoomLikeRepository.saveAll(likes); + + TalkRoomSearchServiceRequest search = TalkRoomSearchServiceRequest.builder() + .page(2) + .size(10) + .order(null) + .build(); + + // when + PageResponse response = talkRoomService.findAllTalkRoom(search); + + // then + assertThat(users.get(0).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(0).getUserId()); + assertThat(users.get(1).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(1).getUserId()); + assertThat(users.get(2).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(2).getUserId()); + assertThat(users.get(3).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(3).getUserId()); + assertThat(users.get(4).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(4).getUserId()); + } + + @Test + @DisplayName("토크방 단건 조회 시 좋아요 개수가 표시 된다.") + void findOneTalkRoomWithLikeCount() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + talkRoomLikeRepository.saveAll(likes); + + // when + TalkRoomFindOneResponse response = talkRoomService.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(5L).isEqualTo(response.getLikeCount()); + } + + @Test + @DisplayName("토크방 단건 조회 시 좋아요한 유저의 ID가 보내진다.") + void findOneTalkRoomWithLikeUserId() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + talkRoomLikeRepository.saveAll(likes); + + // when + TalkRoomFindOneResponse response = talkRoomService.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(users.get(0).getId()).isEqualTo(response.getUserIds().get(0).getUserId()); + assertThat(users.get(1).getId()).isEqualTo(response.getUserIds().get(1).getUserId()); + assertThat(users.get(2).getId()).isEqualTo(response.getUserIds().get(2).getUserId()); + assertThat(users.get(3).getId()).isEqualTo(response.getUserIds().get(3).getUserId()); + assertThat(users.get(4).getId()).isEqualTo(response.getUserIds().get(4).getUserId()); + } + + @Test + @DisplayName("토크방 단건 조회 시 의견 개수가 표시 된다.") + void findOneTalkRoomWithCommentCount() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List comments = IntStream.range(0, 5) + .mapToObj(i -> Comment.builder() + .talkRoom(talkRoom.get(0)) + .user(users.get(0)) + .content("의견 " + i) + .build()) + .collect(Collectors.toList()); + + commentRepository.saveAll(comments); + + // when + TalkRoomFindOneResponse response = talkRoomService.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(5L).isEqualTo(response.getCommentCount()); + } + private static Comment createComment(TalkRoom talkRoom, User user) { return Comment.builder() .talkRoom(talkRoom) diff --git a/src/test/java/com/jisungin/application/talkroomlike/TalkRoomLikeServiceTest.java b/src/test/java/com/jisungin/application/talkroomlike/TalkRoomLikeServiceTest.java new file mode 100644 index 0000000..6a7bd8c --- /dev/null +++ b/src/test/java/com/jisungin/application/talkroomlike/TalkRoomLikeServiceTest.java @@ -0,0 +1,232 @@ +package com.jisungin.application.talkroomlike; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.jisungin.ServiceTestSupport; +import com.jisungin.application.talkroom.TalkRoomService; +import com.jisungin.domain.ReadingStatus; +import com.jisungin.domain.book.Book; +import com.jisungin.domain.book.repository.BookRepository; +import com.jisungin.domain.oauth.OauthId; +import com.jisungin.domain.oauth.OauthType; +import com.jisungin.domain.talkroom.TalkRoom; +import com.jisungin.domain.talkroom.TalkRoomRole; +import com.jisungin.domain.talkroom.repository.TalkRoomRepository; +import com.jisungin.domain.talkroom.repository.TalkRoomRoleRepository; +import com.jisungin.domain.talkroomlike.TalkRoomLike; +import com.jisungin.domain.talkroomlike.repository.TalkRoomLikeRepository; +import com.jisungin.domain.user.User; +import com.jisungin.domain.user.repository.UserRepository; +import com.jisungin.exception.BusinessException; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +class TalkRoomLikeServiceTest extends ServiceTestSupport { + + @Autowired + TalkRoomRepository talkRoomRepository; + + @Autowired + TalkRoomRoleRepository talkRoomRoleRepository; + + @Autowired + TalkRoomService talkRoomService; + + @Autowired + BookRepository bookRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + TalkRoomLikeService talkRoomLikeService; + + @Autowired + TalkRoomLikeRepository talkRoomLikeRepository; + + @AfterEach + void tearDown() { + talkRoomLikeRepository.deleteAllInBatch(); + talkRoomRoleRepository.deleteAllInBatch(); + talkRoomRepository.deleteAllInBatch(); + userRepository.deleteAllInBatch(); + bookRepository.deleteAllInBatch(); + } + + @Test + @DisplayName("유저가 토크방을 좋아요 한다.") + void likeTalkRoom() { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + TalkRoom talkRoom = createTalkRoom(book, user); + talkRoomRepository.save(talkRoom); + + createTalkRoomRole(talkRoom); + + // when + talkRoomLikeService.likeTalkRoom(talkRoom.getId(), user.getId()); + + // then + List talkRoomLikes = talkRoomLikeRepository.findAll(); + assertThat(1).isEqualTo(talkRoomLikes.size()); + } + + @Test + @DisplayName("토크방이 없는 상태에선 좋아요 할 수 없다.") + void likeTalkRoomWithTalkRoomEmpty() { + // given + User user = createUser(); + userRepository.save(user); + + // when // then + assertThatThrownBy(() -> talkRoomLikeService.likeTalkRoom(1L, user.getId())) + .isInstanceOf(BusinessException.class) + .hasMessage("토크방을 찾을 수 없습니다."); + } + + @Test + @DisplayName("로그인을 하지 않는 상태에선 좋아요를 누를 수 없다.") + void likeTalkRoomWithUserEmpty() { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + TalkRoom talkRoom = createTalkRoom(book, user); + talkRoomRepository.save(talkRoom); + + createTalkRoomRole(talkRoom); + // when // then + assertThatThrownBy(() -> talkRoomLikeService.likeTalkRoom(talkRoom.getId(), 100L)) + .isInstanceOf(BusinessException.class) + .hasMessage("사용자를 찾을 수 없습니다."); + } + + @Test + @DisplayName("유저가 좋아요를 취소한다.") + void unLikeTalkRoom() { + // given + User user = createUser(); + userRepository.save(user); + + Book book = createBook(); + bookRepository.save(book); + + TalkRoom talkRoom = createTalkRoom(book, user); + talkRoomRepository.save(talkRoom); + + createTalkRoomRole(talkRoom); + + TalkRoomLike talkRoomLike = createTalkRoomLike(talkRoom, user); + talkRoomLikeRepository.save(talkRoomLike); + + // when + talkRoomLikeService.unLikeTalkRoom(talkRoom.getId(), user.getId()); + + // then + List talkRoomLikes = talkRoomLikeRepository.findAll(); + assertThat(0).isEqualTo(talkRoomLikes.size()); + } + + @Test + @DisplayName("유저A의 좋아요를 유저B가 취소할 순 없다.") + void unLikeTalkRoomWithUserB() { + // given + User user = createUser(); + userRepository.save(user); + + User userB = User.builder() + .name("userB@gmail.com") + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId2") + .oauthType(OauthType.KAKAO) + .build() + ) + .build(); + userRepository.save(userB); + + Book book = createBook(); + bookRepository.save(book); + + TalkRoom talkRoom = createTalkRoom(book, user); + talkRoomRepository.save(talkRoom); + + createTalkRoomRole(talkRoom); + + TalkRoomLike talkRoomLike = createTalkRoomLike(talkRoom, user); + talkRoomLikeRepository.save(talkRoomLike); + + // when // then + assertThatThrownBy(() -> talkRoomLikeService.unLikeTalkRoom(talkRoom.getId(), userB.getId())) + .isInstanceOf(BusinessException.class) + .hasMessage("토크방 좋아요를 찾을 수 없습니다."); + } + + private static TalkRoomLike createTalkRoomLike(TalkRoom talkRoom, User user) { + return TalkRoomLike.builder() + .talkRoom(talkRoom) + .user(user) + .build(); + } + + private void createTalkRoomRole(TalkRoom talkRoom) { + List request = new ArrayList<>(); + request.add("읽는 중"); + request.add("읽음"); + + List readingStatus = ReadingStatus.createReadingStatus(request); + + readingStatus.stream().map(status -> TalkRoomRole.roleCreate(talkRoom, status)) + .forEach(talkRoomRoleRepository::save); + } + + private static TalkRoom createTalkRoom(Book book, User user) { + return TalkRoom.builder() + .book(book) + .title("토크방") + .content("내용") + .user(user) + .build(); + } + + private static User createUser() { + return User.builder() + .name("user@gmail.com") + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId") + .oauthType(OauthType.KAKAO) + .build() + ) + .build(); + } + + private static Book createBook() { + return Book.builder() + .title("제목") + .content("내용") + .authors("작가") + .isbn("11111") + .publisher("publisher") + .dateTime(LocalDateTime.now()) + .imageUrl("www") + .build(); + } + +} \ No newline at end of file diff --git a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryImplTest.java b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryImplTest.java index 050d3aa..2848746 100644 --- a/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryImplTest.java +++ b/src/test/java/com/jisungin/domain/talkroom/repository/TalkRoomRepositoryImplTest.java @@ -16,11 +16,14 @@ import com.jisungin.domain.oauth.OauthType; import com.jisungin.domain.talkroom.TalkRoom; import com.jisungin.domain.talkroom.TalkRoomRole; +import com.jisungin.domain.talkroomlike.TalkRoomLike; +import com.jisungin.domain.talkroomlike.repository.TalkRoomLikeRepository; import com.jisungin.domain.user.User; import com.jisungin.domain.user.repository.UserRepository; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; @@ -44,8 +47,12 @@ class TalkRoomRepositoryImplTest extends RepositoryTestSupport { @Autowired CommentRepository commentRepository; + @Autowired + TalkRoomLikeRepository talkRoomLikeRepository; + @AfterEach void tearDown() { + talkRoomLikeRepository.deleteAllInBatch(); commentRepository.deleteAllInBatch(); talkRoomRoleRepository.deleteAllInBatch(); talkRoomRepository.deleteAllInBatch(); @@ -123,6 +130,241 @@ void talkRoomFindOneComment() { assertThat("user@gmail.com").isEqualTo(findOneTalkRoom.getComments().get(0).getUserName()); } + @Test + @DisplayName("querydsl 좋아요 총 개수 조회") + void likeTalkRoomFindCount() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes1 = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + List likes2 = IntStream.range(5, 10).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(i)) + .build()) + .toList(); + + List likes = new ArrayList<>(); + likes.addAll(likes1); + likes.addAll(likes2); + + talkRoomLikeRepository.saveAll(likes); + + TalkRoomSearchServiceRequest search = TalkRoomSearchServiceRequest.builder() + .page(2) + .size(10) + .order(null) + .build(); + + // when + PageResponse response = talkRoomRepository.findAllTalkRoom(search); + + // then + assertThat(users.get(0).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(0).getUserId()); + assertThat(users.get(1).getId()).isEqualTo(response.getQueryResponse().get(9).getUserIds().get(1).getUserId()); + } + + @Test + @DisplayName("querydsl 토크방 단건 조회 시 좋아요 개수 표시 테스트") + void findOneTalkRoomWithLikeCount() { + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes1 = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + List likes2 = IntStream.range(5, 10).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(i)) + .build()) + .toList(); + + List likes = new ArrayList<>(); + likes.addAll(likes1); + likes.addAll(likes2); + + talkRoomLikeRepository.saveAll(likes); + + // when + TalkRoomFindOneResponse response = talkRoomRepository.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(5L).isEqualTo(response.getLikeCount()); + } + + @Test + @DisplayName("querydsl 토크방 단건 조회 시 의견 개수 표시 테스트") + void findOneTalkRoomWithCommentCount() { + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List comments = IntStream.range(0, 5) + .mapToObj(i -> Comment.builder() + .talkRoom(talkRoom.get(0)) + .user(users.get(0)) + .content("의견 " + i) + .build()) + .collect(Collectors.toList()); + + commentRepository.saveAll(comments); + + // when + TalkRoomFindOneResponse response = talkRoomRepository.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(5L).isEqualTo(response.getCommentCount()); + } + + @Test + @DisplayName("querydsl 토크방 단건 조회 시 좋아요 누른 userId 반환") + void findOneTalkRoomWithUserId() { + // given + List users = IntStream.range(0, 10) + .mapToObj(i -> User.builder() + .name("user@gmail.com " + i) + .profileImage("image") + .oauthId( + OauthId.builder() + .oauthId("oauthId " + i) + .oauthType(OauthType.KAKAO) + .build() + ) + .build()).toList(); + + userRepository.saveAll(users); + + Book book = createBook(); + bookRepository.save(book); + + List talkRoom = IntStream.range(0, 20) + .mapToObj(i -> TalkRoom.builder() + .user(users.get(0)) + .book(book) + .title("토론방 " + i) + .content("내용 " + i) + .build()) + .toList(); + + talkRoomRepository.saveAll(talkRoom); + + for (TalkRoom t : talkRoom) { + createTalkRoomRole(t); + } + + List likes = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() + .user(users.get(i)) + .talkRoom(talkRoom.get(0)) + .build()) + .toList(); + + talkRoomLikeRepository.saveAll(likes); + + // when + TalkRoomFindOneResponse response = talkRoomRepository.findOneTalkRoom(talkRoom.get(0).getId()); + + // then + assertThat(users.get(0).getId()).isEqualTo(response.getUserIds().get(0).getUserId()); + assertThat(users.get(1).getId()).isEqualTo(response.getUserIds().get(1).getUserId()); + assertThat(users.get(2).getId()).isEqualTo(response.getUserIds().get(2).getUserId()); + assertThat(users.get(3).getId()).isEqualTo(response.getUserIds().get(3).getUserId()); + assertThat(users.get(4).getId()).isEqualTo(response.getUserIds().get(4).getUserId()); + } + private static Comment createComment(TalkRoom talkRoom, User user) { return Comment.builder() .talkRoom(talkRoom)