053f7b9ce680e1711292a1cca27f7f602f41917e
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import static org.hamcrest.Matchers.any;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.BDDMockito.given;
6 import static org.mockito.Mockito.mock;
7 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
9 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
10 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
11 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.springframework.data.domain.PageImpl;
21 import org.springframework.data.domain.PageRequest;
22 import org.springframework.http.HttpHeaders;
23 import org.springframework.http.MediaType;
24 import org.springframework.test.context.ContextConfiguration;
25 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26 import org.springframework.test.web.servlet.MockMvc;
27 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
28
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 import de.spring.webservices.domain.Car;
33 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
34
35
36 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ 
37
38 @RunWith(SpringJUnit4ClassRunner.class)
39 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
40 public class CarControllerIntegrationTest {
41         private static final int PAGE = 2;
42         private static final int PAGE_SIZE = 10;
43         private static final String TEMPLATE = "Car: %s";
44         
45         private AwesomeBusinessLogic awesomeBusinessLogic;
46         private CarController controller;
47         private MockMvc mockMvc;
48         
49     @Before
50     public void setup() {
51         awesomeBusinessLogic = mock(AwesomeBusinessLogic.class);
52         controller = new CarController(awesomeBusinessLogic);
53         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
54     }
55
56         @Test
57         public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
58         final List<Car> cars = new ArrayList<>();
59         cars.add(new Car(1L, String.format(TEMPLATE, 1)));
60                 given(awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(new PageImpl<>(cars));
61                 
62                 mockMvc.perform(get("/api/cars/")
63                                 .accept(MediaType.APPLICATION_JSON_UTF8))
64                 
65                 .andExpect(status().isOk())
66                 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
67                 .andExpect(jsonPath("$.content[0].content", is("Car: 1")))
68                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
69         }
70         
71         @Test
72         public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
73                 given(awesomeBusinessLogic.findById(1L)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
74
75                 mockMvc.perform(get("/api/cars/{id}", 1L)
76                                 .accept(MediaType.APPLICATION_JSON_UTF8))
77         
78                 .andExpect(status().isOk())
79                 .andExpect(jsonPath("id", any(Integer.class)))
80                 .andExpect(jsonPath("content", is("Car: 1")))
81                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
82         }
83         
84         @Test
85         public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
86                 Car car = new Car(null, "nothing");
87                 given(awesomeBusinessLogic.create(car)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
88
89                 mockMvc.perform(post("/api/cars/")
90                                 .contentType(MediaType.APPLICATION_JSON_UTF8)
91                                 .content(asJsonString(car))
92                                 .accept(MediaType.APPLICATION_JSON_UTF8))
93                 
94                 .andExpect(status().isCreated())
95                 .andExpect(jsonPath("id", any(Integer.class)))
96                 .andExpect(jsonPath("content", is("Car: 1")))
97                 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
98                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
99         }
100         
101         private static String asJsonString(final Object obj) throws JsonProcessingException {
102                 final ObjectMapper mapper = new ObjectMapper();
103                 return mapper.writeValueAsString(obj);
104         }
105 }