--- /dev/null
+package de.spring.webservices.main;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import de.spring.webservices.rest.business.service.BusinessService;
+
+
+/**
+ * This class is used just like a nice example about how to write and run client
+ * code which will send data to and from the Web Services.
+ *
+ */
+public class MainTest {
+ public ApplicationContext context;
+
+ /**
+ * @param args
+ */
+ public static void main(final String[] args) {
+ final MainTest test = new MainTest();
+
+ test.context = new ClassPathXmlApplicationContext(
+ "classpath:spring-configuration/rest-config.xml");
+
+ final BusinessService example =
+ (BusinessService) test.context.getBean("businessService");
+
+ example.doSomethingWithCars();
+
+ example.doSomethingWithCar(66L);
+
+ example.createsNewCar();
+ }
+}
+++ /dev/null
-package de.spring.webservices.rest.business;
-
-import java.util.List;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import de.spring.webservices.domain.Car;
-import de.spring.webservices.rest.client.CarClientService;
-
-@Service("businessService")
-public class BusinessService {
- private static final Logger LOGGER = LoggerFactory.getLogger(BusinessService.class);
-
- private final CarClientService carClientService;
-
- @Autowired
- public BusinessService(CarClientService carClientService) {
- this.carClientService = carClientService;
- }
-
-
- public void doSomethingWithCars() {
- List<Car> cars = carClientService.doGetCars();
- LOGGER.info("Retrieved cars");
- for (Car car : cars) {
- LOGGER.info("car: " + car.getId());
- LOGGER.info(car.getContent());
- }
- }
-
- public void doSomethingWithCar(long id) {
- Car car = carClientService.doGetCar(id);
- LOGGER.info("Retrieved car");
- LOGGER.info("car: " + car.getId());
- LOGGER.info(car.getContent());
- }
-
- public void createsNewCar() {
- Car newCar = new Car(666L, "just_a_test");
-
- Car car = carClientService.doNewCar(newCar);
- LOGGER.info("New car");
- LOGGER.info("car: " + car.getId());
- LOGGER.info(car.getContent());
- }
-}
--- /dev/null
+package de.spring.webservices.rest.business.service;
+
+
+public interface BusinessService {
+
+ public void doSomethingWithCars();
+
+ public void doSomethingWithCar(long id);
+
+ public void createsNewCar();
+}
--- /dev/null
+package de.spring.webservices.rest.business.service.impl;
+
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import de.spring.webservices.domain.Car;
+import de.spring.webservices.rest.business.service.BusinessService;
+import de.spring.webservices.rest.client.service.CarClientService;
+
+@Service("businessService")
+public class BusinessServiceImpl implements BusinessService {
+ private static final Logger LOGGER = LoggerFactory.getLogger(BusinessServiceImpl.class);
+
+ private final CarClientService carClientService;
+
+ @Autowired
+ public BusinessServiceImpl(CarClientService carClientService) {
+ this.carClientService = carClientService;
+ }
+
+
+ @Override
+ public void doSomethingWithCars() {
+ List<Car> cars = carClientService.doGetCars();
+ LOGGER.info("Retrieved cars");
+ for (Car car : cars) {
+ LOGGER.info("car: " + car.getId());
+ LOGGER.info(car.getContent());
+ }
+ }
+
+ @Override
+ public void doSomethingWithCar(long id) {
+ Car car = carClientService.doGetCar(id);
+ LOGGER.info("Retrieved car");
+ LOGGER.info("car: " + car.getId());
+ LOGGER.info(car.getContent());
+ }
+
+ @Override
+ public void createsNewCar() {
+ Car newCar = new Car(666L, "just_a_test");
+
+ Car car = carClientService.doNewCar(newCar);
+ LOGGER.info("New car");
+ LOGGER.info("car: " + car.getId());
+ LOGGER.info(car.getContent());
+ }
+}
+++ /dev/null
-package de.spring.webservices.rest.client;
-
-import java.net.URI;
-import java.util.Arrays;
-import java.util.List;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
-
-import de.spring.webservices.domain.Car;
-
-@Service("carClientService")
-public class CarClientService {
- private static final Logger LOGGER = LoggerFactory.getLogger(CarClientService.class);
-
- private final String apiCarsUrl;
- private final String apiCarUrl;
- private final RestTemplate restTemplate;
-
- @Autowired
- public CarClientService(@Value("${url.base}${url.cars}") String apiCarsUrl,
- @Value("${url.base}${url.car}") String apiCarUrl, RestTemplate restTemplate) {
- this.apiCarsUrl = apiCarsUrl;
- this.apiCarUrl = apiCarUrl;
- this.restTemplate = restTemplate;
- }
-
-
- public List<Car> doGetCars() {
- ResponseEntity<Car[]> responseEntity = restTemplate.getForEntity(apiCarsUrl, Car[].class);
-
- return Arrays.asList(responseEntity.getBody());
- }
-
- public Car doGetCar(long id) {
- ResponseEntity<Car> responseEntity = restTemplate.getForEntity(
- apiCarUrl.replace(":id", String.valueOf(id)), Car.class);
-
- return responseEntity.getBody();
- }
-
-
- public Car doNewCar(Car car) {
- ResponseEntity<Car> responseEntity = restTemplate.postForEntity(apiCarsUrl, car, Car.class);
- URI newCarLocation = responseEntity.getHeaders().getLocation();
- LOGGER.info("new car location: " + newCarLocation.getPath());
-
- return responseEntity.getBody();
- }
-}
--- /dev/null
+package de.spring.webservices.rest.client.service;
+
+import java.util.List;
+
+import de.spring.webservices.domain.Car;
+
+public interface CarClientService {
+
+ public List<Car> doGetCars();
+
+ public Car doGetCar(long id);
+
+ public Car doNewCar(Car car);
+}
--- /dev/null
+package de.spring.webservices.rest.client.service.impl;
+
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import de.spring.webservices.domain.Car;
+import de.spring.webservices.rest.client.service.CarClientService;
+
+@Service("carClientService")
+public class CarClientServiceImpl implements CarClientService {
+ private static final Logger LOGGER = LoggerFactory.getLogger(CarClientServiceImpl.class);
+
+ private final String apiCarsUrl;
+ private final String apiCarUrl;
+ private final RestTemplate restTemplate;
+
+ @Autowired
+ public CarClientServiceImpl(@Value("${url.base}${url.cars}") String apiCarsUrl,
+ @Value("${url.base}${url.car}") String apiCarUrl, RestTemplate restTemplate) {
+ this.apiCarsUrl = apiCarsUrl;
+ this.apiCarUrl = apiCarUrl;
+ this.restTemplate = restTemplate;
+ }
+
+
+ @Override
+ public List<Car> doGetCars() {
+ ResponseEntity<Car[]> responseEntity = restTemplate.getForEntity(apiCarsUrl, Car[].class);
+
+ return Arrays.asList(responseEntity.getBody());
+ }
+
+ @Override
+ public Car doGetCar(long id) {
+ ResponseEntity<Car> responseEntity = restTemplate.getForEntity(
+ apiCarUrl.replace(":id", String.valueOf(id)), Car.class);
+
+ return responseEntity.getBody();
+ }
+
+ @Override
+ public Car doNewCar(Car car) {
+ ResponseEntity<Car> responseEntity = restTemplate.postForEntity(apiCarsUrl, car, Car.class);
+ URI newCarLocation = responseEntity.getHeaders().getLocation();
+ LOGGER.info("new car location: " + newCarLocation.getPath());
+
+ return responseEntity.getBody();
+ }
+}
+++ /dev/null
-package de.spring.webservices.rest.main;
-
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-import de.spring.webservices.rest.business.BusinessService;
-
-
-/**
- * This class is used just like a nice example about how to write and run client
- * code which will send data to and from the Web Services.
- *
- */
-public class MainTest {
- public ApplicationContext context;
-
- /**
- * @param args
- */
- public static void main(final String[] args) {
- final MainTest test = new MainTest();
-
- test.context = new ClassPathXmlApplicationContext(
- "classpath:spring-configuration/rest-config.xml");
-
- final BusinessService example =
- (BusinessService) test.context.getBean("businessService");
-
- example.doSomethingWithCars();
-
- example.doSomethingWithCar(66L);
-
- example.createsNewCar();
- }
-}
+++ /dev/null
-package de.spring.webservices.rest.business;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.ArgumentCaptor;
-
-import de.spring.webservices.domain.Car;
-import de.spring.webservices.rest.client.CarClientService;
-
-public class BusinessServiceTest {
-
- private CarClientService carClientService;
- private BusinessService businessService;
-
- @Before
- public void createTest() {
- carClientService = mock(CarClientService.class);
- businessService = new BusinessService(carClientService);
- }
-
- @Test
- public void whenDoSomethingWithCarsThenInvokeDoGetCars() {
- Car expectedOne = new Car(66L, "test");
- Car expectedTwo = new Car(99L, "example");
- List<Car> expected = new ArrayList<>();
- expected.add(expectedOne);
- expected.add(expectedTwo);
- when(carClientService.doGetCars()).thenReturn(expected);
-
- businessService.doSomethingWithCars();
-
- verify(carClientService, times(1)).doGetCars();
- }
-
- @Test
- public void whenDoSomethingWithOneCarhenInvokeDoGetCar() {
- Long id = 66L;
- Car expected = new Car(66L, "test");
-
- when(carClientService.doGetCar(id)).thenReturn(expected);
-
- businessService.doSomethingWithCar(id);
-
- verify(carClientService, times(1)).doGetCar(id);
- }
-
- @Test
- public void whenCreateNewCarThenCreateNewOne() {
- Car expected = new Car(66L, "test");
- ArgumentCaptor<Car> argCar = ArgumentCaptor.forClass(Car.class);
-
- when(carClientService.doNewCar(argCar.capture())).thenReturn(expected);
-
- businessService.createsNewCar();
-
- verify(carClientService, times(1)).doNewCar(argCar.getValue());
- }
-}
--- /dev/null
+package de.spring.webservices.rest.business.service;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import de.spring.webservices.domain.Car;
+import de.spring.webservices.rest.business.service.impl.BusinessServiceImpl;
+import de.spring.webservices.rest.client.service.CarClientService;
+
+public class BusinessServiceTest {
+
+ private CarClientService carClientService;
+ private BusinessService businessService;
+
+ @Before
+ public void createTest() {
+ carClientService = mock(CarClientService.class);
+ businessService = new BusinessServiceImpl(carClientService);
+ }
+
+ @Test
+ public void whenDoSomethingWithCarsThenInvokeDoGetCars() {
+ Car expectedOne = new Car(66L, "test");
+ Car expectedTwo = new Car(99L, "example");
+ List<Car> expected = new ArrayList<>();
+ expected.add(expectedOne);
+ expected.add(expectedTwo);
+ when(carClientService.doGetCars()).thenReturn(expected);
+
+ businessService.doSomethingWithCars();
+
+ verify(carClientService, times(1)).doGetCars();
+ }
+
+ @Test
+ public void whenDoSomethingWithOneCarhenInvokeDoGetCar() {
+ Long id = 66L;
+ Car expected = new Car(66L, "test");
+
+ when(carClientService.doGetCar(id)).thenReturn(expected);
+
+ businessService.doSomethingWithCar(id);
+
+ verify(carClientService, times(1)).doGetCar(id);
+ }
+
+ @Test
+ public void whenCreateNewCarThenCreateNewOne() {
+ Car expected = new Car(66L, "test");
+ ArgumentCaptor<Car> argCar = ArgumentCaptor.forClass(Car.class);
+
+ when(carClientService.doNewCar(argCar.capture())).thenReturn(expected);
+
+ businessService.createsNewCar();
+
+ verify(carClientService, times(1)).doNewCar(argCar.getValue());
+ }
+}
+++ /dev/null
-package de.spring.webservices.rest.client;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
-import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
-import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
-import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.MediaType;
-import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.test.web.client.MockRestServiceServer;
-import org.springframework.web.client.RestTemplate;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-
-import de.spring.webservices.domain.Car;
-
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration("classpath*:spring-configuration/rest-config.xml")
-public class CarClientServiceIntegrationTest {
-
- @Value("${url.base}${url.cars}")
- private String apiCarsUrl;
-
- @Value("${url.base}${url.car}")
- private String apiCarUrl;
-
- @Autowired
- private RestTemplate restTemplate;
-
- @Autowired
- private CarClientService carClientService;
-
- @Autowired
- private Jackson2ObjectMapperFactoryBean jsonObjectMapperFactory;
-
- private MockRestServiceServer mockServer;
-
- @Before
- public void createTest() {
- mockServer = MockRestServiceServer.createServer(restTemplate);
- }
-
- @Test
- public void whenGetAllCarsThenRetrieveRequestedCars() throws JsonProcessingException {
- Car expectedOne = new Car(66L, "test");
- List<Car> expected = new ArrayList<>();
- expected.add(expectedOne);
-
- mockServer.expect(requestTo(apiCarsUrl))
- .andExpect(method(HttpMethod.GET))
- .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
-
- List<Car> cars = carClientService.doGetCars();
-
- mockServer.verify();
-
- assertEquals(1, cars.size());
- assertEquals(expectedOne, cars.get(0));
- }
-
- @Test
- public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
- Long id = 66L;
- Car expected = new Car(66L, "test");
-
- mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
- .andExpect(method(HttpMethod.GET))
- .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
-
- Car car = carClientService.doGetCar(id);
-
- mockServer.verify();
-
- assertNotNull(car);
- assertEquals(expected, car);
- }
-
- @Test
- public void whenCreateNewCarThenRetrieveCreatedCar() throws JsonProcessingException {
- Long expectedId = 66L;
- HttpHeaders headers = new HttpHeaders();
- headers.add(HttpHeaders.LOCATION, "/api/cars/" + String.valueOf(expectedId));
- Car expected = new Car(expectedId, "test");
-
- mockServer.expect(requestTo(apiCarsUrl))
- .andExpect(method(HttpMethod.POST))
- .andExpect(content()
- .string(asJsonString(expected)))
- .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
- .headers(headers));
-
- Car car = carClientService.doNewCar(expected);
-
- mockServer.verify();
-
- assertNotNull(car);
- assertEquals(expected, car);
- }
-
- private String asJsonString(final Object obj) throws JsonProcessingException {
- return jsonObjectMapperFactory.getObject().writeValueAsString(obj);
- }
-}
--- /dev/null
+package de.spring.webservices.rest.client.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
+import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.client.MockRestServiceServer;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+import de.spring.webservices.domain.Car;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration("classpath*:spring-configuration/rest-config.xml")
+public class CarClientServiceIntegrationTest {
+
+ @Value("${url.base}${url.cars}")
+ private String apiCarsUrl;
+
+ @Value("${url.base}${url.car}")
+ private String apiCarUrl;
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ @Autowired
+ private CarClientService carClientService;
+
+ @Autowired
+ private Jackson2ObjectMapperFactoryBean jsonObjectMapperFactory;
+
+ private MockRestServiceServer mockServer;
+
+ @Before
+ public void createTest() {
+ mockServer = MockRestServiceServer.createServer(restTemplate);
+ }
+
+ @Test
+ public void whenGetAllCarsThenRetrieveRequestedCars() throws JsonProcessingException {
+ Car expectedOne = new Car(66L, "test");
+ List<Car> expected = new ArrayList<>();
+ expected.add(expectedOne);
+
+ mockServer.expect(requestTo(apiCarsUrl))
+ .andExpect(method(HttpMethod.GET))
+ .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
+
+ List<Car> cars = carClientService.doGetCars();
+
+ mockServer.verify();
+
+ assertEquals(1, cars.size());
+ assertEquals(expectedOne, cars.get(0));
+ }
+
+ @Test
+ public void whenGetCarByIdThenRetrieveRequestedCar() throws JsonProcessingException {
+ Long id = 66L;
+ Car expected = new Car(66L, "test");
+
+ mockServer.expect(requestTo(apiCarUrl.replace(":id", String.valueOf(id))))
+ .andExpect(method(HttpMethod.GET))
+ .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8));
+
+ Car car = carClientService.doGetCar(id);
+
+ mockServer.verify();
+
+ assertNotNull(car);
+ assertEquals(expected, car);
+ }
+
+ @Test
+ public void whenCreateNewCarThenRetrieveCreatedCar() throws JsonProcessingException {
+ Long expectedId = 66L;
+ HttpHeaders headers = new HttpHeaders();
+ headers.add(HttpHeaders.LOCATION, "/api/cars/" + String.valueOf(expectedId));
+ Car expected = new Car(expectedId, "test");
+
+ mockServer.expect(requestTo(apiCarsUrl))
+ .andExpect(method(HttpMethod.POST))
+ .andExpect(content()
+ .string(asJsonString(expected)))
+ .andRespond(withSuccess(asJsonString(expected), MediaType.APPLICATION_JSON_UTF8)
+ .headers(headers));
+
+ Car car = carClientService.doNewCar(expected);
+
+ mockServer.verify();
+
+ assertNotNull(car);
+ assertEquals(expected, car);
+ }
+
+ private String asJsonString(final Object obj) throws JsonProcessingException {
+ return jsonObjectMapperFactory.getObject().writeValueAsString(obj);
+ }
+}
+++ /dev/null
-package de.spring.webservices.rest;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.bind.annotation.RestController;
-
-import de.spring.webservices.domain.Car;
-
-//import io.swagger.annotations.ApiOperation;
-//import io.swagger.annotations.ApiResponse;
-//import io.swagger.annotations.ApiResponses;
-
-@RestController
-@RequestMapping("/api/cars/")
-public class CarController {
- private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
- private static final String TEMPLATE = "Car: %s";
-
- private final AtomicLong counter = new AtomicLong();
-
-// Do I want to release with Swagger dependencies?
-// @ApiOperation(value = "getCars", nickname = "getAllCars", response = Car.class)
-// @ApiResponses({
-// @ApiResponse(code = 404, message ="Not found"),
-// @ApiResponse(code = 400, message ="Invalid input")
-// })
- @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
- @ResponseStatus(HttpStatus.OK)
- public List<Car> cars() {
- final List<Car> cars = new ArrayList<>();
- cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
- cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
- cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
-
- return cars;
- }
-
-// Do I want to release with Swagger dependencies?
-// @ApiOperation(value = "getCar", nickname = "getsOneCar", response = Car.class)
-// @ApiResponses({
-// @ApiResponse(code = 404, message ="Not found"),
-// @ApiResponse(code = 400, message ="Invalid input")
-// })
- @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
- @ResponseStatus(HttpStatus.OK)
- public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
- @PathVariable("id") long id,
- @RequestParam Map<String, String> params,
- @RequestParam(value = "wheel", required = false) String[] wheelParams) {
-
- if (specialHeader != null) {
- LOGGER.info("SPECIAL HEADER: " + specialHeader);
- }
-
- if (params.get("mirror") != null) {
- LOGGER.info("MIRROR: " + params.get("mirror"));
- }
-
- if (params.get("window") != null) {
- LOGGER.info("WINDOW: " + params.get("window"));
- }
-
- if (wheelParams != null) {
- for(String wheel : wheelParams) {
- LOGGER.info(wheel);
- }
- }
-
- try {
- Thread.sleep(10000); //1000 milliseconds is one second.
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
-
-
- return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
- }
-
-// Do I want to release with Swagger dependencies?
-// @ApiOperation(value = "postCat", nickname = "createsNewCar", response = Car.class)
-// @ApiResponses({
-// @ApiResponse(code = 404, message ="Not found"),
-// @ApiResponse(code = 400, message ="Invalid input")
-// })
- @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
- produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
- public ResponseEntity<Car> create(@RequestBody Car car) {
- long count = counter.incrementAndGet();
- HttpHeaders headers = new HttpHeaders();
- headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
-
- return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);
- }
-
-}
--- /dev/null
+package de.spring.webservices.rest.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+import de.spring.webservices.domain.Car;
+
+//import io.swagger.annotations.ApiOperation;
+//import io.swagger.annotations.ApiResponse;
+//import io.swagger.annotations.ApiResponses;
+
+@RestController
+@RequestMapping("/api/cars/")
+public class CarController {
+ private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
+ private static final String TEMPLATE = "Car: %s";
+
+ private final AtomicLong counter = new AtomicLong();
+
+// Do I want to release with Swagger dependencies?
+// @ApiOperation(value = "getCars", nickname = "getAllCars", response = Car.class)
+// @ApiResponses({
+// @ApiResponse(code = 404, message ="Not found"),
+// @ApiResponse(code = 400, message ="Invalid input")
+// })
+ @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
+ @ResponseStatus(HttpStatus.OK)
+ public List<Car> cars() {
+ final List<Car> cars = new ArrayList<>();
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
+ cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
+
+ return cars;
+ }
+
+// Do I want to release with Swagger dependencies?
+// @ApiOperation(value = "getCar", nickname = "getsOneCar", response = Car.class)
+// @ApiResponses({
+// @ApiResponse(code = 404, message ="Not found"),
+// @ApiResponse(code = 400, message ="Invalid input")
+// })
+ @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
+ @ResponseStatus(HttpStatus.OK)
+ public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
+ @PathVariable("id") long id,
+ @RequestParam Map<String, String> params,
+ @RequestParam(value = "wheel", required = false) String[] wheelParams) {
+
+ if (specialHeader != null) {
+ LOGGER.info("SPECIAL HEADER: " + specialHeader);
+ }
+
+ if (params.get("mirror") != null) {
+ LOGGER.info("MIRROR: " + params.get("mirror"));
+ }
+
+ if (params.get("window") != null) {
+ LOGGER.info("WINDOW: " + params.get("window"));
+ }
+
+ if (wheelParams != null) {
+ for(String wheel : wheelParams) {
+ LOGGER.info(wheel);
+ }
+ }
+
+ try {
+ Thread.sleep(10000); //1000 milliseconds is one second.
+ } catch(InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ }
+
+
+ return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
+ }
+
+// Do I want to release with Swagger dependencies?
+// @ApiOperation(value = "postCat", nickname = "createsNewCar", response = Car.class)
+// @ApiResponses({
+// @ApiResponse(code = 404, message ="Not found"),
+// @ApiResponse(code = 400, message ="Invalid input")
+// })
+ @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
+ produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
+ public ResponseEntity<Car> create(@RequestBody Car car) {
+ long count = counter.incrementAndGet();
+ HttpHeaders headers = new HttpHeaders();
+ headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
+
+ return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);
+ }
+
+}
+++ /dev/null
-package de.spring.webservices.rest;
-
-import static org.hamcrest.Matchers.any;
-import static org.hamcrest.Matchers.is;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import de.spring.webservices.domain.Car;
-
-
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
-public class CarControllerIntegrationTest {
- private CarController controller;
- private MockMvc mockMvc;
-
- @Before
- public void setup() {
- controller = new CarController();
- mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
- }
-
- @Test
- public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
- mockMvc.perform(get("/api/cars/")
- .accept(MediaType.APPLICATION_JSON_UTF8))
-
- .andExpect(status().isOk())
- .andExpect(jsonPath("$[0].id", any(Integer.class)))
- .andExpect(jsonPath("$[0].content", is("Car: 1")))
- .andExpect(jsonPath("$[1].content", is("Car: 2")))
- .andExpect(jsonPath("$[1].id", any(Integer.class)))
- .andExpect(jsonPath("$[2].content", is("Car: 3")))
- .andExpect(jsonPath("$[2].id", any(Integer.class)))
- .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
- }
-
- @Test
- public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
- mockMvc.perform(get("/api/cars/{id}", 1L)
- .accept(MediaType.APPLICATION_JSON_UTF8))
-
- .andExpect(status().isOk())
- .andExpect(jsonPath("id", any(Integer.class)))
- .andExpect(jsonPath("content", is("Car: 1")))
- .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
- }
-
- @Test
- public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
- Car car = new Car(2L, "nothing");
- mockMvc.perform(post("/api/cars/")
- .contentType(MediaType.APPLICATION_JSON_UTF8)
- .content(asJsonString(car))
- .accept(MediaType.APPLICATION_JSON_UTF8))
-
- .andExpect(status().isCreated())
- .andExpect(jsonPath("id", any(Integer.class)))
- .andExpect(jsonPath("content", is("Car: 1")))
- .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
- .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
- }
-
- private static String asJsonString(final Object obj) throws JsonProcessingException {
- final ObjectMapper mapper = new ObjectMapper();
- return mapper.writeValueAsString(obj);
- }
-}
--- /dev/null
+package de.spring.webservices.rest.controller;
+
+import static org.hamcrest.Matchers.any;
+import static org.hamcrest.Matchers.is;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import de.spring.webservices.domain.Car;
+
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
+public class CarControllerIntegrationTest {
+ private CarController controller;
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ controller = new CarController();
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
+ }
+
+ @Test
+ public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
+ mockMvc.perform(get("/api/cars/")
+ .accept(MediaType.APPLICATION_JSON_UTF8))
+
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$[0].id", any(Integer.class)))
+ .andExpect(jsonPath("$[0].content", is("Car: 1")))
+ .andExpect(jsonPath("$[1].content", is("Car: 2")))
+ .andExpect(jsonPath("$[1].id", any(Integer.class)))
+ .andExpect(jsonPath("$[2].content", is("Car: 3")))
+ .andExpect(jsonPath("$[2].id", any(Integer.class)))
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
+ }
+
+ @Test
+ public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
+ mockMvc.perform(get("/api/cars/{id}", 1L)
+ .accept(MediaType.APPLICATION_JSON_UTF8))
+
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("id", any(Integer.class)))
+ .andExpect(jsonPath("content", is("Car: 1")))
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
+ }
+
+ @Test
+ public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
+ Car car = new Car(2L, "nothing");
+ mockMvc.perform(post("/api/cars/")
+ .contentType(MediaType.APPLICATION_JSON_UTF8)
+ .content(asJsonString(car))
+ .accept(MediaType.APPLICATION_JSON_UTF8))
+
+ .andExpect(status().isCreated())
+ .andExpect(jsonPath("id", any(Integer.class)))
+ .andExpect(jsonPath("content", is("Car: 1")))
+ .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
+ }
+
+ private static String asJsonString(final Object obj) throws JsonProcessingException {
+ final ObjectMapper mapper = new ObjectMapper();
+ return mapper.writeValueAsString(obj);
+ }
+}