1 package org.jbpm.bpel.service.def; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.Serializable ; 5 import java.util.HashMap ; 6 7 import javax.jms.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.wsdl.Definition; 11 import javax.xml.namespace.QName ; 12 import javax.xml.rpc.handler.HandlerInfo ; 13 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 14 import javax.xml.soap.MessageFactory ; 15 import javax.xml.soap.SOAPMessage ; 16 17 import junit.framework.TestCase; 18 19 import org.w3c.dom.Element ; 20 21 import org.jbpm.bpel.bar.BarApplication; 22 import org.jbpm.bpel.def.BpelDefinition; 23 import org.jbpm.bpel.def.ImportsDefinition; 24 import org.jbpm.bpel.messager.MessagerService; 25 import org.jbpm.bpel.par.JndiProcessDeployer; 26 import org.jbpm.bpel.wsdl.def.PartnerLinkType; 27 import org.jbpm.bpel.wsdl.util.WsdlUtil; 28 import org.jbpm.bpel.xml.BpelConstants; 29 import org.jbpm.bpel.xml.BpelReader; 30 import org.jbpm.bpel.xml.util.NodeUtil; 31 32 36 public class BpelEndpointHandlerTest extends TestCase { 37 38 private BpelEndpointHandler handler; 39 private MessagerService messagerService; 40 private Context initialContext; 41 private JndiProcessDeployer deployer; 42 43 private static final String WSDL_TEXT = 44 "<definitions targetNamespace='http://jbpm.org/bpel/examples'" + 45 " xmlns:tns='http://jbpm.org/bpel/examples'" + 46 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + 47 " xmlns:bpws='http://schemas.xmlsoap.org/ws/2004/03/business-process/'" + 48 " xmlns:plnk='http://schemas.xmlsoap.org/ws/2004/03/partner-link/'" + 49 " xmlns='http://schemas.xmlsoap.org/wsdl/'>" + 50 " <message name='request'>" + 51 " <part name='simplePart' type='xsd:string'/>" + 52 " <part name='elementPart' element='tns:surpriseElement'/>" + 53 " </message>" + 54 " <message name='response'>" + 55 " <part name='intPart' type='xsd:int'/>" + 56 " <part name='complexPart' type='tns:complexType'/>" + 57 " </message>" + 58 " <portType name='pt'>" + 59 " <operation name='op'>" + 60 " <input message='tns:request'/>" + 61 " <output message='tns:response'/>" + 62 " </operation>" + 63 " </portType>" + 64 " <plnk:partnerLinkType name='plt'>" + 65 " <plnk:role name='r1' portType='tns:pt'/>" + 66 " </plnk:partnerLinkType>" + 67 " <bpws:property name='nameProperty' type='xsd:string'/>" + 68 " <bpws:property name='idProperty' type='xsd:int'/>" + 69 " <bpws:propertyAlias propertyName='tns:nameProperty' messageType='tns:request'>" + 70 " <bpws:query>elementPart/tns:surpriseElement/c/@name</bpws:query>" + 71 " </bpws:propertyAlias>" + 72 " <bpws:propertyAlias propertyName='tns:idProperty' messageType='tns:request'>" + 73 " <bpws:query>elementPart/tns:surpriseElement/e</bpws:query>" + 74 " </bpws:propertyAlias>" + 75 "</definitions>"; 76 private static final String REQUEST_ENV_TEXT = 77 "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" + 78 "<soapenv:Body>" + 79 "<foo:op xmlns:foo='urn:someURI'>" + 80 " <simplePart>wazabi</simplePart>" + 81 " <elementPart>" + 82 " <tns:surpriseElement xmlns:tns='http://jbpm.org/bpel/examples'>" + 83 " <b on=\"true\">true</b>" + 84 " <c name=\"venus\"/>" + 85 " <d amount=\"20\"/>" + 86 " <e>30</e>" + 87 " </tns:surpriseElement>" + 88 " </elementPart>" + 89 "</foo:op>" + 90 "</soapenv:Body>" + 91 "</soapenv:Envelope>"; 92 private static final String RESPONSE_ELEM_TEXT = 93 "<vendor:msg0 xmlns:vendor='http://jbpm.org/bpel/examples'>" + 94 " <intPart>2020</intPart>" + 95 " <complexPart attributeOne='hi' attributeTwo='ho'>" + 96 " <ns1:elementOne xmlns:ns1='urn:uriOne'>lets go</ns1:elementOne>" + 97 " <ns2:elementTwo xmlns:ns2='urn:uriTwo'>ram ones</ns2:elementTwo>" + 98 " </complexPart>" + 99 "</vendor:msg0>"; 100 101 private static final String PROCESS_NAME = "testProcess"; 102 private static final long PARTNER_LINK_ID = 1903L; 103 104 public BpelEndpointHandlerTest(String name) { 105 super(name); 106 System.setProperty("javax.xml.soap.MessageFactory", "org.jboss.axis.soap.MessageFactoryImpl"); 107 } 108 109 protected void setUp() throws Exception { 110 BpelDefinition process = new BpelDefinition(PROCESS_NAME); 112 Definition def = WsdlUtil.readText(WSDL_TEXT); 114 ImportsDefinition imports = process.getImports(); 115 imports.addImport(WsdlUtil.createImport(def)); 116 BpelReader.getInstance().registerPropertyAliases(imports); 117 PartnerLinkType plinkType = imports.getPartnerLinkType(new QName (BpelConstants.NS_EXAMPLES, "plt")); 119 PartnerLinkDefinition plink = new PartnerLinkDefinition(); 120 plink.setName("pl"); 121 plink.setPartnerLinkType(plinkType); 122 plink.setMyRole(plinkType.getFirstRole()); 123 plink.setId(PARTNER_LINK_ID); 124 process.getScope().addPartnerLink(plink); 125 initialContext = new InitialContext (); 127 deployer = new JndiProcessDeployer(initialContext); 128 deployer.deployProcessDefinition(process); 129 BarApplication bpelConfig = new BarApplication(); 131 bpelConfig.setConnectionFactoryName("ConnectionFactory"); 132 bpelConfig.setDestinationName("queue/testQueue"); 133 messagerService = MessagerService.buildMessagerService(bpelConfig, process, initialContext); 134 HashMap handlerConfig = new HashMap (); 136 handlerConfig.put(BpelEndpointHandler.PROCESS_NAME_PARAM, process.getName()); 137 handlerConfig.put(BpelEndpointHandler.PARTNER_LINK_NAME_PARAM, plink.getName()); 138 HandlerInfo info = new HandlerInfo (BpelEndpointHandler.class, handlerConfig, new QName [0]); 139 handler = new BpelEndpointHandler(); 140 handler.init(info); 141 } 142 143 protected void tearDown() throws Exception { 144 messagerService.destroy(initialContext); 146 deployer.undeployProcessDefinition(PROCESS_NAME); 148 initialContext.close(); 149 handler.destroy(); 150 } 151 152 public void testGetDestination() throws Exception { 153 Queue destination = (Queue) handler.getDestination(); 154 assertNotNull(destination); 155 assertEquals("testQueue", destination.getQueueName()); 156 } 157 158 public void testGetConnection() throws Exception { 159 Connection connection = handler.getConnection(); 160 ConnectionMetaData metaData = connection.getMetaData(); 161 assertEquals(1, metaData.getJMSMajorVersion()); 162 assertEquals(1, metaData.getJMSMinorVersion()); 163 } 164 165 public void testSendRequest() throws Exception { 166 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage( 167 null, new ByteArrayInputStream (REQUEST_ENV_TEXT.getBytes())); 168 SOAPMessageContext soapContext = new SOAPMessageContextImpl(soapMessage); 169 170 Connection connection = handler.getConnection(); 171 Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); 172 handler.sendRequest(soapContext, session); 173 174 MessageConsumer consumer = session.createConsumer(handler.getDestination()); 175 connection.start(); 176 ObjectMessage message = (ObjectMessage) consumer.receiveNoWait(); 177 message.acknowledge(); 178 session.close(); 179 180 assertEquals(PARTNER_LINK_ID, message.getLongProperty(BpelEndpointHandler.PARTNER_LINK_ID_PROP)); 181 assertEquals("op", message.getStringProperty(BpelEndpointHandler.OPERATION_NAME_PROP)); 182 assertEquals("venus", message.getStringProperty("nameProperty")); 183 assertEquals("30", message.getStringProperty("idProperty")); 184 185 Element messageElement = (Element ) message.getObject(); 186 assertEquals(BpelConstants.NS_VENDOR, messageElement.getNamespaceURI()); 187 assertEquals("msg0", messageElement.getLocalName()); 188 assertNotNull(NodeUtil.getElement(messageElement, "simplePart")); 189 assertNotNull(NodeUtil.getElement(messageElement, "elementPart")); 190 } 191 192 public void testReceiveResponse() throws Exception { 193 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage( 194 null, new ByteArrayInputStream (REQUEST_ENV_TEXT.getBytes())); 195 SOAPMessageContext soapContext = new SOAPMessageContextImpl(soapMessage); 196 197 Connection connection = handler.getConnection(); 198 Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); 199 TemporaryQueue replyTo = session.createTemporaryQueue(); 200 MessageProducer producer = session.createProducer(replyTo); 201 Element messageElement = NodeUtil.parseElement(RESPONSE_ELEM_TEXT); 202 producer.send(session.createObjectMessage((Serializable ) messageElement)); 203 204 handler.receiveResponse(soapContext, session, replyTo); 205 206 session.close(); 207 messageElement = (Element ) soapContext.getProperty(BpelEndpointHandler.MESSAGE_OBJECT_PROP); 208 assertEquals(BpelConstants.NS_EXAMPLES, messageElement.getNamespaceURI()); 209 assertEquals("msg0", messageElement.getLocalName()); 210 assertNotNull(NodeUtil.getElement(messageElement, "intPart")); 211 assertNotNull(NodeUtil.getElement(messageElement, "complexPart")); 212 } 213 } 214 | Popular Tags |