-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TalkRoom 좋아요 기능 테스트 코드 작성 (#40)
- Loading branch information
Showing
5 changed files
with
819 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/com/jisungin/api/talkroomlike/TalkRoomLikeControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("좋아요 취소")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<User> users = IntStream.range(0, 10) | ||
.mapToObj(i -> User.builder() | ||
.name("[email protected] " + 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> 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<TalkRoomLike> 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<TalkRoomFindAllResponse> response = talkRoomService.findAllTalkRoom(search); | ||
|
||
// then | ||
assertThat(5L).isEqualTo(response.getQueryResponse().get(9).getLikeCounts().get(0).getLikeCount()); | ||
} | ||
|
||
@Test | ||
@DisplayName("토크방 페이지 조회 시 토크방에 좋아요 누른 사용자 ID들이 프론트에 전송되어야 한다.") | ||
void findAllTalkRoomWithLikeUserId() { | ||
// given | ||
List<User> users = IntStream.range(0, 10) | ||
.mapToObj(i -> User.builder() | ||
.name("[email protected] " + 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> 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<TalkRoomLike> likes1 = IntStream.range(0, 5).mapToObj(i -> TalkRoomLike.builder() | ||
.user(users.get(i)) | ||
.talkRoom(talkRoom.get(0)) | ||
.build()) | ||
.toList(); | ||
|
||
List<TalkRoomLike> likes2 = IntStream.range(5, 10).mapToObj(i -> TalkRoomLike.builder() | ||
.user(users.get(i)) | ||
.talkRoom(talkRoom.get(i)) | ||
.build()) | ||
.toList(); | ||
|
||
List<TalkRoomLike> 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<TalkRoomFindAllResponse> 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<User> users = IntStream.range(0, 10) | ||
.mapToObj(i -> User.builder() | ||
.name("[email protected] " + 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> 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<TalkRoomLike> 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<User> users = IntStream.range(0, 10) | ||
.mapToObj(i -> User.builder() | ||
.name("[email protected] " + 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> 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<TalkRoomLike> 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<User> users = IntStream.range(0, 10) | ||
.mapToObj(i -> User.builder() | ||
.name("[email protected] " + 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> 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<Comment> 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) | ||
|
Oops, something went wrong.