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