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