KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > service > def > ReplierTest


1 package org.jbpm.bpel.service.def;
2
3 import java.io.StringReader JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.jms.Destination JavaDoc;
7 import javax.jms.MessageConsumer JavaDoc;
8 import javax.jms.ObjectMessage JavaDoc;
9 import javax.jms.Session JavaDoc;
10 import javax.naming.Context JavaDoc;
11 import javax.naming.InitialContext JavaDoc;
12 import javax.wsdl.Definition;
13 import javax.xml.namespace.QName JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import org.w3c.dom.Element JavaDoc;
18 import org.xml.sax.InputSource JavaDoc;
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 /**
38  * @author Alejandro Guízar
39  * @version $Revision: 1.15 $ $Date: 2005/06/23 02:17:26 $
40  */

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 JavaDoc destination;
49   
50   private Context JavaDoc initialContext;
51   
52   private static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc NS_DEF = "http://jbpm.org/wsdl";
142   
143   protected void setUp() throws Exception JavaDoc {
144     process = new BpelDefinition();
145     // read wsdl
146
Definition def = WsdlUtil.readText(WSDL_TEXT);
147     ImportsDefinition imports = process.getImports();
148     imports.addImport(WsdlUtil.createImport(def));
149     BpelReader.getInstance().registerPropertyAliases(imports);
150     // read bpel
151
BpelReader.getInstance().read(process, new InputSource JavaDoc( new StringReader JavaDoc(BPEL_TEXT)) );
152     // replier
153
Sequence root = (Sequence) process.getRoot();
154     replier = ((Reply) root.getNode("rep")).getReplier();
155     // process instance
156
ProcessInstance pi = process.createProcessInstance();
157     token = pi.getRootToken();
158     // message variable
159
MessageVariableInstance mvi = (MessageVariableInstance) replier.getVariable().getInstance(token);
160     mvi.setMessage(NodeUtil.parseElement(REPLY_TEXT));
161     // correlation set
162
CorrelationSetDefinition csd = replier.getCorrelations().getCorrelation("csId").getSet();
163     CorrelationSetInstance csi = csd.getInstance(token);
164     csi.initialize(mvi);
165     // build messager service
166
initialContext = new InitialContext JavaDoc();
167     BarApplication application = new BarApplication();
168     InputSource JavaDoc input = new InputSource JavaDoc( new StringReader JavaDoc(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     // outstanding request
174
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 JavaDoc {
182     MessagerSession.unsetCurrentSession();
183     messagerService.destroy(initialContext);
184     initialContext.close();
185   }
186
187   public void testReply() throws Exception JavaDoc {
188     // send reply
189
replier.reply(token);
190     MessagerSession.getCurrentSession().commitTransaction();
191     // get response
192
Element JavaDoc messageElement = receiveResponse();
193     // int part
194
Element JavaDoc intPart = DOMUtils.getFirstChildElement(messageElement);
195     assertEquals("intPart", intPart.getLocalName());
196     // element part
197
Element JavaDoc complexPart = DOMUtils.getNextSiblingElement(intPart);
198     assertEquals("complexPart", complexPart.getLocalName());
199     // no more parts
200
assertNull(DOMUtils.getNextSiblingElement(complexPart));
201     // correlation sets
202
Correlations correlations = replier.getCorrelations();
203     // id cset
204
CorrelationSetDefinition set = correlations.getCorrelation("csId").getSet();
205     Map JavaDoc properties = set.getInstance(token).getProperties();
206     assertEquals(1, properties.size());
207     assertEquals("30", properties.get(new QName JavaDoc(NS_DEF, "idProperty")));
208     // name cset
209
set = correlations.getCorrelation("csName").getSet();
210     properties = set.getInstance(token).getProperties();
211     assertEquals(1, properties.size());
212     assertEquals("venus", properties.get(new QName JavaDoc(NS_DEF, "nameProperty")));
213   }
214
215   private Element JavaDoc receiveResponse() throws Exception JavaDoc {
216     // get session
217
Session JavaDoc jmsSession = null;
218     try {
219       jmsSession = messagerService.getConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
220       // create consumer
221
MessageConsumer JavaDoc consumer = jmsSession.createConsumer(destination);
222       // receive message
223
ObjectMessage JavaDoc responseMsg = (ObjectMessage JavaDoc) consumer.receiveNoWait();
224       Element JavaDoc messageElem = (Element JavaDoc) responseMsg.getObject();
225       responseMsg.acknowledge();
226       return messageElem;
227     }
228     finally {
229       jmsSession.close();
230     }
231   }
232 }
233
Popular Tags