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.Ignore;
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.RxJavaBusinessLogic;
41 import io.reactivex.Observable;
44 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
48 public class RxJavaCarControllerIntegrationTest {
49 private static final int PAGE = 2;
50 private static final int PAGE_SIZE = 10;
51 private static final String TEMPLATE = "Car: %s";
53 private RxJavaBusinessLogic rxJavaBusinessLogic;
54 private RxJavaCarController controller;
55 private MockMvc mockMvc;
59 rxJavaBusinessLogic = mock(RxJavaBusinessLogic.class);
60 controller = new RxJavaCarController(rxJavaBusinessLogic);
61 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
65 public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
66 final List<Car> cars = new ArrayList<>();
67 cars.add(new Car(1L, String.format(TEMPLATE, 1)));
68 Observable<Page<Car>> observable = Observable.create(observer -> observer.onNext( new PageImpl<>(cars)));
69 given(rxJavaBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(observable);
71 MvcResult result = mockMvc.perform(get("/api/rxjava/cars/")
72 .accept(MediaType.APPLICATION_JSON_UTF8))
73 .andExpect(request().asyncStarted())
74 .andExpect(request().asyncResult(instanceOf(Page.class)))
77 mockMvc.perform(asyncDispatch(result))
78 .andExpect(status().isOk())
79 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
80 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
81 .andExpect(jsonPath("$.content[0].content", is("Car: 1")))
82 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
86 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
87 Observable<Car> observable = Observable.create(observer -> observer.onNext( new Car(1L, String.format(TEMPLATE, 1))));
88 given(rxJavaBusinessLogic.findById(1L)).willReturn(observable);
90 MvcResult result = mockMvc.perform(get("/api/rxjava/cars/{id}", 1L)
91 .accept(MediaType.APPLICATION_JSON_UTF8))
92 .andExpect(request().asyncStarted())
93 .andExpect(request().asyncResult(instanceOf(Car.class)))
96 mockMvc.perform(asyncDispatch(result))
97 .andExpect(status().isOk())
98 .andExpect(jsonPath("id", any(Integer.class)))
99 .andExpect(jsonPath("content", is("Car: 1")))
100 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
104 public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
105 Car car = new Car(null, "nothing");
106 Observable<Car> observable = Observable.create(observer -> observer.onNext( new Car(1L, String.format(TEMPLATE, 1))));
107 given(rxJavaBusinessLogic.createThrowable(car)).willReturn(observable);
109 MvcResult result = mockMvc.perform(post("/api/rxjava/cars/")
110 .contentType(MediaType.APPLICATION_JSON_UTF8)
111 .content(asJsonString(car))
112 .accept(MediaType.APPLICATION_JSON_UTF8))
113 .andExpect(request().asyncStarted())
114 .andExpect(request().asyncResult(instanceOf(ResponseEntity.class)))
117 mockMvc.perform(asyncDispatch(result))
118 .andExpect(status().isCreated())
119 .andExpect(jsonPath("id", any(Integer.class)))
120 .andExpect(jsonPath("content", is("Car: 1")))
121 .andExpect(header().string(HttpHeaders.LOCATION, "/api/rxjava/cars/1"))
122 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
125 private static String asJsonString(final Object obj) throws JsonProcessingException {
126 final ObjectMapper mapper = new ObjectMapper();
127 return mapper.writeValueAsString(obj);