Project

General

Profile

1 1114 tao
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5 3080 jones
 *    Purpose: To test the MetaCatURL class by JUnit
6
 *    Authors: Jing Tao
7 1114 tao
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
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
27 1117 tao
package edu.ucsb.nceas.metacatnettest;
28 1114 tao
29 4145 daigle
import edu.ucsb.nceas.MCTestCase;
30 1114 tao
import edu.ucsb.nceas.metacat.*;
31 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
32 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
33 4145 daigle
34 1114 tao
import junit.framework.Test;
35
import junit.framework.TestSuite;
36
37
import java.io.*;
38
import java.net.*;
39
import java.util.*;
40
41 3169 tao
import org.apache.log4j.Logger;
42 1114 tao
43
/**
44
 * A JUnit test for testing Step class processing
45
 */
46 4145 daigle
public class MetaCatServletNetTest extends MCTestCase {
47 4080 daigle
	private static String metacatURL;
48
	static {
49 4125 daigle
		try {
50 4231 daigle
			metacatURL = PropertyService.getProperty("test.metacatUrl");
51 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
52
			System.err.println("could not find metacat URL in MetacatServletNetTest: "
53
					+ pnfe.getMessage());
54
		} catch (Exception e) {
55
			System.err.println("Exception in initialize option in MetacatServletNetTest: "
56
					+ e.getMessage());
57
		}
58
	}
59 1114 tao
60 4080 daigle
	private String serialNumber;
61 1114 tao
62 4080 daigle
	/**
63
	 * Constructor to build the test
64
	 *
65
	 * @param name the name of the test method
66
	 */
67
	public MetaCatServletNetTest(String name) {
68
		super(name);
69
	}
70 1114 tao
71 4080 daigle
	/**
72
	 * Constructor to build the test
73
	 *
74
	 * @param name the name of the test method
75
	 */
76
	public MetaCatServletNetTest(String name, String serial) {
77
		super(name);
78
		serialNumber = serial;
79
	}
80 1114 tao
81 4080 daigle
	/**
82
	 * Establish a testing framework by initializing appropriate objects
83
	 */
84
	public void setUp() {
85 1114 tao
86 4080 daigle
	}
87 1114 tao
88 4080 daigle
	/**
89
	 * Release any objects after tests are complete
90
	 */
91
	public void tearDown() {
92
	}
93 3562 tao
94 4080 daigle
	/**
95
	 * Create a suite of tests to be run together
96
	 */
97
	public static Test suite() {
98
		double number = 0;
99
		String serial = null;
100 1114 tao
101 4080 daigle
		TestSuite suite = new TestSuite();
102
		suite.addTest(new MetaCatServletNetTest("initialize"));
103
		suite.addTest(new MetaCatServletNetTest("testNCEASLoginFail"));
104
		//Should put a login successfully at the end of login test
105
		//So insert or update can have cookie.
106
		suite.addTest(new MetaCatServletNetTest("testNCEASLogin"));
107 1114 tao
108 4080 daigle
		//create random number for docid, so it can void repeat
109
		number = Math.random() * 100000;
110
		serial = Integer.toString(((new Double(number)).intValue()));
111 5311 daigle
		debug("serial: " + serial);
112 4080 daigle
		suite.addTest(new MetaCatServletNetTest("testInsertXMLDocument", serial));
113
		suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentXMLFormat", serial));
114
		suite.addTest(new MetaCatServletNetTest("testUpdateXMLDocument", serial));
115
		suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentHTMLFormat", serial));
116
		suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentZipFormat", serial));
117 1114 tao
118 4080 daigle
		suite.addTest(new MetaCatServletNetTest("testDeleteXMLDocument", serial));
119 1114 tao
120 4080 daigle
		//insert invalid xml document
121
		number = Math.random() * 100000;
122
		serial = Integer.toString(((new Double(number)).intValue()));
123 4125 daigle
		suite.addTest(new MetaCatServletNetTest("testInsertInvalidateXMLDocument",
124 4080 daigle
						serial));
125
		//insert non well formed document
126
		number = Math.random() * 100000;
127
		serial = Integer.toString(((new Double(number)).intValue()));
128
		suite.addTest(new MetaCatServletNetTest("testInsertNonWellFormedXMLDocument",
129
				serial));
130
		//insert data file
131
		number = Math.random() * 100000;
132
		serial = Integer.toString(((new Double(number)).intValue()));
133
		suite.addTest(new MetaCatServletNetTest("testLogOut"));
134 1114 tao
135 4080 daigle
		return suite;
136
	}
137 1114 tao
138 4080 daigle
	/**
139
	 * Run an initial test that always passes to check that the test
140
	 * harness is working.
141
	 */
142
	public void initialize() {
143
		assertTrue(1 == 1);
144
	}
145 1114 tao
146 4080 daigle
	/**
147 5311 daigle
	 * Test the login to nceas succesfully
148 4080 daigle
	 */
149
	public void testNCEASLogin() {
150 4417 daigle
		debug("\nRunning: testNCEASLogin test");
151 4080 daigle
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
152
		String passwd = "123456";
153
		assertTrue(logIn(user, passwd));
154
		//assertTrue( withProtocol.getProtocol().equals("http"));
155
	}
156 1114 tao
157 4080 daigle
	/**
158 5311 daigle
	 * Test the login to nceas failed
159 4080 daigle
	 */
160
	public void testNCEASLoginFail() {
161 4417 daigle
		debug("\nRunning: testNCEASLoginFail test");
162 4080 daigle
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
163
		String passwd = "12345678";
164
		assertTrue(!logIn(user, passwd));
165 1114 tao
166 4080 daigle
	}
167 1114 tao
168 4080 daigle
	/**
169
	 * Test the login to lter failed
170
	 */
171
	public void testLterReferralLoginFail() {
172 4417 daigle
		debug("\nRunning: testLterReferralLoginFail test");
173 4080 daigle
		String user = "uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
174
		String passwd = "qVyGpveb";
175
		assertTrue(!logIn(user, passwd));
176
		//assertTrue( withProtocol.getProtocol().equals("http"));
177
	}
178 1114 tao
179 4080 daigle
	/**
180
	 * Test insert a xml document successfully
181
	 */
182
	public void testInsertXMLDocument() throws PropertyNotFoundException {
183 4417 daigle
		debug("\nRunning: testInsertXMLDocument test");
184 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
185
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
186 5311 daigle
		debug("insert docid: " + name);
187 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
188
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
189
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
190
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
191
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
192
				+ "</identifier>" + "<allow>"
193
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
194
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
195
				+ "<principal>public</principal>" + "<permission>read</permission>"
196
				+ "</allow>" + "</acl>";
197 5311 daigle
		debug("xml document: " + content);
198 4080 daigle
		assertTrue(handleXMLDocument(content, name, "insert"));
199
200
	}
201
202
	/**
203
	 * Test insert a invalidate xml document successfully
204
	 * In the String, there is no <!Doctype ... Public/System/>
205
	 */
206
	public void testInsertInvalidateXMLDocument() throws PropertyNotFoundException  {
207 4417 daigle
		debug("\nRunning: testInsertInvalidateXMLDocument test");
208 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
209
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
210 5311 daigle
		debug("insert docid: " + name);
211 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
212
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
213
				+ "</identifier>" + "<allow>"
214
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
215
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
216
				+ "<principal>public</principal>" + "<permission>read</permission>"
217
				+ "</allow>" + "</acl>";
218 5311 daigle
		debug("xml document: " + content);
219 4080 daigle
		assertTrue(handleXMLDocument(content, name, "insert"));
220
	}
221
222
	/**
223
	 * Test insert a non well-formed xml document successfully
224
	 * There is no </acl> in this string
225
	 */
226
	public void testInsertNonWellFormedXMLDocument() throws PropertyNotFoundException  {
227 4417 daigle
		debug("\nRunning: testInsertNonWellFormedXMLDocument test");
228 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
229
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
230 5311 daigle
		debug("insert non well-formed docid: " + name);
231 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
232
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
233
				+ "</identifier>" + "<allow>"
234
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
235
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
236
				+ "<principal>public</principal>" + "<permission>read</permission>"
237
				+ "</allow>";
238
239 5311 daigle
		debug("xml document: " + content);
240 4080 daigle
		assertTrue(!handleXMLDocument(content, name, "insert"));
241
	}
242
243
	/**
244
	 * Test read a xml document  in xml format successfully
245
	 */
246
	public void testReadXMLDocumentXMLFormat() throws PropertyNotFoundException  {
247 4417 daigle
		debug("\nRunning: testReadXMLDocumentXMLFormat test");
248 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
249
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
250 4080 daigle
		assertTrue(handleReadAction(name, "xml"));
251
252
	}
253
254
	/**
255
	 * Test read a xml document  in html format successfully
256
	 */
257
	public void testReadXMLDocumentHTMLFormat() throws PropertyNotFoundException {
258 4417 daigle
		debug("\nRunning: testReadXMLDocumentHTMLFormat test");
259 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
260
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
261 4080 daigle
		assertTrue(handleReadAction(name, "html"));
262
263
	}
264
265
	/**
266
	 * Test read a xml document  in zip format successfully
267
	 */
268
	public void testReadXMLDocumentZipFormat() throws PropertyNotFoundException {
269 4417 daigle
		debug("\nRunning: testReadXMLDocumentZipFormat test");
270 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
271
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
272 4080 daigle
		assertTrue(handleReadAction(name, "zip"));
273
274
	}
275
276
	/**
277
	 * Test insert a xml document successfully
278
	 */
279
	public void testUpdateXMLDocument() throws PropertyNotFoundException {
280 4417 daigle
		debug("\nRunning: testUpdateXMLDocument test");
281 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
282
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
283 5311 daigle
		debug("update docid: " + name);
284 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
285
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
286
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
287
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
288
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
289
				+ "</identifier>" + "<allow>"
290
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
291
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
292
				+ "<principal>public</principal>" + "<permission>read</permission>"
293
				+ "</allow>" + "</acl>";
294 5311 daigle
		debug("xml document: " + content);
295 4080 daigle
		assertTrue(handleXMLDocument(content, name, "update"));
296
297
	}
298
299
	/**
300
	 * Test insert a data file successfully
301
	 */
302
	public void testInertDataFile() throws PropertyNotFoundException {
303 4417 daigle
		debug("\nRunning: testInertDataFile test");
304 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
305
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
306 5311 daigle
		debug("insert data file docid: " + name);
307
		debug("insert data file ");
308 4080 daigle
		File hello = new File("test/jones.204.22.xml");
309
310
		assertTrue(insertDataFile(name, hello));
311
	}
312
313
	/**
314
	 * Test delete a xml document successfully
315
	 */
316
	public void testDeleteXMLDocument() throws PropertyNotFoundException {
317 4417 daigle
		debug("\nRunning: testDeleteXMLDocument test");
318 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
319
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
320 5311 daigle
		debug("delete docid: " + name);
321 4080 daigle
		assertTrue(handleDeleteFile(name));
322
	}
323
324
	/**
325
	 * Test logout action
326
	 */
327
	public void testLogOut() {
328 4417 daigle
		debug("\nRunning: testLogOut test");
329 4080 daigle
		assertTrue(handleLogOut());
330
331
	}
332
333
	/**
334
	 * Method to hanld login action
335
	 *
336
	 * @param usrerName, the DN name of the test method
337
	 * @param passWord, the passwd of the user
338
	 */
339
340
	public boolean logIn(String userName, String passWord) {
341
		Properties prop = new Properties();
342
		prop.put("action", "login");
343
		prop.put("qformat", "xml");
344
		prop.put("username", userName);
345
		prop.put("password", passWord);
346
347
		// Now contact metacat
348
		String response = getMetacatString(prop);
349 5311 daigle
		debug("Login Message: " + response);
350 4080 daigle
		boolean connected = false;
351
		if (response.indexOf("<login>") != -1) {
352
			connected = true;
353
		} else {
354
355
			connected = false;
356
		}
357
358
		return connected;
359
	}
360
361
	/**
362
	 * Method to hanld logout action
363
	 *
364
	 * @param usrerName, the DN name of the test method
365
	 * @param passWord, the passwd of the user
366
	 */
367
368
	public boolean handleLogOut() {
369
		boolean disConnected = false;
370
		Properties prop = new Properties();
371
		prop.put("action", "logout");
372
		prop.put("qformat", "xml");
373
374
		String response = getMetacatString(prop);
375 5311 daigle
		debug("Logout Message: " + response);
376 4080 daigle
		edu.ucsb.nceas.morpho.framework.HttpMessage.setCookie(null);
377
378
		if (response.indexOf("<logout>") != -1) {
379
			disConnected = true;
380
		} else {
381
			disConnected = false;
382
		}
383
384
		return disConnected;
385
	}
386
387
	/**
388
	 * Method to hanld read both xml and data file
389
	 *
390
	 * @param docid, the docid of the document want to read
391
	 * @param qformat, the format of document user want to get
392
	 */
393
	public boolean handleReadAction(String docid, String qformat) {
394
		Properties prop = new Properties();
395
		String message = "";
396
		prop.put("action", "read");
397
		prop.put("qformat", qformat);
398
		prop.put("docid", docid);
399
400
		message = getMetacatString(prop);
401
		message = message.trim();
402 4700 daigle
		//MetacatUtil.debugMessage("Read Message: "+message, 30);
403 4080 daigle
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {//there was an error
404
405
			return false;
406
		} else {//successfully
407
			return true;
408
		}
409
410
	}
411
412
	/**
413
	 * Method to hanld inset or update xml document
414
	 *
415
	 * @param xmlDocument, the content of xml qformat
416
	 * @param docid, the docid of the document
417
	 * @param action, insert or update
418
	 */
419
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
420
421
	{ //-attempt to write file to metacat
422
		String access = "no";
423
		StringBuffer fileText = new StringBuffer();
424
		StringBuffer messageBuf = new StringBuffer();
425
		String accessFileId = null;
426
		Properties prop = new Properties();
427
		prop.put("action", action);
428
		prop.put("public", access); //This is the old way of controlling access
429
		prop.put("doctext", xmlDocument);
430
		prop.put("docid", docid);
431
432
		String message = getMetacatString(prop);
433 5311 daigle
		debug("Insert or Update Message: " + message);
434 4080 daigle
		if (message.indexOf("<error>") != -1) {//there was an error
435
436
			return false;
437
		} else if (message.indexOf("<success>") != -1) {//the operation worked
438
			//write the file to the cache and return the file object
439
			return true;
440
441
		} else {//something weird happened.
442
			return false;
443
		}
444
445
	}
446
447
	public boolean handleDeleteFile(String name) {
448
449
		Properties prop = new Properties();
450
		prop.put("action", "delete");
451
		prop.put("docid", name);
452
453
		String message = getMetacatString(prop);
454 5311 daigle
		debug("Delete Message: " + message);
455 4080 daigle
		if (message.indexOf("<error>") != -1) {//there was an error
456
457
			return false;
458
		} else if (message.indexOf("<success>") != -1) {//the operation worked
459
			//write the file to the cache and return the file object
460
			return true;
461
462
		} else {//something weird happened.
463
			return false;
464
		}
465
	}
466
467
	/**
468
	 * sends a data file to the metacat using "multipart/form-data" encoding
469
	 *
470
	 * @param id the id to assign to the file on metacat (e.g., knb.1.1)
471
	 * @param file the file to send
472
	 */
473
	public boolean insertDataFile(String id, File file) {
474
		String response = null;
475
		//Get response for calling sendDataFile function
476
		response = sendDataFile(id, file);
477
478
		if (response.indexOf("success") != -1) {
479
			//insert successfully
480
			return true;
481
		} else {
482
			return false;
483
		}
484
	}
485
486
	/**
487
	 * sends a data file to the metacat using "multipart/form-data" encoding
488
	 *
489
	 * @param id the id to assign to the file on metacat (e.g., knb.1.1)
490
	 * @param file the file to send
491
	 */
492
	public String sendDataFile(String id, File file) {
493
		String response = "";
494
		InputStream returnStream = null;
495
496
		// Now contact metacat and send the request
497
		try {
498
			//FileInputStream data = new FileInputStream(file);
499
			System.setProperty("java.protocol.handler.pkgs", "HTTPClient");
500
			URL url = new URL(metacatURL);
501
			edu.ucsb.nceas.morpho.framework.HttpMessage msg = new edu.ucsb.nceas.morpho.framework.HttpMessage(
502
					url);
503
			Properties args = new Properties();
504
			args.put("action", "upload");
505
			args.put("docid", id);
506
507
			Properties dataStreams = new Properties();
508
			String filename = file.getAbsolutePath();
509
			System.out.println("the absolute path is " + filename);
510
			dataStreams.put("datafile", filename);
511
512
			returnStream = msg.sendPostData(args, dataStreams);
513
514
			InputStreamReader returnStreamReader = new InputStreamReader(returnStream);
515
			StringWriter sw = new StringWriter();
516
			int len;
517
			char[] characters = new char[512];
518
			while ((len = returnStreamReader.read(characters, 0, 512)) != -1) {
519
				sw.write(characters, 0, len);
520
			}
521
			returnStreamReader.close();
522
			response = sw.toString();
523
			sw.close();
524
525
		} catch (Exception e) {
526
			e.printStackTrace(System.err);
527
		}
528
		return response;
529
	}
530
531
	public String getMetacatString(Properties prop) {
532
		String response = null;
533
534
		// Now contact metacat and send the request
535
		try {
536
			InputStreamReader returnStream = new InputStreamReader(
537
					getMetacatInputStream(prop));
538
			StringWriter sw = new StringWriter();
539
			int len;
540
			char[] characters = new char[512];
541
			while ((len = returnStream.read(characters, 0, 512)) != -1) {
542
				sw.write(characters, 0, len);
543
			}
544
			returnStream.close();
545
			response = sw.toString();
546
			sw.close();
547
		} catch (Exception e) {
548
			return null;
549
		}
550
551
		return response;
552
	}
553
554
	/**
555
	 * Send a request to Metacat
556
	 *
557
	 * @param prop the properties to be sent to Metacat
558
	 * @return InputStream as returned by Metacat
559
	 */
560
	public InputStream getMetacatInputStream(Properties prop) {
561
		InputStream returnStream = null;
562
		// Now contact metacat and send the request
563
		try {
564
565
			URL url = new URL(metacatURL);
566
			edu.ucsb.nceas.morpho.framework.HttpMessage msg = new edu.ucsb.nceas.morpho.framework.HttpMessage(
567
					url);
568
			returnStream = msg.sendPostMessage(prop);
569
			return returnStream;
570
		} catch (Exception e) {
571
			e.printStackTrace(System.err);
572
573
		}
574
		return returnStream;
575
576
	}
577
578 1114 tao
}