Skip to content

Commit

Permalink
#322 test: 와인 서비스 페이징 테스트 코드 통과하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyun1206 committed Jan 13, 2025
1 parent 08ad153 commit ec25f5f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ApiResponse<WineWithThreeReviewsResponse> findWineById(
public ApiResponse<List<WineReviewResponse>> showWineReview(
@PathVariable("wineId") Long wineId,
@RequestParam String sortType,
@ParameterObject @PageableDefault(size = 10, sort = "name") Pageable pageable) {
@ParameterObject @PageableDefault(size = 10) Pageable pageable) {

List<WineReviewResponse> wineReviewResponseList = wineService.getWineReviewsAndIsLikedByWineId(wineId, SortType.of(sortType), pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -137,8 +139,10 @@ void findTastingNoteByWineId() {
tastingNoteRepository.save(tastingNote2);
tastingNoteRepository.save(tastingNote3);

Pageable pageable = PageRequest.of(0, 10);

// when
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.LATEST);
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.LATEST, pageable);

// then
assertThat(tastingNotes).hasSize(3)
Expand All @@ -162,8 +166,10 @@ void findTastingNoteByWineIdOldest() {

tastingNoteRepository.saveAll(List.of(tastingNote1, tastingNote2, tastingNote3));

Pageable pageable = PageRequest.of(0, 10);

// when
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.OLDEST);
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.OLDEST, pageable);

// then
assertThat(tastingNotes).hasSize(3)
Expand All @@ -188,8 +194,9 @@ void findTastingNoteByWineIdHighestRating() {

tastingNoteRepository.saveAll(List.of(tastingNote1, tastingNote2, tastingNote3));

Pageable pageable = PageRequest.of(0, 10);
// when
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.HIGH_RATING);
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.HIGH_RATING, pageable);

// then
assertThat(tastingNotes).hasSize(3)
Expand All @@ -214,8 +221,9 @@ void findTastingNoteByWineIdLowestRating() {

tastingNoteRepository.saveAll(List.of(tastingNote1, tastingNote2, tastingNote3));

Pageable pageable = PageRequest.of(0, 10);
// when
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.LOW_RATING);
List<TastingNote> tastingNotes = tastingNoteRepository.findAllTastingNoteBy(wine.getId(), SortType.LOW_RATING, pageable);

// then
assertThat(tastingNotes).hasSize(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

Expand Down Expand Up @@ -71,7 +72,7 @@ void searchWineByNotExistingWineName() throws Exception {
void searchWineByBlankSearchWineParameter() throws Exception {
// given
String wineName = "";
Pageable pageable = PageRequest.of(0, 10);
Pageable pageable = PageRequest.of(0, 10, Sort.by("name"));
when(wineService.searchWinesByName(wineName, pageable)).thenReturn(
List.of(creatWinePreviewResponse(1L, "와인1"),
creatWinePreviewResponse(2L, "와인2"),
Expand Down Expand Up @@ -158,14 +159,15 @@ void findWineReviewByWineIdAndSortType() throws Exception {
// given
Long wineId = 1L;
String sortType = "최신순";
when(wineService.getWineReviewsAndIsLikedByWineId(wineId, SortType.of(sortType)))
Pageable pageable = PageRequest.of(0, 10);
when(wineService.getWineReviewsAndIsLikedByWineId(wineId, SortType.of(sortType), pageable))
.thenReturn(List.of(
createReviewResponse("첫 번째 리뷰 내용", "user1", 5, LocalDateTime.of(2025, 1, 6, 0, 0)),
createReviewResponse("두 번째 리뷰 내용", "user2", 4, LocalDateTime.of(2025, 1, 6, 0, 0)),
createReviewResponse("세 번째 리뷰 내용", "user3", 5, LocalDateTime.of(2025, 1, 6, 0, 0))
));
// when // then
mockMvc.perform(get("/wine/review/{wineId}?sortType={sortType}", wineId, sortType))
mockMvc.perform(get("/wine/review/{wineId}?sortType={sortType}&page=0&size=10", wineId, sortType))
.andDo(print())
.andExpect(jsonPath("$.code").value("COMMON200"))
.andExpect(jsonPath("$.message").value("OK"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand Down Expand Up @@ -191,7 +192,7 @@ void searchWineByName() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();
Pageable pageable = PageRequest.of(0, 10);

// when
List<Wine> wineList1 = wineRepository.searchByName("0년", pageable);
Expand Down Expand Up @@ -223,7 +224,7 @@ void searchWineByNameEng() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년", "popular white sparkling wine 20 years");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년", "red wine that manias find 30 years");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();
Pageable pageable = PageRequest.of(0, 10);
// when
List<Wine> wineList1 = wineRepository.searchByName("0 years", pageable);
List<Wine> wineList2 = wineRepository.searchByName("popular", pageable);
Expand Down Expand Up @@ -253,7 +254,7 @@ void searchWineByNotExistingName() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();
Pageable pageable = PageRequest.of(0, 10);
// when
List<Wine> wineList = wineRepository.searchByName("존재하지 않는 와인 이름으로 검색하기", pageable);
// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;
Expand Down Expand Up @@ -49,8 +50,7 @@ void searchWineByName() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();

Pageable pageable = PageRequest.of(0, 10);
// when
List<WinePreviewResponse> winePreviewList1 = wineService.searchWinesByName("0년", pageable);
List<WinePreviewResponse> winePreviewList2 = wineService.searchWinesByName("대중적", pageable);
Expand Down Expand Up @@ -81,7 +81,7 @@ void searchWineByNameEng() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년", "popular white sparkling wine 20 years");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년", "red wine that manias find 30 years");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();
Pageable pageable = PageRequest.of(0, 10);
// when
List<WinePreviewResponse> winePreviewList1 = wineService.searchWinesByName("0 years", pageable);
List<WinePreviewResponse> winePreviewList2 = wineService.searchWinesByName("popular", pageable);
Expand Down Expand Up @@ -111,7 +111,7 @@ void searchWineByNotExistingName() {
Wine wine3 = createWine("대중적인 화이트 스파클링 와인 20년");
Wine wine4 = createWine("매니아들이 찾는 레드 와인 30년");
wineRepository.saveAll(List.of(wine1, wine2, wine3, wine4));
Pageable pageable = Pageable.unpaged();
Pageable pageable = PageRequest.of(0, 10);
// when
List<WinePreviewResponse> winePreviewList = wineService.searchWinesByName("존재하지 않는 와인 이름으로 검색하기", pageable);
// then
Expand Down Expand Up @@ -262,8 +262,10 @@ void findWineReviewByWineId() {
TastingNote tastingNote5 = createTastingNote(member, wine, "빨간색",
50, 30, 20, 40, 30, 10, "다시 구매할 것 같아요");
tastingNoteRepository.saveAll(List.of(tastingNote1, tastingNote2, tastingNote3, tastingNote4, tastingNote5));

Pageable pageable = PageRequest.of(0, 10);
// when
List<WineReviewResponse> wineReviews = wineService.getWineReviewsAndIsLikedByWineId(wine.getId(), SortType.LATEST);
List<WineReviewResponse> wineReviews = wineService.getWineReviewsAndIsLikedByWineId(wine.getId(), SortType.LATEST, pageable);
// then
assertThat(wineReviews).hasSize(5)
.extracting("review", "rating")
Expand All @@ -279,8 +281,10 @@ void findWineReviewByWineId() {
@DisplayName("잘못된 와인 아이디로 와인 리뷰를 전체 조회하면 예외가 발생한다.")
@Test
void findWineReviewByWrongWineId() {
// given // when // then
assertThatThrownBy(() -> wineService.getWineReviewsAndIsLikedByWineId(-1L, SortType.LATEST))
// given
Pageable pageable = PageRequest.of(0, 10);
// when // then
assertThatThrownBy(() -> wineService.getWineReviewsAndIsLikedByWineId(-1L, SortType.LATEST, pageable))
.isInstanceOf(GeneralException.class)
.hasMessage("와인이 없습니다.");
}
Expand Down

0 comments on commit ec25f5f

Please sign in to comment.