1   /*
2    * $Id: ProcessFactoryTest.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 javax.xml.namespace.QName;
12  
13  import junit.framework.TestCase;
14  
15  import org.apache.commons.digester.Digester;
16  import org.xml.sax.Attributes;
17  import org.xml.sax.helpers.AttributesImpl;
18  
19  import bexee.model.activity.*;
20  import bexee.model.activity.impl.SequenceImpl;
21  import bexee.model.elements.*;
22  import bexee.model.process.*;
23  import bexee.model.process.Process;
24  import bexee.model.process.impl.BPELProcessImpl;
25  import bexee.model.xmltobpel.*;
26  
27  /***
28   * @author Pawel Kowalski
29   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:17 $
30   */
31  
32  public class ProcessFactoryTest extends TestCase implements AttributeNames {
33  
34      private Digester digester;
35  
36      private BPELProcess bpelProcess;
37  
38      private String processName = "LoanFlow";
39  
40      private String targetNamespace = "http://samples.otn.com";
41  
42      private String suppressJoinFailure = "yes";
43  
44      private String[] xmlns_tns = { "xmls:tns", "http://samples.otn.com" };
45  
46      private String[] xmlns_services = { "xmls:services",
47              "http://services.otn.com" };
48  
49      private String xmlns = "http://schemas.xmlsoap.org/ws/2003/03/business-process/";
50  
51      private String firstReceiveName = "receiveInput";
52  
53      private String firstReceivePLinkName = "client";
54  
55      private String firstReceivePortType = "tns:FlowSample";
56  
57      private String firstReceiveOperation = "initiate";
58  
59      private String firstReceiveVariable = "input";
60  
61      private String firstReceiveCreateInstance = "YES";
62  
63      private String defaultQueryExpressionLanguage = "http://www.w3.org/TR/1999/REC-xpath-19991116";
64  
65      private String flowSample = "FlowSample";
66  
67      private String loanService = "LoanService";
68  
69      private String invokeUnitedLoan = "invokeUnitedLoan";
70  
71      private String unitedLoanService = "UnitedLoanService";
72  
73      private String services_LoanService = "services:LoanService";
74  
75      private String initiate = "initiate";
76  
77      private String loanApplicationVarName = "loanApplication";
78  
79      protected void setUp() throws Exception {
80          super.setUp();
81  
82          digester = new Digester();
83          bpelProcess = new BPELProcessImpl();
84  
85          digester.push(bpelProcess);
86      }
87  
88      public void testPartnerLinks() {
89          try {
90  
91              ProcessImplFactory processFactory = new ProcessImplFactory();
92              processFactory.setDigester(digester);
93  
94              Process process = (Process) processFactory
95                      .createObject(getProcessAtts());
96  
97              assertEquals(processName, process.getName());
98              assertEquals(targetNamespace, process.getTargetNamespace());
99              assertEquals(true, process.isSuppressJoinFailure());
100             assertEquals(defaultQueryExpressionLanguage, process
101                     .getExpressionLanguage());
102             assertEquals(defaultQueryExpressionLanguage, process
103                     .getQueryLanguage());
104 
105             PartnerLinkImplFactory pLinkFactory = new PartnerLinkImplFactory();
106             pLinkFactory.setDigester(digester);
107 
108             PartnerLink clientPLink = (PartnerLink) pLinkFactory
109                     .createObject(getClientPLinkAtts());
110             PartnerLink unitedPLink = (PartnerLink) pLinkFactory
111                     .createObject(getUnitedLoanPLinkAtts());
112             PartnerLink americanPLink = (PartnerLink) pLinkFactory
113                     .createObject(getAmericanLoanPLinkAtts());
114 
115             assertEquals(new QName(xmlns_tns[1], flowSample), clientPLink
116                     .getPartnerLinkType());
117             assertEquals(new QName(xmlns_services[1], loanService), unitedPLink
118                     .getPartnerLinkType());
119             assertEquals(new QName(xmlns_services[1], loanService),
120                     americanPLink.getPartnerLinkType());
121 
122             VariableImplFactory varFactory = new VariableImplFactory();
123             varFactory.setDigester(digester);
124 
125             Variable inVar = (Variable) varFactory
126                     .createObject(getInputVarAtts());
127             Variable outVar = (Variable) varFactory
128                     .createObject(getOutputVarAtts());
129             Variable loanAppVar = (Variable) varFactory
130                     .createObject(getLoanAppVarAtts());
131             Variable loan1Var = (Variable) varFactory
132                     .createObject(getLoanOffer1VarAtts());
133             Variable loan2Var = (Variable) varFactory
134                     .createObject(getLoanOffer2VarAtts());
135 
136             Sequence sequence = new SequenceImpl();
137             process.setActivity(sequence);
138 
139             ReceiveImplFactory receiveFactory = new ReceiveImplFactory();
140             receiveFactory.setDigester(digester);
141 
142             Receive receive = (Receive) receiveFactory
143                     .createObject(getFirstReceiveAtts());
144             assertEquals(inVar, receive.getVariable());
145             assertEquals(clientPLink, receive.getPartnerLink());
146             assertEquals(new QName(xmlns_tns[1], flowSample), receive
147                     .getPortType());
148 
149             InvokeImplFactory invokeFactory = new InvokeImplFactory();
150             invokeFactory.setDigester(digester);
151 
152             Invoke invoke = (Invoke) invokeFactory
153                     .createObject(getInvokeAtts());
154             assertEquals(loanAppVar, invoke.getInputVariable());
155             assertEquals(unitedPLink, invoke.getPartnerLink());
156             assertEquals(new QName(xmlns_services[1], loanService), invoke
157                     .getPortType());
158 
159         } catch (Exception e) {
160             e.printStackTrace();
161             fail("Should not throw exception");
162         }
163     }
164 
165     private Attributes getProcessAtts() {
166         AttributesImpl a = new AttributesImpl();
167         a.addAttribute("", "", NAME, "", processName);
168         a.addAttribute("", "", TARGET_NAMESPACE, "", targetNamespace);
169         a.addAttribute("", "", SUPPRESS_JOIN_FAILURE, "", suppressJoinFailure);
170         a.addAttribute("", "", xmlns_tns[0], "", xmlns_tns[1]);
171         a.addAttribute("", "", xmlns_services[0], "", xmlns_services[1]);
172         return a;
173     }
174 
175     private Attributes getClientPLinkAtts() {
176         AttributesImpl a = new AttributesImpl();
177         a.addAttribute("", "", NAME, "", "client");
178         a.addAttribute("", "", PARTNER_LINK_TYPE, "", "tns:" + flowSample);
179         a.addAttribute("", "", PARTNER_ROLE, "", "FlowSampleRequester");
180         a.addAttribute("", "", MY_ROLE, "", "FlowSampleProvider");
181         return a;
182     }
183 
184     private Attributes getUnitedLoanPLinkAtts() {
185         AttributesImpl a = new AttributesImpl();
186         a.addAttribute("", "", NAME, "", "UnitedLoanService");
187         a
188                 .addAttribute("", "", PARTNER_LINK_TYPE, "", "services:"
189                         + loanService);
190         a.addAttribute("", "", PARTNER_ROLE, "", "LoanServiceProvider");
191         a.addAttribute("", "", MY_ROLE, "", "LoanServiceRequester");
192         return a;
193     }
194 
195     private Attributes getAmericanLoanPLinkAtts() {
196         AttributesImpl a = new AttributesImpl();
197         a.addAttribute("", "", NAME, "", "AmericanLoanService");
198         a
199                 .addAttribute("", "", PARTNER_LINK_TYPE, "", "services:"
200                         + loanService);
201         a.addAttribute("", "", PARTNER_ROLE, "", "LoanServiceProvider");
202         a.addAttribute("", "", MY_ROLE, "", "LoanServiceRequester");
203         return a;
204     }
205 
206     private Attributes getInputVarAtts() {
207         AttributesImpl a = new AttributesImpl();
208         a.addAttribute("", "", NAME, "", "input");
209         a
210                 .addAttribute("", "", MESSAGE_TYPE, "",
211                         "tns:FlowSampleRequestMessage");
212         return a;
213     }
214 
215     private Attributes getOutputVarAtts() {
216         AttributesImpl a = new AttributesImpl();
217         a.addAttribute("", "", NAME, "", "output");
218         a.addAttribute("", "", MESSAGE_TYPE, "", "tns:FlowSampleResultMessage");
219         return a;
220     }
221 
222     private Attributes getLoanAppVarAtts() {
223         AttributesImpl a = new AttributesImpl();
224         a.addAttribute("", "", NAME, "", "loanApplication");
225         a.addAttribute("", "", MESSAGE_TYPE, "",
226                 "services:LoanServiceRequestMessage");
227         return a;
228     }
229 
230     private Attributes getLoanOffer1VarAtts() {
231         AttributesImpl a = new AttributesImpl();
232         a.addAttribute("", "", NAME, "", "loanOffer1");
233         a.addAttribute("", "", MESSAGE_TYPE, "",
234                 "services:LoanServiceResultMessage");
235         return a;
236     }
237 
238     private Attributes getLoanOffer2VarAtts() {
239         AttributesImpl a = new AttributesImpl();
240         a.addAttribute("", "", NAME, "", "loanOffer2");
241         a.addAttribute("", "", MESSAGE_TYPE, "",
242                 "services:LoanServiceResultMessage");
243         return a;
244     }
245 
246     private Attributes getFirstReceiveAtts() {
247         AttributesImpl a = new AttributesImpl();
248         a.addAttribute("", "", NAME, "", firstReceiveName);
249         a.addAttribute("", "", PARTNER_LINK, "", firstReceivePLinkName);
250         a.addAttribute("", "", PORT_TYPE, "", firstReceivePortType);
251         a.addAttribute("", "", OPERATION, "", firstReceiveOperation);
252         a.addAttribute("", "", VARIABLE, "", firstReceiveVariable);
253         a.addAttribute("", "", CREATE_INSTANCE, "", firstReceiveCreateInstance);
254         return a;
255     }
256 
257     private Attributes getInvokeAtts() {
258         AttributesImpl a = new AttributesImpl();
259         a.addAttribute("", "", NAME, "", invokeUnitedLoan);
260         a.addAttribute("", "", PARTNER_LINK, "", unitedLoanService);
261         a.addAttribute("", "", PORT_TYPE, "", services_LoanService);
262         a.addAttribute("", "", OPERATION, "", initiate);
263         a.addAttribute("", "", INPUT_VARIABLE, "", loanApplicationVarName);
264         return a;
265     }
266 
267 }