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 5478 berkley
	    System.out.println("Testing nceas login");
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 5311 daigle
	 * Test the login to nceas failed
160 4080 daigle
	 */
161
	public void testNCEASLoginFail() {
162 4417 daigle
		debug("\nRunning: testNCEASLoginFail test");
163 5478 berkley
		System.out.println("Testing nceas login fail");
164 4080 daigle
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
165
		String passwd = "12345678";
166
		assertTrue(!logIn(user, passwd));
167 1114 tao
168 4080 daigle
	}
169 1114 tao
170 4080 daigle
	/**
171
	 * Test the login to lter failed
172
	 */
173
	public void testLterReferralLoginFail() {
174 4417 daigle
		debug("\nRunning: testLterReferralLoginFail test");
175 4080 daigle
		String user = "uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
176
		String passwd = "qVyGpveb";
177
		assertTrue(!logIn(user, passwd));
178
		//assertTrue( withProtocol.getProtocol().equals("http"));
179
	}
180 1114 tao
181 4080 daigle
	/**
182
	 * Test insert a xml document successfully
183
	 */
184
	public void testInsertXMLDocument() throws PropertyNotFoundException {
185 4417 daigle
		debug("\nRunning: testInsertXMLDocument test");
186 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
187
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
188 5311 daigle
		debug("insert docid: " + name);
189 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
190
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
191
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
192
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
193
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
194
				+ "</identifier>" + "<allow>"
195
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
196
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
197
				+ "<principal>public</principal>" + "<permission>read</permission>"
198
				+ "</allow>" + "</acl>";
199 5311 daigle
		debug("xml document: " + content);
200 4080 daigle
		assertTrue(handleXMLDocument(content, name, "insert"));
201
202
	}
203
204
	/**
205
	 * Test insert a invalidate xml document successfully
206
	 * In the String, there is no <!Doctype ... Public/System/>
207
	 */
208
	public void testInsertInvalidateXMLDocument() throws PropertyNotFoundException  {
209 4417 daigle
		debug("\nRunning: testInsertInvalidateXMLDocument test");
210 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
211
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
212 5311 daigle
		debug("insert docid: " + name);
213 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
214
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
215
				+ "</identifier>" + "<allow>"
216
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
217
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
218
				+ "<principal>public</principal>" + "<permission>read</permission>"
219
				+ "</allow>" + "</acl>";
220 5311 daigle
		debug("xml document: " + content);
221 4080 daigle
		assertTrue(handleXMLDocument(content, name, "insert"));
222
	}
223
224
	/**
225
	 * Test insert a non well-formed xml document successfully
226
	 * There is no </acl> in this string
227
	 */
228
	public void testInsertNonWellFormedXMLDocument() throws PropertyNotFoundException  {
229 4417 daigle
		debug("\nRunning: testInsertNonWellFormedXMLDocument test");
230 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
231
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
232 5311 daigle
		debug("insert non well-formed docid: " + name);
233 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
234
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
235
				+ "</identifier>" + "<allow>"
236
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
237
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
238
				+ "<principal>public</principal>" + "<permission>read</permission>"
239
				+ "</allow>";
240
241 5311 daigle
		debug("xml document: " + content);
242 4080 daigle
		assertTrue(!handleXMLDocument(content, name, "insert"));
243
	}
244
245
	/**
246
	 * Test read a xml document  in xml format successfully
247
	 */
248
	public void testReadXMLDocumentXMLFormat() throws PropertyNotFoundException  {
249 4417 daigle
		debug("\nRunning: testReadXMLDocumentXMLFormat test");
250 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
251
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
252 4080 daigle
		assertTrue(handleReadAction(name, "xml"));
253
254
	}
255
256
	/**
257
	 * Test read a xml document  in html format successfully
258
	 */
259
	public void testReadXMLDocumentHTMLFormat() throws PropertyNotFoundException {
260 4417 daigle
		debug("\nRunning: testReadXMLDocumentHTMLFormat test");
261 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
262
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
263 4080 daigle
		assertTrue(handleReadAction(name, "html"));
264
265
	}
266
267
	/**
268
	 * Test read a xml document  in zip format successfully
269
	 */
270
	public void testReadXMLDocumentZipFormat() throws PropertyNotFoundException {
271 4417 daigle
		debug("\nRunning: testReadXMLDocumentZipFormat test");
272 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
273
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
274 4080 daigle
		assertTrue(handleReadAction(name, "zip"));
275
276
	}
277
278
	/**
279
	 * Test insert a xml document successfully
280
	 */
281
	public void testUpdateXMLDocument() throws PropertyNotFoundException {
282 4417 daigle
		debug("\nRunning: testUpdateXMLDocument test");
283 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
284
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
285 5311 daigle
		debug("update docid: " + name);
286 4080 daigle
		String content = "<?xml version=\"1.0\"?>"
287
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
288
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
289
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
290
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
291
				+ "</identifier>" + "<allow>"
292
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
293
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
294
				+ "<principal>public</principal>" + "<permission>read</permission>"
295
				+ "</allow>" + "</acl>";
296 5311 daigle
		debug("xml document: " + content);
297 4080 daigle
		assertTrue(handleXMLDocument(content, name, "update"));
298
299
	}
300
301
	/**
302
	 * Test insert a data file successfully
303
	 */
304
	public void testInertDataFile() throws PropertyNotFoundException {
305 4417 daigle
		debug("\nRunning: testInertDataFile test");
306 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
307
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
308 5311 daigle
		debug("insert data file docid: " + name);
309
		debug("insert data file ");
310 4080 daigle
		File hello = new File("test/jones.204.22.xml");
311
312
		assertTrue(insertDataFile(name, hello));
313
	}
314
315
	/**
316
	 * Test delete a xml document successfully
317
	 */
318
	public void testDeleteXMLDocument() throws PropertyNotFoundException {
319 4417 daigle
		debug("\nRunning: testDeleteXMLDocument test");
320 4212 daigle
		String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
321
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
322 5311 daigle
		debug("delete docid: " + name);
323 4080 daigle
		assertTrue(handleDeleteFile(name));
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 5311 daigle
		debug("Login Message: " + response);
352 4080 daigle
		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 5311 daigle
		debug("Logout Message: " + response);
378 4080 daigle
		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 4700 daigle
		//MetacatUtil.debugMessage("Read Message: "+message, 30);
405 4080 daigle
		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 5311 daigle
		debug("Insert or Update Message: " + message);
436 4080 daigle
		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 5311 daigle
		debug("Delete Message: " + message);
457 4080 daigle
		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
}