1 package de.spring.webservices.rest.controller;
3 import static org.hamcrest.CoreMatchers.instanceOf;
4 import static org.hamcrest.Matchers.any;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.BDDMockito.given;
7 import static org.mockito.Mockito.mock;
8 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
9 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
10 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
11 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
12 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
13 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
14 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.CompletableFuture;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.springframework.data.domain.Page;
25 import org.springframework.data.domain.PageImpl;
26 import org.springframework.data.domain.PageRequest;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.test.context.ContextConfiguration;
31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32 import org.springframework.test.web.servlet.MockMvc;
33 import org.springframework.test.web.servlet.MvcResult;
34 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
39 import de.spring.webservices.domain.Car;
40 import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogic;
43 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
47 public class CompletableFutureCarControllerIntegrationTest {
48 private static final int PAGE = 2;
49 private static final int PAGE_SIZE = 10;
50 private static final String TEMPLATE = "Car: %s";
52 private CompletableFutureBusinessLogic completableFutureBusinessLogic;
53 private CompletableFutureCarController controller;
54 private MockMvc mockMvc;
58 completableFutureBusinessLogic = mock(CompletableFutureBusinessLogic.class);
59 controller = new CompletableFutureCarController(completableFutureBusinessLogic);
60 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
64 public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
65 final List<Car> cars = new ArrayList<>();
66 cars.add(new Car(1L, String.format(TEMPLATE, 1)));
67 CompletableFuture<Page<Car>> future = CompletableFuture.supplyAsync(() -> new PageImpl<>(cars));
68 given(completableFutureBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(future);
70 MvcResult result = mockMvc.perform(get("/api/completablefuture/cars/")
71 .accept(MediaType.APPLICATION_JSON_UTF8))
72 .andExpect(request().asyncStarted())
73 .andExpect(request().asyncResult(instanceOf(Page.class)))
76 mockMvc.perform(asyncDispatch(result))
77 .andExpect(status().isOk())
78 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
79 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
80 .andExpect(jsonPath("$.content[0].content", is("Car: 1")))
81 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
85 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
86 CompletableFuture<Car> expected = CompletableFuture.supplyAsync(() -> new Car(1L, String.format(TEMPLATE, 1)));
87 given(completableFutureBusinessLogic.findById(1L)).willReturn(expected);
89 MvcResult result = mockMvc.perform(get("/api/completablefuture/cars/{id}", 1L)
90 .accept(MediaType.APPLICATION_JSON_UTF8))
91 .andExpect(request().asyncStarted())
92 .andExpect(request().asyncResult(instanceOf(Car.class)))
95 mockMvc.perform(asyncDispatch(result))
96 .andExpect(status().isOk())
97 .andExpect(jsonPath("id", any(Integer.class)))
98 .andExpect(jsonPath("content", is("Car: 1")))
99 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
103 public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
104 Car car = new Car(null, "nothing");
105 CompletableFuture<Car> expected = CompletableFuture.supplyAsync(() -> new Car(1L, String.format(TEMPLATE, 1)));
106 given(completableFutureBusinessLogic.createThrowable(car)).willReturn(expected);
108 MvcResult result = mockMvc.perform(post("/api/completablefuture/cars/")
109 .contentType(MediaType.APPLICATION_JSON_UTF8)
110 .content(asJsonString(car))
111 .accept(MediaType.APPLICATION_JSON_UTF8))
112 .andExpect(request().asyncStarted())
113 .andExpect(request().asyncResult(instanceOf(ResponseEntity.class)))
116 mockMvc.perform(asyncDispatch(result))
117 .andExpect(status().isCreated())
118 .andExpect(jsonPath("id", any(Integer.class)))
119 .andExpect(jsonPath("content", is("Car: 1")))
120 .andExpect(header().string(HttpHeaders.LOCATION, "/api/completablefuture/cars/1"))
121 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
124 private static String asJsonString(final Object obj) throws JsonProcessingException {
125 final ObjectMapper mapper = new ObjectMapper();
126 return mapper.writeValueAsString(obj);