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