001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ServiceRegistry.java,v 1.12 2007/03/29 11:45:27 schnelle Exp $
021: *
022: * $Log: ServiceRegistry.java,v $
023: * Revision 1.12 2007/03/29 11:45:27 schnelle
024: * Added tests for an invaid key and an invalid factory.
025: *
026: * Revision 1.11 2007/03/27 21:59:42 mlipp
027: * Fixed lots of checkstyle warnings.
028: *
029: * Revision 1.10 2007/02/14 10:19:37 schnelle
030: * Cleaend imports.
031: *
032: * Revision 1.9 2007/02/02 08:18:29 schnelle
033: * Prepared tests with an Umlaut.
034: *
035: * Revision 1.8 2007/02/01 12:39:13 schnelle
036: * Use of receiver toool to keep the first activity running.
037: *
038: * Revision 1.7 2007/01/31 22:56:25 mlipp
039: * Fixed.
040: *
041: * Revision 1.6 2007/01/31 15:13:20 schnelle
042: * Unified method to create the base url.
043: *
044: * Revision 1.5 2007/01/31 13:39:45 schnelle
045: * Implemented setUp of test cases.
046: *
047: * Revision 1.4 2007/01/31 10:57:36 schnelle
048: * Adapted to change in receiver key.
049: *
050: * Revision 1.3 2007/01/30 21:02:22 mlipp
051: * Fixed resource path.
052: *
053: * Revision 1.2 2007/01/30 11:56:16 drmlipp
054: * Merged Wf-XML branch.
055: *
056: * Revision 1.1.2.5 2006/12/20 14:38:33 schnelle
057: * Implemented Factory GetDefinition.
058: *
059: * Revision 1.1.2.4 2006/12/19 14:44:12 schnelle
060: * Implementation of GetProperties for ServiceRegistry and Factory.
061: *
062: * Revision 1.1.2.3 2006/12/14 08:51:05 schnelle
063: * Implemented CompleteActivity.
064: *
065: * Revision 1.1.2.2 2006/12/12 09:35:19 schnelle
066: * Implemented ChangeState for Instance.
067: *
068: * Revision 1.1.2.1 2006/12/01 15:31:28 schnelle
069: * Separation of test cases dependend on the role.
070: *
071: * Revision 1.4.6.7 2006/12/01 14:18:00 schnelle
072: * Added support for schema type for data in create process
073: *
074: * Revision 1.4.6.6 2006/12/01 12:50:11 schnelle
075: * Basic import of context data for process creation.
076: *
077: * Revision 1.4.6.5 2006/11/30 12:45:20 schnelle
078: * Basic implementation of Factory CreateInstance.
079: *
080: * Revision 1.4.6.4 2006/11/30 10:38:21 schnelle
081: * Implementation of Factory ListInstance.
082: *
083: * Revision 1.4.6.3 2006/11/29 14:13:03 schnelle
084: * Take respect to namespaces of asap requests and responses.
085: *
086: * Revision 1.4.6.2 2006/11/29 11:05:40 schnelle
087: * Full implementation of the request and response headers.
088: *
089: * Revision 1.4.6.1 2006/11/28 15:32:35 schnelle
090: * Proper selection of the response generator.
091: *
092: * Revision 1.4 2006/09/29 12:32:11 drmlipp
093: * Consistently using WfMOpen as projct name now.
094: *
095: * Revision 1.3 2005/01/24 21:11:39 mlipp
096: * Extended.
097: *
098: * Revision 1.2 2005/01/24 20:26:01 mlipp
099: * Reverted saaj back to 1.1 to fit Axis version.
100: *
101: * Revision 1.1 2005/01/23 21:44:23 mlipp
102: * Initial version.
103: *
104: */
105: package wfxml;
106:
107: import java.io.BufferedReader;
108: import java.io.IOException;
109: import java.io.InputStream;
110: import java.io.InputStreamReader;
111: import java.io.StringReader;
112: import java.util.Iterator;
113:
114: import javax.xml.parsers.DocumentBuilderFactory;
115: import javax.xml.parsers.ParserConfigurationException;
116: import javax.xml.soap.Detail;
117: import javax.xml.soap.Name;
118: import javax.xml.soap.SOAPBody;
119: import javax.xml.soap.SOAPBodyElement;
120: import javax.xml.soap.SOAPElement;
121: import javax.xml.soap.SOAPException;
122: import javax.xml.soap.SOAPFault;
123: import javax.xml.soap.SOAPMessage;
124:
125: import junit.framework.Test;
126: import junit.framework.TestCase;
127: import junit.framework.TestSuite;
128:
129: import org.w3c.dom.NodeList;
130: import org.xml.sax.InputSource;
131: import org.xml.sax.SAXException;
132:
133: /**
134: * Test WfXML service registry resource.
135: * @author Dirk Schnelle
136: */
137: public class ServiceRegistry extends TestCase {
138: /** name of the default test process */
139: private static final String TEST_PROCESS_NAME = "wfxmltestprocess";
140: /** Base URL of the service registry */
141: private static final String BASE_URL = Basic
142: .getBaseUrl("ServiceRegistry");
143:
144: /**
145: * Constructor of this TestCase.
146: * @param name name of the test case.
147: */
148: public ServiceRegistry(String name) {
149: super (name);
150: }
151:
152: /**
153: * Creates this test suite.
154: * @return created test suite.
155: */
156: public static Test suite() {
157: TestSuite suite = new TestSuite();
158: suite.addTest(new ServiceRegistry("newDefinition"));
159: suite.addTest(new ServiceRegistry("getProperties"));
160: suite.addTest(new ServiceRegistry("getPropertiesInvalidKey"));
161: suite
162: .addTest(new ServiceRegistry(
163: "getPropertiesInvalidFactory"));
164: suite.addTest(new ServiceRegistry("listProcDefs"));
165: return suite;
166: }
167:
168: /**
169: * Test Service Registry NewDefinition.
170: * @exception Exception
171: * Test failed.
172: */
173: public void newDefinition() throws Exception {
174: String receiver = BASE_URL;
175: String sender = ServiceRegistry.class.getName()
176: + "/newDefinition";
177: String id = "43";
178:
179: SOAPMessage response = importDefinition(sender, id);
180:
181: Basic.checkNoFault(response);
182:
183: assertEquals(sender, Basic.getHeaderValue(response,
184: "ReceiverKey"));
185: assertEquals(receiver, Basic.getHeaderValue(response,
186: "SenderKey"));
187: assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
188:
189: SOAPBodyElement processlist = Basic
190: .getFirstBodyElement(response);
191: Name answerName = processlist.getElementName();
192: assertEquals(Basic.WFXML_NS, answerName.getURI());
193: assertEquals("NewDefinitionRs", answerName.getLocalName());
194: }
195:
196: /**
197: * Imports the test process into WfMOpen.
198: * @param sender originator of the call
199: * @param id custom id
200: * @return response of the service call
201: * @throws SOAPException
202: * Error creating the SOAP message.
203: * @throws SAXException
204: * Error in the XPDL file.
205: * @throws IOException
206: * Error reading the XPDL file.
207: */
208: static SOAPMessage importDefinition(String sender, String id)
209: throws SOAPException, SAXException, IOException {
210: String receiver = BASE_URL;
211:
212: SOAPMessage message = Basic.createMessage();
213:
214: Basic.fillRequestHeadet(message, receiver, sender, id);
215: SOAPBodyElement action = Basic.createActionElement(message,
216: "NewDefinitionRq");
217: SOAPElement language = action.addChildElement(
218: "ProcessLanguage", "wfxml");
219: language.addTextNode("XPDL");
220:
221: javax.xml.parsers.DocumentBuilderFactory factory = DocumentBuilderFactory
222: .newInstance();
223: javax.xml.parsers.DocumentBuilder builder;
224: try {
225: builder = factory.newDocumentBuilder();
226: } catch (ParserConfigurationException e) {
227: throw new SAXException(e.getMessage(), e);
228: }
229:
230: InputStream is = ServiceRegistry.class
231: .getResourceAsStream("/wfxml/wfxmltest.xpdl");
232: assertTrue(is != null);
233: BufferedReader br = new BufferedReader(new InputStreamReader(
234: is, "UTF-8"));
235: StringBuffer sb = new StringBuffer();
236: String st;
237: while ((st = br.readLine()) != null) {
238: sb.append(st + "\n");
239: }
240:
241: org.w3c.dom.Document document = builder.parse(new InputSource(
242: new StringReader(sb.toString())));
243:
244: SOAPElement definition = action.addChildElement("Definition",
245: "wfxml");
246: org.w3c.dom.Document owner = language.getOwnerDocument();
247: org.w3c.dom.Node node = owner.importNode(document
248: .getFirstChild(), true);
249: definition.appendChild(node);
250: message
251: .setProperty(SOAPMessage.CHARACTER_SET_ENCODING,
252: "UTF-8");
253:
254: SOAPMessage response = Basic.call(message);
255: return response;
256: }
257:
258: /**
259: * Retrieves the factory to handle the test process.
260: * @return factory key.
261: * @throws SOAPException
262: * Error in the SOAP message.
263: */
264: static String getTestProcessFactory() throws SOAPException {
265: String receiver = BASE_URL;
266: String sender = ServiceRegistry.class.getName()
267: + "/getTestProcessFactory";
268: String id = "42";
269:
270: SOAPMessage message = Basic.createMessage();
271: Basic.fillRequestHeadet(message, receiver, sender, id);
272: Basic.createActionElement(message, "ListDefinitionsRq");
273:
274: SOAPMessage response = Basic.call(message);
275:
276: Basic.checkNoFault(response);
277:
278: SOAPBodyElement processlist = Basic
279: .getFirstBodyElement(response);
280: processlist.getElementName();
281: for (Iterator processIterator = processlist.getChildElements(); processIterator
282: .hasNext();) {
283:
284: SOAPElement child = (SOAPElement) processIterator.next();
285:
286: String definitionName = Basic.getChildTextContent(child,
287: "Name");
288: if (definitionName.equals(TEST_PROCESS_NAME)) {
289: return Basic
290: .getChildTextContent(child, "DefinitionKey");
291: }
292: }
293:
294: throw new SOAPException("Process '" + TEST_PROCESS_NAME
295: + "' not found");
296: }
297:
298: /**
299: * Test Service Registry GetProperties.
300: * @exception Exception
301: * Test failed.
302: */
303: public void getProperties() throws Exception {
304: String receiver = BASE_URL;
305: String sender = ServiceRegistry.class.getName()
306: + "/getProperties";
307: String id = "44";
308:
309: SOAPMessage message = Basic.createMessage();
310: Basic.fillRequestHeadet(message, receiver, sender, id);
311: Basic.createActionElement(message, "GetPropertiesRq");
312:
313: SOAPMessage response = Basic.call(message);
314:
315: Basic.checkNoFault(response);
316:
317: assertEquals(sender, Basic.getHeaderValue(response,
318: "ReceiverKey"));
319: assertEquals(receiver, Basic.getHeaderValue(response,
320: "SenderKey"));
321: assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
322:
323: SOAPBodyElement processlist = Basic
324: .getFirstBodyElement(response);
325: Name answerName = processlist.getElementName();
326: assertEquals(Basic.WFXML_NS, answerName.getURI());
327: assertEquals("GetPropertiesRs", answerName.getLocalName());
328: }
329:
330: /**
331: * Test Service Registry GetProperties with an invalid receiver key.
332: * @exception Exception
333: * Test failed.
334: */
335: public void getPropertiesInvalidKey() throws Exception {
336: String receiver = Basic.WFXML_SERVLET + "?Resource=";
337: String sender = ServiceRegistry.class.getName()
338: + "/getPropertiesInvalidKey";
339: String id = "45";
340:
341: SOAPMessage message = Basic.createMessage();
342: Basic.fillRequestHeadet(message, receiver, sender, id);
343: Basic.createActionElement(message, "GetPropertiesRq");
344:
345: SOAPMessage response = Basic.call(message);
346:
347: String code = Basic.getAsapErrorCode(response);
348: // We must obtain ASAP_INVALID_KEY.
349: assertEquals("105", code);
350: }
351:
352: /**
353: * Test Service Registry GetProperties with an invalid factory.
354: * @exception Exception
355: * Test failed.
356: */
357: public void getPropertiesInvalidFactory() throws Exception {
358: String receiver = Basic.WFXML_SERVLET + "?Resource=Invalid";
359: String sender = ServiceRegistry.class.getName()
360: + "/getPropertiesInvalidFactory";
361: String id = "46";
362:
363: SOAPMessage message = Basic.createMessage();
364: Basic.fillRequestHeadet(message, receiver, sender, id);
365: Basic.createActionElement(message, "GetPropertiesRq");
366:
367: SOAPMessage response = Basic.call(message);
368:
369: String code = Basic.getAsapErrorCode(response);
370: // We must obtain ASAP_INVALID_FACTORY.
371: assertEquals("502", code);
372: }
373:
374: /**
375: * Test Service Registry ListDefinitions.
376: * @exception Exception
377: * Test failed.
378: */
379: public void listProcDefs() throws Exception {
380: String receiver = BASE_URL;
381: String sender = ServiceRegistry.class.getName()
382: + "/listProcDefs";
383: String id = "47";
384:
385: SOAPMessage message = Basic.createMessage();
386: Basic.fillRequestHeadet(message, receiver, sender, id);
387: Basic.createActionElement(message, "ListDefinitionsRq");
388:
389: SOAPMessage response = Basic.call(message);
390:
391: Basic.checkNoFault(response);
392:
393: assertEquals(sender, Basic.getHeaderValue(response,
394: "ReceiverKey"));
395: assertEquals(receiver, Basic.getHeaderValue(response,
396: "SenderKey"));
397: assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
398:
399: SOAPBodyElement procDefList = Basic
400: .getFirstBodyElement(response);
401: Name answerName = procDefList.getElementName();
402: assertEquals(Basic.WFXML_NS, answerName.getURI());
403: assertEquals("ListDefinitionsRs", answerName.getLocalName());
404: for (Iterator processIterator = procDefList.getChildElements(); processIterator
405: .hasNext();) {
406:
407: SOAPElement child = (SOAPElement) processIterator.next();
408: Name childName = child.getElementName();
409: assertEquals("wfxml", childName.getPrefix());
410: assertEquals("DefinitionInfo", childName.getLocalName());
411:
412: String definitionKey = Basic.getChildTextContent(child,
413: "DefinitionKey");
414: assertFalse(definitionKey.equals(""));
415: }
416: }
417: }
|