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:
@@ -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;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Sample application to demonstrate testing.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SampleTestApplication {
|
||||
|
||||
// NOTE: this application will intentionally not start without MySQL, the test will
|
||||
// still run.
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleTestApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Simple component that just prints a message. Used to show how different types of
|
||||
* integration tests work.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Component
|
||||
public class WelcomeCommandLineRunner implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
System.out.println("***** WELCOME TO THE DEMO *****");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.domain;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A user of the system.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(unique = true)
|
||||
private String username;
|
||||
|
||||
private VehicleIdentificationNumber vin;
|
||||
|
||||
protected User() {
|
||||
}
|
||||
|
||||
public User(String username, VehicleIdentificationNumber vin) {
|
||||
Assert.hasLength(username, "Username must not be empty");
|
||||
Assert.notNull(vin, "VIN must not be null");
|
||||
this.username = username;
|
||||
this.vin = vin;
|
||||
}
|
||||
|
||||
protected Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public VehicleIdentificationNumber getVin() {
|
||||
return this.vin;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.domain;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
* Domain repository for {@link User}
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public interface UserRepository extends Repository<User, Long> {
|
||||
|
||||
User findByUsername(String username);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.domain;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A Vehicle Identification Number.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public final class VehicleIdentificationNumber {
|
||||
|
||||
private String vin;
|
||||
|
||||
public VehicleIdentificationNumber(String vin) {
|
||||
Assert.notNull(vin, "VIN must not be null");
|
||||
Assert.isTrue(vin.length() == 17, "VIN must be exactly 17 characters");
|
||||
this.vin = vin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.vin.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
return this.vin.equals(((VehicleIdentificationNumber) obj).vin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.vin;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.domain;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
/**
|
||||
* JPA {@link AttributeConverter} for {@link VehicleIdentificationNumber}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Converter(autoApply = true)
|
||||
public class VehicleIdentificationNumberAttributeConverter
|
||||
implements AttributeConverter<VehicleIdentificationNumber, String> {
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(VehicleIdentificationNumber attribute) {
|
||||
return attribute.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public VehicleIdentificationNumber convertToEntityAttribute(String dbData) {
|
||||
return new VehicleIdentificationNumber(dbData);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.web;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public class UserNameNotFoundException extends RuntimeException {
|
||||
|
||||
private String username;
|
||||
|
||||
public UserNameNotFoundException(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.web;
|
||||
|
||||
import sample.test.domain.User;
|
||||
import sample.test.service.VehicleDetails;
|
||||
import sample.test.service.VehicleIdentificationNumberNotFoundException;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller to return vehicle information for a given {@link User}
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RestController
|
||||
public class UserVehicleController {
|
||||
|
||||
private UserVehicleService userVehicleService;
|
||||
|
||||
public UserVehicleController(UserVehicleService userVehicleService) {
|
||||
this.userVehicleService = userVehicleService;
|
||||
}
|
||||
|
||||
@GetMapping(path = "/{username}/vehicle", produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
public String getVehicleDetailsText(@PathVariable String username) {
|
||||
VehicleDetails details = this.userVehicleService.getVehicleDetails(username);
|
||||
return details.getMake() + " " + details.getModel();
|
||||
}
|
||||
|
||||
@GetMapping(path = "/{username}/vehicle", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public VehicleDetails VehicleDetailsJson(@PathVariable String username) {
|
||||
return this.userVehicleService.getVehicleDetails(username);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/{username}/vehicle.html", produces = MediaType.TEXT_HTML_VALUE)
|
||||
public String VehicleDetailsHtml(@PathVariable String username) {
|
||||
VehicleDetails details = this.userVehicleService.getVehicleDetails(username);
|
||||
String makeAndModel = details.getMake() + " " + details.getModel();
|
||||
return "<html><body><h1>" + makeAndModel + "</h1></body></html>";
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
private void handleVinNotFound(VehicleIdentificationNumberNotFoundException ex) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.web;
|
||||
|
||||
import sample.test.domain.User;
|
||||
import sample.test.domain.UserRepository;
|
||||
import sample.test.service.VehicleDetails;
|
||||
import sample.test.service.VehicleDetailsService;
|
||||
import sample.test.service.VehicleIdentificationNumberNotFoundException;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Controller service used to provide vehicle information for a given user.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Component
|
||||
public class UserVehicleService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final VehicleDetailsService vehicleDetailsService;
|
||||
|
||||
public UserVehicleService(UserRepository userRepository,
|
||||
VehicleDetailsService vehicleDetailsService) {
|
||||
this.userRepository = userRepository;
|
||||
this.vehicleDetailsService = vehicleDetailsService;
|
||||
}
|
||||
|
||||
public VehicleDetails getVehicleDetails(String username)
|
||||
throws UserNameNotFoundException,
|
||||
VehicleIdentificationNumberNotFoundException {
|
||||
Assert.notNull(username, "Username must not be null");
|
||||
User user = this.userRepository.findByUsername(username);
|
||||
if (user == null) {
|
||||
throw new UserNameNotFoundException(username);
|
||||
}
|
||||
return this.vehicleDetailsService.getVehicleDetails(user.getVin());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user