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