REST: cleaning up package names and services (interfaces with implementations)
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 8 Jan 2016 22:35:19 +0000 (23:35 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 8 Jan 2016 22:35:19 +0000 (23:35 +0100)
16 files changed:
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/BusinessService.java [deleted file]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/BusinessService.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/CarClientService.java [deleted file]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/CarClientService.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/main/MainTest.java [deleted file]
REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/BusinessServiceTest.java [deleted file]
REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java [new file with mode: 0644]
REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/CarClientServiceIntegrationTest.java [deleted file]
REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java [new file with mode: 0644]
REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/CarController.java [deleted file]
REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/controller/CarController.java [new file with mode: 0644]
REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java [deleted file]
REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java [new file with mode: 0644]

diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java
new file mode 100644 (file)
index 0000000..0f066ad
--- /dev/null
@@ -0,0 +1,35 @@
+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();
+    }
+}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/BusinessService.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/BusinessService.java
deleted file mode 100644 (file)
index 9f76052..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-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());
-       }
-}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/BusinessService.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/BusinessService.java
new file mode 100644 (file)
index 0000000..ca7a7f2
--- /dev/null
@@ -0,0 +1,11 @@
+package de.spring.webservices.rest.business.service;
+
+
+public interface BusinessService {
+
+       public void doSomethingWithCars();
+       
+       public void doSomethingWithCar(long id);
+       
+       public void createsNewCar();
+}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java
new file mode 100644 (file)
index 0000000..b6367e8
--- /dev/null
@@ -0,0 +1,53 @@
+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());
+       }
+}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/CarClientService.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/CarClientService.java
deleted file mode 100644 (file)
index 2d2e2da..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-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();
-       }
-}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/CarClientService.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/CarClientService.java
new file mode 100644 (file)
index 0000000..23966ba
--- /dev/null
@@ -0,0 +1,14 @@
+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);
+}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java
new file mode 100644 (file)
index 0000000..bdb540b
--- /dev/null
@@ -0,0 +1,58 @@
+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();
+       }
+}
diff --git a/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/main/MainTest.java b/REST/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/main/MainTest.java
deleted file mode 100644 (file)
index 721a58b..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-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();
-    }
-}
diff --git a/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/BusinessServiceTest.java b/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/BusinessServiceTest.java
deleted file mode 100644 (file)
index 8d279a6..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-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());
-       }
-}
diff --git a/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java b/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java
new file mode 100644 (file)
index 0000000..53665c4
--- /dev/null
@@ -0,0 +1,67 @@
+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());
+       }
+}
diff --git a/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/CarClientServiceIntegrationTest.java b/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/CarClientServiceIntegrationTest.java
deleted file mode 100644 (file)
index d2c43ad..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-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);
-       }
-}
diff --git a/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java b/REST/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java
new file mode 100644 (file)
index 0000000..b5696be
--- /dev/null
@@ -0,0 +1,117 @@
+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);
+       }
+}
diff --git a/REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/CarController.java b/REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/CarController.java
deleted file mode 100644 (file)
index b5ae241..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-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);
-    }
-
-}
diff --git a/REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/controller/CarController.java b/REST/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/controller/CarController.java
new file mode 100644 (file)
index 0000000..e769bc6
--- /dev/null
@@ -0,0 +1,111 @@
+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);
+    }
+
+}
diff --git a/REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java b/REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java
deleted file mode 100644 (file)
index c551c85..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-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);
-       }
-}
diff --git a/REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java b/REST/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java
new file mode 100644 (file)
index 0000000..c856efb
--- /dev/null
@@ -0,0 +1,85 @@
+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);
+       }
+}