Skip to content

Commit

Permalink
[BugFix/BE] 페이지 조회 시 중복되는 아이템 조회되는 이슈 해결 (#318)
Browse files Browse the repository at this point in the history
* refactor: 기본 정렬 기준 적용

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

* [BE] 페이지 조회 시 중복되는 아이템 조회되는 이슈 피드백 반영완료 (#332)

test: 제품을 조회할 때, 정렬 조건으로 id에 대한 역순을 적용하더라도, 정렬이 되는지 확인하는 test 추가

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>
  • Loading branch information
3 people authored and Ohzzi committed Aug 2, 2022
1 parent 6910731 commit 81ffee8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
Expand All @@ -32,7 +34,13 @@ public Pageable resolveArgument(final MethodParameter methodParameter, final Mod
validatePage(pageArgument);
final String sizeArgument = webRequest.getParameter("size");
validateSize(sizeArgument);
return super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
final Pageable pageable = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
return addSecondarySortById(pageable);
}

private Pageable addSecondarySortById(final Pageable pageable) {
final Sort sort = pageable.getSort().and(Sort.by("id").descending());
return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
}

private void validatePage(final String pageArgument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class ProductAcceptanceTest extends AcceptanceTest {
@Test
void 모든_제품_목록을_페이징하여_조회한다() {
// given
Product product = 제품을_저장한다(KEYBOARD_1.생성());
제품을_저장한다(MOUSE_1.생성());
제품을_저장한다(KEYBOARD_1.생성());
Product product = 제품을_저장한다(MOUSE_1.생성());

// when
ExtractableResponse<Response> response = GET_요청을_보낸다("/api/v1/products?page=0&size=1");
Expand All @@ -84,44 +84,89 @@ class ProductAcceptanceTest extends AcceptanceTest {
@Test
void 특정_카테고리_목록을_페이징하여_조회한다() {
// given
제품을_저장한다(KEYBOARD_1.생성());
제품을_저장한다(KEYBOARD_2.생성());
Product product = 제품을_저장한다(MOUSE_1.생성());
Product keyboard1 = 제품을_저장한다(KEYBOARD_1.생성());
Product keyboard2 = 제품을_저장한다(KEYBOARD_2.생성());
제품을_저장한다(MOUSE_1.생성());

// when
ExtractableResponse<Response> response = GET_요청을_보낸다("/api/v1/products?category=mouse&page=0&size=1");
ExtractableResponse<Response> response = GET_요청을_보낸다("/api/v1/products?category=keyboard&page=0&size=2");

// then
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(response.as(ProductPageResponse.class).getItems())
.extracting("id")
.containsExactly(product.getId()),
.containsExactly(keyboard2.getId(), keyboard1.getId()),
() -> assertThat(response.as(ProductPageResponse.class).isHasNext()).isFalse()
);
}

@Test
void 키보드_목록을_리뷰가_많은_순서로_페이징하여_조회한다() {
// given
Product product1 = 제품을_저장한다(KEYBOARD_1.생성());
Product product2 = 제품을_저장한다(KEYBOARD_2.생성());
String corinneToken = 로그인을_한다(CORINNE_GITHUB.getCode()).getToken();
String minchoToken = 로그인을_한다(MINCHO_GITHUB.getCode()).getToken();
REVIEW_RATING_5.작성_요청을_보낸다(product1.getId(), corinneToken);
REVIEW_RATING_4.작성_요청을_보낸다(product1.getId(), minchoToken);
REVIEW_RATING_3.작성_요청을_보낸다(product2.getId(), minchoToken);

// when
ExtractableResponse<Response> response = GET_요청을_보낸다(
"/api/v1/products?category=keyboard&page=0&size=2&sort=reviewCount,desc");

// then
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(response.as(ProductPageResponse.class).getItems())
.extracting("id")
.containsExactly(product1.getId(), product2.getId()),
() -> assertThat(response.as(ProductPageResponse.class).isHasNext()).isFalse()
);
}

@Test
void 리뷰_개수가_같은_상태에서_제품_목록을_리뷰가_많은_순서로_페이징하여_조회하면_id_역순으로_조회된다() {
// given
Product product1 = 제품을_저장한다(KEYBOARD_1.생성());
Product product2 = 제품을_저장한다(KEYBOARD_2.생성());
String token = 로그인을_한다(CORINNE_GITHUB.getCode()).getToken();
REVIEW_RATING_5.작성_요청을_보낸다(product1.getId(), token);
REVIEW_RATING_4.작성_요청을_보낸다(product1.getId(), token);
REVIEW_RATING_3.작성_요청을_보낸다(product2.getId(), token);

// when
ExtractableResponse<Response> response = GET_요청을_보낸다(
"/api/v1/products?category=keyboard&page=0&size=1&sort=reviewCount,desc");
"/api/v1/products?category=keyboard&page=0&size=2&sort=reviewCount,desc");

// then
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(response.as(ProductPageResponse.class).getItems())
.extracting("id")
.containsExactly(product1.getId()),
() -> assertThat(response.as(ProductPageResponse.class).isHasNext()).isTrue()
.containsExactly(product2.getId(), product1.getId()),
() -> assertThat(response.as(ProductPageResponse.class).isHasNext()).isFalse()
);
}

@Test
void 정렬_조건으로_id_역순으로_페이징하여_조회하면_id_역순으로_조회된다() {
// given
Product product1 = 제품을_저장한다(KEYBOARD_1.생성());
Product product2 = 제품을_저장한다(KEYBOARD_2.생성());
Product product3 = 제품을_저장한다(MOUSE_1.생성());

// when
ExtractableResponse<Response> response = GET_요청을_보낸다(
"/api/v1/products?page=0&size=3&sort=id,desc");

// then
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(response.as(ProductPageResponse.class).getItems())
.extracting("id")
.containsExactly(product3.getId(), product2.getId(), product1.getId()),
() -> assertThat(response.as(ProductPageResponse.class).isHasNext()).isFalse()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public class CustomPageableArgumentResolverTest {

// then
verify(productService)
.findPage(null, PageRequest.of(0, 20));
.findPage(null, PageRequest.of(0, 20, Sort.by("id").descending()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
Expand Down Expand Up @@ -244,7 +245,7 @@ class MemberControllerTest {
void 키워드와_옵션으로_회원을_조회한다() throws Exception {
// given
MemberSearchRequest memberSearchRequest = new MemberSearchRequest("cheese", NONE_CONSTANT, BACKEND_CONSTANT);
Pageable pageable = PageRequest.of(0, 10);
Pageable pageable = PageRequest.of(0, 10, Sort.by("id").descending());
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(CORINNE.생성(1L),
KEYBOARD_1.생성(1L));
Member member = CORINNE.대표장비를_해서_생성(1L, inventoryProduct);
Expand Down Expand Up @@ -280,7 +281,7 @@ class MemberControllerTest {
void 회원_조회_성공_키워드와_옵션값이_주어지지_않을때() throws Exception {
// given
MemberSearchRequest memberSearchRequest = new MemberSearchRequest(null, null, null);
Pageable pageable = PageRequest.of(0, 10);
Pageable pageable = PageRequest.of(0, 10, Sort.by("id").descending());
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(CORINNE.생성(1L),
KEYBOARD_1.생성(1L));
Member member = CORINNE.대표장비를_해서_생성(1L, inventoryProduct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class ProductControllerTest {
.andDo(print());

// then
verify(productService).findPage(KEYBOARD_CONSTANT, PageRequest.of(0, 150, Sort.by("rating").descending()));
verify(productService).findPage(KEYBOARD_CONSTANT,
PageRequest.of(0, 150, Sort.by("rating", "id").descending()));
}

@Test
Expand All @@ -79,7 +80,7 @@ class ProductControllerTest {
.andDo(print());

// then
verify(productService).findPage(null, PageRequest.of(0, 150, Sort.by("rating").descending()));
verify(productService).findPage(null, PageRequest.of(0, 150, Sort.by("rating", "id").descending()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public ReviewControllerTest() {
.andDo(print());

// then
verify(reviewService).findPage(PageRequest.of(0, 150, Sort.by("rating").descending()));
verify(reviewService).findPage(PageRequest.of(0, 150, Sort.by("rating", "id").descending()));
}

@Test
Expand All @@ -367,7 +367,8 @@ public ReviewControllerTest() {
.andDo(print());

// then
verify(reviewService).findPageByProductId(PRODUCT_ID, PageRequest.of(0, 150, Sort.by("rating").descending()));
verify(reviewService).findPageByProductId(PRODUCT_ID,
PageRequest.of(0, 150, Sort.by("rating", "id").descending()));
}

@Test
Expand All @@ -382,7 +383,9 @@ public ReviewControllerTest() {
.andDo(print());

// then
verify(reviewService).findPageByProductId(0L, PageRequest.of(0, 150, Sort.by("rating").descending()));
PageRequest pageable = PageRequest.of(0, 150,
Sort.by("rating", "id").descending());
verify(reviewService).findPageByProductId(0L, pageable);
}

@Test
Expand Down

0 comments on commit 81ffee8

Please sign in to comment.