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: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
10
 * '$Revision: 4080 $'
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.metacat.*;
30
import edu.ucsb.nceas.metacat.service.PropertyService;
31
import edu.ucsb.nceas.metacat.util.SystemUtil;
32
import edu.ucsb.nceas.utilities.HttpMessage;
33
import edu.ucsb.nceas.utilities.Options; //import edu.ucsb.nceas.morpho.framework.*;
34
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
35
import junit.framework.Test;
36
import junit.framework.TestCase;
37
import junit.framework.TestResult;
38
import junit.framework.TestSuite;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41

    
42
import java.io.*;
43
import java.net.*;
44
import java.util.*;
45

    
46
/**
47
 * A JUnit test for testing Step class processing
48
 */
49
public class MetaCatServletTest extends TestCase {
50
	private static String metacatURL;
51
	private String serialNumber;
52
	private static final Log log = LogFactory
53
			.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
54
	/* Initialize Options*/
55
	static {
56
		try {
57
			metacatURL = SystemUtil.getServletURL();
58
			Options.initialize(new File("build/tests/metacat.properties"));
59
		} catch (Exception e) {
60
			System.err.println("Exception in initialize option in MetacatServletNetTest "
61
					+ e.getMessage());
62
		}
63
	}
64

    
65
	/**
66
	 * Constructor to build the test
67
	 *
68
	 * @param name the name of the test method
69
	 */
70
	public MetaCatServletTest(String name) {
71
		super(name);
72
	}
73

    
74
	/**
75
	 * Constructor to build the test
76
	 *
77
	 * @param name the name of the test method
78
	 */
79
	public MetaCatServletTest(String name, String serial) {
80
		super(name);
81
		serialNumber = serial;
82
	}
83

    
84
	/**
85
	 * Establish a testing framework by initializing appropriate objects
86
	 */
87
	public void setUp() {
88

    
89
	}
90

    
91
	/**
92
	 * Release any objects after tests are complete
93
	 */
94
	public void tearDown() {
95
	}
96

    
97
	/**
98
	 * Create a suite of tests to be run together
99
	 */
100
	public static Test suite() {
101
		double number = 0;
102
		String serial = null;
103

    
104
		TestSuite suite = new TestSuite();
105
		suite.addTest(new MetaCatServletTest("initialize"));
106
		suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
107
		suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
108
		suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
109
		suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
110
		suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
111
		//Should put a login successfully at the end of login test
112
		//So insert or update can have cookie.
113
		suite.addTest(new MetaCatServletTest("testNCEASLogin"));
114

    
115
		//create random number for docid, so it can void repeat
116
		number = Math.random() * 100000;
117
		serial = Integer.toString(((new Double(number)).intValue()));
118
		log.debug("serial: " + serial);
119
		suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
120
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
121
		suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
122

    
123
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", serial));
124
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", serial));
125

    
126
		suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
127

    
128
		//insert invalid xml document
129
		number = Math.random() * 100000;
130
		serial = Integer.toString(((new Double(number)).intValue()));
131
		suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
132
		//insert non well formed document
133
		number = Math.random() * 100000;
134
		serial = Integer.toString(((new Double(number)).intValue()));
135
		suite
136
				.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
137
						serial));
138

    
139
		suite.addTest(new MetaCatServletTest("testLogOut"));
140

    
141
		return suite;
142
	}
143

    
144
	/**
145
	 * Run an initial test that always passes to check that the test
146
	 * harness is working.
147
	 */
148
	public void initialize() {
149
		assertTrue(1 == 1);
150
	}
151

    
152
	/**
153
	 * Test the login to neceas succesfully
154
	 */
155
	public void testNCEASLogin() {
156
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
157
		String passwd = "123456";
158
		assertTrue(logIn(user, passwd));
159
		//assertTrue( withProtocol.getProtocol().equals("http"));
160
	}
161

    
162
	/**
163
	 * Test the login to neceas failed
164
	 */
165
	public void testNCEASLoginFail() {
166
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
167
		String passwd = "12345678";
168
		assertTrue(!logIn(user, passwd));
169

    
170
	}
171

    
172
	/**
173
	 * Test the login to lter succesfully
174
	 */
175
	public void testLterReferralLogin() {
176
		String user = null;
177
		String passwd = null;
178
		try {
179
			user = PropertyService.getProperty("lteruser");
180
			passwd = PropertyService.getProperty("lterpassword");
181
		} catch (PropertyNotFoundException pnfe) {
182
			fail("Could not find property: " + pnfe.getMessage());
183
		}
184
		assertTrue(logIn(user, passwd));
185

    
186
	}
187

    
188
	/**
189
	 * Test the login to lter failed
190
	 */
191
	public void testLterReferralLoginFail() {
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
		String user = null;
203
		String passwd = null;
204
		try {
205
			user = PropertyService.getProperty("piscouser");
206
			passwd = PropertyService.getProperty("piscopassword");
207
		} catch (PropertyNotFoundException pnfe) {
208
			fail("Could not find property: " + pnfe.getMessage());
209
		}
210
		assertTrue(logIn(user, passwd));
211
		// assertTrue( withProtocol.getProtocol().equals("http"));
212
	}
213

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

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

    
249
	}
250

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

    
275
	/**
276
	 * Test insert a non well-formed xml document successfully
277
	 * There is no </acl> in this string
278
	 */
279
	public void testInsertNonWellFormedXMLDocument() {
280
		String name = null;
281
		try {
282
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
283
				+ PropertyService.getProperty("accNumSeparator") + "1";
284
		} catch (PropertyNotFoundException pnfe) {
285
			fail("Could not find property: " + pnfe.getMessage());
286
		}
287
		log.debug("insert non well-formed docid: " + name);
288
		String content = "<?xml version=\"1.0\"?>"
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>";
295

    
296
		log.debug("xml document: " + content);
297
		assertTrue(!handleXMLDocument(content, name, "insert"));
298
	}
299

    
300
	/**
301
	 * Test read a xml document  in xml format successfully
302
	 */
303
	public void testReadXMLDocumentXMLFormat() {
304
		String name = null;
305
		try {
306
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
307
				+ PropertyService.getProperty("accNumSeparator") + "1";
308
		} catch (PropertyNotFoundException pnfe) {
309
			fail("Could not find property: " + pnfe.getMessage());
310
		}
311
		assertTrue(handleReadAction(name, "xml"));
312

    
313
	}
314

    
315
	/**
316
	 * Test read a xml document  in html format successfully
317
	 */
318
	public void testReadXMLDocumentHTMLFormat() {
319
		String name = null;
320
		try {
321
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
322
				+ PropertyService.getProperty("accNumSeparator") + "1";
323
		} catch (PropertyNotFoundException pnfe) {
324
			fail("Could not find property: " + pnfe.getMessage());
325
		}	
326
		assertTrue(handleReadAction(name, "html"));
327

    
328
	}
329

    
330
	/**
331
	 * Test read a xml document  in zip format successfully
332
	 */
333
	public void testReadXMLDocumentZipFormat() {
334
		String name = null;
335
		try {
336
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
337
				+ PropertyService.getProperty("accNumSeparator") + "1";
338
		} catch (PropertyNotFoundException pnfe) {
339
			fail("Could not find property: " + pnfe.getMessage());
340
		}	
341
		assertTrue(handleReadAction(name, "zip"));
342

    
343
	}
344

    
345
	/**
346
	 * Test insert a xml document successfully
347
	 */
348
	public void testUpdateXMLDocument() {
349
		String name = null;
350
		try {
351
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
352
				+ PropertyService.getProperty("accNumSeparator") + "2";
353
		} catch (PropertyNotFoundException pnfe) {
354
			fail("Could not find property: " + pnfe.getMessage());
355
		}		
356
		log.debug("update docid: " + name);
357
		String content = "<?xml version=\"1.0\"?>"
358
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
359
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
360
				+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
361
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
362
				+ "</identifier>" + "<allow>"
363
				+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
364
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
365
				+ "<principal>public</principal>" + "<permission>read</permission>"
366
				+ "</allow>" + "</acl>";
367
		log.debug("xml document: " + content);
368
		assertTrue(handleXMLDocument(content, name, "update"));
369

    
370
	}
371

    
372
	/**
373
	 * Test delete a xml document successfully
374
	 */
375
	public void testDeleteXMLDocument() {
376
		String name = null;
377
		try {
378
			name = "john" + PropertyService.getProperty("accNumSeparator") + serialNumber
379
				+ PropertyService.getProperty("accNumSeparator") + "2";
380
		} catch (PropertyNotFoundException pnfe) {
381
			fail("Could not find property: " + pnfe.getMessage());
382
		}
383
		log.debug("delete docid: " + name);
384
		assertTrue(handleDeleteFile(name));
385

    
386
	}
387

    
388
	/**
389
	 * Test logout action
390
	 */
391
	public void testLogOut() {
392

    
393
		assertTrue(handleLogOut());
394

    
395
	}
396

    
397
	/**
398
	 * Method to hanld login action
399
	 *
400
	 * @param usrerName, the DN name of the test method
401
	 * @param passWord, the passwd of the user
402
	 */
403

    
404
	public boolean logIn(String userName, String passWord) {
405
		Properties prop = new Properties();
406
		prop.put("action", "login");
407
		prop.put("qformat", "xml");
408
		prop.put("username", userName);
409
		prop.put("password", passWord);
410

    
411
		// Now contact metacat
412
		String response = getMetacatString(prop);
413
		log.debug("Login Message: " + response);
414
		boolean connected = false;
415
		if (response.indexOf("<login>") != -1) {
416
			connected = true;
417
		} else {
418

    
419
			connected = false;
420
		}
421

    
422
		return connected;
423
	}
424

    
425
	/**
426
	 * Method to hanld logout action
427
	 *
428
	 * @param usrerName, the DN name of the test method
429
	 * @param passWord, the passwd of the user
430
	 */
431

    
432
	public boolean handleLogOut() {
433
		boolean disConnected = false;
434
		Properties prop = new Properties();
435
		prop.put("action", "logout");
436
		prop.put("qformat", "xml");
437

    
438
		String response = getMetacatString(prop);
439
		log.debug("Logout Message: " + response);
440
		HttpMessage.setCookie(null);
441

    
442
		if (response.indexOf("<logout>") != -1) {
443
			disConnected = true;
444
		} else {
445
			disConnected = false;
446
		}
447

    
448
		return disConnected;
449
	}
450

    
451
	/**
452
	 * Method to hanld read both xml and data file
453
	 *
454
	 * @param docid, the docid of the document want to read
455
	 * @param qformat, the format of document user want to get
456
	 */
457
	public boolean handleReadAction(String docid, String qformat) {
458
		Properties prop = new Properties();
459
		String message = "";
460
		prop.put("action", "read");
461
		prop.put("qformat", qformat);
462
		prop.put("docid", docid);
463

    
464
		message = getMetacatString(prop);
465
		message = message.trim();
466
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {//there was an error
467

    
468
			return false;
469
		} else {//successfully
470
			return true;
471
		}
472

    
473
	}
474

    
475
	/**
476
	 * Method to hanld inset or update xml document
477
	 *
478
	 * @param xmlDocument, the content of xml qformat
479
	 * @param docid, the docid of the document
480
	 * @param action, insert or update
481
	 */
482
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
483

    
484
	{ //-attempt to write file to metacat
485
		String access = "no";
486
		StringBuffer fileText = new StringBuffer();
487
		StringBuffer messageBuf = new StringBuffer();
488
		String accessFileId = null;
489
		Properties prop = new Properties();
490
		prop.put("action", action);
491
		prop.put("public", access); //This is the old way of controlling access
492
		prop.put("doctext", xmlDocument);
493
		prop.put("docid", docid);
494

    
495
		String message = getMetacatString(prop);
496
		log.debug("Insert or Update Message: " + message);
497
		if (message.indexOf("<error>") != -1) {//there was an error
498

    
499
			return false;
500
		} else if (message.indexOf("<success>") != -1) {//the operation worked
501
			//write the file to the cache and return the file object
502
			return true;
503

    
504
		} else {//something weird happened.
505
			return false;
506
		}
507

    
508
	}
509

    
510
	public boolean handleDeleteFile(String name) {
511

    
512
		Properties prop = new Properties();
513
		prop.put("action", "delete");
514
		prop.put("docid", name);
515

    
516
		String message = getMetacatString(prop);
517
		log.debug("Delete Message: " + message);
518
		if (message.indexOf("<error>") != -1) {//there was an error
519

    
520
			return false;
521
		} else if (message.indexOf("<success>") != -1) {//the operation worked
522
			//write the file to the cache and return the file object
523
			return true;
524

    
525
		} else {//something weird happened.
526
			return false;
527
		}
528
	}
529

    
530
	public String getMetacatString(Properties prop) {
531
		String response = null;
532

    
533
		// Now contact metacat and send the request
534
		try {
535
			InputStreamReader returnStream = new InputStreamReader(
536
					getMetacatInputStream(prop));
537
			StringWriter sw = new StringWriter();
538
			int len;
539
			char[] characters = new char[512];
540
			while ((len = returnStream.read(characters, 0, 512)) != -1) {
541
				sw.write(characters, 0, len);
542
			}
543
			returnStream.close();
544
			response = sw.toString();
545
			sw.close();
546
		} catch (Exception e) {
547
			return null;
548
		}
549

    
550
		return response;
551
	}
552

    
553
	/**
554
	 * Send a request to Metacat
555
	 *
556
	 * @param prop the properties to be sent to Metacat
557
	 * @return InputStream as returned by Metacat
558
	 */
559
	public InputStream getMetacatInputStream(Properties prop) {
560
		InputStream returnStream = null;
561
		// Now contact metacat and send the request
562
		try {
563

    
564
			URL url = new URL(metacatURL);
565
			HttpMessage msg = new HttpMessage(url);
566
			returnStream = msg.sendPostMessage(prop);
567
			return returnStream;
568
		} catch (Exception e) {
569
			e.printStackTrace(System.err);
570

    
571
		}
572
		return returnStream;
573

    
574
	}
575

    
576
}
(7-7/18)