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