1   /*
2    * $Id: BPELProcessFactoryImplTest.java,v 1.1 2004/12/15 14:18:17 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.model;
10  
11  import java.io.*;
12  import java.net.URL;
13  import java.util.*;
14  
15  import javax.xml.namespace.QName;
16  
17  import bexee.BexeeTestCase;
18  import bexee.model.activity.*;
19  import bexee.model.activity.impl.*;
20  import bexee.model.elements.*;
21  import bexee.model.process.*;
22  import bexee.model.process.Process;
23  
24  /***
25   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:17 $
26   * @author Pawel Kowalski
27   */
28  public class BPELProcessFactoryImplTest extends BexeeTestCase {
29  
30      private BPELProcessFactory factory;
31  
32      protected void setUp() throws Exception {
33          super.setUp();
34          factory = BPELProcessFactory.getInstance();
35      }
36  
37      protected void tearDown() throws Exception {
38          super.tearDown();
39      }
40  
41      public void testCreateBPELProcessFactory() {
42          BPELProcessFactory factory = BPELProcessFactory.getInstance();
43          assertNotNull(factory);
44      }
45  
46      public final void testGetRulesURL() {
47  
48          BPELProcessFactoryImpl factoryImpl = (BPELProcessFactoryImpl) BPELProcessFactory
49                  .getInstance();
50  
51          URL rulesURL = factoryImpl.getRulesURL();
52          
53          assertNotNull(rulesURL);
54      }
55  
56      public void testCreateBaseBPELProcess() {
57          try {
58  
59              InputStream inputStream = getLoader().getResourceAsStream(
60                      "baseBPEL.xml");
61              BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
62  
63              Process process = bpelProcess.getProcess();
64  
65              assertNotNull(process);
66              assertEquals("BaseBPEL", process.getName());
67              assertTrue(process.isSuppressJoinFailure());
68              assertFalse(process.isAbstractProcess());
69  
70          } catch (BPELDocumentException e) {
71              e.printStackTrace();
72              fail("must create a BPELProcess without throwing an Exception");
73          }
74      }
75      
76      public void testCreateTravelBpel() {
77          try {
78  
79              InputStream inputStream = getLoader().getResourceAsStream(
80                      "BookTravel.bpel");
81              BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
82  
83              Process process = bpelProcess.getProcess();
84  
85              assertNotNull(process);
86              assertEquals("TravelProcess", process.getName());
87              assertTrue(process.isSuppressJoinFailure());
88              assertFalse(process.isAbstractProcess());
89  
90          } catch (BPELDocumentException e) {
91              e.printStackTrace();
92              fail("must create a BPELProcess without throwing an Exception");
93          }
94      }
95  
96      public void testPartnerLinksProcess() {
97          try {
98  
99              InputStream inputStream = getLoader().getResourceAsStream(
100                     "partnerLinksBPEL.xml");
101             BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
102 
103             Process process = bpelProcess.getProcess();
104 
105             assertNotNull(process);
106             assertEquals("PartnerLinksBPEL", process.getName());
107             assertTrue(process.isSuppressJoinFailure());
108             assertFalse(process.isAbstractProcess());
109 
110             PartnerLinks partnerLinks = process.getPartnerLinks();
111 
112             assertNotNull(partnerLinks);
113             List listOfpartnerLinks = partnerLinks.getPartnerLinks();
114             Iterator iter = listOfpartnerLinks.iterator();
115 
116             PartnerLink partnerLink = (PartnerLink) iter.next();
117             assertNotNull(partnerLink);
118             assertEquals("client", partnerLink.getName());
119 
120             assertTrue(iter.hasNext());
121 
122             partnerLink = (PartnerLink) iter.next();
123             assertNotNull(partnerLink);
124             assertEquals("service", partnerLink.getName());
125 
126             assertFalse(iter.hasNext());
127 
128         } catch (BPELDocumentException e) {
129             e.printStackTrace();
130             fail("must create a BPELProcess without throwing an Exception");
131         }
132     }
133 
134     public void testFunctionDetailsExample() {
135         try {
136             InputStream inputStream = getLoader().getResourceAsStream(
137                     "functionDetailsExample.xml");
138             BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
139 
140             Process process = bpelProcess.getProcess();
141 
142             // test the process
143             //
144             assertNotNull(process);
145             assertEquals("bexeeExample", process.getName());
146             assertFalse(process.isSuppressJoinFailure());
147             assertFalse(process.isAbstractProcess());
148 
149             // test the partner links
150             //
151             PartnerLinks partnerLinks = process.getPartnerLinks();
152             assertNotNull(partnerLinks);
153 
154             // test the particular partner links
155             //
156             List listOfpartnerLinks = partnerLinks.getPartnerLinks();
157             Iterator iter = listOfpartnerLinks.iterator();
158             PartnerLink partnerLink = (PartnerLink) iter.next();
159             assertNotNull(partnerLink);
160             assertEquals("client", partnerLink.getName());
161             assertTrue(iter.hasNext());
162             partnerLink = (PartnerLink) iter.next();
163             assertNotNull(partnerLink);
164             assertEquals("service", partnerLink.getName());
165             assertFalse(iter.hasNext());
166 
167             // test the variables
168             //
169             Variables variables = process.getVariables();
170             assertNotNull(variables);
171 
172             // test the particular variables
173             //
174             List listOfVariables = variables.getVariables();
175             Iterator varIter = listOfVariables.iterator();
176             Variable variable = (Variable) varIter.next();
177             assertNotNull(variable);
178             assertEquals("input", variable.getName());
179             assertEquals(new QName("http://bexee.sourceforge.net",
180                     "ReceiveRequestMessage"), variable.getMessageType());
181             assertTrue(varIter.hasNext());
182             variable = (Variable) varIter.next();
183             assertNotNull(variable);
184             assertEquals("response", variable.getName());
185             assertEquals(new QName("http://bexee.sourceforge.net",
186                     "ReceiveResultMessage"), variable.getMessageType());
187             assertFalse(iter.hasNext());
188 
189             // test the root activity
190             //
191             Activity rootActivity = process.getActivity();
192             assertEquals(SequenceImpl.class, rootActivity.getClass());
193 
194             Sequence mainSequence = (Sequence) rootActivity;
195             List activitiesList = mainSequence.getActivities();
196             Iterator iterator = activitiesList.iterator();
197             assertTrue(iterator.hasNext());
198             Activity activity = (Activity) iterator.next();
199 
200             assertEquals(ReceiveImpl.class, activity.getClass());
201             assertTrue(iterator.hasNext());
202             activity = (Activity) iterator.next();
203 
204             assertEquals(InvokeImpl.class, activity.getClass());
205             assertTrue(iterator.hasNext());
206             activity = (Activity) iterator.next();
207 
208             assertEquals(ReceiveImpl.class, activity.getClass());
209             assertFalse(iterator.hasNext());
210 
211         } catch (BPELDocumentException e) {
212             e.printStackTrace();
213             fail("must create a BPELProcess without throwing an Exception");
214         }
215     }
216 
217     public void testFunctionDetailsExampleTwo() {
218         try {
219             InputStream inputStream = getLoader().getResourceAsStream(
220                     "functionDetailsExampleTwo.xml");
221             BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
222 
223             Process process = bpelProcess.getProcess();
224 
225             // test the process
226             //
227             assertNotNull(process);
228             assertEquals("bexeeExample", process.getName());
229             assertFalse(process.isSuppressJoinFailure());
230             assertFalse(process.isAbstractProcess());
231 
232             // test the partner links
233             //
234             PartnerLinks partnerLinks = process.getPartnerLinks();
235             assertNotNull(partnerLinks);
236 
237             // test the particular partner links
238             //
239             List listOfpartnerLinks = partnerLinks.getPartnerLinks();
240             Iterator iter = listOfpartnerLinks.iterator();
241             PartnerLink partnerLink = (PartnerLink) iter.next();
242             assertNotNull(partnerLink);
243             assertEquals("client", partnerLink.getName());
244             assertTrue(iter.hasNext());
245             partnerLink = (PartnerLink) iter.next();
246             assertNotNull(partnerLink);
247             assertEquals("service", partnerLink.getName());
248             assertFalse(iter.hasNext());
249 
250             // test the variables
251             //
252             Variables variables = process.getVariables();
253             assertNotNull(variables);
254 
255             // test the particular variables
256             //
257             List listOfVariables = variables.getVariables();
258             Iterator varIter = listOfVariables.iterator();
259             Variable variable = (Variable) varIter.next();
260             assertNotNull(variable);
261             assertEquals("input", variable.getName());
262             assertEquals(new QName("http://bexee.sourceforge.net",
263                     "ReceiveRequestMessage"), variable.getMessageType());
264             assertTrue(varIter.hasNext());
265             variable = (Variable) varIter.next();
266             assertNotNull(variable);
267             assertEquals("response", variable.getName());
268             assertEquals(new QName("http://bexee.sourceforge.net",
269                     "ReceiveResultMessage"), variable.getMessageType());
270             assertFalse(iter.hasNext());
271 
272             // test the root activity
273             //
274             Activity rootActivity = process.getActivity();
275             assertEquals(FlowImpl.class, rootActivity.getClass());
276 
277         } catch (BPELDocumentException e) {
278             e.printStackTrace();
279             fail("must create a BPELProcess without throwing an Exception");
280         }
281     }
282 
283     public void testComplexBPEL() {
284         try {
285             // process values
286             //
287             String processName = "LoanFlow";
288             String targetNamespace = "http://samples.otn.com";
289             String suppressJoinFailure = "yes";
290             String[] xmlns_tns = { "xmls:tns", "http://samples.otn.com" };
291             String[] xmlns_services = { "xmls:services",
292                     "http://services.otn.com" };
293             String xmlns = "http://schemas.xmlsoap.org/ws/2003/03/business-process/";
294 
295             // partnerlinks
296             //
297             PartnerLink clientPLink = null;
298             String pLinkNameClient = "client";
299             PartnerLink servicePLinkOne = null;
300             String pLinkNameServiceOne = "UnitedLoanService";
301             PartnerLink servicePLinkTwo = null;
302             String pLinkNameServiceTwo = "AmericanLoanService";
303 
304             // variables
305             //
306             Variable inputVariable = null;
307             String inputVarName = "input";
308             Variable outputVariable = null;
309             String outputVarName = "output";
310             Variable loanAppVariable = null;
311             String loanAppVarName = "loanApplication";
312             Variable loanOneVariable = null;
313             String loanOneVarName = "loanOffer1";
314             Variable loanTwoVariable = null;
315             String loanTwoVarName = "loanOffer2";
316 
317             // create the bpelProcess from a file
318             //
319             InputStream inputStream = getLoader().getResourceAsStream(
320                     "complexBPEL.xml");
321             BPELProcess bpelProcess = factory.createBPELProcess(inputStream);
322             Process process = bpelProcess.getProcess();
323 
324             // do the tests
325             //
326             assertEquals(processName, process.getName());
327             assertEquals(targetNamespace, process.getTargetNamespace());
328             assertEquals(true, process.isSuppressJoinFailure());
329             assertEquals(false, process.isAbstractProcess());
330             assertEquals(false, process.isInstanceCompensable());
331 
332             List partnerLinks = process.getPartnerLinks().getPartnerLinks();
333             for (Iterator iter = partnerLinks.iterator(); iter.hasNext();) {
334                 PartnerLink pLink = (PartnerLink) iter.next();
335                 if (pLink.getName().equals(pLinkNameClient)) {
336                     clientPLink = pLink;
337                     assertEquals(new QName(xmlns_tns[1], "FlowSample"), pLink
338                             .getPartnerLinkType());
339                     assertEquals("FlowSampleRequester", pLink.getPartnerRole());
340                     assertEquals("FlowSampleProvider", pLink.getMyRole());
341                 } else if (pLink.getName().equals(pLinkNameServiceOne)) {
342                     servicePLinkOne = pLink;
343                     assertEquals(new QName(xmlns_services[1], "LoanService"),
344                             pLink.getPartnerLinkType());
345                     assertEquals("LoanServiceProvider", pLink.getPartnerRole());
346                     assertEquals("LoanServiceRequester", pLink.getMyRole());
347                 } else if (pLink.getName().equals(pLinkNameServiceTwo)) {
348                     servicePLinkTwo = pLink;
349                     assertEquals(new QName(xmlns_services[1], "LoanService"),
350                             pLink.getPartnerLinkType());
351                     assertEquals("LoanServiceProvider", pLink.getPartnerRole());
352                     assertEquals("LoanServiceRequester", pLink.getMyRole());
353                 }
354             }
355 
356             List variables = process.getVariables().getVariables();
357             for (Iterator iter = variables.iterator(); iter.hasNext();) {
358                 Variable var = (Variable) iter.next();
359                 if (var.getName().equals(inputVarName)) {
360                     inputVariable = var;
361                 } else if (var.getName().equals(outputVarName)) {
362                     outputVariable = var;
363                 } else if (var.getName().equals(loanAppVarName)) {
364                     loanAppVariable = var;
365                 } else if (var.getName().equals(loanOneVarName)) {
366                     loanOneVariable = var;
367                 } else if (var.getName().equals(loanTwoVarName)) {
368                     loanTwoVariable = var;
369                 }
370             }
371 
372             Sequence rootActivity = (Sequence) process.getActivity();
373             List activities = rootActivity.getActivities();
374             Iterator iter = activities.iterator();
375 
376             Receive receiveOne = (Receive) iter.next();
377             assertEquals("receiveInput", receiveOne.getName());
378             assertEquals(clientPLink, receiveOne.getPartnerLink());
379             assertEquals("initiate", receiveOne.getOperation());
380             assertEquals(inputVariable, receiveOne.getVariable());
381             assertEquals(true, receiveOne.isCreateInstance());
382 
383         } catch (BPELDocumentException e) {
384             e.printStackTrace();
385             fail("must create a BPELProcess without throwing an Exception");
386         }
387     }
388 
389 }