Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a metadata catalog as a java Servlet
4
 *  Copyright: 2006 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones, Dan Higgins, Jivka Bojilova, Chad Berkley, Matthew Perry
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-09-24 11:28:29 -0700 (Wed, 24 Sep 2008) $'
10
 * '$Revision: 4384 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
package edu.ucsb.nceas.workflowschedulertest;
27

    
28
import java.io.IOException;
29
import java.io.StringReader;
30
import java.util.Hashtable;
31

    
32
import javax.xml.xpath.XPath;
33
import javax.xml.xpath.XPathExpression;
34
import javax.xml.xpath.XPathFactory;
35
import org.xml.sax.InputSource;
36

    
37
import edu.ucsb.nceas.MCTestCase;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
40
import edu.ucsb.nceas.metacat.properties.PropertyService;
41
import edu.ucsb.nceas.metacat.shared.ServiceException;
42
import edu.ucsb.nceas.metacat.util.RequestUtil;
43
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
44

    
45
import junit.framework.Test;
46
import junit.framework.TestSuite;
47

    
48

    
49
/*
50
 * A junit test class to test public methods in AuthLdap class.
51
 */
52
public class WorkflowSchedulerTest extends MCTestCase {
53
	// Constants
54
	private static String docId = null;
55
	private static String dataDocId = null;
56
	private static String sessionId = null;
57
	private static String workflowJobName;
58
	private static String workflowSchedulerUrl;
59
	private static XPathFactory xPathFactory = null;
60
	
61
	private static final String WORKFLOW_ID_BASE = "urn:lsid:gamma.msi.ucsb.edu/OpenAuth/:";
62
	private static String WORKFLOW_ID = null;
63
	private static final String KAR_ID_BASE = "urn:lsid:gamma.msi.ucsb.edu/OpenAuth/:";
64
	private static String KAR_ID = null;
65
	private static final String WORKFLOW_NAME = "tpc02-water-flow-base";
66
	private static final String START_TIME = "10/10/2008 10:10:10";
67
	private static final String END_TIME = "10/10/2009 10:10:10";
68
	private static final String INTERVAL_VALUE = "100";
69
	private static final String INTERVAL_UNIT = "min";
70

    
71
	static {
72
		try {
73
			PropertyService.getInstance();
74
			workflowSchedulerUrl = PropertyService
75
					.getProperty("test.workflowSchedulerUrl");
76
		} catch (PropertyNotFoundException pnfe) {
77
			System.err.println("Could not get property in static block: "
78
					+ pnfe.getMessage());
79
		} catch (ServiceException se) {
80
			System.err.println("Service problem in static block: " + se.getMessage());
81
		}
82
	}
83

    
84
	/**
85
	 * Constructor to build the test
86
	 * 
87
	 * @param name
88
	 *            the name of the test method
89
	 */
90
	public WorkflowSchedulerTest(String name) {
91
		super(name);
92
		
93
		xPathFactory = XPathFactory.newInstance();
94

    
95
	}
96
	
97
	/**
98
	 * Establish a testing framework by initializing appropriate objects
99
	 */
100
	public void setUp() throws Exception {
101
		metacatConnectionNeeded = true;
102
		
103
		super.setUp();	
104
		//debug("session id: " + sessionId);
105
		//debug("doc id: " + docId);
106
		//debug("data doc id: " + dataDocId);
107
		
108
		try {
109
			if (sessionId == null) {
110
				String loginResult = m.login(username, password);
111
				//debug("WorkflowSchedulerTest.initialize - login result: " + loginResult);
112
				if (loginResult.contains("<sessionId>")) {
113
					int startIndex = loginResult.indexOf("<sessionId>") + 11;
114
					int endIndex = loginResult.indexOf("</sessionId>");
115
					sessionId = loginResult.substring(startIndex, endIndex);
116
					//debug("WorkflowSchedulerTest.initialize - session id: " + sessionId);
117
				} else {
118
					fail("could not get session ID from login results");
119
				}
120
			}
121

    
122
			if (docId == null) {
123
				// insert a document for us to test with
124
				docId = generateDocumentId() + ".1";
125

    
126
				String testDoc = 
127
					getTestDocFromFile(testdatadir + "tpc02-water-flow-base.xml");
128
				// debug("test doc: " + testDoc);
129
				insertDocumentId(docId, testDoc, SUCCESS, false);
130

    
131
				WORKFLOW_ID = WORKFLOW_ID_BASE + docId.replace('.', ':');
132
			}
133
			
134
			if (dataDocId == null) {
135
				// insert a data document (kar file) for us to test with
136
				dataDocId = generateDocumentId() + ".1";
137

    
138
				String filePath = testdatadir + "888.42.1-tpc02-water-flow-base-10.kar";
139
				// debug("test doc: " + testDoc);
140
				uploadDocumentId(dataDocId, filePath, SUCCESS, false);
141

    
142
				KAR_ID = KAR_ID_BASE + dataDocId.replace('.', ':');
143
			}
144
		} catch (IOException ioe) {
145
			fail("WorkflowSchedulerTest.initialize - I/O exception: " + ioe.getMessage());
146
		} catch (MetacatInaccessibleException mie) {
147
			fail("WorkflowSchedulerTest.initialize - Metacat Inaccessible: " + mie.getMessage());
148
		} catch (MetacatAuthException mae) {
149
			fail("WorkflowSchedulerTest.initialize - Metacat auth issue: " + mae.getMessage());
150
		}	
151

    
152
	}
153

    
154
	/**
155
	 * Create a suite of tests to be run together
156
	 */
157
	public static Test suite() {
158
		TestSuite suite = new TestSuite();
159
		suite.addTest(new WorkflowSchedulerTest("initialize"));
160
		suite.addTest(new WorkflowSchedulerTest("scheduleJobTest"));
161
		suite.addTest(new WorkflowSchedulerTest("unscheduleJobTest"));
162
		suite.addTest(new WorkflowSchedulerTest("rescheduleJobTest"));
163
		suite.addTest(new WorkflowSchedulerTest("deleteJobTest"));
164
		return suite;
165
	}
166

    
167
	/**
168
	 * Run an initial test that always passes to check that the test harness is
169
	 * working.
170
	 */
171
	public void initialize() {
172
		assertTrue(1 == 1);
173
	}
174
    
175
    public void scheduleJobTest() {
176
		try {	
177
			Hashtable<String, String[]> params = new Hashtable<String, String[]>();
178

    
179
			params.put("action", new String[] { "scheduleWorkflow" });
180
			params.put("workflowid",
181
					new String[] { WORKFLOW_ID });
182
			params.put("karid",
183
					new String[] { KAR_ID });
184
			params.put("workflowname", new String[] { WORKFLOW_NAME });
185
			params.put("starttime", new String[] { START_TIME });
186
			params.put("endtime", new String[] { END_TIME });
187
			params.put("intervalvalue", new String[] { INTERVAL_VALUE });
188
			params.put("intervalunit", new String[] { INTERVAL_UNIT });	
189
			params.put("sessionid", new String[] { sessionId });
190
			
191
			debug("WorkflowSchedulerTest.scheduleJobTest - sending to url: " + workflowSchedulerUrl);
192
			
193
			String schedulerResponse = RequestUtil.get(workflowSchedulerUrl, params);
194
			debug("WorkflowSchedulerTest.scheduleJobTest - response: " + schedulerResponse);
195
			
196
			XPath xPath=xPathFactory.newXPath();
197
			XPathExpression xPathExpression = xPath.compile("/success");
198
			String successMessage = xPathExpression.evaluate(new InputSource(new StringReader(schedulerResponse)));
199

    
200
			assertTrue("success message did not start with 'Scheduled'", 
201
					successMessage != null && successMessage.startsWith("Scheduled:"));
202
			
203
			String[] successMessageArray = successMessage.split(" ");
204
			
205
			workflowJobName = successMessageArray[1];
206
			debug("WorkflowSchedulerTest.scheduleJobTest - workflow job name: " + workflowJobName);
207
			
208
			Hashtable<String, String[]> getWorkflowParams = new Hashtable<String, String[]>();
209
			getWorkflowParams.put("action", new String[] { "getScheduledWorkflow" });
210
			getWorkflowParams.put("workflowid", new String[] { WORKFLOW_ID });
211
			
212
			String getWorkflowsResponse = RequestUtil.get(workflowSchedulerUrl, getWorkflowParams);
213
			//debug("WorkflowSchedulerTest.scheduleJobTest - get runs for workflow id " + WORKFLOW_ID + " returns: " + getWorkflowsResponse);
214
			
215
			XPath getWorkflowsXPath=xPathFactory.newXPath();
216
			XPathExpression getStatusXPathExpression = getWorkflowsXPath.compile("/scheduledWorkflowResultset/scheduledJob[name=\"" + workflowJobName + "\"]/status");
217
			String status = getStatusXPathExpression.evaluate(new InputSource(new StringReader(getWorkflowsResponse)));
218
			
219
			assertTrue(status != null && status.equals("scheduled"));
220
			
221
		} catch (Exception e) {
222
			fail("WorkflowSchedulerTest.scheduleJobTest - General exception:\n" + e.getMessage());
223
		}
224
	}
225
    
226
    public void unscheduleJobTest() {
227
		try {
228
			String loginResult = m.login(username, password);
229
			String sessionId = null;
230
			if (loginResult.contains("<sessionId>")) {
231
				int startIndex = loginResult.indexOf("<sessionId>") + 11;
232
				int endIndex = loginResult.indexOf("</sessionId>");
233
				sessionId = loginResult.substring(startIndex, endIndex);
234
			} else {
235
				fail("could not get session ID from login results");
236
			}
237
			
238
			Hashtable<String, String[]> params = new Hashtable<String, String[]>();
239

    
240
			params.put("action", new String[] { "unscheduleWorkflow" });
241
			params.put("workflowjobname", new String[] { workflowJobName });
242
			params.put("workflowid", new String[] { WORKFLOW_ID });
243
			params.put("workflowname", new String[] { "tpc02-water-flow-base" });
244
			params.put("sessionid", new String[] { sessionId });
245

    
246
			String schedulerResponse = RequestUtil.get(workflowSchedulerUrl, params);
247
			debug("WorkflowSchedulerTest.unScheduleJobTest - response: " + schedulerResponse);
248
			
249
			XPath xPath=xPathFactory.newXPath();
250
			XPathExpression xPathExpression = xPath.compile("/success");
251
			String successMessage = xPathExpression.evaluate(new InputSource(new StringReader(schedulerResponse)));
252

    
253
			assertTrue("success message did not start with 'Unscheduled'", 
254
					successMessage != null && successMessage.startsWith("Unscheduled:"));
255
			
256
			Hashtable<String, String[]> getWorkflowParams = new Hashtable<String, String[]>();
257
			getWorkflowParams.put("action", new String[] { "getScheduledWorkflow" });
258
			getWorkflowParams.put("workflowid", new String[] { WORKFLOW_ID });
259
			
260
			String getWorkflowsResponse = RequestUtil.get(workflowSchedulerUrl, getWorkflowParams);
261
			//debug("WorkflowSchedulerTest.unScheduleJobTest - get runs for workflow id " + WORKFLOW_ID + " returns: " + getWorkflowsResponse);
262
			
263
			XPath getWorkflowsXPath=xPathFactory.newXPath();
264
			XPathExpression getStatusXPathExpression = getWorkflowsXPath.compile("/scheduledWorkflowResultset/scheduledJob[name=\"" + workflowJobName + "\"]/status");
265
			String status = getStatusXPathExpression.evaluate(new InputSource(new StringReader(getWorkflowsResponse)));
266
			
267
			assertTrue(status != null && status.equals("unscheduled"));
268

    
269
		} catch (Exception e) {
270
			fail("WorkflowSchedulerTest.unScheduleJobTest - General exception:\n" + e.getMessage());
271
		}
272
	}
273
    
274
    public void rescheduleJobTest() {
275
		try {
276
			String loginResult = m.login(username, password);
277
			String sessionId = null;
278
			if (loginResult.contains("<sessionId>")) {
279
				int startIndex = loginResult.indexOf("<sessionId>") + 11;
280
				int endIndex = loginResult.indexOf("</sessionId>");
281
				sessionId = loginResult.substring(startIndex, endIndex);
282
			} else {
283
				fail("could not get session ID from login results");
284
			}
285
			
286
			Hashtable<String, String[]> params = new Hashtable<String, String[]>();
287

    
288
			params.put("action", new String[] { "rescheduleWorkflow" });
289
			params.put("workflowjobname", new String[] { workflowJobName });
290
			params.put("workflowid",
291
					new String[] { WORKFLOW_ID });
292
			params.put("workflowname", new String[] { "tpc02-water-flow-base" });
293
			params.put("sessionid", new String[] { sessionId });
294

    
295
			String schedulerResponse = RequestUtil.get(workflowSchedulerUrl, params);
296
			debug("WorkflowSchedulerTest.reScheduleJobTest - response: " + schedulerResponse);
297

    
298
			XPath xPath=xPathFactory.newXPath();
299
			XPathExpression xPathExpression = xPath.compile("/success");
300
			String successMessage = xPathExpression.evaluate(new InputSource(new StringReader(schedulerResponse)));
301

    
302
			assertTrue("success message did not start with 'Rescheduled'", 
303
					successMessage != null && successMessage.startsWith("Rescheduled:"));
304
			
305
			Hashtable<String, String[]> getWorkflowParams = new Hashtable<String, String[]>();
306
			getWorkflowParams.put("action", new String[] { "getScheduledWorkflow" });
307
			getWorkflowParams.put("workflowid", new String[] { WORKFLOW_ID });
308
			
309
			String getWorkflowsResponse = RequestUtil.get(workflowSchedulerUrl, getWorkflowParams);
310
			//debug("WorkflowSchedulerTest.reScheduleJobTest - get runs for workflow id " + WORKFLOW_ID + " returns: " + getWorkflowsResponse);
311
			
312
			XPath getWorkflowsXPath=xPathFactory.newXPath();
313
			XPathExpression getStatusXPathExpression = getWorkflowsXPath.compile("/scheduledWorkflowResultset/scheduledJob[name=\"" + workflowJobName + "\"]/status");
314
			String status = getStatusXPathExpression.evaluate(new InputSource(new StringReader(getWorkflowsResponse)));
315
			
316
			assertTrue(status != null && status.equals("scheduled"));
317

    
318
		} catch (Exception e) {
319
			fail("WorkflowSchedulerTest.reScheduleJobTest - General exception:\n" + e.getMessage());
320
		}
321
	}
322
    
323
    public void deleteJobTest() {
324
		try {
325
			String loginResult = m.login(username, password);
326
			String sessionId = null;
327
			if (loginResult.contains("<sessionId>")) {
328
				int startIndex = loginResult.indexOf("<sessionId>") + 11;
329
				int endIndex = loginResult.indexOf("</sessionId>");
330
				sessionId = loginResult.substring(startIndex, endIndex);
331
			} else {
332
				fail("could not get session ID from login results");
333
			}
334
			
335
			Hashtable<String, String[]> params = new Hashtable<String, String[]>();
336

    
337
			params.put("action", new String[] { "deleteScheduledWorkflow" });
338
			params.put("workflowjobname", new String[] { workflowJobName });
339
			params.put("sessionid", new String[] { sessionId });
340

    
341
			String schedulerResponse = RequestUtil.get(workflowSchedulerUrl, params);
342
			debug("WorkflowSchedulerTest.deleteJobTest - response: " + schedulerResponse);
343

    
344
			XPath xPath=xPathFactory.newXPath();
345
			XPathExpression xPathExpression = xPath.compile("/success");
346
			String successMessage = xPathExpression.evaluate(new InputSource(new StringReader(schedulerResponse)));
347

    
348
			assertTrue("success message did not start with 'Deleted'", 
349
					successMessage != null && successMessage.startsWith("Deleted:"));
350
			
351
			Hashtable<String, String[]> getWorkflowParams = new Hashtable<String, String[]>();
352
			getWorkflowParams.put("action", new String[] { "getScheduledWorkflow" });
353
			getWorkflowParams.put("workflowid", new String[] { WORKFLOW_ID });
354
			
355
			String getWorkflowsResponse = RequestUtil.get(workflowSchedulerUrl, getWorkflowParams);
356
			//debug("WorkflowSchedulerTest.deleteJobTest - get runs for workflow id " + WORKFLOW_ID + " returns: " + getWorkflowsResponse);
357
	
358
			XPath getWorkflowsXPath=xPathFactory.newXPath();
359
			XPathExpression getStatusXPathExpression = getWorkflowsXPath.compile("/scheduledWorkflowResultset/scheduledJob[name=\"" + workflowJobName + "\"]/status");
360
			String status = getStatusXPathExpression.evaluate(new InputSource(new StringReader(getWorkflowsResponse)));
361
			
362
			assertTrue(status == null || status.equals(""));
363

    
364
		} catch (Exception e) {
365
			fail("WorkflowSchedulerTest.deleteJobTest - General exception:\n" + e.getMessage());
366
		}
367
	}
368

    
369
}
    (1-1/1)