-
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 all 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
19 changes: 19 additions & 0 deletions
19
.../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,19 @@ | ||
| package com.example.solidconnection.custom.resolver; | ||
|
|
||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.web.PageableHandlerMethodArgumentResolver; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class CustomPageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolver { | ||
|
|
||
| private static final int DEFAULT_PAGE = 0; | ||
| private static final int MAX_SIZE = 50; | ||
| private static final int DEFAULT_SIZE = 10; | ||
|
|
||
| public CustomPageableHandlerMethodArgumentResolver() { | ||
| setMaxPageSize(MAX_SIZE); | ||
| setOneIndexedParameters(true); | ||
| setFallbackPageable(PageRequest.of(DEFAULT_PAGE, DEFAULT_SIZE)); | ||
| } | ||
| } |
26 changes: 0 additions & 26 deletions
26
src/main/java/com/example/solidconnection/util/PagingUtils.java
This file was deleted.
Oops, something went wrong.
133 changes: 133 additions & 0 deletions
133
...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,133 @@ | ||
| 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.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| 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 java.util.stream.Stream; | ||
|
|
||
| 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 = 0; | ||
| private static final int DEFAULT_SIZE = 10; | ||
| private static final int MAX_SIZE = 50; | ||
|
|
||
| @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 유효한_페이지_파라미터가_있으면_해당_값을_사용한다() { | ||
| // given | ||
| int expectedPage = 2; | ||
| request.setParameter(PAGE_PARAMETER, String.valueOf(expectedPage)); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(expectedPage - 1); | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| @Test | ||
| void 유효한_사이즈_파라미터가_있으면_해당_값을_사용한다() { | ||
| // given | ||
| int expectedSize = 20; | ||
| request.setParameter(SIZE_PARAMETER, String.valueOf(expectedSize)); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE); | ||
| assertThat(pageable.getPageSize()).isEqualTo(expectedSize); | ||
| } | ||
|
|
||
| @Test | ||
| void 사이즈_파라미터가_최대값을_초과하면_최대값을_사용한다() { | ||
| // given | ||
| request.setParameter(SIZE_PARAMETER, String.valueOf(MAX_SIZE + 1)); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageSize()).isEqualTo(MAX_SIZE); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("provideInvalidParameters") | ||
| void 페이지_파라미터가_유효하지_않으면_기본_값을_사용한다(String testName, String pageParam) { | ||
| // given | ||
| request.setParameter(PAGE_PARAMETER, pageParam); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(DEFAULT_PAGE); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("provideInvalidParameters") | ||
| void 사이즈_파라미터가_유효하지_않으면_기본_값을_사용한다(String testName, String sizeParam) { | ||
| // given | ||
| request.setParameter(SIZE_PARAMETER, sizeParam); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageSize()).isEqualTo(DEFAULT_SIZE); | ||
| } | ||
|
|
||
| static Stream<Arguments> provideInvalidParameters() { | ||
| return Stream.of( | ||
| Arguments.of("null", null), | ||
| Arguments.of("빈 문자열", ""), | ||
| Arguments.of("0", "0"), | ||
| Arguments.of("음수", "-1"), | ||
| Arguments.of("문자열", "invalid") | ||
| ); | ||
| } | ||
|
|
||
| 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(이 부분을 짚을까 고민이 되었지만..!
규혁님이 앞으로도 테스트 코드 리팩터링을 할 예정이니, 생각해볼만한 지점이 될것 같다 생각해서 말씀드립니다.
다만 이 부분은 제가 너무 과하게 생각하는걸수도 있어요..😓 아니다 싶으면 규혁님 의견대로 가도 됩니다!)
제 생각에 이 테스트 코드는 "페이지와 사이즈의 정책을 동시에 테스트하는 함수" 같아요.
테스트 코드는 작은 영역을 테스트할수록 좋아요.
그 목적이 명확해질 뿐 아니라, 변경에도 유연하게 대처할 수 있기 때문입니다.
예를 들어, '페이지는 기본 값으로 대체되지만, 사이즈는 예외를 던지게 한다' 처럼 정책이 수정된다면,
이 함수는 두개로 나뉘어야 할거예요.
그래서 저라면 '페이지_파라미터가_유효하지_않으면' 함수와 '사이즈_파라미터가_유효하지_않으면' 함수 둘로 나눌 것 같아요.
그렇게 되면 95~100 라인은 중복되는 테스트이니 없앨 수도 있고요!
그리고 동시에 두가지를 테스트하려고하니, 테스트 함수에 전해줘야 하는 인자도
(String testName, String pageParam, String sizeParam, int expectedPage, int expectedSize)이렇게 다섯개나 되어 가독성이 떨어진다는 점도 생각해볼만 한 것 같아요~
cf. 만약에 "유효하지 않은 경우"를 두개로 분리하시기로 했다면, "유효한 경우"도 분리하는게 통일감있을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 통일성이 있어야할 거 같다고 생각합니다!
사실 그래서 성공 케이스에서 페이지랑 사이즈를 동시에 확인하고 있으니 실패 케이스도 그렇게 하는 게 통일감이 있겠다 싶어서 그랬습니다. 😓 그러다보니 자연스레 인자도 늘어나버렸네요.
말씀해주신 대로 분리를 하는 게 나중에 정책이 바뀌었을 때 더 유연하게 대처할 수 있을 거 같다고 생각해요! 반영해놓겠습니다!