65d752adcd21be617f8a959a4cdd1ef4a9010435
[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                 Car expectedTwo = new Car(99L, "example");
57                 List<Car> expected = new ArrayList<>();
58                 expected.add(expectedOne);
59                 expected.add(expectedTwo);
60                 
61                 mockServer.expect(requestTo(apiCarsUrl))
62                                         .andExpect(method(HttpMethod.GET))
63                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
64
65                 List<Car> cars = carClientService.doGetCars();
66
67                 mockServer.verify();
68                 
69                 assertEquals(2, cars.size());
70                 assertEquals(expectedOne, cars.get(0));
71                 assertEquals(expectedTwo, cars.get(1));
72         }
73         
74         @Test
75         public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
76                 Long id = 66L;
77                 Car expected = new Car(66L, "test");
78                 
79                 mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
80                                         .andExpect(method(HttpMethod.GET))
81                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
82
83                 Car car = carClientService.doGetCar(id);
84
85                 mockServer.verify();
86                 
87                 assertNotNull(car);
88                 assertEquals(expected, car);
89         }
90         
91         @Test
92         public void whenCreateNewCarThenRetrieveCreatedCar() throws JsonProcessingException {
93                 Long expectedId = 66L;
94                 HttpHeaders headers = new HttpHeaders();
95                 headers.add(HttpHeaders.LOCATION, "/api/cars/" + String.valueOf(expectedId));
96                 Car expected = new Car(expectedId, "test");
97                 
98                 mockServer.expect(requestTo(apiCarsUrl))
99                                         .andExpect(method(HttpMethod.POST))
100                                         .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
101                                                         .headers(headers));
102
103                 Car car = carClientService.doNewCar(expected);
104
105                 mockServer.verify();
106                 
107                 assertNotNull(car);
108                 assertEquals(expected, car);
109         }
110         
111         private static String asJsonString(final Object obj) throws JsonProcessingException {
112                 final ObjectMapper mapper = new ObjectMapper();
113                 return mapper.writeValueAsString(obj);
114         }
115 }