Skip to content

Commit caaa8ad

Browse files
committed
Add integration tests to verify direct field access works.
1 parent 0590cbc commit caaa8ad

File tree

4 files changed

+399
-0
lines changed

4 files changed

+399
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mattbertolini.spring.test.web.bind;
18+
19+
import com.mattbertolini.spring.web.bind.annotation.CookieParameter;
20+
import com.mattbertolini.spring.web.bind.annotation.FormParameter;
21+
import com.mattbertolini.spring.web.bind.annotation.HeaderParameter;
22+
import com.mattbertolini.spring.web.bind.annotation.PathParameter;
23+
import com.mattbertolini.spring.web.bind.annotation.RequestParameter;
24+
import com.mattbertolini.spring.web.bind.annotation.SessionParameter;
25+
26+
@SuppressWarnings("unused")
27+
public class DirectFieldAccessBean {
28+
@CookieParameter("cookie_parameter")
29+
private String cookieParameter;
30+
31+
@FormParameter("form_parameter")
32+
private String formParameter;
33+
34+
@HeaderParameter("header_parameter")
35+
private String headerParameter;
36+
37+
@PathParameter("path_parameter")
38+
private String pathParameter;
39+
40+
@RequestParameter("request_parameter")
41+
private String requestParameter;
42+
43+
@SessionParameter("session_parameter")
44+
private String sessionParameter;
45+
46+
public String getCookieParameter() {
47+
return cookieParameter;
48+
}
49+
50+
public String getFormParameter() {
51+
return formParameter;
52+
}
53+
54+
public String getHeaderParameter() {
55+
return headerParameter;
56+
}
57+
58+
public String getPathParameter() {
59+
return pathParameter;
60+
}
61+
62+
public String getRequestParameter() {
63+
return requestParameter;
64+
}
65+
66+
public String getSessionParameter() {
67+
return sessionParameter;
68+
}
69+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mattbertolini.spring.test.web.bind;
18+
19+
import com.mattbertolini.spring.web.bind.annotation.BeanParameter;
20+
import org.springframework.http.MediaType;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
@RestController
26+
public class DirectFieldAccessController {
27+
@GetMapping(value = "/cookieParameter", produces = MediaType.TEXT_PLAIN_VALUE)
28+
public String cookieParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
29+
return directFieldAccessBean.getCookieParameter();
30+
}
31+
32+
@PostMapping(value = "/formParameter", produces = MediaType.TEXT_PLAIN_VALUE)
33+
public String formParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
34+
return directFieldAccessBean.getFormParameter();
35+
}
36+
37+
@GetMapping(value = "/headerParameter", produces = MediaType.TEXT_PLAIN_VALUE)
38+
public String headerParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
39+
return directFieldAccessBean.getHeaderParameter();
40+
}
41+
42+
@SuppressWarnings("MVCPathVariableInspection")
43+
@GetMapping(value = "/pathParameter/{path_parameter}", produces = MediaType.TEXT_PLAIN_VALUE)
44+
public String pathParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
45+
return directFieldAccessBean.getPathParameter();
46+
}
47+
48+
@GetMapping(value = "/requestParameter", produces = MediaType.TEXT_PLAIN_VALUE)
49+
public String requestParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
50+
return directFieldAccessBean.getRequestParameter();
51+
}
52+
53+
@GetMapping(value = "/sessionParameter", produces = MediaType.TEXT_PLAIN_VALUE)
54+
public String sessionParameter(@BeanParameter DirectFieldAccessBean directFieldAccessBean) {
55+
return directFieldAccessBean.getSessionParameter();
56+
}
57+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2019-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mattbertolini.spring.web.reactive.test;
18+
19+
import com.mattbertolini.spring.test.web.bind.DirectFieldAccessController;
20+
import com.mattbertolini.spring.web.reactive.bind.config.BinderConfiguration;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.format.support.FormattingConversionService;
26+
import org.springframework.http.MediaType;
27+
import org.springframework.lang.NonNull;
28+
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
29+
import org.springframework.test.web.reactive.server.WebTestClient;
30+
import org.springframework.validation.Validator;
31+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
32+
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
33+
import org.springframework.web.context.WebApplicationContext;
34+
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
35+
36+
import static org.springframework.web.reactive.function.BodyInserters.fromFormData;
37+
38+
@SpringJUnitWebConfig(classes = {DirectFieldAccessIntegrationTest.Context.class})
39+
public class DirectFieldAccessIntegrationTest {
40+
41+
private WebTestClient webTestClient;
42+
43+
@BeforeEach
44+
public void setUp(WebApplicationContext webApplicationContext) {
45+
webTestClient = WebTestClient.bindToApplicationContext(webApplicationContext).build();
46+
}
47+
48+
@Test
49+
void cookieParameter() {
50+
webTestClient.get()
51+
.uri("/cookieParameter")
52+
.accept(MediaType.TEXT_PLAIN)
53+
.cookie("cookie_parameter", "expectedValue")
54+
.exchange()
55+
.expectStatus().isOk()
56+
.expectBody(String.class).isEqualTo("expectedValue");
57+
}
58+
59+
@Test
60+
void formParameter() {
61+
webTestClient.post()
62+
.uri("/formParameter")
63+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
64+
.body(fromFormData("form_parameter", "expectedValue"))
65+
.accept(MediaType.TEXT_PLAIN)
66+
.exchange()
67+
.expectStatus().isOk()
68+
.expectBody(String.class).isEqualTo("expectedValue");
69+
}
70+
71+
@Test
72+
void headerParameter() {
73+
webTestClient.get()
74+
.uri("/headerParameter")
75+
.accept(MediaType.TEXT_PLAIN)
76+
.header("header_parameter", "expectedValue")
77+
.exchange()
78+
.expectStatus().isOk()
79+
.expectBody(String.class).isEqualTo("expectedValue");
80+
}
81+
82+
@Test
83+
void pathParameter() {
84+
webTestClient.get()
85+
.uri("/pathParameter/expectedValue")
86+
.accept(MediaType.TEXT_PLAIN)
87+
.exchange()
88+
.expectStatus().isOk()
89+
.expectBody(String.class).isEqualTo("expectedValue");
90+
}
91+
92+
@Test
93+
void requestParameter() {
94+
webTestClient.get()
95+
.uri("/requestParameter?request_parameter=expectedValue")
96+
.accept(MediaType.TEXT_PLAIN)
97+
.exchange()
98+
.expectStatus().isOk()
99+
.expectBody(String.class).isEqualTo("expectedValue");
100+
}
101+
102+
@Test
103+
void sessionParameter() {
104+
webTestClient.mutateWith(SessionMutator.session()
105+
.attribute("session_parameter", "expectedValue"))
106+
.get().uri("/sessionParameter")
107+
.accept(MediaType.TEXT_PLAIN)
108+
.exchange()
109+
.expectStatus().isOk()
110+
.expectBody(String.class).isEqualTo("expectedValue");
111+
}
112+
113+
@Configuration
114+
static class Context extends WebFluxConfigurationSupport {
115+
116+
@Override
117+
@NonNull
118+
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer(@NonNull FormattingConversionService mvcConversionService, @NonNull Validator mvcValidator) {
119+
ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer(mvcConversionService, mvcValidator);
120+
initializer.setDirectFieldAccess(true);
121+
return initializer;
122+
}
123+
124+
@Bean
125+
public BinderConfiguration binderConfiguration() {
126+
return new BinderConfiguration();
127+
}
128+
129+
@Bean
130+
public LocalValidatorFactoryBean validator() {
131+
return new LocalValidatorFactoryBean();
132+
}
133+
134+
@Bean
135+
public DirectFieldAccessController controller() {
136+
return new DirectFieldAccessController();
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)