e7eaedad824abc618e416f2ee9785b17082461cd
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
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;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 import de.spring.webservices.domain.Car;
39 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
40 import io.reactivex.Observable;
41
42
43 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ 
44
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";
51         
52         private RxJavaBusinessLogic rxJavaBusinessLogic;
53         private RxJavaCarController controller;
54         private MockMvc mockMvc;
55         
56     @Before
57     public void setup() {
58         rxJavaBusinessLogic = mock(RxJavaBusinessLogic.class);
59         controller = new RxJavaCarController(rxJavaBusinessLogic);
60         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
61     }
62
63         @Test
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);
69                 
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)))
74                                 .andReturn();
75                 
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));
82         }
83         
84         @Test
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);
88
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)))
93                                 .andReturn();
94         
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));
100         }
101         
102         @Test
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);
107
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)))
114                                 .andReturn();
115                 
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));
122         }
123         
124         private static String asJsonString(final Object obj) throws JsonProcessingException {
125                 final ObjectMapper mapper = new ObjectMapper();
126                 return mapper.writeValueAsString(obj);
127         }
128 }