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-17 18:32:33 -0700 (Wed, 17 Sep 2003) $'
|
9
|
* '$Revision: 1825 $'
|
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 =
|
56
|
"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
|
57
|
//"http://knb.ecoinformatics.org/knb/servlet/metacat";
|
58
|
private String wrongMetacatUrl=
|
59
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
60
|
private String username = "@mcuser@";
|
61
|
private String password = "@mcpassword@";
|
62
|
private String anotheruser = "@mcanotheruser@";
|
63
|
private String anotherpassword = "@mcanotherpassword@";
|
64
|
private String failpass = "uidfnkj43987yfdn";
|
65
|
private String prefix = "test";
|
66
|
private String newdocid = null;
|
67
|
private String testfile = "test/jones.204.22.xml";
|
68
|
private String queryFile = "test/query.xml";
|
69
|
private String testdocument = "";
|
70
|
|
71
|
private Metacat m;
|
72
|
|
73
|
/**
|
74
|
* Constructor to build the test
|
75
|
*
|
76
|
* @param name the name of the test method
|
77
|
*/
|
78
|
public MetacatClientTest(String name)
|
79
|
{
|
80
|
super(name);
|
81
|
newdocid = generateDocid();
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Establish a testing framework by initializing appropriate objects
|
86
|
*/
|
87
|
public void setUp()
|
88
|
{
|
89
|
try {
|
90
|
FileReader fr = new FileReader(testfile);
|
91
|
testdocument = IOUtil.getAsString(fr, true);
|
92
|
} catch (IOException ioe) {
|
93
|
fail("Can't read test data to run the test: " + testfile);
|
94
|
}
|
95
|
|
96
|
try {
|
97
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
98
|
} catch (MetacatInaccessibleException mie) {
|
99
|
System.err.println("Metacat is: " + metacatUrl);
|
100
|
fail("Metacat connection failed." + mie.getMessage());
|
101
|
}
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Release any objects after tests are complete
|
106
|
*/
|
107
|
public void tearDown()
|
108
|
{
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Create a suite of tests to be run together
|
113
|
*/
|
114
|
public static Test suite()
|
115
|
{
|
116
|
TestSuite suite = new TestSuite();
|
117
|
suite.addTest(new MetacatClientTest("initialize"));
|
118
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
119
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
120
|
suite.addTest(new MetacatClientTest("login"));
|
121
|
suite.addTest(new MetacatClientTest("insert"));
|
122
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
123
|
suite.addTest(new MetacatClientTest("read"));
|
124
|
suite.addTest(new MetacatClientTest("query"));
|
125
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
126
|
suite.addTest(new MetacatClientTest("update"));
|
127
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
128
|
suite.addTest(new MetacatClientTest("delete"));
|
129
|
suite.addTest(new MetacatClientTest("inaccessiblemetacat"));
|
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
|
/**
|
209
|
* Test the read() function with a known document
|
210
|
*/
|
211
|
public void read()
|
212
|
{
|
213
|
try {
|
214
|
m.login(username, password);
|
215
|
Reader r = m.read(newdocid+".1");
|
216
|
String doc = IOUtil.getAsString(r, true);
|
217
|
doc = doc +"\n";
|
218
|
System.err.println(doc);
|
219
|
assertTrue(doc.equals(testdocument));
|
220
|
} catch (MetacatAuthException mae) {
|
221
|
fail("Authorization failed:\n" + mae.getMessage());
|
222
|
} catch (MetacatInaccessibleException mie) {
|
223
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
224
|
} catch (Exception e) {
|
225
|
fail("General exception:\n" + e.getMessage());
|
226
|
}
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* A user try to read a document which it doesn't have read permission
|
231
|
*/
|
232
|
public void invalidRead()
|
233
|
{
|
234
|
try {
|
235
|
m.login(anotheruser, anotherpassword);
|
236
|
Reader r = m.read(newdocid+".1");
|
237
|
String doc = IOUtil.getAsString(r, true);
|
238
|
assertTrue(doc.indexOf("<error>") != -1);
|
239
|
System.err.println(doc);
|
240
|
} catch (MetacatAuthException mae) {
|
241
|
fail("Authorization failed:\n" + mae.getMessage());
|
242
|
} catch (MetacatInaccessibleException mie) {
|
243
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
244
|
} catch (InsufficientKarmaException ike) {
|
245
|
assertTrue(1 == 1);
|
246
|
} catch (MetacatException e) {
|
247
|
fail("Metacat exception:\n" + e.getMessage());
|
248
|
} catch (IOException ioe) {
|
249
|
fail("IO exception:\n" + ioe.getMessage());
|
250
|
}
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* Test the query() function with a known document
|
255
|
*/
|
256
|
public void query()
|
257
|
{
|
258
|
try {
|
259
|
FileReader fr = new FileReader(queryFile);
|
260
|
Reader r = m.query(fr);
|
261
|
String result = IOUtil.getAsString(r, true);
|
262
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
263
|
} catch (MetacatInaccessibleException mie) {
|
264
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
265
|
} catch (Exception e) {
|
266
|
fail("General exception:\n" + e.getMessage());
|
267
|
}
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* Test the insert() function with a known document
|
272
|
*/
|
273
|
public void insert()
|
274
|
{
|
275
|
try {
|
276
|
String identifier = newdocid + ".1";
|
277
|
m.login(username, password);
|
278
|
String response = m.insert(identifier,
|
279
|
new StringReader(testdocument), null);
|
280
|
assertTrue(response.indexOf("<success>") != -1);
|
281
|
assertTrue(response.indexOf(identifier) != -1);
|
282
|
System.err.println(response);
|
283
|
|
284
|
} catch (MetacatAuthException mae) {
|
285
|
fail("Authorization failed:\n" + mae.getMessage());
|
286
|
} catch (MetacatInaccessibleException mie) {
|
287
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
288
|
} catch (InsufficientKarmaException ike) {
|
289
|
assertTrue(1 == 1);
|
290
|
fail("Insufficient karma:\n" + ike.getMessage());
|
291
|
} catch (MetacatException me) {
|
292
|
fail("Metacat Error:\n" + me.getMessage());
|
293
|
} catch (Exception e) {
|
294
|
fail("General exception:\n" + e.getMessage());
|
295
|
}
|
296
|
}
|
297
|
|
298
|
/**
|
299
|
* Test the invalidUpdate() function. A user try to update a document
|
300
|
* which it doesn't have permission
|
301
|
*/
|
302
|
public void invalidUpdate()
|
303
|
{
|
304
|
try {
|
305
|
String identifier = newdocid + ".2";
|
306
|
m.login(anotheruser, anotherpassword);
|
307
|
String response = m.update(identifier,
|
308
|
new StringReader(testdocument), null);
|
309
|
assertTrue(response.indexOf("<success>") == -1);
|
310
|
|
311
|
} catch (MetacatAuthException mae) {
|
312
|
fail("Authorization failed:\n" + mae.getMessage());
|
313
|
} catch (MetacatInaccessibleException mie) {
|
314
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
315
|
} catch (InsufficientKarmaException ike) {
|
316
|
assertTrue(1 == 1);
|
317
|
} catch (MetacatException me) {
|
318
|
fail("Metacat Error:\n" + me.getMessage());
|
319
|
} catch (Exception e) {
|
320
|
fail("General exception:\n" + e.getMessage());
|
321
|
}
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Test the update() function with a known document
|
326
|
*/
|
327
|
public void update()
|
328
|
{
|
329
|
try {
|
330
|
String identifier = newdocid + ".2";
|
331
|
m.login(username, password);
|
332
|
String response = m.update(identifier,
|
333
|
new StringReader(testdocument), null);
|
334
|
assertTrue(response.indexOf("<success>") != -1);
|
335
|
assertTrue(response.indexOf(identifier) != -1);
|
336
|
System.err.println(response);
|
337
|
|
338
|
} catch (MetacatAuthException mae) {
|
339
|
fail("Authorization failed:\n" + mae.getMessage());
|
340
|
} catch (MetacatInaccessibleException mie) {
|
341
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
342
|
} catch (InsufficientKarmaException ike) {
|
343
|
fail("Insufficient karma:\n" + ike.getMessage());
|
344
|
} catch (MetacatException me) {
|
345
|
fail("Metacat Error:\n" + me.getMessage());
|
346
|
} catch (Exception e) {
|
347
|
fail("General exception:\n" + e.getMessage());
|
348
|
}
|
349
|
}
|
350
|
|
351
|
/**
|
352
|
* A user try delete a document which it doesn't have permission
|
353
|
*/
|
354
|
public void invalidDelete()
|
355
|
{
|
356
|
try {
|
357
|
String identifier = newdocid + ".2";
|
358
|
m.login(anotheruser, anotherpassword);
|
359
|
String response = m.delete(identifier);
|
360
|
assertTrue(response.indexOf("<success>") == -1);
|
361
|
System.err.println(response);
|
362
|
|
363
|
} catch (MetacatAuthException mae) {
|
364
|
fail("Authorization failed:\n" + mae.getMessage());
|
365
|
} catch (MetacatInaccessibleException mie) {
|
366
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
367
|
} catch (InsufficientKarmaException ike) {
|
368
|
assertTrue(1 == 1);
|
369
|
} catch (MetacatException me) {
|
370
|
assertTrue(1 == 1);
|
371
|
} catch (Exception e) {
|
372
|
fail("General exception:\n" + e.getMessage());
|
373
|
}
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* Test the delete() function with a known document
|
378
|
*/
|
379
|
public void delete()
|
380
|
{
|
381
|
try {
|
382
|
String identifier = newdocid + ".2";
|
383
|
m.login(username, password);
|
384
|
String response = m.delete(identifier);
|
385
|
assertTrue(response.indexOf("<success>") != -1);
|
386
|
System.err.println(response);
|
387
|
|
388
|
} catch (MetacatAuthException mae) {
|
389
|
fail("Authorization failed:\n" + mae.getMessage());
|
390
|
} catch (MetacatInaccessibleException mie) {
|
391
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
392
|
} catch (InsufficientKarmaException ike) {
|
393
|
fail("Insufficient karma:\n" + ike.getMessage());
|
394
|
} catch (MetacatException me) {
|
395
|
fail("Metacat Error:\n" + me.getMessage());
|
396
|
} catch (Exception e) {
|
397
|
fail("General exception:\n" + e.getMessage());
|
398
|
}
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
403
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
404
|
* exception, this is right.
|
405
|
*/
|
406
|
public void inaccessiblemetacat()
|
407
|
{
|
408
|
Metacat mWrong = null;
|
409
|
try {
|
410
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
411
|
} catch (MetacatInaccessibleException mie) {
|
412
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
413
|
}
|
414
|
|
415
|
try {
|
416
|
mWrong.login(username, password);
|
417
|
} catch (MetacatInaccessibleException mie) {
|
418
|
assertTrue(1 == 1);
|
419
|
} catch (MetacatAuthException mae) {
|
420
|
fail("Authorization failed:\n" + mae.getMessage());
|
421
|
}
|
422
|
}
|
423
|
|
424
|
/**
|
425
|
* Create a hopefully unique docid for testing insert and update. Does
|
426
|
* not include the 'revision' part of the id.
|
427
|
*
|
428
|
* @return a String docid based on the current date and time
|
429
|
*/
|
430
|
private String generateDocid()
|
431
|
{
|
432
|
StringBuffer docid = new StringBuffer(prefix);
|
433
|
docid.append(".");
|
434
|
|
435
|
// Create a calendar to get the date formatted properly
|
436
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
437
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
438
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
439
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
440
|
Calendar calendar = new GregorianCalendar(pdt);
|
441
|
Date trialTime = new Date();
|
442
|
calendar.setTime(trialTime);
|
443
|
docid.append(calendar.get(Calendar.YEAR));
|
444
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
445
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
446
|
docid.append(calendar.get(Calendar.MINUTE));
|
447
|
docid.append(calendar.get(Calendar.SECOND));
|
448
|
|
449
|
return docid.toString();
|
450
|
}
|
451
|
}
|