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