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;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.springframework.data.domain.Page;
24 import org.springframework.data.domain.PageImpl;
25 import org.springframework.data.domain.PageRequest;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.MediaType;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.test.context.ContextConfiguration;
30 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31 import org.springframework.test.web.servlet.MockMvc;
32 import org.springframework.test.web.servlet.MvcResult;
33 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
38 import de.spring.webservices.domain.Car;
39 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
40 import io.reactivex.Observable;
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 RxJavaCarControllerIntegrationTest {
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 RxJavaBusinessLogic rxJavaBusinessLogic;
53 private RxJavaCarController controller;
54 private MockMvc mockMvc;
58 rxJavaBusinessLogic = mock(RxJavaBusinessLogic.class);
59 controller = new RxJavaCarController(rxJavaBusinessLogic);
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 Observable<Page<Car>> observable = Observable.create(observer -> observer.onNext( new PageImpl<>(cars)));
68 given(rxJavaBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(observable);
70 MvcResult result = mockMvc.perform(get("/api/rxjava/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 Observable<Car> observable = Observable.create(observer -> observer.onNext( new Car(1L, String.format(TEMPLATE, 1))));
87 given(rxJavaBusinessLogic.findById(1L)).willReturn(observable);
89 MvcResult result = mockMvc.perform(get("/api/rxjava/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 Observable<Car> observable = Observable.create(observer -> observer.onNext( new Car(1L, String.format(TEMPLATE, 1))));
106 given(rxJavaBusinessLogic.createThrowable(car)).willReturn(observable);
108 MvcResult result = mockMvc.perform(post("/api/rxjava/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/rxjava/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);