1 package org.jbpm.bpel.service.def; 2 3 import java.io.StringReader ; 4 import java.util.Map ; 5 6 import javax.jms.Destination ; 7 import javax.jms.MessageConsumer ; 8 import javax.jms.ObjectMessage ; 9 import javax.jms.Session ; 10 import javax.naming.Context ; 11 import javax.naming.InitialContext ; 12 import javax.wsdl.Definition; 13 import javax.xml.namespace.QName ; 14 15 import junit.framework.TestCase; 16 17 import org.w3c.dom.Element ; 18 import org.xml.sax.InputSource ; 19 20 import com.ibm.wsdl.util.xml.DOMUtils; 21 22 import org.jbpm.graph.exe.ProcessInstance; 23 import org.jbpm.graph.exe.Token; 24 25 import org.jbpm.bpel.bar.BarApplication; 26 import org.jbpm.bpel.data.exe.MessageVariableInstance; 27 import org.jbpm.bpel.def.*; 28 import org.jbpm.bpel.messager.MessagerService; 29 import org.jbpm.bpel.messager.MessagerSession; 30 import org.jbpm.bpel.service.exe.CorrelationSetInstance; 31 import org.jbpm.bpel.service.exe.OutstandingRequest; 32 import org.jbpm.bpel.wsdl.util.WsdlUtil; 33 import org.jbpm.bpel.xml.BpelReader; 34 import org.jbpm.bpel.xml.BarDescriptorReader; 35 import org.jbpm.bpel.xml.util.NodeUtil; 36 37 41 public class ReplierTest extends TestCase { 42 43 private BpelDefinition process; 44 private Replier replier; 45 private Token token; 46 47 private MessagerService messagerService; 48 private Destination destination; 49 50 private Context initialContext; 51 52 private static final String WSDL_TEXT = 53 "<definitions targetNamespace='http://jbpm.org/wsdl'" + 54 " xmlns:tns='http://jbpm.org/wsdl'" + 55 " xmlns:sns='http://jbpm.org/xsd'" + 56 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + 57 " xmlns:bpws='http://schemas.xmlsoap.org/ws/2004/03/business-process/'" + 58 " xmlns:plnk='http://schemas.xmlsoap.org/ws/2004/03/partner-link/'" + 59 " xmlns='http://schemas.xmlsoap.org/wsdl/'>" + 60 " <message name='request'>" + 61 " <part name='simplePart' type='xsd:string'/>" + 62 " <part name='elementPart' element='sns:surpriseElement'/>" + 63 " </message>" + 64 " <message name='response'>" + 65 " <part name='intPart' type='xsd:int'/>" + 66 " <part name='complexPart' type='sns:complexType'/>" + 67 " </message>" + 68 " <portType name='pt'>" + 69 " <operation name='op'>" + 70 " <input message='tns:request'/>" + 71 " <output message='tns:response'/>" + 72 " </operation>" + 73 " </portType>" + 74 " <plnk:partnerLinkType name='plt'>" + 75 " <plnk:role name='r1' portType='tns:pt'/>" + 76 " </plnk:partnerLinkType>" + 77 " <bpws:property name='nameProperty' type='xsd:string'/>" + 78 " <bpws:property name='idProperty' type='xsd:int'/>" + 79 " <bpws:propertyAlias propertyName='tns:nameProperty' messageType='tns:request'>" + 80 " <bpws:query>elementPart/sns:surpriseElement/c/@name</bpws:query>" + 81 " </bpws:propertyAlias>" + 82 " <bpws:propertyAlias propertyName='tns:idProperty' messageType='tns:request'>" + 83 " <bpws:query>elementPart/sns:surpriseElement/e</bpws:query>" + 84 " </bpws:propertyAlias>" + 85 " <bpws:propertyAlias propertyName='tns:nameProperty' messageType='tns:response'>" + 86 " <bpws:query>complexPart/c/@name</bpws:query>" + 87 " </bpws:propertyAlias>" + 88 " <bpws:propertyAlias propertyName='tns:idProperty' messageType='tns:response'>" + 89 " <bpws:query>intPart</bpws:query>" + 90 " </bpws:propertyAlias>" + 91 "</definitions>"; 92 private static final String BPEL_TEXT = 93 "<process xmlns:def='http://jbpm.org/wsdl' xmlns:vendor='http://jbpm.org'" + 94 " xmlns='http://schemas.xmlsoap.org/ws/2004/03/business-process/'>" + 95 " <partnerLinks>" + 96 " <partnerLink name='pl' partnerLinkType='def:plt' myRole='r1'/>" + 97 " </partnerLinks>" + 98 " <variables>" + 99 " <variable name='req' messageType='def:request'/>" + 100 " <variable name='rsp' messageType='def:response'/>" + 101 " </variables>" + 102 " <correlationSets>" + 103 " <correlationSet name='csId' properties='def:idProperty'/>" + 104 " <correlationSet name='csName' properties='def:nameProperty'/>" + 105 " </correlationSets>" + 106 " <sequence>" + 107 " <receive name='rec' partnerLink='pl' operation='op' variable='req'" + 108 " messageExchange='swing' createInstance='yes'>" + 109 " <correlations>" + 110 " <correlation set='csId' initiate='yes'/>" + 111 " </correlations>" + 112 " </receive>" + 113 " <reply name='rep' partnerLink='pl' operation='op' variable='rsp' " + 114 " messageExchange='swing'>" + 115 " <correlations>" + 116 " <correlation set='csId'/>" + 117 " <correlation set='csName' initiate='yes'/>" + 118 " </correlations>" + 119 " </reply>" + 120 " </sequence>" + 121 "</process>"; 122 private static final String BAR_DESCRIPTOR_TEXT = 123 "<bpelApplication xmlns='http://jbpm.org/bpel'>" + 124 " <connectionFactory name='ConnectionFactory'/>" + 125 " <partnerLinks>" + 126 " <partnerLink name='pl' endpointInfoName='testEndpointInfo'>" + 127 " <destination name='queue/testQueue'/>" + 128 " </partnerLink>" + 129 " </partnerLinks>" + 130 "</bpelApplication>"; 131 private static final String REPLY_TEXT = 132 "<vendor:msg0 xmlns:vendor='http://jbpm.org/bpel'>" + 133 "<intPart>30</intPart>" + 134 "<complexPart>" + 135 "<b on=\"true\">true</b>" + 136 "<c name=\"venus\"/>" + 137 "<d amount=\"20\"/>" + 138 "<e>30</e>" + 139 "</complexPart>" + 140 "</vendor:msg0>"; 141 private static final String NS_DEF = "http://jbpm.org/wsdl"; 142 143 protected void setUp() throws Exception { 144 process = new BpelDefinition(); 145 Definition def = WsdlUtil.readText(WSDL_TEXT); 147 ImportsDefinition imports = process.getImports(); 148 imports.addImport(WsdlUtil.createImport(def)); 149 BpelReader.getInstance().registerPropertyAliases(imports); 150 BpelReader.getInstance().read(process, new InputSource ( new StringReader (BPEL_TEXT)) ); 152 Sequence root = (Sequence) process.getRoot(); 154 replier = ((Reply) root.getNode("rep")).getReplier(); 155 ProcessInstance pi = process.createProcessInstance(); 157 token = pi.getRootToken(); 158 MessageVariableInstance mvi = (MessageVariableInstance) replier.getVariable().getInstance(token); 160 mvi.setMessage(NodeUtil.parseElement(REPLY_TEXT)); 161 CorrelationSetDefinition csd = replier.getCorrelations().getCorrelation("csId").getSet(); 163 CorrelationSetInstance csi = csd.getInstance(token); 164 csi.initialize(mvi); 165 initialContext = new InitialContext (); 167 BarApplication application = new BarApplication(); 168 InputSource input = new InputSource ( new StringReader (BAR_DESCRIPTOR_TEXT)); 169 BarDescriptorReader.getInstance().read(application, input ); 170 messagerService = MessagerService.buildMessagerService(application, process, initialContext); 171 messagerService.getConnection().start(); 172 messagerService.openSession().setAsCurrentSession(); 173 PartnerLinkDefinition partnerLink = replier.getPartnerLink(); 175 destination = messagerService.getPartnerLinkInfo(partnerLink).getDestination(); 176 OutstandingRequest request = new OutstandingRequest(destination, null); 177 request.setReceiver(((Receive) root.getNode("rec")).getReceiver()); 178 partnerLink.getInstance(token).registerRequest(request); 179 } 180 181 protected void tearDown() throws Exception { 182 MessagerSession.unsetCurrentSession(); 183 messagerService.destroy(initialContext); 184 initialContext.close(); 185 } 186 187 public void testReply() throws Exception { 188 replier.reply(token); 190 MessagerSession.getCurrentSession().commitTransaction(); 191 Element messageElement = receiveResponse(); 193 Element intPart = DOMUtils.getFirstChildElement(messageElement); 195 assertEquals("intPart", intPart.getLocalName()); 196 Element complexPart = DOMUtils.getNextSiblingElement(intPart); 198 assertEquals("complexPart", complexPart.getLocalName()); 199 assertNull(DOMUtils.getNextSiblingElement(complexPart)); 201 Correlations correlations = replier.getCorrelations(); 203 CorrelationSetDefinition set = correlations.getCorrelation("csId").getSet(); 205 Map properties = set.getInstance(token).getProperties(); 206 assertEquals(1, properties.size()); 207 assertEquals("30", properties.get(new QName (NS_DEF, "idProperty"))); 208 set = correlations.getCorrelation("csName").getSet(); 210 properties = set.getInstance(token).getProperties(); 211 assertEquals(1, properties.size()); 212 assertEquals("venus", properties.get(new QName (NS_DEF, "nameProperty"))); 213 } 214 215 private Element receiveResponse() throws Exception { 216 Session jmsSession = null; 218 try { 219 jmsSession = messagerService.getConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE); 220 MessageConsumer consumer = jmsSession.createConsumer(destination); 222 ObjectMessage responseMsg = (ObjectMessage ) consumer.receiveNoWait(); 224 Element messageElem = (Element ) responseMsg.getObject(); 225 responseMsg.acknowledge(); 226 return messageElem; 227 } 228 finally { 229 jmsSession.close(); 230 } 231 } 232 } 233 | Popular Tags |