06ce4108f058db72f92badcc572d8b5e5d17474b
[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 @Ignore
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         cars.add(new Car(2L, String.format(TEMPLATE, 2)));
61         cars.add(new Car(3L, String.format(TEMPLATE, 3)));
62         
63                 given(awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(new PageImpl<>(cars));
64                 
65                 mockMvc.perform(get("/api/cars/")
66                                 .accept(MediaType.APPLICATION_JSON_UTF8))
67                 
68                 .andExpect(status().isOk())
69                 .andExpect(jsonPath("$[0].id", any(Integer.class)))
70                 .andExpect(jsonPath("$[0].content", is("Car: 1")))
71                 .andExpect(jsonPath("$[1].content", is("Car: 2")))
72                 .andExpect(jsonPath("$[1].id", any(Integer.class)))
73                 .andExpect(jsonPath("$[2].content", is("Car: 3")))
74                 .andExpect(jsonPath("$[2].id", any(Integer.class)))
75                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
76         }
77         
78         @Test
79         public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
80                 mockMvc.perform(get("/api/cars/{id}", 1L)
81                                 .accept(MediaType.APPLICATION_JSON_UTF8))
82         
83                 .andExpect(status().isOk())
84                 .andExpect(jsonPath("id", any(Integer.class)))
85                 .andExpect(jsonPath("content", is("Car: 1")))
86                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
87         }
88         
89         @Test
90         public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
91                 Car car = new Car(2L, "nothing");
92                 mockMvc.perform(post("/api/cars/")
93                                 .contentType(MediaType.APPLICATION_JSON_UTF8)
94                                 .content(asJsonString(car))
95                                 .accept(MediaType.APPLICATION_JSON_UTF8))
96                 
97                 .andExpect(status().isCreated())
98                 .andExpect(jsonPath("id", any(Integer.class)))
99                 .andExpect(jsonPath("content", is("Car: 1")))
100                 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
101                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
102         }
103         
104         private static String asJsonString(final Object obj) throws JsonProcessingException {
105                 final ObjectMapper mapper = new ObjectMapper();
106                 return mapper.writeValueAsString(obj);
107         }
108 }