1616
1717package io .servicecomb .demo .springmvc .tests ;
1818
19+ import static java .time .temporal .ChronoUnit .SECONDS ;
1920import static org .hamcrest .core .Is .is ;
21+ import static org .junit .Assert .assertArrayEquals ;
22+ import static org .junit .Assert .assertEquals ;
2023import static org .junit .Assert .assertThat ;
24+ import static org .junit .Assert .fail ;
25+ import static org .springframework .http .HttpHeaders .CONTENT_TYPE ;
26+ import static org .springframework .http .HttpMethod .GET ;
27+ import static org .springframework .http .HttpMethod .POST ;
28+ import static org .springframework .http .HttpMethod .PUT ;
29+ import static org .springframework .http .HttpStatus .ACCEPTED ;
2130import static org .springframework .http .HttpStatus .OK ;
31+ import static org .springframework .http .MediaType .APPLICATION_FORM_URLENCODED_VALUE ;
32+ import static org .springframework .http .MediaType .APPLICATION_JSON ;
2233
34+ import io .servicecomb .common .rest .codec .RestObjectMapper ;
35+ import io .servicecomb .demo .compute .Person ;
36+ import io .servicecomb .demo .server .User ;
37+ import java .io .IOException ;
38+ import java .time .ZonedDateTime ;
39+ import java .util .Date ;
40+ import java .util .HashMap ;
41+ import java .util .Map ;
2342import org .junit .BeforeClass ;
2443import org .junit .Test ;
44+ import org .springframework .core .ParameterizedTypeReference ;
45+ import org .springframework .http .HttpEntity ;
46+ import org .springframework .http .HttpHeaders ;
47+ import org .springframework .http .HttpMethod ;
2548import org .springframework .http .ResponseEntity ;
49+ import org .springframework .util .LinkedMultiValueMap ;
50+ import org .springframework .util .MultiValueMap ;
2651import org .springframework .web .client .RestTemplate ;
52+ import org .springframework .web .client .UnknownHttpStatusCodeException ;
2753
2854public class SpringMvcIntegrationTest {
2955
56+ private final String baseUrl = "http://127.0.0.1:8080/" ;
3057 private final RestTemplate restTemplate = new RestTemplate ();
3158
59+ private final String codeFirstUrl = baseUrl + "codeFirstSpringmvc/" ;
60+ private final String controllerUrl = baseUrl + "controller/" ;
61+
3262 @ BeforeClass
3363 public static void setUp () throws Exception {
3464 SpringMvcTestMain .main (new String [0 ]);
@@ -37,7 +67,7 @@ public static void setUp() throws Exception {
3767 @ Test
3868 public void ableToQueryAtRootBasePath () {
3969 ResponseEntity <String > responseEntity = restTemplate
40- .getForEntity ("http://127.0.0.1:8080/ sayHi?name=Mike" , String .class );
70+ .getForEntity (baseUrl + " sayHi?name=Mike" , String .class );
4171
4272 assertThat (responseEntity .getStatusCode (), is (OK ));
4373 assertThat (responseEntity .getBody (), is ("Hi Mike" ));
@@ -46,7 +76,7 @@ public void ableToQueryAtRootBasePath() {
4676 @ Test
4777 public void ableToQueryAtRootPath () {
4878 ResponseEntity <String > responseEntity = restTemplate
49- .getForEntity ("http://127.0.0.1:8080/" , String .class );
79+ .getForEntity (baseUrl , String .class );
5080
5181 assertThat (responseEntity .getStatusCode (), is (OK ));
5282 assertThat (responseEntity .getBody (), is ("Welcome home" ));
@@ -55,9 +85,267 @@ public void ableToQueryAtRootPath() {
5585 @ Test
5686 public void ableToQueryAtNonRootPath () {
5787 ResponseEntity <String > responseEntity = restTemplate
58- .getForEntity ("http://127.0.0.1:8080/ french/bonjour?name=Mike" , String .class );
88+ .getForEntity (baseUrl + " french/bonjour?name=Mike" , String .class );
5989
6090 assertThat (responseEntity .getStatusCode (), is (OK ));
6191 assertThat (responseEntity .getBody (), is ("Bonjour Mike" ));
6292 }
93+
94+ @ Test
95+ public void ableToPostMap () {
96+ Map <String , User > users = new HashMap <>();
97+ users .put ("user1" , userOfNames ("name11" , "name12" ));
98+ users .put ("user2" , userOfNames ("name21" , "name22" ));
99+
100+ ParameterizedTypeReference <Map <String , User >> reference = new ParameterizedTypeReference <Map <String , User >>() {
101+ };
102+ ResponseEntity <Map <String , User >> responseEntity = restTemplate .exchange (codeFirstUrl + "testUserMap" ,
103+ POST ,
104+ jsonRequest (users ),
105+ reference );
106+
107+ assertThat (responseEntity .getStatusCode (), is (OK ));
108+
109+ Map <String , User > body = responseEntity .getBody ();
110+ assertArrayEquals (body .get ("user1" ).getNames (), new String []{"name11" , "name12" });
111+ assertArrayEquals (body .get ("user2" ).getNames (), new String []{"name21" , "name22" });
112+ }
113+
114+ private User userOfNames (String ... names ) {
115+ User user1 = new User ();
116+ user1 .setNames (names );
117+ return user1 ;
118+ }
119+
120+ @ Test
121+ public void ableToConsumeTextPlain () {
122+ String body = "a=1" ;
123+
124+ String result = restTemplate .postForObject (
125+ codeFirstUrl + "textPlain" ,
126+ body ,
127+ String .class );
128+
129+ assertThat (jsonOf (result , String .class ), is (body ));
130+ }
131+
132+ @ Test
133+ public void ableToPostBytes () throws IOException {
134+ byte [] body = new byte []{0 , 1 , 2 };
135+
136+ byte [] result = restTemplate .postForObject (
137+ codeFirstUrl + "bytes" ,
138+ jsonRequest (RestObjectMapper .INSTANCE .writeValueAsBytes (body )),
139+ byte [].class );
140+
141+ result = RestObjectMapper .INSTANCE .readValue (result , byte [].class );
142+
143+ assertEquals (1 , result [0 ]);
144+ assertEquals (1 , result [1 ]);
145+ assertEquals (2 , result [2 ]);
146+ assertEquals (3 , result .length );
147+ }
148+
149+ @ Test
150+ public void ableToPostDate () throws Exception {
151+ ZonedDateTime date = ZonedDateTime .now ().truncatedTo (SECONDS );
152+ MultiValueMap <String , String > body = new LinkedMultiValueMap <>();
153+ body .add ("date" , RestObjectMapper .INSTANCE .convertToString (Date .from (date .toInstant ())));
154+
155+ HttpHeaders headers = new HttpHeaders ();
156+ headers .add (CONTENT_TYPE , APPLICATION_FORM_URLENCODED_VALUE );
157+
158+ int seconds = 1 ;
159+ Date result = restTemplate .postForObject (codeFirstUrl + "addDate?seconds={seconds}" ,
160+ new HttpEntity <>(body , headers ),
161+ Date .class ,
162+ seconds );
163+
164+ assertThat (result , is (Date .from (date .plusSeconds (seconds ).toInstant ())));
165+ }
166+
167+ @ Test
168+ public void ableToDeleteWithQueryString () {
169+ ResponseEntity <String > responseEntity = restTemplate .exchange (codeFirstUrl + "addstring?s=a&s=b" ,
170+ HttpMethod .DELETE ,
171+ null ,
172+ String .class );
173+
174+ assertThat (responseEntity .getBody (), is ("ab" ));
175+ }
176+
177+ @ Test
178+ public void ableToGetBoolean () {
179+ boolean result = restTemplate .getForObject (codeFirstUrl + "istrue" , boolean .class );
180+
181+ assertThat (result , is (true ));
182+ }
183+
184+ @ Test
185+ public void putsEndWithPathParam () {
186+ ResponseEntity <String > responseEntity = restTemplate
187+ .exchange (codeFirstUrl + "sayhi/{name}" , PUT , null , String .class , "world" );
188+
189+ assertThat (responseEntity .getStatusCode (), is (ACCEPTED ));
190+ assertThat (jsonOf (responseEntity .getBody (), String .class ), is ("world sayhi" ));
191+ }
192+
193+ @ Test
194+ public void putsContainingPathParam () {
195+ ResponseEntity <String > responseEntity = restTemplate
196+ .exchange (codeFirstUrl + "sayhi/{name}/v2" , PUT , null , String .class , "world" );
197+
198+ assertThat (jsonOf (responseEntity .getBody (), String .class ), is ("world sayhi 2" ));
199+ }
200+
201+ @ Test
202+ public void ableToPostWithHeader () {
203+ Person person = new Person ();
204+ person .setName ("person name" );
205+
206+ HttpHeaders headers = new HttpHeaders ();
207+ headers .setContentType (APPLICATION_JSON );
208+ headers .add ("prefix" , "prefix prefix" );
209+
210+ HttpEntity <Person > requestEntity = new HttpEntity <>(person , headers );
211+ ResponseEntity <String > responseEntity = restTemplate
212+ .postForEntity (codeFirstUrl + "saysomething" , requestEntity , String .class );
213+
214+ assertThat (jsonOf (responseEntity .getBody (), String .class ), is ("prefix prefix person name" ));
215+ }
216+
217+ @ Test
218+ public void ableToPostObjectAsJson () {
219+ Map <String , String > personFieldMap = new HashMap <>();
220+ personFieldMap .put ("name" , "person name from map" );
221+
222+ Person person = restTemplate
223+ .postForObject (codeFirstUrl + "sayhello" , jsonRequest (personFieldMap ), Person .class );
224+ assertThat (person .toString (), is ("hello person name from map" ));
225+
226+ Person input = new Person ();
227+ input .setName ("person name from Object" );
228+ person = restTemplate .postForObject (codeFirstUrl + "sayhello" , jsonRequest (input ), Person .class );
229+
230+ assertThat (person .toString (), is ("hello person name from Object" ));
231+ }
232+
233+ @ Test
234+ public void ableToPostForm () {
235+ MultiValueMap <String , String > params = new LinkedMultiValueMap <>();
236+ params .add ("a" , "5" );
237+ params .add ("b" , "3" );
238+
239+ HttpHeaders headers = new HttpHeaders ();
240+ headers .add (CONTENT_TYPE , APPLICATION_FORM_URLENCODED_VALUE );
241+ int result = restTemplate
242+ .postForObject (codeFirstUrl + "add" , new HttpEntity <>(params , headers ), Integer .class );
243+
244+ assertThat (result , is (8 ));
245+ }
246+
247+ @ Test
248+ public void ableToExchangeCookie () {
249+ Map <String , String > params = new HashMap <>();
250+ params .put ("a" , "5" );
251+
252+ HttpHeaders headers = new HttpHeaders ();
253+ headers .add (HttpHeaders .COOKIE , "b=3" );
254+
255+ HttpEntity <?> requestEntity = new HttpEntity <>(headers );
256+ ResponseEntity <Integer > result = restTemplate .exchange (
257+ codeFirstUrl + "reduce?a={a}" ,
258+ GET ,
259+ requestEntity ,
260+ Integer .class ,
261+ params );
262+
263+ assertThat (result .getBody (), is (2 ));
264+ }
265+
266+ @ Test
267+ public void getsEndWithRequestVariables () {
268+ int result = restTemplate .getForObject (
269+ controllerUrl + "add?a={a}&b={b}" ,
270+ Integer .class ,
271+ 3 , 4 );
272+
273+ assertThat (result , is (7 ));
274+ }
275+
276+ @ Test
277+ public void postsEndWithPathParam () {
278+ String result = restTemplate .postForObject (
279+ controllerUrl + "sayhello/{name}" ,
280+ null ,
281+ String .class ,
282+ "world" );
283+
284+ assertThat (jsonOf (result , String .class ), is ("hello world" ));
285+ }
286+
287+ @ Test
288+ public void ableToPostObjectAsJsonWithRequestVariable () {
289+ Person input = new Person ();
290+ input .setName ("world" );
291+
292+ String result = restTemplate .postForObject (
293+ controllerUrl + "saysomething?prefix={prefix}" ,
294+ jsonRequest (input ),
295+ String .class ,
296+ "hello" );
297+
298+ assertThat (jsonOf (result , String .class ), is ("hello world" ));
299+ }
300+
301+ @ Test
302+ public void ensureServerWorksFine () {
303+ String result = restTemplate .getForObject (
304+ controllerUrl + "sayhi?name=world" ,
305+ String .class );
306+
307+ assertThat (jsonOf (result , String .class ), is ("hi world [world]" ));
308+ }
309+
310+ @ Test
311+ public void ensureServerBlowsUp () {
312+
313+ try {
314+ restTemplate .getForObject (
315+ controllerUrl + "sayhi?name=throwexception" ,
316+ String .class );
317+ fail ("Exception excepted, but thrown none" );
318+ } catch (UnknownHttpStatusCodeException e ) {
319+ assertThat (e .getRawStatusCode (), is (590 ));
320+ }
321+ }
322+
323+ @ Test
324+ public void ableToSetCustomHeader () {
325+ HttpHeaders headers = new HttpHeaders ();
326+ headers .set ("name" , "world" );
327+
328+ HttpEntity <?> requestEntity = new HttpEntity <>(headers );
329+ ResponseEntity <String > result = restTemplate .exchange (
330+ controllerUrl + "sayhei" ,
331+ GET ,
332+ requestEntity ,
333+ String .class );
334+
335+ assertThat (jsonOf (result .getBody (), String .class ), is ("hei world" ));
336+ }
337+
338+ private <T > HttpEntity <T > jsonRequest (T body ) {
339+ HttpHeaders headers = new HttpHeaders ();
340+ headers .setContentType (APPLICATION_JSON );
341+ return new HttpEntity <>(body , headers );
342+ }
343+
344+ private <T > T jsonOf (String json , Class <T > aClass ) {
345+ try {
346+ return RestObjectMapper .INSTANCE .readValue (json , aClass );
347+ } catch (IOException e ) {
348+ throw new IllegalStateException ("Failed to read JSON from " + json + e );
349+ }
350+ }
63351}
0 commit comments