package org.jbpm.bpel.service.soap;
import java.util.Iterator;
import java.util.Random;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.WSDLException;
import javax.xml.namespace.QName;
import junit.framework.TestCase;
import org.w3c.dom.Element;
import org.jbpm.bpel.exe.Fault;
import org.jbpm.bpel.wsdl.util.WsdlUtil;
import org.jbpm.bpel.xml.util.XmlUtil;
/**
* @author Alejandro Guízar
* @version $Revision: 1.3 $ $Date: 2005/12/24 07:38:24 $
*/
public class PortCallerTest extends TestCase {
static final String NS_TRANSLATOR = "http://example.com/translator";
static final String NS_TYPES = "http://example.com/translator/types";
static final QName TRANSLATOR_SERVICE = new QName(NS_TRANSLATOR, "translatorService");
static final String TEXT_WSDL_LOCATION = "http://localhost:8080/translator/text?wsdl";
static final String DOC_WSDL_LOCATION = "http://localhost:8080/translator/document?wsdl";
public PortCallerTest(String name) {
super(name);
}
public void testCall_document_requestResponse() throws Exception {
String inputText =
"" +
" " +
" " +
" " +
" " +
" " +
" hi" +
" bye" +
" " +
" " +
" " +
" " +
"";
PortCaller caller = createCaller(DOC_WSDL_LOCATION, TRANSLATOR_SERVICE, "documentTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
Element outputData = caller.call("translate", inputData);
Element docAccessor = XmlUtil.getElement(outputData, "document");
Element docElem = XmlUtil.getElement(docAccessor, NS_TYPES, "document");
Element headElem = XmlUtil.getElement(docElem, "head");
assertEquals("carta", headElem.getAttribute("title"));
assertEquals("es", headElem.getAttribute("language"));
Element bodyElem = XmlUtil.getElement(docElem, "body");
Iterator paragraphElems = XmlUtil.getElements(bodyElem, null, "paragraph");
assertEquals("hola", XmlUtil.getValue((Element) paragraphElems.next()));
assertEquals("adi\u00f3s", XmlUtil.getValue((Element) paragraphElems.next()));
assertFalse(paragraphElems.hasNext());
}
public void testCall_document_requestFault() throws Exception {
String inputText =
"" +
" " +
" " +
" " +
" " +
" " +
" hi" +
" wawa" +
" " +
" " +
" " +
" " +
"";
PortCaller caller = createCaller(DOC_WSDL_LOCATION, TRANSLATOR_SERVICE, "documentTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
try {
caller.call("translate", inputData);
fail("call should have thrown a fault");
}
catch (Fault f) {
assertEquals(new QName(NS_TRANSLATOR, "textNotTranslatable"), f.getName());
assertEquals(new QName(NS_TRANSLATOR, "textNotTranslatableFault"), f.getType().getName());
Element faultData = (Element) f.getData();
Element detailAccessor = XmlUtil.getElement(faultData, "detail");
Element textNotTranElem = XmlUtil.getElement(detailAccessor, NS_TYPES, "textNotTranslatable");
Element textElem = XmlUtil.getElement(textNotTranElem, "text");
assertEquals("wawa", XmlUtil.getValue(textElem));
}
}
public void testCall_document_oneWay() throws Exception {
String clientName = createClientName();
String inputText =
"" +
" " +
" " +
" " +
" " +
" " +
" hi" +
" bye" +
" " +
" " +
" " +
" " +
"";
PortCaller caller = createCaller(DOC_WSDL_LOCATION, TRANSLATOR_SERVICE, "documentTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
caller.callOneWay("quoteTranslation", inputData);
inputText =
"" +
" " +
" " +
" " +
"";
inputData = XmlUtil.parseElement(inputText);
/* quote is an one-way operation, so the status change might not be reflected immediately */
Thread.sleep(500);
Element outputData = caller.call("getQuotationStatus", inputData);
Element statusAccessor = XmlUtil.getElement(outputData, "statusResponse");
Element statusElem = XmlUtil.getElement(statusAccessor, NS_TYPES, "statusResponse");
assertEquals("received", statusElem.getAttribute("status"));
}
public void testCall_rpc_requestResponse() throws Exception {
String inputText =
"" +
" hi" +
" en" +
" es" +
"";
PortCaller caller = createCaller(TEXT_WSDL_LOCATION, TRANSLATOR_SERVICE, "textTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
Element outputData = caller.call("translate", inputData);
Element textAccessor = XmlUtil.getElement(outputData, "translatedText");
assertEquals("hola", XmlUtil.getValue(textAccessor));
}
public void testCall_rpc_requestFault() throws Exception {
String inputText =
"" +
" hi" +
" en" +
" ja" +
"";
PortCaller caller = createCaller(TEXT_WSDL_LOCATION, TRANSLATOR_SERVICE, "textTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
try {
caller.call("translate", inputData);
fail("call should have thrown a fault");
}
catch (Fault f) {
assertEquals(new QName(NS_TRANSLATOR, "dictionaryNotAvailable"), f.getName());
assertEquals(new QName(NS_TRANSLATOR, "dictionaryNotAvailableFault"), f.getType().getName());
assertTrue(f.getData() instanceof Element);
}
}
public void testCall_rpc_oneWay() throws Exception {
String clientName = createClientName();
String inputText =
"" +
" " + clientName + "" +
" hi" +
" en" +
" es" +
"";
PortCaller caller = createCaller(TEXT_WSDL_LOCATION, TRANSLATOR_SERVICE, "textTranslatorPort");
Element inputData = XmlUtil.parseElement(inputText);
caller.callOneWay("quoteTranslation", inputData);
inputText =
"" +
" " + clientName + "" +
"";
inputData = XmlUtil.parseElement(inputText);
/* quote is an one-way operation, so the status change might not be reflected immediately */
Thread.sleep(500);
Element outputData = caller.call("getQuotationStatus", inputData);
Element statusAccessor = XmlUtil.getElement(outputData, "status");
assertEquals("received", XmlUtil.getValue(statusAccessor));
}
private PortCaller createCaller(String wsdlLocation, QName serviceName, String portName)
throws WSDLException {
// read wsdl
Definition def = WsdlUtil.getFactory().newWSDLReader().readWSDL(wsdlLocation);
Service service = def.getService(serviceName);
Port port = service.getPort(portName);
// configure caller
return new PortCaller(port);
}
private static String createClientName() {
return "client" + new Random().nextInt(100000);
}
static {
System.setProperty("javax.xml.soap.MessageFactory", "org.jboss.ws.soap.MessageFactoryImpl");
System.setProperty("javax.xml.soap.SOAPConnectionFactory", "org.jboss.ws.soap.SOAPConnectionFactoryImpl");
}
}