efffd5dad09d0d1cb22b6bcbbe6d6a7e036099e3
[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.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;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 import de.spring.webservices.domain.Car;
40 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
41 import rx.Observable;
42
43
44 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ 
45
46 @Ignore
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";
53         
54         private RxJavaBusinessLogic rxJavaBusinessLogic;
55         private RxJavaCarController controller;
56         private MockMvc mockMvc;
57         
58     @Before
59     public void setup() {
60         rxJavaBusinessLogic = mock(RxJavaBusinessLogic.class);
61         controller = new RxJavaCarController(rxJavaBusinessLogic);
62         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
63     }
64
65         @Test
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);
71                 
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)))
76                                 .andReturn();
77                 
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));
84         }
85         
86         @Test
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);
90
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)))
95                                 .andReturn();
96         
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));
102         }
103         
104         @Test
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);
109
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)))
116                                 .andReturn();
117                 
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));
124         }
125         
126         private static String asJsonString(final Object obj) throws JsonProcessingException {
127                 final ObjectMapper mapper = new ObjectMapper();
128                 return mapper.writeValueAsString(obj);
129         }
130 }