SAAJ Unit testing client for JAX-WS web services

JAX-WS is the simplest java web services. In this blog, I would like to use first example explained in the book Java Web Services: Up and Running, by Martin Kalin ( Thumbs up thanks Martin, great book). Here the SEI and SBI created from the above book.

SEI

package com.ojitha.ws.ex1;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface TimeServer {
	@WebMethod String getTimeAsString();
	@WebMethod long getTimeAsElapsed();
}

SBI

package com.ojitha.ws.ex1;

import java.util.Date;

import javax.jws.WebService;

@WebService(endpointInterface="com.ojitha.ws.ex1.TimeServer")
public class TimeSeverImpl implements TimeServer {

	@Override
	public String getTimeAsString() {
		return new Date().toString();
	}

	@Override
	public long getTimeAsElapsed() {
		return new Date().getTime();
	}
	


}

The main objective of this blog is to introduce a SAAJ API based JUnit testing.  In the following JUnit class, sendRequst() pass the name space (first parameter) which is the package name of the SEI. The second parameter is the web service method name. The sendRequst() consume the web service and return the response SOAP message. This message is input the is correct isCorrectResponse() method. If the response is as expected: web service method postfix the “Response”, the return true, otherwise false.

package com.ojitha.ws.ex1;

import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Endpoint;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TimeSeverImplTest {

	private static final String ENDPOINT_URL = "http://127.0.0.1:9876/ts";
	private static final String NAME_SPACE = "http://ex1.ws.ojitha.com/";
	Endpoint endpoint;
	

	@Before
	public void setUp() throws Exception {
		endpoint = Endpoint.publish(ENDPOINT_URL, new TimeSeverImpl());
	}

	@After
	public void tearDown() throws Exception {
		endpoint.stop();
	}

	@Test
	public void testGetTimeAsString() {

		try {
			SOAPMessage response = sendRequest(NAME_SPACE, "getTimeAsString");
			Assert.assertTrue(isCorrectResponse("getTimeAsStringResponse", response));
		} catch (SOAPException e) {
			fail("soap message failed");
		} catch (MalformedURLException e) {
			fail("Malformed URL");
		}

	}

	@Test
	public void testGetTimeAsElapsed() {
		try {
			SOAPMessage response = sendRequest(NAME_SPACE, "getTimeAsElapsed");
			Assert.assertTrue(isCorrectResponse("getTimeAsElapsedResponse", response));
		} catch (SOAPException e) {
			fail("soap message failed");
		} catch (MalformedURLException e) {
			fail("Malformed URL");
		}
	}

	private SOAPMessage sendRequest(String ns, String methodName) throws SOAPException,
			MalformedURLException {
		// create soap message
		SOAPMessage response = null;

		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage request = messageFactory.createMessage();
		SOAPPart soapPart = request.getSOAPPart();
		SOAPEnvelope envelop = soapPart.getEnvelope();
	
		SOAPBody body = envelop.getBody();
		QName name = new QName(ns, methodName, "xx");
		body.addBodyElement(name);

		SOAPConnectionFactory connectionFactory = SOAPConnectionFactory
				.newInstance();
		SOAPConnection soapConnection = connectionFactory.createConnection();

		// sending the message
		URL to = new URL(ENDPOINT_URL);
		//URL to = new URL("http://127.0.0.1:9875/ts");
		response = soapConnection.call(request, to);

		return response;
	}
	
	private boolean isCorrectResponse(String responseLocalName, SOAPMessage response) throws SOAPException{
		SOAPBody responseBody = response.getSOAPBody();
		Iterator<Node> it = responseBody.getChildElements();
		boolean find = false;
		while (it.hasNext()) {
			Node node = it.next();
			if (responseLocalName.equals(node.getLocalName())) {
				find = true;
				break;
			}
		}
		return find;
	}
}

In the above code, I tried to keep the code simple. In the setup() method, the web service published and in the tearDown() method, stop. You can see the WSDL if you issue http://127.0.0.1:9876/ts?wsdl,  this will display the following WSDL in your browser. This WSDL never written, that is the very good Sarcastic smile.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python