Skip to content

Commit

Permalink
test: 최근 의견이 달린 토론방 조회 API 기능 테스트 코드 작성 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
AHNYUNKI committed Apr 23, 2024
1 parent 87f4338 commit 428df50
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<User> listUsers() {
return IntStream.range(0, 10)
.mapToObj(i -> User.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TalkRoom> talkRooms = IntStream.range(1, 11)
.mapToObj(i -> TalkRoom.builder()
.user(user)
.book(book)
.title("토론방" + i)
.content("내용" + i)
.build())
.toList();
talkRoomRepository.saveAll(talkRooms);

List<Comment> 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<TalkRoomQueryResponse> 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<TalkRoom> 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<TalkRoomQueryResponse> 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)
Expand Down

0 comments on commit 428df50

Please sign in to comment.