Project

General

Profile

1
/**
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
 *    Authors: Jing Tao
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-08-24 14:42:25 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5035 $'
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
package edu.ucsb.nceas.metacattest;
28

    
29
import edu.ucsb.nceas.MCTestCase;
30
import edu.ucsb.nceas.metacat.properties.PropertyService;
31
import edu.ucsb.nceas.utilities.HttpMessage;
32
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35

    
36
import java.io.*;
37
import java.net.*;
38
import java.util.*;
39

    
40
/**
41
 * A JUnit test for testing Step class processing
42
 */
43
public class MetaCatServletTest extends MCTestCase {
44
	private static String metacatURL;
45
	private String serialNumber;
46

    
47
	/* Initialize properties */
48
	static {
49
		try {
50
			metacatURL = PropertyService.getProperty("test.metacatUrl");
51
		} catch (PropertyNotFoundException pnfe) {
52
			System.err.println("Could not get property in static block: "
53
					+ pnfe.getMessage());
54
		}
55
	}
56

    
57
	/**
58
	 * Constructor to build the test
59
	 * 
60
	 * @param name
61
	 *            the name of the test method
62
	 */
63
	public MetaCatServletTest(String name) {
64
		super(name);
65
	}
66

    
67
	/**
68
	 * Constructor to build the test
69
	 * 
70
	 * @param name
71
	 *            the name of the test method
72
	 */
73
	public MetaCatServletTest(String name, String serial) {
74
		super(name);
75
		serialNumber = serial;
76
	}
77

    
78
	/**
79
	 * Establish a testing framework by initializing appropriate objects
80
	 */
81
	public void setUp() {
82

    
83
	}
84

    
85
	/**
86
	 * Release any objects after tests are complete
87
	 */
88
	public void tearDown() {
89
	}
90

    
91
	/**
92
	 * Create a suite of tests to be run together
93
	 */
94
	public static Test suite() {
95
		double number = 0;
96
		String serial = null;
97

    
98
		TestSuite suite = new TestSuite();
99
		suite.addTest(new MetaCatServletTest("initialize"));
100
		suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
101
		suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
102
		suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
103
		suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
104
		suite.addTest(new MetaCatServletTest("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 MetaCatServletTest("testNCEASLogin"));
108

    
109
		// create random number for docid, so it can void repeat
110
		number = Math.random() * 100000;
111
		serial = Integer.toString(((new Double(number)).intValue()));
112
		debug("serial: " + serial);
113
		suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
114
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
115
		suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
116

    
117
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", serial));
118
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", serial));
119

    
120
		suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
121

    
122
		// insert invalid xml document
123
		number = Math.random() * 100000;
124
		serial = Integer.toString(((new Double(number)).intValue()));
125
		suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
126
		// insert non well formed document
127
		number = Math.random() * 100000;
128
		serial = Integer.toString(((new Double(number)).intValue()));
129
		suite
130
				.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
131
						serial));
132

    
133
		suite.addTest(new MetaCatServletTest("testLogOut"));
134

    
135
		return suite;
136
	}
137

    
138
	/**
139
	 * Run an initial test that always passes to check that the test harness is
140
	 * working.
141
	 */
142
	public void initialize() {
143
		assertTrue(1 == 1);
144
	}
145

    
146
	/**
147
	 * Test the login to neceas succesfully
148
	 */
149
	public void testNCEASLogin() {
150
		debug("\nRunning: testNCEASLogin test");
151
		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

    
157
	/**
158
	 * Test the login to neceas failed
159
	 */
160
	public void testNCEASLoginFail() {
161
		debug("\nRunning: testNCEASLoginFail test");
162
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
163
		String passwd = "12345678";
164
		assertTrue(!logIn(user, passwd));
165

    
166
	}
167

    
168
	/**
169
	 * Test the login to lter succesfully
170
	 */
171
	public void testLterReferralLogin() {
172
		debug("\nRunning: testLterReferralLogin test");
173
		String user = null;
174
		String passwd = null;
175
		try {
176
			user = PropertyService.getProperty("test.lterUser");
177
			passwd = PropertyService.getProperty("test.lterPassword");
178
		} catch (PropertyNotFoundException pnfe) {
179
			fail("Could not find property: " + pnfe.getMessage());
180
		}
181

    
182
		debug("Logging into lter: " + user + " : " + passwd);
183
		assertTrue(logIn(user, passwd));
184

    
185
	}
186

    
187
	/**
188
	 * Test the login to lter failed
189
	 */
190
	public void testLterReferralLoginFail() {
191
		debug("\nRunning: testLterReferralLoginFail test");
192
		String user = "uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
193
		String passwd = "qVyGpveb";
194
		assertTrue(!logIn(user, passwd));
195
		// assertTrue( withProtocol.getProtocol().equals("http"));
196
	}
197

    
198
	/**
199
	 * Test the login to pisco succesfully
200
	 */
201
	public void testPiscoReferralLogin() {
202
		debug("\nRunning: testPiscoReferralLogin test");
203
		String user = null;
204
		String passwd = null;
205
		try {
206
			user = PropertyService.getProperty("test.piscoUser");
207
			passwd = PropertyService.getProperty("test.piscoPassword");
208
		} catch (PropertyNotFoundException pnfe) {
209
			fail("Could not find property: " + pnfe.getMessage());
210
		}
211
		debug("logging in pisco user: " + user + ":" + passwd);
212
		assertTrue(logIn(user, passwd));
213
		// assertTrue( withProtocol.getProtocol().equals("http"));
214
	}
215

    
216
	/**
217
	 * Test the login to pisco failed
218
	 */
219
	public void testPiscoReferralLoginFail() {
220
		debug("\nRunning: testPiscoReferralLoginFail test");
221
		String user = "uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
222
		String passwd = "hello";
223
		assertTrue(!logIn(user, passwd));
224
		// assertTrue( withProtocol.getProtocol().equals("http"));
225
	}
226

    
227
	/**
228
	 * Test insert a xml document successfully
229
	 */
230
	public void testInsertXMLDocument() {
231
		debug("\nRunning: testInsertXMLDocument test");
232
		String name = null;
233
		try {
234
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
235
					+ serialNumber
236
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
237
		} catch (PropertyNotFoundException pnfe) {
238
			fail("Could not find property: " + pnfe.getMessage());
239
		}
240
		debug("insert docid: " + name);
241
		String content = "<?xml version=\"1.0\"?>"
242
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
243
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
244
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
245
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
246
				+ "</identifier>" + "<allow>"
247
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
248
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
249
				+ "<principal>public</principal>" + "<permission>read</permission>"
250
				+ "</allow>" + "</acl>";
251
		debug("xml document: " + content);
252
		assertTrue(handleXMLDocument(content, name, "insert"));
253

    
254
	}
255

    
256
	/**
257
	 * Test insert a invalidate xml document successfully In the String, there
258
	 * is no <!Doctype ... Public/System/>
259
	 */
260
	public void testInsertInvalidateXMLDocument() {
261
		debug("\nRunning: testInsertInvalidateXMLDocument test");
262
		String name = null;
263
		try {
264
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
265
					+ serialNumber
266
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
267
		} catch (PropertyNotFoundException pnfe) {
268
			fail("Could not find property: " + pnfe.getMessage());
269
		}
270
		debug("insert docid: " + name);
271
		String content = "<?xml version=\"1.0\"?>"
272
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
273
				+ "</identifier>" + "<allow>"
274
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
275
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
276
				+ "<principal>public</principal>" + "<permission>read</permission>"
277
				+ "</allow>" + "</acl>";
278
		debug("xml document: " + content);
279
		assertTrue(handleXMLDocument(content, name, "insert"));
280
	}
281

    
282
	/**
283
	 * Test insert a non well-formed xml document successfully There is no
284
	 * </acl> in this string
285
	 */
286
	public void testInsertNonWellFormedXMLDocument() {
287
		debug("\nRunning: testInsertNonWellFormedXMLDocument test");
288
		String name = null;
289
		try {
290
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
291
					+ serialNumber
292
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
293
		} catch (PropertyNotFoundException pnfe) {
294
			fail("Could not find property: " + pnfe.getMessage());
295
		}
296
		debug("insert non well-formed docid: " + name);
297
		String content = "<?xml version=\"1.0\"?>"
298
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
299
				+ "</identifier>" + "<allow>"
300
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
301
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
302
				+ "<principal>public</principal>" + "<permission>read</permission>"
303
				+ "</allow>";
304

    
305
		debug("xml document: " + content);
306
		assertTrue(!handleXMLDocument(content, name, "insert"));
307
	}
308

    
309
	/**
310
	 * Test read a xml document in xml format successfully
311
	 */
312
	public void testReadXMLDocumentXMLFormat() {
313
		debug("\nRunning: testReadXMLDocumentXMLFormat test");
314
		String name = null;
315
		try {
316
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
317
					+ serialNumber
318
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
319
		} catch (PropertyNotFoundException pnfe) {
320
			fail("Could not find property: " + pnfe.getMessage());
321
		}
322
		assertTrue(handleReadAction(name, "xml"));
323

    
324
	}
325

    
326
	/**
327
	 * Test read a xml document in html format successfully
328
	 */
329
	public void testReadXMLDocumentHTMLFormat() {
330
		debug("\nRunning: testReadXMLDocumentHTMLFormat test");
331
		String name = null;
332
		try {
333
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
334
					+ serialNumber
335
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
336
		} catch (PropertyNotFoundException pnfe) {
337
			fail("Could not find property: " + pnfe.getMessage());
338
		}
339
		assertTrue(handleReadAction(name, "html"));
340

    
341
	}
342

    
343
	/**
344
	 * Test read a xml document in zip format successfully
345
	 */
346
	public void testReadXMLDocumentZipFormat() {
347
		debug("\nRunning: testReadXMLDocumentZipFormat test");
348
		String name = null;
349
		try {
350
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
351
					+ serialNumber
352
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
353
		} catch (PropertyNotFoundException pnfe) {
354
			fail("Could not find property: " + pnfe.getMessage());
355
		}
356
		assertTrue(handleReadAction(name, "zip"));
357

    
358
	}
359

    
360
	/**
361
	 * Test insert a xml document successfully
362
	 */
363
	public void testUpdateXMLDocument() {
364
		debug("\nRunning: testUpdateXMLDocument test");
365
		String name = null;
366
		try {
367
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
368
					+ serialNumber
369
					+ PropertyService.getProperty("document.accNumSeparator") + "2";
370
		} catch (PropertyNotFoundException pnfe) {
371
			fail("Could not find property: " + pnfe.getMessage());
372
		}
373
		debug("update docid: " + name);
374
		String content = "<?xml version=\"1.0\"?>"
375
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
376
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
377
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
378
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
379
				+ "</identifier>" + "<allow>"
380
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
381
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
382
				+ "<principal>public</principal>" + "<permission>read</permission>"
383
				+ "</allow>" + "</acl>";
384
		debug("xml document: " + content);
385
		assertTrue(handleXMLDocument(content, name, "update"));
386

    
387
	}
388

    
389
	/**
390
	 * Test delete a xml document successfully
391
	 */
392
	public void testDeleteXMLDocument() {
393
		debug("\nRunning: testDeleteXMLDocument test");
394
		String name = null;
395
		try {
396
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
397
					+ serialNumber
398
					+ PropertyService.getProperty("document.accNumSeparator") + "2";
399
		} catch (PropertyNotFoundException pnfe) {
400
			fail("Could not find property: " + pnfe.getMessage());
401
		}
402
		debug("delete docid: " + name);
403
		assertTrue(handleDeleteFile(name));
404

    
405
	}
406

    
407
	/**
408
	 * Test logout action
409
	 */
410
	public void testLogOut() {
411
		debug("\nRunning: testLogOut test");
412
		assertTrue(handleLogOut());
413

    
414
	}
415

    
416
	/**
417
	 * Method to hanld login action
418
	 * 
419
	 * @param usrerName,
420
	 *            the DN name of the test method
421
	 * @param passWord,
422
	 *            the passwd of the user
423
	 */
424

    
425
	public boolean logIn(String userName, String passWord) {
426
		Properties prop = new Properties();
427
		prop.put("action", "login");
428
		prop.put("qformat", "xml");
429
		prop.put("username", userName);
430
		prop.put("password", passWord);
431

    
432
		// Now contact metacat
433
		String response = getMetacatString(prop);
434
		debug("Login Message: " + response);
435
		boolean connected = false;
436
		if (response.indexOf("<login>") != -1) {
437
			connected = true;
438
		} else {
439

    
440
			connected = false;
441
		}
442

    
443
		return connected;
444
	}
445

    
446
	/**
447
	 * Method to hanld logout action
448
	 * 
449
	 * @param usrerName,
450
	 *            the DN name of the test method
451
	 * @param passWord,
452
	 *            the passwd of the user
453
	 */
454

    
455
	public boolean handleLogOut() {
456
		boolean disConnected = false;
457
		Properties prop = new Properties();
458
		prop.put("action", "logout");
459
		prop.put("qformat", "xml");
460

    
461
		String response = getMetacatString(prop);
462
		debug("Logout Message: " + response);
463
		HttpMessage.setCookie(null);
464

    
465
		if (response.indexOf("<logout>") != -1) {
466
			disConnected = true;
467
		} else {
468
			disConnected = false;
469
		}
470

    
471
		return disConnected;
472
	}
473

    
474
	/**
475
	 * Method to hanld read both xml and data file
476
	 * 
477
	 * @param docid,
478
	 *            the docid of the document want to read
479
	 * @param qformat,
480
	 *            the format of document user want to get
481
	 */
482
	public boolean handleReadAction(String docid, String qformat) {
483
		Properties prop = new Properties();
484
		String message = "";
485
		prop.put("action", "read");
486
		prop.put("qformat", qformat);
487
		prop.put("docid", docid);
488

    
489
		message = getMetacatString(prop);
490
		message = message.trim();
491
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {// there
492
																						// was
493
																						// an
494
																						// error
495

    
496
			return false;
497
		} else {// successfully
498
			return true;
499
		}
500

    
501
	}
502

    
503
	/**
504
	 * Method to hanld inset or update xml document
505
	 * 
506
	 * @param xmlDocument,
507
	 *            the content of xml qformat
508
	 * @param docid,
509
	 *            the docid of the document
510
	 * @param action,
511
	 *            insert or update
512
	 */
513
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
514

    
515
	{ // -attempt to write file to metacat
516
		String access = "no";
517
		StringBuffer fileText = new StringBuffer();
518
		StringBuffer messageBuf = new StringBuffer();
519
		String accessFileId = null;
520
		Properties prop = new Properties();
521
		prop.put("action", action);
522
		prop.put("public", access); // This is the old way of controlling access
523
		prop.put("doctext", xmlDocument);
524
		prop.put("docid", docid);
525

    
526
		String message = getMetacatString(prop);
527
		debug("Insert or Update Message: " + message);
528
		if (message.indexOf("<error>") != -1) {// there was an error
529

    
530
			return false;
531
		} else if (message.indexOf("<success>") != -1) {// the operation worked
532
			// write the file to the cache and return the file object
533
			return true;
534

    
535
		} else {// something weird happened.
536
			return false;
537
		}
538

    
539
	}
540

    
541
	public boolean handleDeleteFile(String name) {
542

    
543
		Properties prop = new Properties();
544
		prop.put("action", "delete");
545
		prop.put("docid", name);
546

    
547
		String message = getMetacatString(prop);
548
		debug("Delete Message: " + message);
549
		if (message.indexOf("<error>") != -1) {// there was an error
550

    
551
			return false;
552
		} else if (message.indexOf("<success>") != -1) {// the operation worked
553
			// write the file to the cache and return the file object
554
			return true;
555

    
556
		} else {// something weird happened.
557
			return false;
558
		}
559
	}
560

    
561
	public String getMetacatString(Properties prop) {
562
		String response = null;
563

    
564
		// Now contact metacat and send the request
565
		try {
566
			InputStreamReader returnStream = new InputStreamReader(
567
					getMetacatInputStream(prop));
568
			StringWriter sw = new StringWriter();
569
			int len;
570
			char[] characters = new char[512];
571
			while ((len = returnStream.read(characters, 0, 512)) != -1) {
572
				sw.write(characters, 0, len);
573
			}
574
			returnStream.close();
575
			response = sw.toString();
576
			sw.close();
577
		} catch (Exception e) {
578
			return null;
579
		}
580

    
581
		return response;
582
	}
583

    
584
	/**
585
	 * Send a request to Metacat
586
	 * 
587
	 * @param prop
588
	 *            the properties to be sent to Metacat
589
	 * @return InputStream as returned by Metacat
590
	 */
591
	public InputStream getMetacatInputStream(Properties prop) {
592
		InputStream returnStream = null;
593
		// Now contact metacat and send the request
594
		try {
595

    
596
			URL url = new URL(metacatURL);
597
			HttpMessage msg = new HttpMessage(url);
598
			returnStream = msg.sendPostMessage(prop);
599
			return returnStream;
600
		} catch (Exception e) {
601
			e.printStackTrace(System.err);
602

    
603
		}
604
		return returnStream;
605

    
606
	}
607

    
608
}
(9-9/22)