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