From: Gustavo Martin Morcuende Date: Thu, 4 Jun 2015 22:29:27 +0000 (+0200) Subject: integration and unit tests X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2289501873c58c9cdcbd863cc60090e14f322496;p=SpringWebServicesForFun%2F.git integration and unit tests --- diff --git a/README.md b/README.md index 235ed8a..9fe3e8b 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ mvn jetty:run mvn clean install -Dmaven.test.skip=true +TODO: maven-jaxb2-plugin in order to reuse generated Java code from other projects (cxf-xjc-plugin doesn't implement such feature +without generating episodes by myself) TODO: custom bindings when creating Java code from wsdl. TODO: using Jetty instead of Tomcat TODO: logging Spring information diff --git a/web-services-spring-server/pom.xml b/web-services-spring-server/pom.xml index 4909a82..f7ee949 100644 --- a/web-services-spring-server/pom.xml +++ b/web-services-spring-server/pom.xml @@ -76,6 +76,11 @@ spring-ws-test test + + org.mockito + mockito-core + test + diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java b/web-services-spring-server/src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java deleted file mode 100644 index 3d5646e..0000000 --- a/web-services-spring-server/src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.spring.webservices.business; - -import org.springframework.stereotype.Service; - -import de.spring.webservices.auto.CustomBindingExampleRequest; -import de.spring.webservices.auto.CustomBindingExampleResponse; -import de.spring.webservices.operations.Operations; - - -@Service("customBindingBusinessExample") -public class CustomBindingBusinessExample implements - Operations.RequestResponse { - - - @Override - public CustomBindingExampleResponse requestResponse( - final CustomBindingExampleRequest request) { - final CustomBindingExampleResponse response = new CustomBindingExampleResponse(); - - response.setData("CUSTOM BINDING SNAKE EYES AND " + request.getData()); - - return response; - } - -} diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/endpoints/ExampleEndPoint.java b/web-services-spring-server/src/main/java/de/spring/webservices/endpoints/ExampleEndPoint.java index 6a91779..8fb4e40 100644 --- a/web-services-spring-server/src/main/java/de/spring/webservices/endpoints/ExampleEndPoint.java +++ b/web-services-spring-server/src/main/java/de/spring/webservices/endpoints/ExampleEndPoint.java @@ -14,6 +14,7 @@ import de.spring.webservices.auto.ExampleRequest; import de.spring.webservices.auto.ExampleResponse; import de.spring.webservices.operations.Operations; import de.spring.webservices.operations.Operations.RequestResponse; +import de.spring.webservices.services.ExampleService; @Endpoint @@ -21,36 +22,36 @@ public class ExampleEndPoint { private static final String NAMESPACE_URI = "http://gumartinm.name/spring-ws/example"; private final Operations.RequestResponse - customBindingExample; + customBindingExampleService; + + private final ExampleService exampleService; @Autowired public ExampleEndPoint( - RequestResponse customBindingExample) { - this.customBindingExample = customBindingExample; + RequestResponse customBindingExampleService, + ExampleService exampleService) { + this.customBindingExampleService = customBindingExampleService; + this.exampleService = exampleService; } @PayloadRoot(localPart = "ExampleRequest", namespace = NAMESPACE_URI) @ResponsePayload - public ExampleResponse order( - @RequestPayload final ExampleRequest requestObject, + public ExampleResponse exampleResponse( + @RequestPayload final ExampleRequest request, @RequestPayload final Element element, final MessageContext messageContext) { - final ExampleResponse response = new ExampleResponse(); - - response.setData("SNAKE EYES AND " + requestObject.getData()); - - return response; + return this.exampleService.doResponse(request); } @PayloadRoot(localPart = "CustomBindingExampleRequest", namespace = NAMESPACE_URI) @ResponsePayload - public CustomBindingExampleResponse order( + public CustomBindingExampleResponse cuntomBindingExampleResponse( @RequestPayload final CustomBindingExampleRequest requestObject, @RequestPayload final Element element, final MessageContext messageContext) { - return this.customBindingExample.requestResponse(requestObject); + return this.customBindingExampleService.requestResponse(requestObject); } } diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/services/ExampleService.java b/web-services-spring-server/src/main/java/de/spring/webservices/services/ExampleService.java new file mode 100644 index 0000000..9d7eb79 --- /dev/null +++ b/web-services-spring-server/src/main/java/de/spring/webservices/services/ExampleService.java @@ -0,0 +1,11 @@ +package de.spring.webservices.services; + +import de.spring.webservices.auto.ExampleRequest; +import de.spring.webservices.auto.ExampleResponse; + + +public interface ExampleService { + + public ExampleResponse doResponse(ExampleRequest request); + +} diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java b/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java new file mode 100644 index 0000000..caa7e41 --- /dev/null +++ b/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/CustomBindingExampleServiceImpl.java @@ -0,0 +1,27 @@ +package de.spring.webservices.services.impl; + +import org.springframework.stereotype.Service; + +import de.spring.webservices.auto.CustomBindingExampleRequest; +import de.spring.webservices.auto.CustomBindingExampleResponse; +import de.spring.webservices.auto.ParentEnumType; +import de.spring.webservices.operations.Operations; + + +@Service("customBindingExampleService") +public class CustomBindingExampleServiceImpl implements + Operations.RequestResponse { + + + @Override + public CustomBindingExampleResponse requestResponse(final CustomBindingExampleRequest request) { + + CustomBindingExampleResponse response = new CustomBindingExampleResponse(); + + response.setData("CUSTOM BINDING SNAKE EYES AND " + request.getData()); + response.setParentEnum(ParentEnumType.FIRST); + + return response; + } + +} diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/ExampleServiceImpl.java b/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/ExampleServiceImpl.java new file mode 100644 index 0000000..5f2d50b --- /dev/null +++ b/web-services-spring-server/src/main/java/de/spring/webservices/services/impl/ExampleServiceImpl.java @@ -0,0 +1,23 @@ +package de.spring.webservices.services.impl; + +import org.springframework.stereotype.Service; + +import de.spring.webservices.auto.ExampleRequest; +import de.spring.webservices.auto.ExampleResponse; +import de.spring.webservices.services.ExampleService; + + +@Service("exampleServiceImpl") +public class ExampleServiceImpl implements ExampleService { + + @Override + public ExampleResponse doResponse(final ExampleRequest request) { + + ExampleResponse response = new ExampleResponse(); + + response.setData("SNAKE EYES AND " + request.getData()); + + return response; + } + +} diff --git a/web-services-spring-server/src/main/resources/spring-configuration/ws/soap-ws.xml b/web-services-spring-server/src/main/resources/spring-configuration/ws/soap-ws.xml index d77e7ab..378780d 100644 --- a/web-services-spring-server/src/main/resources/spring-configuration/ws/soap-ws.xml +++ b/web-services-spring-server/src/main/resources/spring-configuration/ws/soap-ws.xml @@ -45,7 +45,7 @@ - + diff --git a/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointIntegrationTest.java b/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointIntegrationTest.java index 2b3a19a..f0204cb 100644 --- a/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointIntegrationTest.java +++ b/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointIntegrationTest.java @@ -17,7 +17,7 @@ import org.springframework.xml.transform.StringSource; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("/spring-configuration/ws/soap-ws.xml") +@ContextConfiguration(locations = { "classpath*:spring-configuration/ws/soap-ws.xml" } ) public class ExampleEndPointIntegrationTest { @Autowired @@ -42,6 +42,21 @@ public class ExampleEndPointIntegrationTest { ""); mockClient.sendRequest(withPayload(requestPayload)).andExpect( payload(responsePayload)); + + + final Source customRequestPayload = new StringSource( + "" + + "SCARLETT" + + "2015-06-03T10:20:30Z" + + "FIRST" + + ""); + final Source customResponsePayload = new StringSource( + "" + + "CUSTOM BINDING SNAKE EYES AND SCARLETT" + + "FIRST" + + ""); + mockClient.sendRequest(withPayload(customRequestPayload)).andExpect( + payload(customResponsePayload)); } } diff --git a/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointTest.java b/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointTest.java new file mode 100644 index 0000000..5b04c2f --- /dev/null +++ b/web-services-spring-server/src/test/java/de/spring/webservices/endpoints/ExampleEndPointTest.java @@ -0,0 +1,52 @@ +package de.spring.webservices.endpoints; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.Before; +import org.junit.Test; + +import de.spring.webservices.auto.CustomBindingExampleRequest; +import de.spring.webservices.auto.CustomBindingExampleResponse; +import de.spring.webservices.auto.ExampleRequest; +import de.spring.webservices.operations.Operations; +import de.spring.webservices.services.ExampleService; + + +public class ExampleEndPointTest { + + private ExampleService exampleService; + + private Operations.RequestResponse + customBindingExampleService; + + private ExampleEndPoint exampleEndPoint; + + @Before + public void init() { + exampleService = mock(ExampleService.class); + customBindingExampleService = mock(Operations.RequestResponse.class); + exampleEndPoint = new ExampleEndPoint(customBindingExampleService, exampleService); + } + + @Test + public void givenExampleRequestThenInvokeExampleService() { + ExampleRequest request = new ExampleRequest(); + request.setData("SCARLETT"); + + exampleEndPoint.exampleResponse(request, null, null); + + verify(exampleService).doResponse(request); + } + + @Test + public void givenCustomBindingExampleRequestThenInvokeCustomBindingExampleService() { + CustomBindingExampleRequest request = new CustomBindingExampleRequest(); + request.setData("SCARLETT"); + + exampleEndPoint.cuntomBindingExampleResponse(request, null, null); + + verify(customBindingExampleService).requestResponse(request); + } + +} diff --git a/web-services-spring-server/src/test/java/de/spring/webservices/services/ExampleServiceTest.java b/web-services-spring-server/src/test/java/de/spring/webservices/services/ExampleServiceTest.java new file mode 100644 index 0000000..493c76d --- /dev/null +++ b/web-services-spring-server/src/test/java/de/spring/webservices/services/ExampleServiceTest.java @@ -0,0 +1,32 @@ +package de.spring.webservices.services; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import de.spring.webservices.auto.ExampleRequest; +import de.spring.webservices.auto.ExampleResponse; +import de.spring.webservices.services.impl.ExampleServiceImpl; + +public class ExampleServiceTest { + + private ExampleService exampleService; + + @Before + public void init() { + exampleService = new ExampleServiceImpl(); + } + + @Test + public void givenExampleRequestThenReturnExampleResponse() { + ExampleRequest request = new ExampleRequest(); + request.setData("SCARLETT"); + ExampleResponse expected = new ExampleResponse(); + expected.setData("SNAKE EYES AND " + request.getData()); + + ExampleResponse actual = exampleService.doResponse(request); + + Assert.assertEquals(expected.getData(), actual.getData()); + } + +} diff --git a/web-services-spring-server/src/test/java/de/spring/webservices/services/impl/CustomBindingExampleServiceTest.java b/web-services-spring-server/src/test/java/de/spring/webservices/services/impl/CustomBindingExampleServiceTest.java new file mode 100644 index 0000000..45c019a --- /dev/null +++ b/web-services-spring-server/src/test/java/de/spring/webservices/services/impl/CustomBindingExampleServiceTest.java @@ -0,0 +1,33 @@ +package de.spring.webservices.services.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import de.spring.webservices.auto.CustomBindingExampleRequest; +import de.spring.webservices.auto.CustomBindingExampleResponse; +import de.spring.webservices.operations.Operations; + + +public class CustomBindingExampleServiceTest { + + private Operations.RequestResponse + customBindingExampleService; + + @Before + public void init() { + customBindingExampleService = new CustomBindingExampleServiceImpl(); + } + + @Test + public void givenCustomBindingExampleRequestThenReturnCustomBindingExampleResponse() { + CustomBindingExampleRequest request = new CustomBindingExampleRequest(); + request.setData("SCARLETT"); + CustomBindingExampleResponse expected = new CustomBindingExampleResponse(); + expected.setData("CUSTOM BINDING SNAKE EYES AND " + request.getData()); + + CustomBindingExampleResponse actual = customBindingExampleService.requestResponse(request); + + Assert.assertEquals(expected.getData(), actual.getData()); + } +} diff --git a/web-services-spring/pom.xml b/web-services-spring/pom.xml index c1085dd..3158e24 100644 --- a/web-services-spring/pom.xml +++ b/web-services-spring/pom.xml @@ -98,6 +98,12 @@ ${spring.ws.version} test + + org.mockito + mockito-core + 2.0.11-beta + test +