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
			String schedulerResponse = RequestUtil.get(workflowSchedulerUrl, params);
192
			debug("WorkflowSchedulerTest.scheduleJobTest - response: " + schedulerResponse);
193
			
194
			XPath xPath=xPathFactory.newXPath();
195
			XPathExpression xPathExpression = xPath.compile("/success");
196
			String successMessage = xPathExpression.evaluate(new InputSource(new StringReader(schedulerResponse)));
197

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
367
}
    (1-1/1)