-
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
Changes from 10 commits
63714ef
c91aa31
8e1487c
d4f128a
c2f0d60
78ee605
e75eb46
a8731a3
7583459
c3c1330
53aabb8
1f4819c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 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 { | ||
|
|
||
| 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)); | ||
| } | ||
|
|
||
| @Override | ||
| public Pageable resolveArgument(MethodParameter methodParameter, | ||
| ModelAndViewContainer mavContainer, | ||
| NativeWebRequest webRequest, | ||
| WebDataBinderFactory binderFactory) { | ||
| return super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 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; | ||
| 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); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("provideInvalidParameters") | ||
| void 파라미터가_유효하지_않으면_기본_값을_사용한다(String testName, String pageParam, String sizeParam, int expectedPage, int expectedSize) { | ||
| // given | ||
| request.setParameter(PAGE_PARAMETER, pageParam); | ||
| request.setParameter(SIZE_PARAMETER, sizeParam); | ||
|
|
||
| // when | ||
| Pageable pageable = customPageableHandlerMethodArgumentResolver | ||
| .resolveArgument(parameter, null, webRequest, null); | ||
|
|
||
| // then | ||
| assertThat(pageable.getPageNumber()).isEqualTo(expectedPage); | ||
| assertThat(pageable.getPageSize()).isEqualTo(expectedSize); | ||
| } | ||
|
|
||
| static Stream<Arguments> provideInvalidParameters() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (이 부분을 짚을까 고민이 되었지만..! 제 생각에 이 테스트 코드는 "페이지와 사이즈의 정책을 동시에 테스트하는 함수" 같아요. 그리고 동시에 두가지를 테스트하려고하니, 테스트 함수에 전해줘야 하는 인자도 cf. 만약에 "유효하지 않은 경우"를 두개로 분리하시기로 했다면, "유효한 경우"도 분리하는게 통일감있을 것 같습니다.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 통일성이 있어야할 거 같다고 생각합니다! 말씀해주신 대로 분리를 하는 게 나중에 정책이 바뀌었을 때 더 유연하게 대처할 수 있을 거 같다고 생각해요! 반영해놓겠습니다! |
||
| return Stream.of( | ||
| Arguments.of("Page null", null, "20", DEFAULT_PAGE, 20), | ||
| Arguments.of("Page 빈 문자열", "", "20", DEFAULT_PAGE, 20), | ||
| Arguments.of("Page 0", "0", "20", DEFAULT_PAGE, 20), | ||
| Arguments.of("Page 음수", "-1", "20", DEFAULT_PAGE, 20), | ||
| Arguments.of("Page 문자열", "invalid", "20", DEFAULT_PAGE, 20), | ||
|
|
||
| Arguments.of("Size null", "2", null, 1, DEFAULT_SIZE), | ||
| Arguments.of("Size 빈 문자열", "2", "", 1, DEFAULT_SIZE), | ||
| Arguments.of("Size 0", "2", "0", 1, DEFAULT_SIZE), | ||
| Arguments.of("Size 음수", "2", "-1", 1, DEFAULT_SIZE), | ||
| Arguments.of("Size 문자열", "2", "invalid", 1, DEFAULT_SIZE), | ||
| Arguments.of("Size 최대값 초과", "2", String.valueOf(MAX_SIZE + 1), 1, MAX_SIZE), | ||
|
||
|
|
||
| Arguments.of("모두 null", null, null, DEFAULT_PAGE, DEFAULT_SIZE), | ||
| Arguments.of("모두 빈 문자열", "", "", DEFAULT_PAGE, DEFAULT_SIZE), | ||
| Arguments.of("모두 0", "0", "0", DEFAULT_PAGE, DEFAULT_SIZE), | ||
| Arguments.of("모두 음수", "-1", "-1", DEFAULT_PAGE, DEFAULT_SIZE), | ||
| Arguments.of("모두 문자열", "invalid", "invalid", DEFAULT_PAGE, DEFAULT_SIZE) | ||
| ); | ||
| } | ||
|
|
||
| private static class TestController { | ||
|
|
||
| public void pageableMethod(Pageable pageable) { | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
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.
부모 클래스의 함수를 그대로 호출하는 함수를 Override 할 필요는 없어보입니다. 이 override 함수는 없애는게 좋겠습니다😊