diff --git a/src/main/java/com/funeat/product/application/ProductService.java b/src/main/java/com/funeat/product/application/ProductService.java index 26e860fe..74fd2032 100644 --- a/src/main/java/com/funeat/product/application/ProductService.java +++ b/src/main/java/com/funeat/product/application/ProductService.java @@ -136,18 +136,20 @@ public RankingProductsResponse getTop3Products() { } public SearchProductsResponse searchProducts(final String query, final Long lastProductId) { - final List products = findAllByNameContaining(query, lastProductId); + final List findProducts = findAllByNameContaining(query, lastProductId); + final int resultSize = getResultSize(findProducts); + final List products = findProducts.subList(0, resultSize); - final boolean hasNext = products.size() > DEFAULT_PAGE_SIZE; + final boolean hasNext = hasNextPage(findProducts); final List productDtos = products.stream() .map(SearchProductDto::toDto) - .collect(Collectors.toList()); + .toList(); return SearchProductsResponse.toResponse(hasNext, productDtos); } private List findAllByNameContaining(final String query, final Long lastProductId) { - final PageRequest size = PageRequest.ofSize(DEFAULT_PAGE_SIZE); + final PageRequest size = PageRequest.ofSize(DEFAULT_CURSOR_PAGINATION_SIZE); if (lastProductId == 0) { return productRepository.findAllByNameContainingFirst(query, size); } diff --git a/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java b/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java index 74fe0f28..87c0d300 100644 --- a/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java +++ b/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java @@ -31,7 +31,6 @@ import static com.funeat.fixture.MemberFixture.멤버3; import static com.funeat.fixture.PageFixture.FIRST_PAGE; import static com.funeat.fixture.PageFixture.PAGE_SIZE; -import static com.funeat.fixture.PageFixture.SECOND_PAGE; import static com.funeat.fixture.PageFixture.가격_내림차순; import static com.funeat.fixture.PageFixture.가격_오름차순; import static com.funeat.fixture.PageFixture.과거순; @@ -41,7 +40,6 @@ import static com.funeat.fixture.PageFixture.응답_페이지_생성; import static com.funeat.fixture.PageFixture.좋아요수_내림차순; import static com.funeat.fixture.PageFixture.첫페이지O; -import static com.funeat.fixture.PageFixture.첫페이지X; import static com.funeat.fixture.PageFixture.총_데이터_개수; import static com.funeat.fixture.PageFixture.총_페이지; import static com.funeat.fixture.PageFixture.최신순; @@ -421,7 +419,7 @@ class searchProducts_성공_테스트 { // then STATUS_CODE를_검증한다(응답, 정상_처리); - 상품_자동_완성_검색_결과를_검증한다(응답, List.of(상품2, 상품1)); + 상품_자동_완성_검색_결과를_검증한다(응답, false, List.of(상품2, 상품1)); } @Test @@ -436,7 +434,7 @@ class searchProducts_성공_테스트 { // then STATUS_CODE를_검증한다(응답, 정상_처리); - 상품_자동_완성_검색_결과를_검증한다(응답, Collections.emptyList()); + 상품_자동_완성_검색_결과를_검증한다(응답, false, Collections.emptyList()); } @Test @@ -474,7 +472,22 @@ class searchProducts_성공_테스트 { // then STATUS_CODE를_검증한다(응답, 정상_처리); - 상품_자동_완성_검색_결과를_검증한다(응답, List.of(상품11, 상품1, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3)); + 상품_자동_완성_검색_결과를_검증한다(응답, true, List.of(상품11, 상품1, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3)); + } + + @Test + void 검색_결과가_10개_이상일_때_해당_페이지_결과_10개만_반환한다() { + // given + final var 카테고리 = 카테고리_간편식사_생성(); + 단일_카테고리_저장(카테고리); + 반복_애플망고_상품_저장(11, 카테고리); + + // when + final var 응답 = 상품_자동_완성_검색_요청("망고", 0L); + + // then + STATUS_CODE를_검증한다(응답, 정상_처리); + 상품_자동_완성_검색_결과를_검증한다(응답, true, List.of(상품11, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3, 상품2)); } } @@ -683,11 +696,15 @@ class getProductRecipes_성공_테스트 { .isEqualTo(productIds); } - private void 상품_자동_완성_검색_결과를_검증한다(final ExtractableResponse response, final List productIds) { - final var actual = response.jsonPath() + private void 상품_자동_완성_검색_결과를_검증한다(final ExtractableResponse response, final boolean hasNext, + final List productIds) { + final var actualHasNext = response.jsonPath() + .getBoolean("hasNext"); + final var actualProducts = response.jsonPath() .getList("products", SearchProductDto.class); - assertThat(actual).extracting(SearchProductDto::getId) + assertThat(actualHasNext).isEqualTo(hasNext); + assertThat(actualProducts).extracting(SearchProductDto::getId) .isEqualTo(productIds); }