-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: @PageableDefault 및 페이지 검증 로직을 ArgumentResolver로 개선 #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Gyuhyeok99
merged 12 commits into
solid-connection:develop
from
Gyuhyeok99:refactor/246-custom-page-request
Apr 8, 2025
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
63714ef
refactor: 커스텀 PageRequest 및 ArgumentResolver 추가
Gyuhyeok99 c91aa31
refactor: 커스텀 PageRequest 및 ArgumentResolver 등록 및 적용
Gyuhyeok99 8e1487c
refactor: 매직 넘버 상수로 교체
Gyuhyeok99 d4f128a
feat: 커스텀 페이지 요청 argument resolver 추가
Gyuhyeok99 c2f0d60
feat: 커스텀 페이지 요청 argument resolver 적용
Gyuhyeok99 78ee605
Merge branch 'develop' into refactor/246-custom-page-request
Gyuhyeok99 e75eb46
refactor: 모든 유효하지 않는 값에 대해서 기본값으로 대체
Gyuhyeok99 a8731a3
refactor: 최대 사이즈 초과 시 최대 사이즈 반환하도록 수정
Gyuhyeok99 7583459
refactor: DEFAULT_PAGE 0으로 변경
Gyuhyeok99 c3c1330
refactor: ParameterizedTest를 활용하여 중복 테스트 코드 제거
Gyuhyeok99 53aabb8
refactor: 불필요한 override 제거
Gyuhyeok99 1f4819c
refactor: 페이징 처리 테스트코드 분리 및 가독성 개선
Gyuhyeok99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
.../example/solidconnection/custom/resolver/CustomPageableHandlerMethodArgumentResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.example.solidconnection.custom.resolver; | ||
|
|
||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.web.PageableHandlerMethodArgumentResolver; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.bind.support.WebDataBinderFactory; | ||
| import org.springframework.web.context.request.NativeWebRequest; | ||
| import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
|
||
| @Component | ||
| public class CustomPageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolver { | ||
|
|
||
| public static final int MAX_SIZE = 50; | ||
| private static final int DEFAULT_SIZE = 10; | ||
|
|
||
| public CustomPageableHandlerMethodArgumentResolver() { | ||
| setOneIndexedParameters(true); | ||
| setFallbackPageable(PageRequest.of(0, 10)); | ||
| } | ||
|
|
||
| @Override | ||
| public Pageable resolveArgument(MethodParameter methodParameter, | ||
| ModelAndViewContainer mavContainer, | ||
| NativeWebRequest webRequest, | ||
| WebDataBinderFactory binderFactory) { | ||
| Pageable pageable = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory); | ||
| if (pageable.getPageSize() > MAX_SIZE) { | ||
| return PageRequest.of(pageable.getPageNumber(), DEFAULT_SIZE, pageable.getSort()); | ||
| } | ||
| return pageable; | ||
| } | ||
nayonsoso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
26 changes: 0 additions & 26 deletions
26
src/main/java/com/example/solidconnection/util/PagingUtils.java
This file was deleted.
Oops, something went wrong.
161 changes: 161 additions & 0 deletions
161
...mple/solidconnection/custom/resolver/CustomPageableHandlerMethodArgumentResolverTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package com.example.solidconnection.custom.resolver; | ||
|
|
||
| import com.example.solidconnection.support.TestContainerSpringBootTest; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.mock.web.MockHttpServletRequest; | ||
| import org.springframework.web.context.request.NativeWebRequest; | ||
| import org.springframework.web.context.request.ServletWebRequest; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| import static com.example.solidconnection.custom.resolver.CustomPageableHandlerMethodArgumentResolver.MAX_SIZE; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @TestContainerSpringBootTest | ||
| @DisplayName("커스텀 페이지 요청 argument resolver 테스트") | ||
| class CustomPageableHandlerMethodArgumentResolverTest { | ||
|
|
||
| private static final String PAGE_PARAMETER = "page"; | ||
| private static final String SIZE_PARAMETER = "size"; | ||
| private static final int DEFAULT_PAGE = 1; | ||
nayonsoso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final int DEFAULT_SIZE = 10; | ||
|
|
||
| @Autowired | ||
| private CustomPageableHandlerMethodArgumentResolver customPageableHandlerMethodArgumentResolver; | ||
|
|
||
| private MockHttpServletRequest request; | ||
| private NativeWebRequest webRequest; | ||
| private MethodParameter parameter; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws NoSuchMethodException { | ||
| request = new MockHttpServletRequest(); | ||
| webRequest = new ServletWebRequest(request); | ||
| Method method = TestController.class.getMethod("pageableMethod", Pageable.class); | ||
| parameter = new MethodParameter(method, 0); | ||
| } | ||
|
|
||
| @Test | ||
| void 파라미터가_없으면_기본값을_사용한다() { | ||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 유효한_파라미터가_있으면_해당_값을_사용한다() { | ||
| // given | ||
| int expectedPage = 2; | ||
| int expectedSize = 20; | ||
| request.setParameter(PAGE_PARAMETER, String.valueOf(expectedPage)); | ||
| request.setParameter(SIZE_PARAMETER, String.valueOf(expectedSize)); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(expectedPage - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(expectedSize); | ||
| } | ||
|
|
||
| @Test | ||
| void 파라미터가_숫자가_아니면_기본값을_사용한다() { | ||
nayonsoso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // given | ||
| request.setParameter(PAGE_PARAMETER, "invalid"); | ||
| request.setParameter(SIZE_PARAMETER, "invalid"); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 페이지_파라미터가_최소값보다_작으면_기본값을_사용한다() { | ||
| // given | ||
| request.setParameter(PAGE_PARAMETER, "0"); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 페이지_파라미터가_음수이면_기본값을_사용한다() { | ||
| // given | ||
| request.setParameter(PAGE_PARAMETER, "-1"); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 사이즈_파라미터가_최소값보다_작으면_기본값을_사용한다() { | ||
| // given | ||
| request.setParameter(SIZE_PARAMETER, "0"); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 사이즈_파라미터가_최대값보다_크면_기본값을_사용한다() { | ||
| // given | ||
| request.setParameter(SIZE_PARAMETER, String.valueOf(MAX_SIZE + 1)); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 사이즈_파라미터가_음수이면_기본값을_사용한다() { | ||
| // given | ||
| request.setParameter(SIZE_PARAMETER, "-1"); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| private static class TestController { | ||
|
|
||
| public void pageableMethod(Pageable pageable) { | ||
| } | ||
| } | ||
| } | ||
61 changes: 0 additions & 61 deletions
61
src/test/java/com/example/solidconnection/util/PagingUtilsTest.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.