Add a new spring-boot-sample-test application

Add a sample application that demonstrates recently added testing
features.

See gh-4901
This commit is contained in:
Phillip Webb
2016-03-23 22:22:12 -07:00
parent 81d5635571
commit 247bdf9c84
30 changed files with 1582 additions and 0 deletions
@@ -0,0 +1,70 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.test.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sample.test.domain.VehicleIdentificationNumber;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
/**
* {@link VehicleDetailsService} backed by a remote REST service.
*
* @author Phillip Webb
*/
@Service
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;
}
@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);
try {
return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
}
catch (HttpStatusCodeException ex) {
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {
throw new VehicleIdentificationNumberNotFoundException(vin, ex);
}
throw ex;
}
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.test.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Properties for the service.
*
* @author Phillip Webb
*/
@Component
@ConfigurationProperties
public class ServiceProperties {
private String vehicleServiceRootUrl = "http://localhost:8080/vs/";
public String getVehicleServiceRootUrl() {
return this.vehicleServiceRootUrl;
}
public void setVehicleServiceRootUrl(String vehicleServiceRootUrl) {
this.vehicleServiceRootUrl = vehicleServiceRootUrl;
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.test.service;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.util.Assert;
/**
* Details of a single vehicle.
*
* @author Phillip Webb
*/
public class VehicleDetails {
private final String make;
private final String model;
@JsonCreator
public VehicleDetails(@JsonProperty("make") String make,
@JsonProperty("model") String model) {
Assert.notNull(make, "Make must not be null");
Assert.notNull(model, "Model must not be null");
this.make = make;
this.model = model;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
@Override
public int hashCode() {
return this.make.hashCode() * 31 + this.model.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != getClass()) {
return false;
}
VehicleDetails other = (VehicleDetails) obj;
return this.make.equals(other.make) && this.model.equals(other.model);
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.test.service;
import sample.test.domain.VehicleIdentificationNumber;
/**
* A service to obtain {@link VehicleDetails} given a {@link VehicleIdentificationNumber}.
*
* @author Phillip Webb
*/
public interface VehicleDetailsService {
/**
* Get vehicle details for a given {@link VehicleIdentificationNumber}.
* @param vin the vehicle identification number
* @return vehicle details
* @throws VehicleIdentificationNumberNotFoundException if the VIN is not known
*/
VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
throws VehicleIdentificationNumberNotFoundException;
}
@@ -0,0 +1,43 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.test.service;
import sample.test.domain.VehicleIdentificationNumber;
/**
* Exception thrown when a {@link VehicleIdentificationNumber} is not found.
*
* @author Phillip Webb
*/
public class VehicleIdentificationNumberNotFoundException extends RuntimeException {
private VehicleIdentificationNumber vehicleIdentificationNumber;
public VehicleIdentificationNumberNotFoundException(VehicleIdentificationNumber vin) {
this(vin, null);
}
public VehicleIdentificationNumberNotFoundException(VehicleIdentificationNumber vin,
Throwable cause) {
super("Unable to find VehicleIdentificationNumber " + vin, cause);
this.vehicleIdentificationNumber = vin;
}
public VehicleIdentificationNumber getVehicleIdentificationNumber() {
return this.vehicleIdentificationNumber;
}
}