Polish ModelAttributeMethodArgumentResolverTests
This commit is contained in:
parent
c5bdd9d79d
commit
8af9648c43
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.web.reactive.result.method.annotation;
|
package org.springframework.web.reactive.result.method.annotation;
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
@ -112,7 +111,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createAndBind() throws Exception {
|
void createAndBind() {
|
||||||
testBindPojo("pojo", this.testMethod.annotPresent(ModelAttribute.class).arg(Pojo.class), value -> {
|
testBindPojo("pojo", this.testMethod.annotPresent(ModelAttribute.class).arg(Pojo.class), value -> {
|
||||||
assertThat(value.getClass()).isEqualTo(Pojo.class);
|
assertThat(value.getClass()).isEqualTo(Pojo.class);
|
||||||
return (Pojo) value;
|
return (Pojo) value;
|
||||||
|
@ -120,7 +119,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createAndBindToMono() throws Exception {
|
void createAndBindToMono() {
|
||||||
MethodParameter parameter = this.testMethod
|
MethodParameter parameter = this.testMethod
|
||||||
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Pojo.class);
|
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Pojo.class);
|
||||||
|
|
||||||
|
@ -133,7 +132,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createAndBindToSingle() throws Exception {
|
void createAndBindToSingle() {
|
||||||
MethodParameter parameter = this.testMethod
|
MethodParameter parameter = this.testMethod
|
||||||
.annotPresent(ModelAttribute.class).arg(Single.class, Pojo.class);
|
.annotPresent(ModelAttribute.class).arg(Single.class, Pojo.class);
|
||||||
|
|
||||||
|
@ -146,7 +145,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createButDoNotBind() throws Exception {
|
void createButDoNotBind() {
|
||||||
MethodParameter parameter =
|
MethodParameter parameter =
|
||||||
this.testMethod.annotPresent(ModelAttribute.class).arg(NonBindingPojo.class);
|
this.testMethod.annotPresent(ModelAttribute.class).arg(NonBindingPojo.class);
|
||||||
|
|
||||||
|
@ -157,7 +156,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createButDoNotBindToMono() throws Exception {
|
void createButDoNotBindToMono() {
|
||||||
MethodParameter parameter =
|
MethodParameter parameter =
|
||||||
this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, NonBindingPojo.class);
|
this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, NonBindingPojo.class);
|
||||||
|
|
||||||
|
@ -170,7 +169,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createButDoNotBindToSingle() throws Exception {
|
void createButDoNotBindToSingle() {
|
||||||
MethodParameter parameter =
|
MethodParameter parameter =
|
||||||
this.testMethod.annotPresent(ModelAttribute.class).arg(Single.class, NonBindingPojo.class);
|
this.testMethod.annotPresent(ModelAttribute.class).arg(Single.class, NonBindingPojo.class);
|
||||||
|
|
||||||
|
@ -183,7 +182,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createButDoNotBindToPojo(String modelKey, MethodParameter methodParameter,
|
private void createButDoNotBindToPojo(String modelKey, MethodParameter methodParameter,
|
||||||
Function<Object, NonBindingPojo> valueExtractor) throws Exception {
|
Function<Object, NonBindingPojo> valueExtractor) {
|
||||||
|
|
||||||
Object value = createResolver()
|
Object value = createResolver()
|
||||||
.resolveArgument(methodParameter, this.bindContext, postForm("name=Enigma"))
|
.resolveArgument(methodParameter, this.bindContext, postForm("name=Enigma"))
|
||||||
|
@ -202,7 +201,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindExisting() throws Exception {
|
void bindExisting() {
|
||||||
Pojo pojo = new Pojo();
|
Pojo pojo = new Pojo();
|
||||||
pojo.setName("Jim");
|
pojo.setName("Jim");
|
||||||
this.bindContext.getModel().addAttribute(pojo);
|
this.bindContext.getModel().addAttribute(pojo);
|
||||||
|
@ -217,7 +216,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindExistingMono() throws Exception {
|
void bindExistingMono() {
|
||||||
Pojo pojo = new Pojo();
|
Pojo pojo = new Pojo();
|
||||||
pojo.setName("Jim");
|
pojo.setName("Jim");
|
||||||
this.bindContext.getModel().addAttribute("pojoMono", Mono.just(pojo));
|
this.bindContext.getModel().addAttribute("pojoMono", Mono.just(pojo));
|
||||||
|
@ -232,7 +231,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindExistingSingle() throws Exception {
|
void bindExistingSingle() {
|
||||||
Pojo pojo = new Pojo();
|
Pojo pojo = new Pojo();
|
||||||
pojo.setName("Jim");
|
pojo.setName("Jim");
|
||||||
this.bindContext.getModel().addAttribute("pojoSingle", Single.just(pojo));
|
this.bindContext.getModel().addAttribute("pojoSingle", Single.just(pojo));
|
||||||
|
@ -247,7 +246,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindExistingMonoToMono() throws Exception {
|
void bindExistingMonoToMono() {
|
||||||
Pojo pojo = new Pojo();
|
Pojo pojo = new Pojo();
|
||||||
pojo.setName("Jim");
|
pojo.setName("Jim");
|
||||||
String modelKey = "pojoMono";
|
String modelKey = "pojoMono";
|
||||||
|
@ -264,8 +263,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testBindPojo(String modelKey, MethodParameter param, Function<Object, Pojo> valueExtractor)
|
private void testBindPojo(String modelKey, MethodParameter param, Function<Object, Pojo> valueExtractor) {
|
||||||
throws Exception {
|
|
||||||
|
|
||||||
Object value = createResolver()
|
Object value = createResolver()
|
||||||
.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25"))
|
.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25"))
|
||||||
|
@ -284,13 +282,13 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validationErrorForPojo() throws Exception {
|
void validationErrorForPojo() {
|
||||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Pojo.class);
|
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Pojo.class);
|
||||||
testValidationError(parameter, Function.identity());
|
testValidationError(parameter, Function.identity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validationErrorForMono() throws Exception {
|
void validationErrorForMono() {
|
||||||
MethodParameter parameter = this.testMethod
|
MethodParameter parameter = this.testMethod
|
||||||
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Pojo.class);
|
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Pojo.class);
|
||||||
|
|
||||||
|
@ -303,7 +301,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validationErrorForSingle() throws Exception {
|
void validationErrorForSingle() {
|
||||||
MethodParameter parameter = this.testMethod
|
MethodParameter parameter = this.testMethod
|
||||||
.annotPresent(ModelAttribute.class).arg(Single.class, Pojo.class);
|
.annotPresent(ModelAttribute.class).arg(Single.class, Pojo.class);
|
||||||
|
|
||||||
|
@ -316,13 +314,13 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validationErrorWithoutBindingForPojo() throws Exception {
|
void validationErrorWithoutBindingForPojo() {
|
||||||
MethodParameter parameter = this.testMethod.annotPresent(ModelAttribute.class).arg(ValidatedPojo.class);
|
MethodParameter parameter = this.testMethod.annotPresent(ModelAttribute.class).arg(ValidatedPojo.class);
|
||||||
testValidationErrorWithoutBinding(parameter, Function.identity());
|
testValidationErrorWithoutBinding(parameter, Function.identity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validationErrorWithoutBindingForMono() throws Exception {
|
void validationErrorWithoutBindingForMono() {
|
||||||
MethodParameter parameter = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, ValidatedPojo.class);
|
MethodParameter parameter = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, ValidatedPojo.class);
|
||||||
|
|
||||||
testValidationErrorWithoutBinding(parameter, resolvedArgumentMono -> {
|
testValidationErrorWithoutBinding(parameter, resolvedArgumentMono -> {
|
||||||
|
@ -343,20 +341,16 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testValidationError(MethodParameter parameter, Function<Mono<?>, Mono<?>> valueMonoExtractor)
|
private void testValidationError(MethodParameter parameter, Function<Mono<?>, Mono<?>> valueMonoExtractor) {
|
||||||
throws URISyntaxException {
|
|
||||||
|
|
||||||
testValidationError(parameter, valueMonoExtractor, "age=invalid", "age", "invalid");
|
testValidationError(parameter, valueMonoExtractor, "age=invalid", "age", "invalid");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testValidationErrorWithoutBinding(MethodParameter parameter, Function<Mono<?>, Mono<?>> valueMonoExtractor)
|
private void testValidationErrorWithoutBinding(MethodParameter parameter, Function<Mono<?>, Mono<?>> valueMonoExtractor) {
|
||||||
throws URISyntaxException {
|
|
||||||
|
|
||||||
testValidationError(parameter, valueMonoExtractor, "name=Enigma", "name", null);
|
testValidationError(parameter, valueMonoExtractor, "name=Enigma", "name", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor,
|
private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor,
|
||||||
String formData, String field, String rejectedValue) throws URISyntaxException {
|
String formData, String field, String rejectedValue) {
|
||||||
|
|
||||||
Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, postForm(formData));
|
Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, postForm(formData));
|
||||||
mono = valueMonoExtractor.apply(mono);
|
mono = valueMonoExtractor.apply(mono);
|
||||||
|
@ -373,7 +367,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindDataClass() throws Exception {
|
void bindDataClass() {
|
||||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(DataClass.class);
|
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(DataClass.class);
|
||||||
|
|
||||||
Object value = createResolver()
|
Object value = createResolver()
|
||||||
|
@ -401,7 +395,7 @@ class ModelAttributeMethodArgumentResolverTests {
|
||||||
return new ModelAttributeMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance(), false);
|
return new ModelAttributeMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServerWebExchange postForm(String formData) throws URISyntaxException {
|
private ServerWebExchange postForm(String formData) {
|
||||||
return MockServerWebExchange.from(MockServerHttpRequest.post("/")
|
return MockServerWebExchange.from(MockServerHttpRequest.post("/")
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
.body(formData));
|
.body(formData));
|
||||||
|
|
Loading…
Reference in New Issue