1 package de.spring.webservices.rest.controller;
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;
14 import java.util.ArrayList;
15 import java.util.List;
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;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
32 import de.spring.webservices.domain.Car;
33 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
36 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/
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";
45 private AwesomeBusinessLogic awesomeBusinessLogic;
46 private CarController controller;
47 private MockMvc mockMvc;
51 awesomeBusinessLogic = mock(AwesomeBusinessLogic.class);
52 controller = new CarController(awesomeBusinessLogic);
53 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
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));
62 mockMvc.perform(get("/api/cars/")
63 .accept(MediaType.APPLICATION_JSON_UTF8))
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));
72 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
73 given(awesomeBusinessLogic.findById(1L)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
75 mockMvc.perform(get("/api/cars/{id}", 1L)
76 .accept(MediaType.APPLICATION_JSON_UTF8))
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));
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)));
89 mockMvc.perform(post("/api/cars/")
90 .contentType(MediaType.APPLICATION_JSON_UTF8)
91 .content(asJsonString(car))
92 .accept(MediaType.APPLICATION_JSON_UTF8))
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));
101 private static String asJsonString(final Object obj) throws JsonProcessingException {
102 final ObjectMapper mapper = new ObjectMapper();
103 return mapper.writeValueAsString(obj);