c551c85685fe77cb481f7676a998287a1c6b9af3
[JavaForFun] /
1 package de.spring.webservices.rest;
2
3 import static org.hamcrest.Matchers.any;
4 import static org.hamcrest.Matchers.is;
5 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
7 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
8 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
9 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.springframework.http.HttpHeaders;
16 import org.springframework.http.MediaType;
17 import org.springframework.test.context.ContextConfiguration;
18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19 import org.springframework.test.web.servlet.MockMvc;
20 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24
25 import de.spring.webservices.domain.Car;
26
27
28 @RunWith(SpringJUnit4ClassRunner.class)
29 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
30 public class CarControllerIntegrationTest {
31         private CarController controller;
32         private MockMvc mockMvc;
33         
34     @Before
35     public void setup() {
36         controller = new CarController();
37         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
38     }
39
40         @Test
41         public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
42                 mockMvc.perform(get("/api/cars/")
43                                 .accept(MediaType.APPLICATION_JSON_UTF8))
44                 
45                 .andExpect(status().isOk())
46                 .andExpect(jsonPath("$[0].id", any(Integer.class)))
47                 .andExpect(jsonPath("$[0].content", is("Car: 1")))
48                 .andExpect(jsonPath("$[1].content", is("Car: 2")))
49                 .andExpect(jsonPath("$[1].id", any(Integer.class)))
50                 .andExpect(jsonPath("$[2].content", is("Car: 3")))
51                 .andExpect(jsonPath("$[2].id", any(Integer.class)))
52                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
53         }
54         
55         @Test
56         public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
57                 mockMvc.perform(get("/api/cars/{id}", 1L)
58                                 .accept(MediaType.APPLICATION_JSON_UTF8))
59         
60                 .andExpect(status().isOk())
61                 .andExpect(jsonPath("id", any(Integer.class)))
62                 .andExpect(jsonPath("content", is("Car: 1")))
63                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
64         }
65         
66         @Test
67         public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
68                 Car car = new Car(2L, "nothing");
69                 mockMvc.perform(post("/api/cars/")
70                                 .contentType(MediaType.APPLICATION_JSON_UTF8)
71                                 .content(asJsonString(car))
72                                 .accept(MediaType.APPLICATION_JSON_UTF8))
73                 
74                 .andExpect(status().isCreated())
75                 .andExpect(jsonPath("id", any(Integer.class)))
76                 .andExpect(jsonPath("content", is("Car: 1")))
77                 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
78                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
79         }
80         
81         private static String asJsonString(final Object obj) throws JsonProcessingException {
82                 final ObjectMapper mapper = new ObjectMapper();
83                 return mapper.writeValueAsString(obj);
84         }
85 }