Pages

Wednesday, April 23, 2014

SPRING-WS 2.X + JUNIT + MAVEN + CARGO

More information: http://projects.spring.io/spring-ws/
Download Eclipse project from Github: https://github.com/wcursino/simpleSpring-ws

SimpleEndpoint.java :
package com.cursino.wilson;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
@Endpoint
public class SimpleEndpoint {
    private static final String NAME_SPACE = "http://javacaffeine.com/service/schemas";
    @Autowired
    private SimpleService simpleService;
    public void setSimpleService(SimpleService simpleService) {
        this.simpleService = simpleService;
    }    @PayloadRoot(namespace = NAME_SPACE, localPart = "SimpleRequest")
    public @ResponsePayload
    Element handleRequest(@RequestPayload Element request) {
        Namespace ns = Namespace.getNamespace("wsc", NAME_SPACE);
        int a = new Integer(request.getChild("a", ns).getValue().trim());
        int b = new Integer(request.getChild("b", ns).getValue().trim());
        Element response = new Element("SimpleResponse", ns);
        response.addNamespaceDeclaration(ns);
        Element outcomes = new Element("sum", ns);
        outcomes.setText(simpleService.sum(a, b));
        response.addContent(outcomes);
        return response;
    }
}

SimpleService.java : 
package com.cursino.wilson;
import org.springframework.stereotype.Service;
@Service
public class SimpleService {
    public String sum(int a, int b) {
        return "The sum of A(" + a + ") and B(" + b + ") is " + (a + b);
    }
}

SimpleEndpointTest.java:
package com.cursino.wilson;

import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;
import static org.springframework.ws.test.server.ResponseMatchers.validPayload;
import javax.xml.transform.Source;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-ws-servlet.xml")
public class SimpleEndpointTest {
    private MockWebServiceClient mockClient;
    private Resource xsd;
    @Before
    public void createClient() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(ApplicationConfig.class);
        context.refresh();
        Assert.assertNotNull(context.getBean(SimpleEndpoint.class));
        xsd = context.getResource("file:./src/main/webapp/WEB-INF/simple.xsd");
        mockClient = MockWebServiceClient.createClient(context);
    }
    @Test
    public void outcomesEndpointTest() throws Exception {
        Source requestPayload = new StringSource("<wsc:SimpleRequest "
                + "xmlns:wsc=\"http://javacaffeine.com/service/schemas\">"
                + "<wsc:a>1</wsc:a>"
                + "<wsc:b>2</wsc:b>"
                + "</wsc:SimpleRequest>");
        Source responsePayload = new StringSource("<wsc:SimpleResponse "
                + "xmlns:wsc=\"http://javacaffeine.com/service/schemas\">"
                + "<wsc:sum>The sum of A(1) and B(2) is 3</wsc:sum>"
                + "</wsc:SimpleResponse>");
        mockClient.sendRequest(withPayload(requestPayload))
        .andExpect(validPayload(xsd)); // validate based on the XSD.
        
        mockClient.sendRequest(withPayload(requestPayload))
        .andExpect(payload(responsePayload)); // validate based on response.
    }
}
@Configuration
@ComponentScan("com.cursino.wilson")
class ApplicationConfig {}// Workaround to register the annotated beans.

simple.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:wsc="http://javacaffeine.com/service/schemas"
 elementFormDefault="qualified"
 targetNamespace="http://javacaffeine.com/service/schemas">
    <xs:element name="SimpleRequest">
       <xs:complexType>
       <xs:sequence>
          <xs:element name="atype="xs:integer" />
          <xs:element name="btype="xs:integer" />
       </xs:sequence>
    </xs:complexType>
    </xs:element>
       <xs:element name="SimpleResponse">
         <xs:complexType>
       <xs:sequence>
         <xs:element name="sumtype="xs:string" />
           </xs:sequence>
      </xs:complexType>
    </xs:element>
</xs:schema>

spring-ws-servlet.xml:

<?xml version="1.0encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:sws="http://www.springframework.org/schema/web-services"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/web-services 
http://www.springframework.org/schema/web-services/web-services-2.0.xsd     
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.cursino.wilson" />
    <sws:annotation-driven />
    <sws:dynamic-wsdl 
        id="simple
        portTypeName="SimplePort"
        locationUri="/service/
        targetNamespace="http://javacaffeine.com/service/definitions">
        <sws:xsd location="/WEB-INF/simple.xsd" />
    </sws:dynamic-wsdl>
</beans>

web.xml: 
<?xml version="1.0encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
"
    id="simpleSpringWS"
    version="3.0"
    metadata-complete="true">
    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0xmlns:xsi="http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd
">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cursino.wilson</groupId>
    <artifactId>simpleSpringWS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warName>simpleSpringWS</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-test</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <!-- Business layer -->
        <dependency>
            <groupId>com.wilson.cursino</groupId>
            <artifactId>PaiGowDraw</artifactId>
            <version>0.1-SNAPSHOT</version>
        </dependency>
        <!-- Spring Webservice -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <!-- Parsers -->
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>

Run the project with Cargo:
mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run -Dcargo.maven.containerId=tomcat7x -Dcargo.maven.containerUrl=http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip

It will download and run a Tomcat in local.

simple.wsdl  
Based on the XSD and some conventions, Spring-WS can create the WSDL. You can access it through http://localhost:8080/simpleSpringWS/service/simple.wsdl

SoapUI:

With SoapUI (www.soapui.org) you can easily test test web service.



No comments :