-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
212 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/veselov/companybot/annotation/PagingParam.java
This file contains 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,15 @@ | ||
package ru.veselov.companybot.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Mark object that receives Request Params: page и size | ||
* and send it to PagingRequestParamResolver | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PagingParam { | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/ru/veselov/companybot/config/PagingRequestParamsResolver.java
This file contains 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,52 @@ | ||
package ru.veselov.companybot.config; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver; | ||
import ru.veselov.companybot.annotation.PagingParam; | ||
import ru.veselov.companybot.dto.PagingParams; | ||
|
||
/** | ||
* Resolve request params to object marked with {@link PagingParam} annotation | ||
* | ||
* @see PagingParam | ||
* @see PagingParams | ||
*/ | ||
public class PagingRequestParamsResolver extends RequestParamMethodArgumentResolver { | ||
|
||
private static final String PAGE = "page"; | ||
|
||
private static final String SIZE = "size"; | ||
|
||
|
||
public PagingRequestParamsResolver(boolean useDefaultResolution) { | ||
super(useDefaultResolution); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(PagingParam.class); | ||
} | ||
|
||
@Override | ||
protected Object resolveName(@NonNull String name, | ||
@NonNull MethodParameter parameter, | ||
@NonNull NativeWebRequest request) { | ||
return new PagingParams( | ||
resolvePage(request), | ||
resolveSize(request) | ||
); | ||
} | ||
|
||
private Integer resolvePage(NativeWebRequest request) { | ||
String page = request.getParameter(PAGE); | ||
return page == null ? 0 : Integer.parseInt(page); | ||
} | ||
|
||
private Integer resolveSize(NativeWebRequest request) { | ||
String size = request.getParameter(SIZE); | ||
return size == null ? 20 : Integer.parseInt(size); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/ru/veselov/companybot/config/WebMvcConfiguration.java
This file contains 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,36 @@ | ||
package ru.veselov.companybot.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.filter.CommonsRequestLoggingFilter; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@Slf4j | ||
public class WebMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addArgumentResolvers(@NonNull List<HandlerMethodArgumentResolver> resolvers) { | ||
WebMvcConfigurer.super.addArgumentResolvers(resolvers); | ||
resolvers.add(new PagingRequestParamsResolver(true)); | ||
log.debug("Setting up Request Param resolvers"); | ||
} | ||
|
||
@Bean | ||
public CommonsRequestLoggingFilter logFilter() { | ||
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); | ||
filter.setIncludeQueryString(true); | ||
filter.setIncludePayload(false); | ||
filter.setMaxPayloadLength(10000); | ||
filter.setIncludeHeaders(false); | ||
filter.setIncludeClientInfo(true); | ||
log.debug("Setting up Request filter for logging http request/response"); | ||
return filter; | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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 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,17 @@ | ||
package ru.veselov.companybot.dto; | ||
|
||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.PositiveOrZero; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class PagingParams { | ||
|
||
@PositiveOrZero | ||
private Integer page; | ||
|
||
@Positive | ||
private Integer size; | ||
} |
This file contains 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 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 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 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
25 changes: 25 additions & 0 deletions
25
src/test/java/ru/veselov/companybot/it/controller/InquiryControllerIntegrationTest.java
This file contains 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,25 @@ | ||
package ru.veselov.companybot.it.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import ru.veselov.companybot.config.BotMocks; | ||
import ru.veselov.companybot.config.EnableTestContainers; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureMockMvc | ||
@DirtiesContext | ||
@Import({BotMocks.class}) | ||
@ActiveProfiles("test") | ||
@EnableTestContainers | ||
class InquiryControllerIntegrationTest { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
|
||
} |
Oops, something went wrong.