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 List<Car> expected = new ArrayList<>();
57 expected.add(expectedOne);
59 mockServer.expect(requestTo(apiCarsUrl))
60 .andExpect(method(HttpMethod.GET))
61 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
63 List<Car> cars = carClientService.doGetCars();
67 assertEquals(1, cars.size());
68 assertEquals(expectedOne, cars.get(0));
72 public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
74 Car expected = new Car(66L, "test");
76 mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
77 .andExpect(method(HttpMethod.GET))
78 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
80 Car car = carClientService.doGetCar(id);
85 assertEquals(expected, car);
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");
95 mockServer.expect(requestTo(apiCarsUrl))
96 .andExpect(method(HttpMethod.POST))
97 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
100 Car car = carClientService.doNewCar(expected);
105 assertEquals(expected, car);
108 private static String asJsonString(final Object obj) throws JsonProcessingException {
109 final ObjectMapper mapper = new ObjectMapper();
110 return mapper.writeValueAsString(obj);