1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ package io .microsphere .spring .boot .webmvc .autoconfigure ;
19+
20+ import com .fasterxml .jackson .databind .ObjectMapper ;
21+ import io .microsphere .spring .test .domain .User ;
22+ import io .microsphere .spring .test .web .controller .TestController ;
23+ import io .microsphere .spring .test .webmvc .RouterFunctionTestConfig ;
24+ import jakarta .servlet .ServletException ;
25+ import org .junit .jupiter .api .BeforeEach ;
26+ import org .junit .jupiter .api .Disabled ;
27+ import org .junit .jupiter .api .Test ;
28+ import org .springframework .beans .factory .annotation .Autowired ;
29+ import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
30+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
31+ import org .springframework .test .context .web .WebAppConfiguration ;
32+ import org .springframework .test .web .servlet .MockMvc ;
33+ import org .springframework .test .web .servlet .RequestBuilder ;
34+ import org .springframework .web .context .ConfigurableWebApplicationContext ;
35+
36+ import static org .junit .jupiter .api .Assertions .assertThrows ;
37+ import static org .springframework .http .MediaType .APPLICATION_JSON ;
38+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
39+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
40+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
41+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
42+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
43+ import static org .springframework .test .web .servlet .setup .MockMvcBuilders .webAppContextSetup ;
44+
45+ /**
46+ * Abstract class of {@link WebMvcAutoConfiguration} test
47+ *
48+ * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
49+ * @see WebMvcAutoConfiguration
50+ * @since 1.0.0
51+ */
52+ @ Disabled
53+ @ WebAppConfiguration
54+ @ SpringJUnitConfig (classes = {
55+ TestController .class , // Test Controller
56+ RouterFunctionTestConfig .class // Test RouterFunction
57+ })
58+ @ EnableAutoConfiguration
59+ class AbstractWebMvcAutoConfigurationTest {
60+
61+ @ Autowired
62+ protected ConfigurableWebApplicationContext context ;
63+
64+ @ Autowired
65+ protected TestController testController ;
66+
67+ protected MockMvc mockMvc ;
68+
69+ @ BeforeEach
70+ void setUp () {
71+ this .mockMvc = webAppContextSetup (this .context ).build ();
72+ }
73+
74+ @ Test
75+ void test () throws Exception {
76+ testWebEndpoints ();
77+ }
78+
79+ /**
80+ * Test the Web Endpoints
81+ *
82+ * @see #testHelloWorld()
83+ * @see #testGreeting()
84+ * @see #testUser()
85+ * @see #testError()
86+ * @see #testResponseEntity()
87+ * @see #testUpdatePerson()
88+ */
89+ protected void testWebEndpoints () throws Exception {
90+ this .testHelloWorld ();
91+ this .testGreeting ();
92+ this .testUser ();
93+ this .testError ();
94+ this .testResponseEntity ();
95+ this .testUpdatePerson ();
96+ }
97+
98+ /**
99+ * Test {@link TestController#helloWorld()}
100+ *
101+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
102+ */
103+ protected void testHelloWorld () throws Exception {
104+ this .mockMvc .perform (get ("/test/helloworld" ))
105+ .andExpect (status ().isOk ())
106+ .andExpect (content ().string (this .testController .helloWorld ()));
107+ }
108+
109+ /**
110+ * Test {@link TestController#helloWorld()}
111+ *
112+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
113+ */
114+ protected void testGreeting () throws Exception {
115+ String pattern = "/test/greeting/{message}" ;
116+ String message = "Mercy" ;
117+ this .mockMvc .perform (get (pattern , message ))
118+ .andExpect (status ().isOk ())
119+ .andExpect (content ().string (this .testController .greeting (message )));
120+ }
121+
122+ /**
123+ * Test {@link TestController#helloWorld()}
124+ *
125+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
126+ */
127+ protected void testUser () throws Exception {
128+ ObjectMapper objectMapper = new ObjectMapper ();
129+ User user = new User ();
130+ user .setName ("Mercy" );
131+ user .setAge (18 );
132+ String json = objectMapper .writeValueAsString (user );
133+ this .mockMvc .perform (post ("/test/user" )
134+ .contentType (APPLICATION_JSON )
135+ .content (json ))
136+ .andExpect (status ().isOk ())
137+ .andExpect (content ().string (json ));
138+ }
139+
140+ /**
141+ * Test {@link TestController#helloWorld()}
142+ *
143+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
144+ */
145+ protected void testError () {
146+ assertThrows (ServletException .class , () -> this .mockMvc .perform (get ("/test/error" )
147+ .param ("message" , "For testing" )).andReturn ());
148+ }
149+
150+ /**
151+ * Test {@link TestController#helloWorld()}
152+ *
153+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
154+ */
155+ protected void testResponseEntity () throws Exception {
156+ this .mockMvc .perform (put ("/test/response-entity" ))
157+ .andExpect (status ().isOk ())
158+ .andExpect (content ().string (this .testController .responseEntity ().getBody ()));
159+ }
160+
161+ /**
162+ * Test {@link TestController#helloWorld()}
163+ *
164+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
165+ */
166+ protected void testView () throws Exception {
167+ this .mockMvc .perform (get ("/test/view" ))
168+ .andExpect (status ().isOk ())
169+ .andExpect (content ().string ("" ));
170+ }
171+
172+ /**
173+ * Test RouterFunctionTestConfig#nestedPersonRouterFunction(PersonHandler)
174+ *
175+ * @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
176+ */
177+ protected void testUpdatePerson () throws Exception {
178+ this .mockMvc .perform (put ("/test/person/{id}" , "1" ))
179+ .andExpect (status ().isOk ());
180+ }
181+ }
0 commit comments