Skip to content

Commit

Permalink
Update spring-boot-sample-test REST code
Browse files Browse the repository at this point in the history
Update RemoteVehicleDetailsService and the related test to use the new
RestTemplateBuilder class and @restclienttest annotation.

See spring-projectsgh-6030
See spring-projectsgh-5507
  • Loading branch information
philwebb committed May 31, 2016
1 parent 0a47594 commit b843306
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.LoggerFactory;
import sample.test.domain.VehicleIdentificationNumber;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
Expand All @@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService {
private static final Logger logger = LoggerFactory
.getLogger(RemoteVehicleDetailsService.class);

private final ServiceProperties properties;

private final RestTemplate restTemplate;

public RemoteVehicleDetailsService(ServiceProperties properties) {
this.properties = properties;
this.restTemplate = new RestTemplate();
}

protected final RestTemplate getRestTemplate() {
return this.restTemplate;
public RemoteVehicleDetailsService(ServiceProperties properties,
RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.rootUri(properties.getVehicleServiceRootUrl()).build();
}

@Override
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
throws VehicleIdentificationNumberNotFoundException {
Assert.notNull(vin, "VIN must not be null");
String url = this.properties.getVehicleServiceRootUrl() + "vehicle/{vin}/details";
logger.debug("Retrieving vehicle data for: " + vin + " from: " + url);
logger.debug("Retrieving vehicle data for: " + vin);
try {
return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
return this.restTemplate.getForObject("/vehicle/{vin}/details",
VehicleDetails.class, vin);
}
catch (HttpStatusCodeException ex) {
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@ConfigurationProperties
public class ServiceProperties {

private String vehicleServiceRootUrl = "http://localhost:8080/vs/";
private String vehicleServiceRootUrl = "http://localhost:8080/vs";

public String getVehicleServiceRootUrl() {
return this.vehicleServiceRootUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package sample.test.service;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import sample.test.domain.VehicleIdentificationNumber;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.HttpServerErrorException;

Expand All @@ -39,25 +42,21 @@
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@RestClientTest({ RemoteVehicleDetailsService.class, ServiceProperties.class })
public class RemoteVehicleDetailsServiceTests {

private static final String VIN = "00000000000000000";

@Rule
public ExpectedException thrown = ExpectedException.none();

@Autowired
private RemoteVehicleDetailsService service;

@Autowired
private MockRestServiceServer server;

@Before
public void setup() {
ServiceProperties properties = new ServiceProperties();
properties.setVehicleServiceRootUrl("http://example.com/");
this.service = new RemoteVehicleDetailsService(properties);
this.server = MockRestServiceServer.createServer(this.service.getRestTemplate());
}

@Test
public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
Expand All @@ -68,7 +67,7 @@ public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exceptio
@Test
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
MediaType.APPLICATION_JSON));
VehicleDetails details = this.service
Expand All @@ -80,7 +79,7 @@ public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
@Test
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withStatus(HttpStatus.NOT_FOUND));
this.thrown.expect(VehicleIdentificationNumberNotFoundException.class);
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
Expand All @@ -89,7 +88,7 @@ public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
@Test
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException()
throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withServerError());
this.thrown.expect(HttpServerErrorException.class);
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
Expand Down

0 comments on commit b843306

Please sign in to comment.