Skip to content

Commit

Permalink
feat: TalkRoom 좋아요 기능 테스트 코드 작성 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
AHNYUNKI committed Mar 27, 2024
1 parent dfc3dcb commit e3f728f
Show file tree
Hide file tree
Showing 5 changed files with 819 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/test/java/com/jisungin/ControllerTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +18,8 @@
@WebMvcTest(controllers = {
TalkRoomController.class,
CommentController.class,
ReviewController.class
ReviewController.class,
TalkRoomLikeController.class
})
public abstract class ControllerTestSupport {

Expand All @@ -38,4 +41,7 @@ public abstract class ControllerTestSupport {
@MockBean
protected ReviewService reviewService;

@MockBean
protected TalkRoomLikeService talkRoomLikeService;

}
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("좋아요 취소"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit e3f728f

Please sign in to comment.