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