Tuesday, December 20, 2011

Testing and Using the SOAP Client

As with the rest service, we theoretically have a SOAP service up and running, but how do we know that it is functional?  Well, with Glassfish, we have an automatic tester that we can use.  It will generate the code from the WSDL and give us inputs for each method we need to supply parameters for. (Note:  In order to allow the tester to compile the code, we have to have the tools.jar file from JDK 1.5 or greater in the classpath of the JEE container.  To simplify things, I simply copied it into the lib directory for glassfish.  Or you could have put it in the domain lib, or included it with your app.  Or, modify the startup to include the jdk libs.)

Navigate to: http://localhost:8080/SOAPDemoWebService/SOAPDemoWebService?Tester

This generates a screen like:

If we fill in the field for find with Tower and click on find, we get:
This allows us to test each method manually.

Next we generate a JUnit test to quickly test all the methods.
SOAPClentBeanTest.java

package com.sample.service.client;

import org.junit.Test;
import static org.junit.Assert.*;

public class SOAPClientBeanTest {

    public SOAPClientBeanTest() {
    }

    @Test
    public void testCreateFindDelete() {
        System.out.println("create");
        SOAPClientBean instance = new SOAPClientBean();
        instance.setName("A New Part");
        instance.setParentName("Tower");
        String expResult = "";
        String result = instance.create();
        System.out.println(instance.getMsg());
        assertFalse(instance.getName().isEmpty());
        assertFalse(instance.getParentName().isEmpty());
        instance.find();
        assertFalse(instance.getName().isEmpty());
        assertFalse(instance.getParentName().isEmpty());
        assertFalse(instance.getMsg().isEmpty());
        System.out.println(instance.getMsg());
        instance.delete();
        instance.find();
        assertTrue(instance.getName().isEmpty());
        assertTrue(instance.getParentName().isEmpty());
    }
}


And we see that we can create, find and remove a part.

Lastly, we create a UI Interface just like we did for our REST client:

...
<p:dialog id="soapClient" header="PartEntity SOAP Client : Create, Find, Delete" widgetVar="soapDlg">
    <h:form id="soapForm" prependId="false">
        <h:panelGrid id="soapPanelGrid" columns="2" cellpadding="5" >
            <h:outputLabel for="soapName" value="Name for Create, Find or Delete: " />
            <h:inputText value="#{soapBean.name}" id="soapName" />
            <h:outputLabel for="restParentName" value="Parent Part Name: " />
            <h:inputText value="#{soapBean.parentName}" id="soapParentName" />
            <h:outputText value="Results: "/>
            <h:outputText value="#{soapBean.msg}" id="soapMsg"/>
            <f:facet name="footer">
                <p:commandButton actionListener="#{soapBean.create}" update="soapMsg" value="Create" /> 
                <p:commandButton value="Delete" actionListener="#{soapBean.delete}" update="soapMsg"/>
                <p:commandButton value="Find" actionListener="#{soapBean.find}" update="soapMsg"/>    
                <p:commandButton type="button" immediate="true" value="Close" onclick="soapDlg.hide();" />  
            </f:facet>
        </h:panelGrid>
    </h:form>
</p:dialog>
...


This gives us a dialog that looks like:

So, as with the REST exercise, we have tested our service, and implemented a UI for our service.  It was approximately the same amount of effor to create each round trip service using the automated tools for creating them and implementing the xml translations, but, there is a huge push out there towards REST.  Until recently, Amazon.com supported both REST and SOAP, but because only 3% of its customers used SOAP, they discontinued it.  That gives a clear indication as to the direction of the industry.

No comments:

Post a Comment