1 package de.spring.webservices.rest.client.service;
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;
10 import java.util.ArrayList;
11 import java.util.List;
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;
27 import com.fasterxml.jackson.core.JsonProcessingException;
29 import de.spring.webservices.domain.Car;
31 @RunWith(SpringJUnit4ClassRunner.class)
32 @ContextConfiguration("classpath*:spring-configuration/rest-config.xml")
33 public class CarClientServiceIntegrationTest {
35 @Value("${url.base}${url.cars}")
36 private String apiCarsUrl;
38 @Value("${url.base}${url.car}")
39 private String apiCarUrl;
42 private RestTemplate restTemplate;
45 private CarClientService carClientService;
48 private Jackson2ObjectMapperFactoryBean jsonObjectMapperFactory;
50 private MockRestServiceServer mockServer;
53 public void createTest() {
54 mockServer = MockRestServiceServer.createServer(restTemplate);
58 public void whenGetAllCarsThenRetrieveRequestedCars() throws JsonProcessingException {
59 Car expectedOne = new Car(66L, "test");
60 List<Car> expected = new ArrayList<>();
61 expected.add(expectedOne);
63 mockServer.expect(requestTo(apiCarsUrl))
64 .andExpect(method(HttpMethod.GET))
65 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
67 List<Car> cars = carClientService.doGetCars();
71 assertEquals(1, cars.size());
72 assertEquals(expectedOne, cars.get(0));
76 public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
78 Car expected = new Car(66L, "test");
80 mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
81 .andExpect(method(HttpMethod.GET))
82 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
84 Car car = carClientService.doGetCar(id);
89 assertEquals(expected, car);
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");
99 mockServer.expect(requestTo(apiCarsUrl))
100 .andExpect(method(HttpMethod.POST))
102 .string(asJsonString(expected)))
103 .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
106 Car car = carClientService.doNewCar(expected);
111 assertEquals(expected, car);
114 private String asJsonString(final Object obj) throws JsonProcessingException {
115 return jsonObjectMapperFactory.getObject().writeValueAsString(obj);