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