Project

General

Profile

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