1 package de.spring.webservices.rest.client;
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;
9 import java.util.ArrayList;
10 import java.util.List;
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;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
28 import de.spring.webservices.domain.Car;
30 @RunWith(SpringJUnit4ClassRunner.class)
31 @ContextConfiguration("classpath*:spring-configuration/rest-config.xml")
32 public class CarClientServiceIntegrationTest {
34 @Value("${url.base}${url.cars}")
35 private String apiCarsUrl;
37 @Value("${url.base}${url.car}")
38 private String apiCarUrl;
41 private RestTemplate restTemplate;
44 private CarClientService carClientService;
46 private MockRestServiceServer mockServer;
49 public void createTest() {
50 mockServer = MockRestServiceServer.createServer(restTemplate);
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);
61 mockServer.expect(requestTo(apiCarsUrl))
62 .andExpect(method(HttpMethod.GET))
63 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
65 List<Car> cars = carClientService.doGetCars();
69 assertEquals(2, cars.size());
70 assertEquals(expectedOne, cars.get(0));
71 assertEquals(expectedTwo, cars.get(1));
75 public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
77 Car expected = new Car(66L, "test");
79 mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
80 .andExpect(method(HttpMethod.GET))
81 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
83 Car car = carClientService.doGetCar(id);
88 assertEquals(expected, car);
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");
98 mockServer.expect(requestTo(apiCarsUrl))
99 .andExpect(method(HttpMethod.POST))
100 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
103 Car car = carClientService.doNewCar(expected);
108 assertEquals(expected, car);
111 private static String asJsonString(final Object obj) throws JsonProcessingException {
112 final ObjectMapper mapper = new ObjectMapper();
113 return mapper.writeValueAsString(obj);