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: jones $'
|
8
|
* '$Date: 2003-09-22 15:26:51 -0700 (Mon, 22 Sep 2003) $'
|
9
|
* '$Revision: 1829 $'
|
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 edu.ucsb.nceas.metacat.client.*;
|
29
|
import edu.ucsb.nceas.utilities.HttpMessage;
|
30
|
import edu.ucsb.nceas.utilities.IOUtil;
|
31
|
|
32
|
import java.io.FileReader;
|
33
|
import java.io.InputStreamReader;
|
34
|
import java.io.IOException;
|
35
|
import java.io.Reader;
|
36
|
import java.io.StringReader;
|
37
|
import java.io.StringWriter;
|
38
|
import java.text.DateFormat;
|
39
|
import java.util.Calendar;
|
40
|
import java.util.GregorianCalendar;
|
41
|
import java.util.Date;
|
42
|
import java.util.SimpleTimeZone;
|
43
|
import java.util.TimeZone;
|
44
|
|
45
|
import junit.framework.Test;
|
46
|
import junit.framework.TestCase;
|
47
|
import junit.framework.TestResult;
|
48
|
import junit.framework.TestSuite;
|
49
|
|
50
|
/**
|
51
|
* A JUnit test for testing Step class processing
|
52
|
*/
|
53
|
public class MetacatClientTest extends TestCase
|
54
|
{
|
55
|
private String metacatUrl = "@systemidserver@@servlet-path@";
|
56
|
private String wrongMetacatUrl=
|
57
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
58
|
private String username = "@mcuser@";
|
59
|
private String password = "@mcpassword@";
|
60
|
private String anotheruser = "@mcanotheruser@";
|
61
|
private String anotherpassword = "@mcanotherpassword@";
|
62
|
private String failpass = "uidfnkj43987yfdn";
|
63
|
private String prefix = "test";
|
64
|
private String newdocid = null;
|
65
|
private String testfile = "test/jones.204.22.xml";
|
66
|
private String queryFile = "test/query.xml";
|
67
|
private String testdocument = "";
|
68
|
private Metacat m;
|
69
|
|
70
|
/**
|
71
|
* Constructor to build the test
|
72
|
*
|
73
|
* @param name the name of the test method
|
74
|
*/
|
75
|
public MetacatClientTest(String name)
|
76
|
{
|
77
|
super(name);
|
78
|
newdocid = generateDocid();
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Establish a testing framework by initializing appropriate objects
|
83
|
*/
|
84
|
public void setUp()
|
85
|
{
|
86
|
try {
|
87
|
FileReader fr = new FileReader(testfile);
|
88
|
testdocument = IOUtil.getAsString(fr, true);
|
89
|
} catch (IOException ioe) {
|
90
|
fail("Can't read test data to run the test: " + testfile);
|
91
|
}
|
92
|
|
93
|
try {
|
94
|
System.err.println("Test Metacat: " + metacatUrl);
|
95
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
96
|
} catch (MetacatInaccessibleException mie) {
|
97
|
System.err.println("Metacat is: " + metacatUrl);
|
98
|
fail("Metacat connection failed." + mie.getMessage());
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Release any objects after tests are complete
|
104
|
*/
|
105
|
public void tearDown()
|
106
|
{
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Create a suite of tests to be run together
|
111
|
*/
|
112
|
public static Test suite()
|
113
|
{
|
114
|
TestSuite suite = new TestSuite();
|
115
|
suite.addTest(new MetacatClientTest("initialize"));
|
116
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
117
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
118
|
suite.addTest(new MetacatClientTest("login"));
|
119
|
suite.addTest(new MetacatClientTest("insert"));
|
120
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
121
|
suite.addTest(new MetacatClientTest("read"));
|
122
|
suite.addTest(new MetacatClientTest("query"));
|
123
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
124
|
suite.addTest(new MetacatClientTest("update"));
|
125
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
126
|
suite.addTest(new MetacatClientTest("delete"));
|
127
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
128
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
129
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
130
|
return suite;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Run an initial test that always passes to check that the test
|
135
|
* harness is working.
|
136
|
*/
|
137
|
public void initialize()
|
138
|
{
|
139
|
assertTrue(1 == 1);
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Test the login() function with valid credentials
|
144
|
*/
|
145
|
public void login()
|
146
|
{
|
147
|
// Try a valid login
|
148
|
try {
|
149
|
String response = m.login(username, password);
|
150
|
System.err.println("Login response: " + response);
|
151
|
assertTrue(response != null);
|
152
|
assertTrue(response.indexOf("<login>") != -1);
|
153
|
String sessionId = m.getSessionId();
|
154
|
System.err.println("Session ID: " + m.getSessionId());
|
155
|
assertTrue(sessionId != null);
|
156
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
157
|
} catch (MetacatAuthException mae) {
|
158
|
fail("Authorization failed:\n" + mae.getMessage());
|
159
|
} catch (MetacatInaccessibleException mie) {
|
160
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Test the login() function with INVALID credentials
|
166
|
*/
|
167
|
public void invalidLogin()
|
168
|
{
|
169
|
// Try an invalid login
|
170
|
try {
|
171
|
m.login(username, failpass);
|
172
|
fail("Authorization should have failed.");
|
173
|
} catch (MetacatAuthException mae) {
|
174
|
assertTrue(1 == 1);
|
175
|
} catch (MetacatInaccessibleException mie) {
|
176
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
177
|
}
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Test the logout() function. When logout, user will be public, it couldn't
|
182
|
* insert a document.
|
183
|
*/
|
184
|
public void logoutAndInvalidInsert()
|
185
|
{
|
186
|
try {
|
187
|
String identifier = newdocid + ".1";
|
188
|
m.login(username, password);
|
189
|
m.logout();
|
190
|
String response = m.insert(identifier,
|
191
|
new StringReader(testdocument), null);
|
192
|
System.err.println("Response in logout: "+response);
|
193
|
assertTrue(response.indexOf("<success>") == -1);
|
194
|
} catch (MetacatAuthException mae) {
|
195
|
fail("Authorization failed:\n" + mae.getMessage());
|
196
|
} catch (MetacatInaccessibleException mie) {
|
197
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
198
|
} catch (InsufficientKarmaException ike) {
|
199
|
fail("Insufficient karma:\n" + ike.getMessage());
|
200
|
} catch (MetacatException me) {
|
201
|
fail("Metacat Error:\n" + me.getMessage());
|
202
|
} catch (Exception e) {
|
203
|
fail("General exception:\n" + e.getMessage());
|
204
|
}
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Test the read() function with a known document
|
209
|
*/
|
210
|
public void read()
|
211
|
{
|
212
|
try {
|
213
|
m.login(username, password);
|
214
|
Reader r = m.read(newdocid+".1");
|
215
|
String doc = IOUtil.getAsString(r, true);
|
216
|
doc = doc +"\n";
|
217
|
System.err.println(doc);
|
218
|
assertTrue(doc.equals(testdocument));
|
219
|
} catch (MetacatAuthException mae) {
|
220
|
fail("Authorization failed:\n" + mae.getMessage());
|
221
|
} catch (MetacatInaccessibleException mie) {
|
222
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
223
|
} catch (Exception e) {
|
224
|
fail("General exception:\n" + e.getMessage());
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* A user try to read a document which it doesn't have read permission
|
230
|
*/
|
231
|
public void invalidRead()
|
232
|
{
|
233
|
try {
|
234
|
m.login(anotheruser, anotherpassword);
|
235
|
Reader r = m.read(newdocid+".1");
|
236
|
String doc = IOUtil.getAsString(r, true);
|
237
|
assertTrue(doc.indexOf("<error>") != -1);
|
238
|
System.err.println(doc);
|
239
|
} catch (MetacatAuthException mae) {
|
240
|
fail("Authorization failed:\n" + mae.getMessage());
|
241
|
} catch (MetacatInaccessibleException mie) {
|
242
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
243
|
} catch (InsufficientKarmaException ike) {
|
244
|
assertTrue(1 == 1);
|
245
|
} catch (MetacatException e) {
|
246
|
fail("Metacat exception:\n" + e.getMessage());
|
247
|
} catch (IOException ioe) {
|
248
|
fail("IO exception:\n" + ioe.getMessage());
|
249
|
}
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Test the query() function with a known document
|
254
|
*/
|
255
|
public void query()
|
256
|
{
|
257
|
try {
|
258
|
m.login(username,password);
|
259
|
FileReader fr = new FileReader(queryFile);
|
260
|
Reader r = m.query(fr);
|
261
|
System.err.println("Starting query...");
|
262
|
String result = IOUtil.getAsString(r, true);
|
263
|
System.err.println("Query result:\n" + result);
|
264
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
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
|
* Test the insert() function with a known document
|
276
|
*/
|
277
|
public void insert()
|
278
|
{
|
279
|
try {
|
280
|
String identifier = newdocid + ".1";
|
281
|
m.login(username, password);
|
282
|
String response = m.insert(identifier,
|
283
|
new StringReader(testdocument), null);
|
284
|
assertTrue(response.indexOf("<success>") != -1);
|
285
|
assertTrue(response.indexOf(identifier) != -1);
|
286
|
System.err.println(response);
|
287
|
|
288
|
} catch (MetacatAuthException mae) {
|
289
|
fail("Authorization failed:\n" + mae.getMessage());
|
290
|
} catch (MetacatInaccessibleException mie) {
|
291
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
292
|
} catch (InsufficientKarmaException ike) {
|
293
|
assertTrue(1 == 1);
|
294
|
fail("Insufficient karma:\n" + ike.getMessage());
|
295
|
} catch (MetacatException me) {
|
296
|
fail("Metacat Error:\n" + me.getMessage());
|
297
|
} catch (Exception e) {
|
298
|
fail("General exception:\n" + e.getMessage());
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Test the invalidUpdate() function. A user try to update a document
|
304
|
* which it doesn't have permission
|
305
|
*/
|
306
|
public void invalidUpdate()
|
307
|
{
|
308
|
try {
|
309
|
String identifier = newdocid + ".2";
|
310
|
m.login(anotheruser, anotherpassword);
|
311
|
String response = m.update(identifier,
|
312
|
new StringReader(testdocument), null);
|
313
|
assertTrue(response.indexOf("<success>") == -1);
|
314
|
|
315
|
} catch (MetacatAuthException mae) {
|
316
|
fail("Authorization failed:\n" + mae.getMessage());
|
317
|
} catch (MetacatInaccessibleException mie) {
|
318
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
319
|
} catch (InsufficientKarmaException ike) {
|
320
|
assertTrue(1 == 1);
|
321
|
} catch (MetacatException me) {
|
322
|
fail("Metacat Error:\n" + me.getMessage());
|
323
|
} catch (Exception e) {
|
324
|
fail("General exception:\n" + e.getMessage());
|
325
|
}
|
326
|
}
|
327
|
|
328
|
/**
|
329
|
* Test the update() function with a known document
|
330
|
*/
|
331
|
public void update()
|
332
|
{
|
333
|
try {
|
334
|
String identifier = newdocid + ".2";
|
335
|
m.login(username, password);
|
336
|
String response = m.update(identifier,
|
337
|
new StringReader(testdocument), null);
|
338
|
assertTrue(response.indexOf("<success>") != -1);
|
339
|
assertTrue(response.indexOf(identifier) != -1);
|
340
|
System.err.println(response);
|
341
|
|
342
|
} catch (MetacatAuthException mae) {
|
343
|
fail("Authorization failed:\n" + mae.getMessage());
|
344
|
} catch (MetacatInaccessibleException mie) {
|
345
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
346
|
} catch (InsufficientKarmaException ike) {
|
347
|
fail("Insufficient karma:\n" + ike.getMessage());
|
348
|
} catch (MetacatException me) {
|
349
|
fail("Metacat Error:\n" + me.getMessage());
|
350
|
} catch (Exception e) {
|
351
|
fail("General exception:\n" + e.getMessage());
|
352
|
}
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* A user try delete a document which it doesn't have permission
|
357
|
*/
|
358
|
public void invalidDelete()
|
359
|
{
|
360
|
try {
|
361
|
String identifier = newdocid + ".2";
|
362
|
m.login(anotheruser, anotherpassword);
|
363
|
String response = m.delete(identifier);
|
364
|
assertTrue(response.indexOf("<success>") == -1);
|
365
|
System.err.println(response);
|
366
|
|
367
|
} catch (MetacatAuthException mae) {
|
368
|
fail("Authorization failed:\n" + mae.getMessage());
|
369
|
} catch (MetacatInaccessibleException mie) {
|
370
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
371
|
} catch (InsufficientKarmaException ike) {
|
372
|
assertTrue(1 == 1);
|
373
|
} catch (MetacatException me) {
|
374
|
assertTrue(1 == 1);
|
375
|
} catch (Exception e) {
|
376
|
fail("General exception:\n" + e.getMessage());
|
377
|
}
|
378
|
}
|
379
|
|
380
|
/**
|
381
|
* Test the delete() function with a known document
|
382
|
*/
|
383
|
public void delete()
|
384
|
{
|
385
|
try {
|
386
|
String identifier = newdocid + ".2";
|
387
|
m.login(username, password);
|
388
|
String response = m.delete(identifier);
|
389
|
assertTrue(response.indexOf("<success>") != -1);
|
390
|
System.err.println(response);
|
391
|
|
392
|
} catch (MetacatAuthException mae) {
|
393
|
fail("Authorization failed:\n" + mae.getMessage());
|
394
|
} catch (MetacatInaccessibleException mie) {
|
395
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
396
|
} catch (InsufficientKarmaException ike) {
|
397
|
fail("Insufficient karma:\n" + ike.getMessage());
|
398
|
} catch (MetacatException me) {
|
399
|
fail("Metacat Error:\n" + me.getMessage());
|
400
|
} catch (Exception e) {
|
401
|
fail("General exception:\n" + e.getMessage());
|
402
|
}
|
403
|
}
|
404
|
|
405
|
/**
|
406
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
407
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
408
|
* exception, this is right.
|
409
|
*/
|
410
|
public void inaccessibleMetacat()
|
411
|
{
|
412
|
Metacat mWrong = null;
|
413
|
try {
|
414
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
415
|
} catch (MetacatInaccessibleException mie) {
|
416
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
417
|
}
|
418
|
|
419
|
try {
|
420
|
mWrong.login(username, password);
|
421
|
} catch (MetacatInaccessibleException mie) {
|
422
|
assertTrue(1 == 1);
|
423
|
} catch (MetacatAuthException mae) {
|
424
|
fail("Authorization failed:\n" + mae.getMessage());
|
425
|
}
|
426
|
}
|
427
|
|
428
|
/**
|
429
|
* Try to perform an action that requires a session to be valid without
|
430
|
* having logged in first by setting the sessionId from a previous
|
431
|
* session. Then insert a document.
|
432
|
*/
|
433
|
public void reuseSession()
|
434
|
{
|
435
|
String oldSessionId = "";
|
436
|
try {
|
437
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
438
|
String response = mtemp.login(username, password);
|
439
|
oldSessionId = mtemp.getSessionId();
|
440
|
System.err.println("SessionId (mtemp): " + oldSessionId);
|
441
|
} catch (MetacatAuthException mae) {
|
442
|
fail("Authorization failed:\n" + mae.getMessage());
|
443
|
} catch (MetacatInaccessibleException mie) {
|
444
|
System.err.println("Metacat is: " + metacatUrl);
|
445
|
fail("Metacat connection failed." + mie.getMessage());
|
446
|
}
|
447
|
|
448
|
try {
|
449
|
String identifier = generateDocid() + ".1";
|
450
|
Metacat m2 = null;
|
451
|
try {
|
452
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
453
|
} catch (MetacatInaccessibleException mie) {
|
454
|
System.err.println("Metacat is: " + metacatUrl);
|
455
|
fail("Metacat connection failed." + mie.getMessage());
|
456
|
}
|
457
|
System.err.println("SessionId (m2): " + m2.getSessionId());
|
458
|
m2.setSessionId(oldSessionId);
|
459
|
System.err.println("SessionId (m2 after set): " + m2.getSessionId());
|
460
|
String response = m2.insert(identifier,
|
461
|
new StringReader(testdocument), null);
|
462
|
System.err.println("Reuse Insert response: " + response);
|
463
|
assertTrue(response.indexOf("<success>") != -1);
|
464
|
} catch (MetacatInaccessibleException mie) {
|
465
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
466
|
} catch (InsufficientKarmaException ike) {
|
467
|
fail("Insufficient karma:\n" + ike.getMessage());
|
468
|
} catch (MetacatException me) {
|
469
|
fail("Metacat Error:\n" + me.getMessage());
|
470
|
} catch (Exception e) {
|
471
|
fail("General exception:\n" + e.getMessage());
|
472
|
}
|
473
|
}
|
474
|
|
475
|
/**
|
476
|
* Try to perform an action that requires a session to be valid without
|
477
|
* having logged in first, but use an invalid session identifier.
|
478
|
* Then insert a document, which should fail.
|
479
|
*/
|
480
|
public void reuseInvalidSession()
|
481
|
{
|
482
|
System.err.println("Starting resuseInvalidSession test...");
|
483
|
String oldSessionId = "foobar";
|
484
|
try {
|
485
|
String identifier = generateDocid() + ".1";
|
486
|
Metacat m3 = null;
|
487
|
try {
|
488
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
489
|
} catch (MetacatInaccessibleException mie) {
|
490
|
System.err.println("Metacat is: " + metacatUrl);
|
491
|
fail("Metacat connection failed." + mie.getMessage());
|
492
|
}
|
493
|
System.err.println("SessionId (m3): " + m3.getSessionId());
|
494
|
m3.setSessionId(oldSessionId);
|
495
|
System.err.println("SessionId (m3 after set): " + m3.getSessionId());
|
496
|
System.err.println("Performing resuseInvalidSession insert: " +
|
497
|
identifier);
|
498
|
String response = m3.insert(identifier,
|
499
|
new StringReader(testdocument), null);
|
500
|
System.err.println("ReuseInvalid Insert response: " + response);
|
501
|
assertTrue(response.indexOf("<success>") == -1);
|
502
|
} catch (MetacatInaccessibleException mie) {
|
503
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
504
|
} catch (InsufficientKarmaException ike) {
|
505
|
fail("Insufficient karma:\n" + ike.getMessage());
|
506
|
} catch (MetacatException me) {
|
507
|
fail("Metacat Error:\n" + me.getMessage());
|
508
|
} catch (Exception e) {
|
509
|
fail("General exception:\n" + e.getMessage());
|
510
|
}
|
511
|
}
|
512
|
/**
|
513
|
* Create a hopefully unique docid for testing insert and update. Does
|
514
|
* not include the 'revision' part of the id.
|
515
|
*
|
516
|
* @return a String docid based on the current date and time
|
517
|
*/
|
518
|
private String generateDocid()
|
519
|
{
|
520
|
StringBuffer docid = new StringBuffer(prefix);
|
521
|
docid.append(".");
|
522
|
|
523
|
// Create a calendar to get the date formatted properly
|
524
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
525
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
526
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
527
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
528
|
Calendar calendar = new GregorianCalendar(pdt);
|
529
|
Date trialTime = new Date();
|
530
|
calendar.setTime(trialTime);
|
531
|
docid.append(calendar.get(Calendar.YEAR));
|
532
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
533
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
534
|
docid.append(calendar.get(Calendar.MINUTE));
|
535
|
docid.append(calendar.get(Calendar.SECOND));
|
536
|
|
537
|
return docid.toString();
|
538
|
}
|
539
|
}
|