1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 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
|
*
|
7
|
* '$Author: daigle $'
|
8
|
* '$Date: 2010-04-15 09:47:55 -0700 (Thu, 15 Apr 2010) $'
|
9
|
* '$Revision: 5317 $'
|
10
|
*
|
11
|
* This program is free software; you can redistribute it and/or modify
|
12
|
* it under the terms of the GNU General Public License as published by
|
13
|
* the Free Software Foundation; either version 2 of the License, or
|
14
|
* (at your option) any later version.
|
15
|
*
|
16
|
* This program is distributed in the hope that it will be useful,
|
17
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
* GNU General Public License for more details.
|
20
|
*
|
21
|
* You should have received a copy of the GNU General Public License
|
22
|
* along with this program; if not, write to the Free Software
|
23
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
24
|
*/
|
25
|
|
26
|
package edu.ucsb.nceas.metacattest.client;
|
27
|
|
28
|
import java.io.File;
|
29
|
import java.io.FileReader;
|
30
|
import java.io.FileWriter;
|
31
|
import java.io.InputStreamReader;
|
32
|
import java.io.IOException;
|
33
|
import java.io.Reader;
|
34
|
import java.io.StringReader;
|
35
|
import java.io.StringWriter;
|
36
|
import java.util.Calendar;
|
37
|
import java.util.Date;
|
38
|
import java.util.GregorianCalendar;
|
39
|
import java.util.SimpleTimeZone;
|
40
|
import java.util.TimeZone;
|
41
|
|
42
|
import edu.ucsb.nceas.MCTestCase;
|
43
|
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
|
44
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
45
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
46
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
47
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
48
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
49
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
50
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
51
|
import edu.ucsb.nceas.utilities.FileUtil;
|
52
|
import edu.ucsb.nceas.utilities.IOUtil;
|
53
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
54
|
import junit.framework.Test;
|
55
|
import junit.framework.TestSuite;
|
56
|
import java.io.FileInputStream;
|
57
|
|
58
|
import org.apache.log4j.Logger;
|
59
|
|
60
|
/**
|
61
|
* A JUnit test for testing Step class processing
|
62
|
*/
|
63
|
public class MetacatClientTest extends MCTestCase
|
64
|
{
|
65
|
|
66
|
private String wrongMetacatUrl=
|
67
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
68
|
|
69
|
private static String metacatUrl;
|
70
|
private static String username;
|
71
|
private static String password;
|
72
|
private static String anotheruser;
|
73
|
private static String anotherpassword;
|
74
|
static {
|
75
|
try {
|
76
|
metacatUrl = PropertyService.getProperty("test.metacatUrl");
|
77
|
username = PropertyService.getProperty("test.mcUser");
|
78
|
password = PropertyService.getProperty("test.mcPassword");
|
79
|
anotheruser = PropertyService.getProperty("test.mcAnotherUser");
|
80
|
anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
|
81
|
} catch (PropertyNotFoundException pnfe) {
|
82
|
System.err.println("Could not get property in static block: "
|
83
|
+ pnfe.getMessage());
|
84
|
}
|
85
|
}
|
86
|
private String failpass = "uidfnkj43987yfdn";
|
87
|
private String prefix = "test";
|
88
|
private String newdocid = null;
|
89
|
private String testfile = "test/jones.204.22.xml";
|
90
|
private String onlinetestdatafile = "test/onlineDataFile1";
|
91
|
private String queryFile = "test/query.xml";
|
92
|
private String testdocument = "";
|
93
|
private Metacat m;
|
94
|
private String spatialTestFile = "test/spatialEml.xml";
|
95
|
private static final int TIME = 5;
|
96
|
private static final String DOCID = "docid";
|
97
|
private static final String DATAID = "dataid";
|
98
|
|
99
|
/**
|
100
|
* Constructor to build the test
|
101
|
*
|
102
|
* @param name the name of the test method
|
103
|
*/
|
104
|
public MetacatClientTest(String name)
|
105
|
{
|
106
|
super(name);
|
107
|
newdocid = generateDocid();
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Establish a testing framework by initializing appropriate objects
|
112
|
*/
|
113
|
public void setUp()
|
114
|
{
|
115
|
try {
|
116
|
FileReader fr = new FileReader(testfile);
|
117
|
testdocument = IOUtil.getAsString(fr, true);
|
118
|
} catch (IOException ioe) {
|
119
|
fail("Can't read test data to run the test: " + testfile);
|
120
|
}
|
121
|
|
122
|
try {
|
123
|
debug("Test Metacat: " + metacatUrl);
|
124
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
125
|
} catch (MetacatInaccessibleException mie) {
|
126
|
System.err.println("Metacat is: " + metacatUrl);
|
127
|
fail("Metacat connection failed." + mie.getMessage());
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Release any objects after tests are complete
|
133
|
*/
|
134
|
public void tearDown()
|
135
|
{
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Create a suite of tests to be run together
|
140
|
*/
|
141
|
public static Test suite()
|
142
|
{
|
143
|
TestSuite suite = new TestSuite();
|
144
|
suite.addTest(new MetacatClientTest("initialize"));
|
145
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
146
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
147
|
suite.addTest(new MetacatClientTest("login"));
|
148
|
suite.addTest(new MetacatClientTest("insert"));
|
149
|
suite.addTest(new MetacatClientTest("getNewestDocRevision"));
|
150
|
suite.addTest(new MetacatClientTest("getLastDocid"));
|
151
|
suite.addTest(new MetacatClientTest("upload"));
|
152
|
suite.addTest(new MetacatClientTest("upload_stream"));
|
153
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
154
|
suite.addTest(new MetacatClientTest("read"));
|
155
|
suite.addTest(new MetacatClientTest("query"));
|
156
|
suite.addTest(new MetacatClientTest("queryWithQformat"));
|
157
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
158
|
suite.addTest(new MetacatClientTest("update"));
|
159
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
160
|
suite.addTest(new MetacatClientTest("delete"));
|
161
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
162
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
163
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
164
|
suite.addTest(new MetacatClientTest("insertSpatialDocs"));
|
165
|
return suite;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Run an initial test that always passes to check that the test
|
170
|
* harness is working.
|
171
|
*/
|
172
|
public void initialize()
|
173
|
{
|
174
|
assertTrue(1 == 1);
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Test the login() function with valid credentials
|
179
|
*/
|
180
|
public void login()
|
181
|
{
|
182
|
debug("\nStarting login test...");
|
183
|
// Try a valid login
|
184
|
try {
|
185
|
String response = m.login(username, password);
|
186
|
debug("login(): response=" + response);
|
187
|
assertTrue(response != null);
|
188
|
assertTrue(response.indexOf("<login>") != -1);
|
189
|
String sessionId = m.getSessionId();
|
190
|
debug("login(): Session ID=" + m.getSessionId());
|
191
|
assertTrue(sessionId != null);
|
192
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
193
|
} catch (MetacatAuthException mae) {
|
194
|
fail("Authorization failed:\n" + mae.getMessage());
|
195
|
} catch (MetacatInaccessibleException mie) {
|
196
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
197
|
}
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Test the login() function with INVALID credentials
|
202
|
*/
|
203
|
public void invalidLogin()
|
204
|
{
|
205
|
debug("\nStarting invalidLogin test...");
|
206
|
// Try an invalid login
|
207
|
try {
|
208
|
m.login(username, failpass);
|
209
|
fail("Authorization should have failed.");
|
210
|
} catch (MetacatAuthException mae) {
|
211
|
assertTrue(1 == 1);
|
212
|
} catch (MetacatInaccessibleException mie) {
|
213
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
214
|
}
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Test the logout() function. When logout, user will be public, it couldn't
|
219
|
* insert a document.
|
220
|
*/
|
221
|
public void logoutAndInvalidInsert()
|
222
|
{
|
223
|
debug("\nStarting logoutAndInvalidInsert test...");
|
224
|
try {
|
225
|
String identifier = newdocid + ".1";
|
226
|
m.login(username, password);
|
227
|
m.logout();
|
228
|
String response = m.insert(identifier,
|
229
|
new StringReader(testdocument), null);
|
230
|
debug("logoutAndInvalidInsert(): Response in logout="+response);
|
231
|
assertTrue(response.indexOf("<success>") == -1);
|
232
|
} catch (MetacatAuthException mae) {
|
233
|
fail("Authorization failed:\n" + mae.getMessage());
|
234
|
} catch (MetacatInaccessibleException mie) {
|
235
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
236
|
} catch (InsufficientKarmaException ike) {
|
237
|
fail("Insufficient karma:\n" + ike.getMessage());
|
238
|
} catch (MetacatException me) {
|
239
|
if(me.getMessage().
|
240
|
indexOf("Permission denied for user public inser") == -1){
|
241
|
fail("Metacat Error:\n" + me.getMessage());
|
242
|
}
|
243
|
} catch (Exception e) {
|
244
|
fail("General exception:\n" + e.getMessage());
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Test the read() function with a known document
|
250
|
*/
|
251
|
public void read()
|
252
|
{
|
253
|
debug("\nStarting read test...");
|
254
|
try {
|
255
|
m.login(username, password);
|
256
|
String docid = readIdFromFile(DOCID);
|
257
|
Reader r = new InputStreamReader(m.read(docid+".1"));
|
258
|
String doc = IOUtil.getAsString(r, true);
|
259
|
doc = doc +"\n";
|
260
|
if (!doc.equals(testdocument)) {
|
261
|
debug("doc :" + doc);
|
262
|
debug("testdocument:" + testdocument);
|
263
|
}
|
264
|
assertTrue(doc.equals(testdocument));
|
265
|
} catch (MetacatAuthException mae) {
|
266
|
fail("Authorization failed:\n" + mae.getMessage());
|
267
|
} catch (MetacatInaccessibleException mie) {
|
268
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
269
|
} catch (Exception e) {
|
270
|
fail("General exception:\n" + e.getMessage());
|
271
|
}
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* A user try to read a document which it doesn't have read permission
|
276
|
*/
|
277
|
public void invalidRead()
|
278
|
{
|
279
|
debug("\nStarting invalidRead test...");
|
280
|
try {
|
281
|
m.login(anotheruser, anotherpassword);
|
282
|
String docid = readIdFromFile(DOCID);
|
283
|
Reader r = new InputStreamReader(m.read(docid+".1"));
|
284
|
String doc = IOUtil.getAsString(r, true);
|
285
|
assertTrue(doc.indexOf("<error>") != -1);
|
286
|
debug("invalidRead(): doc="+ doc);
|
287
|
} catch (MetacatAuthException mae) {
|
288
|
fail("Authorization failed:\n" + mae.getMessage());
|
289
|
} catch (MetacatInaccessibleException mie) {
|
290
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
291
|
} catch (InsufficientKarmaException ike) {
|
292
|
assertTrue(1 == 1);
|
293
|
} catch (MetacatException e) {
|
294
|
fail("Metacat exception:\n" + e.getMessage());
|
295
|
} catch (IOException ioe) {
|
296
|
fail("IO exception:\n" + ioe.getMessage());
|
297
|
} catch (DocumentNotFoundException ne) {
|
298
|
fail("DocumentNotFound exception:\n" + ne.getMessage());
|
299
|
|
300
|
} catch(Exception e) {
|
301
|
fail("Exception:\n" + e.getMessage());
|
302
|
}
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* Test the query() function with a known document
|
307
|
*/
|
308
|
public void query()
|
309
|
{
|
310
|
debug("\nStarting query test...");
|
311
|
try {
|
312
|
m.login(username,password);
|
313
|
FileReader fr = new FileReader(queryFile);
|
314
|
Reader r = m.query(fr);
|
315
|
String result = IOUtil.getAsString(r, true);
|
316
|
debug("query(): Query result=\n" + result);
|
317
|
String docid = readIdFromFile(DOCID);
|
318
|
assertTrue(result.indexOf(docid+".1")!=-1);
|
319
|
assertTrue(result.indexOf("<?xml")!=-1);
|
320
|
} catch (MetacatAuthException mae) {
|
321
|
fail("Authorization failed:\n" + mae.getMessage());
|
322
|
} catch (MetacatInaccessibleException mie) {
|
323
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
324
|
} catch (Exception e) {
|
325
|
fail("General exception:\n" + e.getMessage());
|
326
|
}
|
327
|
}
|
328
|
/**
|
329
|
* Test the query() function with a known document
|
330
|
*/
|
331
|
public void queryWithQformat()
|
332
|
{
|
333
|
debug("\nStarting queryWithQformat test...");
|
334
|
try {
|
335
|
m.login(username,password);
|
336
|
FileReader fr = new FileReader(queryFile);
|
337
|
String qformat = "knb";
|
338
|
Reader r = m.query(fr, qformat);
|
339
|
String result = IOUtil.getAsString(r, true);
|
340
|
debug("queryWithQformat(): Query result=\n" + result);
|
341
|
String docid = readIdFromFile(DOCID);
|
342
|
assertTrue(result.indexOf(docid+".1")!=-1);
|
343
|
assertTrue(result.indexOf("<html>")!=-1);
|
344
|
} catch (MetacatAuthException mae) {
|
345
|
fail("Authorization failed:\n" + mae.getMessage());
|
346
|
} catch (MetacatInaccessibleException mie) {
|
347
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
348
|
} catch (Exception e) {
|
349
|
fail("General exception:\n" + e.getMessage());
|
350
|
}
|
351
|
}
|
352
|
|
353
|
/**
|
354
|
* Test the insert() function with a known document
|
355
|
*/
|
356
|
public void insert()
|
357
|
{
|
358
|
debug("\nStarting insert test...");
|
359
|
try {
|
360
|
String identifier = newdocid + ".1";
|
361
|
//write newdocid into file for persistance
|
362
|
writeIdToFile(DOCID, newdocid);
|
363
|
m.login(username, password);
|
364
|
String response = m.insert(identifier,
|
365
|
new StringReader(testdocument), null);
|
366
|
assertTrue(response.indexOf("<success>") != -1);
|
367
|
assertTrue(response.indexOf(identifier) != -1);
|
368
|
debug("insert(): response=" + response);
|
369
|
|
370
|
} catch (MetacatAuthException mae) {
|
371
|
fail("Authorization failed:\n" + mae.getMessage());
|
372
|
} catch (MetacatInaccessibleException mie) {
|
373
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
374
|
} catch (InsufficientKarmaException ike) {
|
375
|
assertTrue(1 == 1);
|
376
|
fail("Insufficient karma:\n" + ike.getMessage());
|
377
|
} catch (MetacatException me) {
|
378
|
fail("Metacat Error:\n" + me.getMessage());
|
379
|
} catch (Exception e) {
|
380
|
fail("General exception:\n" + e.getMessage());
|
381
|
}
|
382
|
}
|
383
|
|
384
|
/**
|
385
|
* Test the upload() function with a data file.
|
386
|
* 1. Insert version 1 successfully.
|
387
|
* 2. Update version 2 successfully
|
388
|
* 3. Update version 2 again and should be failed.
|
389
|
*/
|
390
|
public void upload()
|
391
|
{
|
392
|
debug("\nStarting upload test...");
|
393
|
try {
|
394
|
newdocid = generateDocid();
|
395
|
String identifier = newdocid + ".1";
|
396
|
writeIdToFile(DATAID, newdocid);
|
397
|
m.login(username, password);
|
398
|
String response = m.upload(identifier,
|
399
|
new File(onlinetestdatafile));
|
400
|
assertTrue(response.indexOf("<success>") != -1);
|
401
|
assertTrue(response.indexOf(identifier) != -1);
|
402
|
identifier = newdocid +".2";
|
403
|
response = m.upload(identifier,
|
404
|
new File(onlinetestdatafile));
|
405
|
assertTrue(response.indexOf("<success>") != -1);
|
406
|
assertTrue(response.indexOf(identifier) != -1);
|
407
|
debug("upload(): response=" + response);
|
408
|
//upload the same identifier again. it should return an error
|
409
|
try
|
410
|
{
|
411
|
response = m.upload(identifier,
|
412
|
new File(onlinetestdatafile));
|
413
|
fail("Metacat shouldn't successfully upload the same identifier twice "+response);
|
414
|
}
|
415
|
catch(Exception ee)
|
416
|
{
|
417
|
assertTrue(ee.getMessage().indexOf("<error>") != -1);
|
418
|
}
|
419
|
|
420
|
} catch (MetacatAuthException mae) {
|
421
|
fail("Authorization failed:\n" + mae.getMessage());
|
422
|
} catch (MetacatInaccessibleException mie) {
|
423
|
mie.printStackTrace();
|
424
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
425
|
} catch (InsufficientKarmaException ike) {
|
426
|
assertTrue(1 == 1);
|
427
|
fail("Insufficient karma:\n" + ike.getMessage());
|
428
|
} catch (MetacatException me) {
|
429
|
fail("Metacat Error:\n" + me.getMessage());
|
430
|
} catch (Exception e) {
|
431
|
fail("General exception:\n" + e.getMessage());
|
432
|
}
|
433
|
}
|
434
|
|
435
|
|
436
|
|
437
|
/**
|
438
|
* Test the upload() function by passing an InputStream
|
439
|
*/
|
440
|
public void upload_stream()
|
441
|
{
|
442
|
debug("\nStarting upload_stream test...");
|
443
|
try {
|
444
|
newdocid = generateDocid();
|
445
|
String identifier = newdocid + ".1";
|
446
|
m.login(username, password);
|
447
|
File testFile = new File(onlinetestdatafile);
|
448
|
String response = m.upload(identifier, "onlineDataFile1",
|
449
|
new FileInputStream(testFile),
|
450
|
(int) testFile.length());
|
451
|
|
452
|
assertTrue(response.indexOf("<success>") != -1);
|
453
|
assertTrue(response.indexOf(identifier) != -1);
|
454
|
identifier = newdocid + ".2";
|
455
|
response = m.upload(identifier, "onlineDataFile1",
|
456
|
new FileInputStream(testFile),
|
457
|
(int) testFile.length());
|
458
|
|
459
|
assertTrue(response.indexOf("<success>") != -1);
|
460
|
assertTrue(response.indexOf(identifier) != -1);
|
461
|
debug("upload_stream(): response=" + response);
|
462
|
|
463
|
} catch (MetacatAuthException mae) {
|
464
|
fail("Authorization failed:\n" + mae.getMessage());
|
465
|
} catch (MetacatInaccessibleException mie) {
|
466
|
mie.printStackTrace();
|
467
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
468
|
} catch (InsufficientKarmaException ike) {
|
469
|
assertTrue(1 == 1);
|
470
|
fail("Insufficient karma:\n" + ike.getMessage());
|
471
|
} catch (MetacatException me) {
|
472
|
fail("Metacat Error:\n" + me.getMessage());
|
473
|
} catch (Exception e) {
|
474
|
fail("General exception:\n" + e.getMessage());
|
475
|
}
|
476
|
}
|
477
|
|
478
|
/**
|
479
|
* Test the invalidUpdate() function. A user try to update a document
|
480
|
* which it doesn't have permission
|
481
|
*/
|
482
|
public void invalidUpdate()
|
483
|
{
|
484
|
debug("\nStarting invalidUpdate test...");
|
485
|
try {
|
486
|
String docid = readIdFromFile(DOCID);
|
487
|
String identifier = docid + ".2";
|
488
|
m.login(anotheruser, anotherpassword);
|
489
|
String response = m.update(identifier,
|
490
|
new StringReader(testdocument), null);
|
491
|
assertTrue(response.indexOf("<success>") == -1);
|
492
|
|
493
|
} catch (MetacatAuthException mae) {
|
494
|
fail("Authorization failed:\n" + mae.getMessage());
|
495
|
} catch (MetacatInaccessibleException mie) {
|
496
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
497
|
} catch (InsufficientKarmaException ike) {
|
498
|
assertTrue(1 == 1);
|
499
|
} catch (MetacatException me) {
|
500
|
fail("Metacat Error:\n" + me.getMessage());
|
501
|
} catch (Exception e) {
|
502
|
fail("General exception:\n" + e.getMessage());
|
503
|
}
|
504
|
}
|
505
|
|
506
|
/**
|
507
|
* Test the update() function with a known document
|
508
|
*/
|
509
|
public void update()
|
510
|
{
|
511
|
debug("\nStarting update test...");
|
512
|
try {
|
513
|
String docid = readIdFromFile(DOCID);
|
514
|
String identifier = docid + ".2";
|
515
|
m.login(username, password);
|
516
|
String response = m.update(identifier,
|
517
|
new StringReader(testdocument), null);
|
518
|
assertTrue(response.indexOf("<success>") != -1);
|
519
|
assertTrue(response.indexOf(identifier) != -1);
|
520
|
debug("update(): response=" + response);
|
521
|
|
522
|
} catch (MetacatAuthException mae) {
|
523
|
fail("Authorization failed:\n" + mae.getMessage());
|
524
|
} catch (MetacatInaccessibleException mie) {
|
525
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
526
|
} catch (InsufficientKarmaException ike) {
|
527
|
fail("Insufficient karma:\n" + ike.getMessage());
|
528
|
} catch (MetacatException me) {
|
529
|
fail("Metacat Error:\n" + me.getMessage());
|
530
|
} catch (Exception e) {
|
531
|
fail("General exception:\n" + e.getMessage());
|
532
|
}
|
533
|
}
|
534
|
|
535
|
/**
|
536
|
* A user try delete a document which it doesn't have permission
|
537
|
*/
|
538
|
public void invalidDelete()
|
539
|
{
|
540
|
debug("\nStarting invalidDelete test...");
|
541
|
try {
|
542
|
String identifier = newdocid + ".2";
|
543
|
m.login(anotheruser, anotherpassword);
|
544
|
String response = m.delete(identifier);
|
545
|
assertTrue(response.indexOf("<success>") == -1);
|
546
|
debug("invalidDelete(): response=" + response);
|
547
|
|
548
|
} catch (MetacatAuthException mae) {
|
549
|
fail("Authorization failed:\n" + mae.getMessage());
|
550
|
} catch (MetacatInaccessibleException mie) {
|
551
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
552
|
} catch (InsufficientKarmaException ike) {
|
553
|
assertTrue(1 == 1);
|
554
|
} catch (MetacatException me) {
|
555
|
assertTrue(1 == 1);
|
556
|
} catch (Exception e) {
|
557
|
fail("General exception:\n" + e.getMessage());
|
558
|
}
|
559
|
}
|
560
|
|
561
|
/**
|
562
|
* Test the delete() function with a known document
|
563
|
*/
|
564
|
public void delete()
|
565
|
{
|
566
|
debug("\nStarting delete test...");
|
567
|
try {
|
568
|
Thread.sleep(10000);
|
569
|
String docid = readIdFromFile(DOCID);
|
570
|
String identifier = docid + ".2";
|
571
|
m.login(username, password);
|
572
|
String response = m.delete(identifier);
|
573
|
assertTrue(response.indexOf("<success>") != -1);
|
574
|
// delete the docid persistence file.
|
575
|
deleteFile(DOCID);
|
576
|
deleteFile(DATAID);
|
577
|
debug("delete(): response=" + response);
|
578
|
|
579
|
} catch (MetacatAuthException mae) {
|
580
|
fail("Authorization failed:\n" + mae.getMessage());
|
581
|
} catch (MetacatInaccessibleException mie) {
|
582
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
583
|
} catch (InsufficientKarmaException ike) {
|
584
|
fail("Insufficient karma:\n" + ike.getMessage());
|
585
|
} catch (MetacatException me) {
|
586
|
fail("Metacat Error:\n" + me.getMessage());
|
587
|
} catch (Exception e) {
|
588
|
fail("General exception:\n" + e.getMessage());
|
589
|
}
|
590
|
}
|
591
|
|
592
|
/**
|
593
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
594
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
595
|
* exception, this is right.
|
596
|
*/
|
597
|
public void inaccessibleMetacat()
|
598
|
{
|
599
|
debug("\nStarting inaccessibleMetacat test...");
|
600
|
Metacat mWrong = null;
|
601
|
try {
|
602
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
603
|
} catch (MetacatInaccessibleException mie) {
|
604
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
605
|
}
|
606
|
|
607
|
try {
|
608
|
mWrong.login(username, password);
|
609
|
} catch (MetacatInaccessibleException mie) {
|
610
|
assertTrue(1 == 1);
|
611
|
} catch (MetacatAuthException mae) {
|
612
|
fail("Authorization failed:\n" + mae.getMessage());
|
613
|
}
|
614
|
}
|
615
|
|
616
|
/**
|
617
|
* Try to perform an action that requires a session to be valid without
|
618
|
* having logged in first by setting the sessionId from a previous
|
619
|
* session. Then insert a document.
|
620
|
*/
|
621
|
public void reuseSession()
|
622
|
{
|
623
|
debug("\nStarting reuseSession test...");
|
624
|
String oldSessionId = "";
|
625
|
try {
|
626
|
debug("creating metacat connection to: " + metacatUrl);
|
627
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
628
|
debug("creating metacat connection with credentials: " + username + ":" + password);
|
629
|
String response = mtemp.login(username, password);
|
630
|
oldSessionId = mtemp.getSessionId();
|
631
|
debug("preparing to reuse session with oldSessionId:" + oldSessionId);
|
632
|
} catch (MetacatAuthException mae) {
|
633
|
fail("Authorization failed:\n" + mae.getMessage());
|
634
|
} catch (MetacatInaccessibleException mie) {
|
635
|
System.err.println("Metacat is: " + metacatUrl);
|
636
|
fail("Metacat connection failed." + mie.getMessage());
|
637
|
}
|
638
|
|
639
|
try {
|
640
|
String identifier = generateDocid() + ".1";
|
641
|
Metacat m2 = null;
|
642
|
try {
|
643
|
debug("Creating second connection and logging out of it.");
|
644
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
645
|
m2.login(username, password);
|
646
|
m2.logout();
|
647
|
} catch (MetacatInaccessibleException mie) {
|
648
|
System.err.println("Could not connect to metacat at: " + metacatUrl
|
649
|
+ " : " + mie.getMessage());
|
650
|
fail("Metacat connection failed." + mie.getMessage());
|
651
|
}
|
652
|
m2.setSessionId(oldSessionId);
|
653
|
debug("Reusing second session with session id :" + m2.getSessionId());
|
654
|
String response = m2.insert(identifier,
|
655
|
new StringReader(testdocument), null);
|
656
|
debug("Reuse second session insert response: " + response);
|
657
|
assertTrue(response.indexOf("<success>") != -1);
|
658
|
} catch (MetacatInaccessibleException mie) {
|
659
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
660
|
} catch (InsufficientKarmaException ike) {
|
661
|
fail("Insufficient karma:\n" + ike.getMessage());
|
662
|
} catch (MetacatException me) {
|
663
|
fail("Metacat Error:\n" + me.getMessage());
|
664
|
} catch (Exception e) {
|
665
|
fail("General exception:\n" + e.getMessage());
|
666
|
}
|
667
|
}
|
668
|
|
669
|
/**
|
670
|
* Try to perform an action that requires a session to be valid without
|
671
|
* having logged in first, but use an invalid session identifier.
|
672
|
* Then insert a document, which should fail.
|
673
|
*/
|
674
|
public void reuseInvalidSession()
|
675
|
{
|
676
|
debug("\nStarting resuseInvalidSession test...");
|
677
|
String oldSessionId = "foobar";
|
678
|
try {
|
679
|
String identifier = generateDocid() + ".1";
|
680
|
Metacat m3 = null;
|
681
|
try {
|
682
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
683
|
m3.logout();
|
684
|
} catch (MetacatInaccessibleException mie) {
|
685
|
System.err.println("Metacat is: " + metacatUrl);
|
686
|
fail("Metacat connection failed." + mie.getMessage());
|
687
|
}
|
688
|
debug("reuseInvalidSession(): SessionId (m3): " + m3.getSessionId());
|
689
|
m3.setSessionId(oldSessionId);
|
690
|
debug("reuseInvalidSession(): SessionId(m3 after set)=" + m3.getSessionId());
|
691
|
debug("reuseInvalidSession(): identifier=" + identifier);
|
692
|
String response = m3.insert(identifier,
|
693
|
new StringReader(testdocument), null);
|
694
|
debug("reuseInvalidSession(): response=" + response);
|
695
|
assertTrue(response.indexOf("<success>") == -1);
|
696
|
} catch (MetacatInaccessibleException mie) {
|
697
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
698
|
} catch (InsufficientKarmaException ike) {
|
699
|
fail("Insufficient karma:\n" + ike.getMessage());
|
700
|
} catch (MetacatException me) {
|
701
|
if(me.getMessage().
|
702
|
indexOf("Permission denied for user public inser") == -1){
|
703
|
fail("Metacat Error:\n" + me.getMessage());
|
704
|
}
|
705
|
} catch (Exception e) {
|
706
|
fail("General exception:\n" + e.getMessage());
|
707
|
}
|
708
|
}
|
709
|
|
710
|
/**
|
711
|
* Get the most recent document id for a given scope and be sure
|
712
|
* that it matches the one we last inserted. Assumes this test is run
|
713
|
* immediately following a successful insert() test.
|
714
|
*/
|
715
|
public void getLastDocid()
|
716
|
{
|
717
|
debug("\nStarting getLastDocid test...");
|
718
|
try {
|
719
|
Metacat m3 = null;
|
720
|
try {
|
721
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
722
|
} catch (MetacatInaccessibleException mie) {
|
723
|
System.err.println("Metacat is: " + metacatUrl);
|
724
|
fail("Metacat connection failed." + mie.getMessage());
|
725
|
}
|
726
|
String lastId = m3.getLastDocid(prefix);
|
727
|
debug("getLastDocid(): Last Id = " + lastId);
|
728
|
|
729
|
//get docid from file
|
730
|
String docid = readIdFromFile(DOCID);
|
731
|
debug("getLastDocid(): File Id = " + docid + ".1");
|
732
|
assertTrue(lastId.equals(docid + ".1"));
|
733
|
} catch (MetacatException me) {
|
734
|
fail("Metacat Error:\n" + me.getMessage());
|
735
|
} catch (Exception e) {
|
736
|
fail("General exception:\n" + e.getMessage());
|
737
|
}
|
738
|
}
|
739
|
|
740
|
/**
|
741
|
* Try to perform an action that requires a session to be valid without
|
742
|
* having logged in first, but use an invalid session identifier.
|
743
|
* Then insert a document, which should fail.
|
744
|
*/
|
745
|
public void getNewestDocRevision()
|
746
|
{
|
747
|
debug("\nStarting getNewestDocRevision test...");
|
748
|
try {
|
749
|
|
750
|
Metacat m3 = null;
|
751
|
try {
|
752
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
753
|
} catch (MetacatInaccessibleException mie) {
|
754
|
System.err.println("Metacat is: " + metacatUrl);
|
755
|
fail("Metacat connection failed." + mie.getMessage());
|
756
|
}
|
757
|
// get docid from file
|
758
|
String docid = readIdFromFile(DOCID);
|
759
|
int revision = m3.getNewestDocRevision(docid);
|
760
|
debug("getNewestDocRevision(): revision=" + revision);
|
761
|
assertTrue(revision == 1);
|
762
|
} catch (MetacatException me) {
|
763
|
if(me.getMessage().
|
764
|
indexOf("Permission denied for user public inser") == -1){
|
765
|
fail("Metacat Error:\n" + me.getMessage());
|
766
|
}
|
767
|
} catch (Exception e) {
|
768
|
fail("General exception:\n" + e.getMessage());
|
769
|
}
|
770
|
}
|
771
|
|
772
|
/**
|
773
|
* Try to insert a bunch of eml documents which contain spatial information
|
774
|
* This test is used to try to find a bug in spatial part of metacat
|
775
|
*/
|
776
|
public void insertSpatialDocs() throws IOException
|
777
|
{
|
778
|
debug("\nStarting insertSpatialDocs test...");
|
779
|
|
780
|
FileReader fr = new FileReader(spatialTestFile);
|
781
|
String spatialtestdocument = IOUtil.getAsString(fr, true);
|
782
|
debug("insertSpatialDocs(): the eml is "+spatialtestdocument);
|
783
|
for (int i=0; i<TIME; i++)
|
784
|
{
|
785
|
try {
|
786
|
Thread.sleep(20000);
|
787
|
newdocid = generateDocid();
|
788
|
String identifier = newdocid + ".1";
|
789
|
debug("insertSpatialDocs(): the docid is "+identifier);
|
790
|
m.login(username, password);
|
791
|
String response = m.insert(identifier,
|
792
|
new StringReader(spatialtestdocument), null);
|
793
|
assertTrue(response.indexOf("<success>") != -1);
|
794
|
assertTrue(response.indexOf(identifier) != -1);
|
795
|
Thread.sleep(20000);
|
796
|
identifier = newdocid +".2";
|
797
|
response = m.update(identifier,
|
798
|
new StringReader(spatialtestdocument), null);
|
799
|
assertTrue(response.indexOf("<success>") != -1);
|
800
|
Thread.sleep(20000);
|
801
|
String response2 = m.delete(identifier);
|
802
|
assertTrue(response2.indexOf("<success>") != -1);
|
803
|
debug("insertSpatialDocs(): response=" + response);
|
804
|
|
805
|
} catch (MetacatAuthException mae) {
|
806
|
fail("Authorization failed:\n" + mae.getMessage());
|
807
|
} catch (MetacatInaccessibleException mie) {
|
808
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
809
|
} catch (InsufficientKarmaException ike) {
|
810
|
assertTrue(1 == 1);
|
811
|
fail("Insufficient karma:\n" + ike.getMessage());
|
812
|
} catch (MetacatException me) {
|
813
|
fail("Metacat Error:\n" + me.getMessage());
|
814
|
} catch (Exception e) {
|
815
|
fail("General exception:\n" + e.getMessage());
|
816
|
}
|
817
|
}
|
818
|
}
|
819
|
|
820
|
/**
|
821
|
* Create a hopefully unique docid for testing insert and update. Does
|
822
|
* not include the 'revision' part of the id.
|
823
|
*
|
824
|
* @return a String docid based on the current date and time
|
825
|
*/
|
826
|
private String generateDocid()
|
827
|
{
|
828
|
StringBuffer docid = new StringBuffer(prefix);
|
829
|
docid.append(".");
|
830
|
|
831
|
// Create a calendar to get the date formatted properly
|
832
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
833
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
834
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
835
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
836
|
Calendar calendar = new GregorianCalendar(pdt);
|
837
|
Date trialTime = new Date();
|
838
|
calendar.setTime(trialTime);
|
839
|
|
840
|
int time = 0;
|
841
|
|
842
|
docid.append(calendar.get(Calendar.YEAR));
|
843
|
|
844
|
time = calendar.get(Calendar.DAY_OF_YEAR);
|
845
|
if(time < 10){
|
846
|
docid.append("0");
|
847
|
docid.append("0");
|
848
|
docid.append(time);
|
849
|
} else if(time < 100) {
|
850
|
docid.append("0");
|
851
|
docid.append(time);
|
852
|
} else {
|
853
|
docid.append(time);
|
854
|
}
|
855
|
|
856
|
time = calendar.get(Calendar.HOUR_OF_DAY);
|
857
|
if(time < 10){
|
858
|
docid.append("0");
|
859
|
docid.append(time);
|
860
|
} else {
|
861
|
docid.append(time);
|
862
|
}
|
863
|
|
864
|
time = calendar.get(Calendar.MINUTE);
|
865
|
if(time < 10){
|
866
|
docid.append("0");
|
867
|
docid.append(time);
|
868
|
} else {
|
869
|
docid.append(time);
|
870
|
}
|
871
|
|
872
|
time = calendar.get(Calendar.SECOND);
|
873
|
if(time < 10){
|
874
|
docid.append("0");
|
875
|
docid.append(time);
|
876
|
} else {
|
877
|
docid.append(time);
|
878
|
}
|
879
|
|
880
|
//sometimes this number is not unique, so we append a random number
|
881
|
int random = (new Double(Math.random()*100)).intValue();
|
882
|
docid.append(random);
|
883
|
|
884
|
return docid.toString();
|
885
|
}
|
886
|
|
887
|
/*
|
888
|
* Write id to a file for persistance
|
889
|
*/
|
890
|
private void writeIdToFile(String fileName, String id) throws Exception
|
891
|
{
|
892
|
File file = new File(fileName);
|
893
|
StringReader reader = new StringReader(id);
|
894
|
FileWriter writer = new FileWriter(file);
|
895
|
char [] buffer = new char[1024];
|
896
|
int c = reader.read(buffer);
|
897
|
while (c != -1)
|
898
|
{
|
899
|
writer.write(buffer, 0, c);
|
900
|
c = reader.read(buffer);
|
901
|
}
|
902
|
writer.close();
|
903
|
reader.close();
|
904
|
}
|
905
|
|
906
|
/*
|
907
|
* Read id from a given file
|
908
|
*/
|
909
|
private String readIdFromFile(String fileName) throws Exception
|
910
|
{
|
911
|
File file = new File(fileName);
|
912
|
FileReader reader = new FileReader(file);
|
913
|
StringWriter writer = new StringWriter();
|
914
|
char [] buffer = new char[1024];
|
915
|
int c = reader.read(buffer);
|
916
|
while (c != -1)
|
917
|
{
|
918
|
writer.write(buffer, 0, c);
|
919
|
c = reader.read(buffer);
|
920
|
}
|
921
|
reader.close();
|
922
|
return writer.toString();
|
923
|
}
|
924
|
|
925
|
/*
|
926
|
* Delete the given file
|
927
|
*/
|
928
|
private void deleteFile(String fileName) throws Exception
|
929
|
{
|
930
|
File file = new File(fileName);
|
931
|
file.delete();
|
932
|
}
|
933
|
}
|