d2c43ad2c3b450fdc5e7df9ab8c32c4e84cfd74b
[SpringWebServicesForFun/.git] /
1 package de.spring.webservices.rest.client;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
6 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
7 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
8 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.beans.factory.annotation.Value;
18 import org.springframework.http.HttpHeaders;
19 import org.springframework.http.HttpMethod;
20 import org.springframework.http.MediaType;
21 import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
22 import org.springframework.test.context.ContextConfiguration;
23 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24 import org.springframework.test.web.client.MockRestServiceServer;
25 import org.springframework.web.client.RestTemplate;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28
29 import de.spring.webservices.domain.Car;
30
31 @RunWith(SpringJUnit4ClassRunner.class)
32 @ContextConfiguration("classpath*:spring-configuration/rest-config.xml")
33 public class CarClientServiceIntegrationTest {
34         
35         @Value("${url.base}${url.cars}")
36         private String apiCarsUrl;
37         
38         @Value("${url.base}${url.car}")
39         private String apiCarUrl;
40         
41     @Autowired
42     private RestTemplate restTemplate;
43     
44     @Autowired
45         private CarClientService carClientService;
46     
47     @Autowired
48     private Jackson2ObjectMapperFactoryBean jsonObjectMapperFactory;
49
50     private MockRestServiceServer mockServer;
51
52     @Before
53     public void createTest() {
54         mockServer = MockRestServiceServer.createServer(restTemplate);
55     }
56
57         @Test
58         public void whenGetAllCarsThenRetrieveRequestedCars() throws JsonProcessingException {
59                 Car expectedOne = new Car(66L, "test");
60                 List<Car> expected = new ArrayList<>();
61                 expected.add(expectedOne);
62                 
63                 mockServer.expect(requestTo(apiCarsUrl))
64                                         .andExpect(method(HttpMethod.GET))
65                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
66
67                 List<Car> cars = carClientService.doGetCars();
68
69                 mockServer.verify();
70                 
71                 assertEquals(1, cars.size());
72                 assertEquals(expectedOne, cars.get(0));
73         }
74         
75         @Test
76         public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
77                 Long id = 66L;
78                 Car expected = new Car(66L, "test");
79                 
80                 mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
81                                         .andExpect(method(HttpMethod.GET))
82                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
83
84                 Car car = carClientService.doGetCar(id);
85
86                 mockServer.verify();
87                 
88                 assertNotNull(car);
89                 assertEquals(expected, car);
90         }
91         
92         @Test
93         public void whenCreateNewCarThenRetrieveCreatedCar() throws JsonProcessingException {
94                 Long expectedId = 66L;
95                 HttpHeaders headers = new HttpHeaders();
96                 headers.add(HttpHeaders.LOCATION, "/api/cars/" + String.valueOf(expectedId));
97                 Car expected = new Car(expectedId, "test");
98                 
99                 mockServer.expect(requestTo(apiCarsUrl))
100                                         .andExpect(method(HttpMethod.POST))
101                                         .andExpect(content()
102                                                         .string(asJsonString(expected)))
103                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
104                                                         .headers(headers));
105
106                 Car car = carClientService.doNewCar(expected);
107
108                 mockServer.verify();
109                 
110                 assertNotNull(car);
111                 assertEquals(expected, car);
112         }
113         
114         private String asJsonString(final Object obj) throws JsonProcessingException {
115                 return jsonObjectMapperFactory.getObject().writeValueAsString(obj);
116         }
117 }