Revision 2242
Added by sgarg over 20 years ago
test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
package edu.ucsb.nceas.metacattest.client; |
27 | 27 |
|
28 |
import edu.ucsb.nceas.metacat.client.*; |
|
29 |
import edu.ucsb.nceas.utilities.HttpMessage; |
|
30 |
import edu.ucsb.nceas.utilities.IOUtil; |
|
31 |
|
|
28 |
import java.io.File; |
|
32 | 29 |
import java.io.FileReader; |
33 |
import java.io.InputStreamReader; |
|
34 | 30 |
import java.io.IOException; |
35 | 31 |
import java.io.Reader; |
36 | 32 |
import java.io.StringReader; |
37 |
import java.io.StringWriter; |
|
38 |
import java.text.DateFormat; |
|
39 | 33 |
import java.util.Calendar; |
34 |
import java.util.Date; |
|
40 | 35 |
import java.util.GregorianCalendar; |
41 |
import java.util.Date; |
|
42 | 36 |
import java.util.SimpleTimeZone; |
43 | 37 |
import java.util.TimeZone; |
44 | 38 |
|
39 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
|
40 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
41 |
import edu.ucsb.nceas.metacat.client.MetacatAuthException; |
|
42 |
import edu.ucsb.nceas.metacat.client.MetacatException; |
|
43 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
44 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
45 |
import edu.ucsb.nceas.utilities.IOUtil; |
|
45 | 46 |
import junit.framework.Test; |
46 | 47 |
import junit.framework.TestCase; |
47 |
import junit.framework.TestResult; |
|
48 | 48 |
import junit.framework.TestSuite; |
49 | 49 |
|
50 | 50 |
/** |
... | ... | |
63 | 63 |
private String prefix = "test"; |
64 | 64 |
private String newdocid = null; |
65 | 65 |
private String testfile = "test/jones.204.22.xml"; |
66 |
private String onlinetestdatafile = "test/onlineDataFile1"; |
|
66 | 67 |
private String queryFile = "test/query.xml"; |
67 | 68 |
private String testdocument = ""; |
68 | 69 |
private Metacat m; |
... | ... | |
98 | 99 |
fail("Metacat connection failed." + mie.getMessage()); |
99 | 100 |
} |
100 | 101 |
} |
101 |
|
|
102 |
|
|
102 | 103 |
/** |
103 | 104 |
* Release any objects after tests are complete |
104 | 105 |
*/ |
105 | 106 |
public void tearDown() |
106 | 107 |
{ |
107 | 108 |
} |
108 |
|
|
109 |
|
|
109 | 110 |
/** |
110 | 111 |
* Create a suite of tests to be run together |
111 | 112 |
*/ |
112 | 113 |
public static Test suite() |
113 | 114 |
{ |
114 |
TestSuite suite = new TestSuite();
|
|
115 |
TestSuite suite = new TestSuite(); |
|
115 | 116 |
suite.addTest(new MetacatClientTest("initialize")); |
116 | 117 |
suite.addTest(new MetacatClientTest("invalidLogin")); |
117 | 118 |
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert")); |
118 | 119 |
suite.addTest(new MetacatClientTest("login")); |
119 | 120 |
suite.addTest(new MetacatClientTest("insert")); |
121 |
suite.addTest(new MetacatClientTest("upload")); |
|
120 | 122 |
suite.addTest(new MetacatClientTest("invalidRead")); |
121 | 123 |
suite.addTest(new MetacatClientTest("read")); |
122 | 124 |
suite.addTest(new MetacatClientTest("query")); |
... | ... | |
129 | 131 |
suite.addTest(new MetacatClientTest("reuseInvalidSession")); |
130 | 132 |
return suite; |
131 | 133 |
} |
132 |
|
|
134 |
|
|
133 | 135 |
/** |
134 | 136 |
* Run an initial test that always passes to check that the test |
135 | 137 |
* harness is working. |
... | ... | |
138 | 140 |
{ |
139 | 141 |
assertTrue(1 == 1); |
140 | 142 |
} |
141 |
|
|
143 |
|
|
142 | 144 |
/** |
143 | 145 |
* Test the login() function with valid credentials |
144 | 146 |
*/ |
... | ... | |
176 | 178 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
177 | 179 |
} |
178 | 180 |
} |
179 |
|
|
181 |
|
|
180 | 182 |
/** |
181 | 183 |
* Test the logout() function. When logout, user will be public, it couldn't |
182 | 184 |
* insert a document. |
... | ... | |
187 | 189 |
String identifier = newdocid + ".1"; |
188 | 190 |
m.login(username, password); |
189 | 191 |
m.logout(); |
190 |
String response = m.insert(identifier,
|
|
192 |
String response = m.insert(identifier, |
|
191 | 193 |
new StringReader(testdocument), null); |
192 | 194 |
System.err.println("Response in logout: "+response); |
193 | 195 |
assertTrue(response.indexOf("<success>") == -1); |
... | ... | |
224 | 226 |
fail("General exception:\n" + e.getMessage()); |
225 | 227 |
} |
226 | 228 |
} |
227 |
|
|
229 |
|
|
228 | 230 |
/** |
229 | 231 |
* A user try to read a document which it doesn't have read permission |
230 | 232 |
*/ |
... | ... | |
248 | 250 |
fail("IO exception:\n" + ioe.getMessage()); |
249 | 251 |
} |
250 | 252 |
} |
251 |
|
|
253 |
|
|
252 | 254 |
/** |
253 | 255 |
* Test the query() function with a known document |
254 | 256 |
*/ |
... | ... | |
279 | 281 |
try { |
280 | 282 |
String identifier = newdocid + ".1"; |
281 | 283 |
m.login(username, password); |
282 |
String response = m.insert(identifier,
|
|
284 |
String response = m.insert(identifier, |
|
283 | 285 |
new StringReader(testdocument), null); |
284 | 286 |
assertTrue(response.indexOf("<success>") != -1); |
285 | 287 |
assertTrue(response.indexOf(identifier) != -1); |
... | ... | |
300 | 302 |
} |
301 | 303 |
|
302 | 304 |
/** |
305 |
* Test the upload() function with a known document |
|
306 |
*/ |
|
307 |
public void upload() |
|
308 |
{ |
|
309 |
try { |
|
310 |
newdocid = generateDocid(); |
|
311 |
String identifier = newdocid + ".1"; |
|
312 |
m.login(username, password); |
|
313 |
String response = m.upload(identifier, |
|
314 |
new File(onlinetestdatafile)); |
|
315 |
assertTrue(response.indexOf("<success>") != -1); |
|
316 |
assertTrue(response.indexOf(identifier) != -1); |
|
317 |
System.err.println(response); |
|
318 |
|
|
319 |
} catch (MetacatAuthException mae) { |
|
320 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
321 |
} catch (MetacatInaccessibleException mie) { |
|
322 |
mie.printStackTrace(); |
|
323 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
324 |
} catch (InsufficientKarmaException ike) { |
|
325 |
assertTrue(1 == 1); |
|
326 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
327 |
} catch (MetacatException me) { |
|
328 |
fail("Metacat Error:\n" + me.getMessage()); |
|
329 |
} catch (Exception e) { |
|
330 |
fail("General exception:\n" + e.getMessage()); |
|
331 |
} |
|
332 |
} |
|
333 |
|
|
334 |
/** |
|
303 | 335 |
* Test the invalidUpdate() function. A user try to update a document |
304 | 336 |
* which it doesn't have permission |
305 | 337 |
*/ |
... | ... | |
308 | 340 |
try { |
309 | 341 |
String identifier = newdocid + ".2"; |
310 | 342 |
m.login(anotheruser, anotherpassword); |
311 |
String response = m.update(identifier,
|
|
343 |
String response = m.update(identifier, |
|
312 | 344 |
new StringReader(testdocument), null); |
313 | 345 |
assertTrue(response.indexOf("<success>") == -1); |
314 |
|
|
346 |
|
|
315 | 347 |
} catch (MetacatAuthException mae) { |
316 | 348 |
fail("Authorization failed:\n" + mae.getMessage()); |
317 | 349 |
} catch (MetacatInaccessibleException mie) { |
... | ... | |
324 | 356 |
fail("General exception:\n" + e.getMessage()); |
325 | 357 |
} |
326 | 358 |
} |
327 |
|
|
359 |
|
|
328 | 360 |
/** |
329 | 361 |
* Test the update() function with a known document |
330 | 362 |
*/ |
... | ... | |
333 | 365 |
try { |
334 | 366 |
String identifier = newdocid + ".2"; |
335 | 367 |
m.login(username, password); |
336 |
String response = m.update(identifier,
|
|
368 |
String response = m.update(identifier, |
|
337 | 369 |
new StringReader(testdocument), null); |
338 | 370 |
assertTrue(response.indexOf("<success>") != -1); |
339 | 371 |
assertTrue(response.indexOf(identifier) != -1); |
... | ... | |
351 | 383 |
fail("General exception:\n" + e.getMessage()); |
352 | 384 |
} |
353 | 385 |
} |
354 |
|
|
386 |
|
|
355 | 387 |
/** |
356 | 388 |
* A user try delete a document which it doesn't have permission |
357 | 389 |
*/ |
... | ... | |
376 | 408 |
fail("General exception:\n" + e.getMessage()); |
377 | 409 |
} |
378 | 410 |
} |
379 |
|
|
411 |
|
|
380 | 412 |
/** |
381 | 413 |
* Test the delete() function with a known document |
382 | 414 |
*/ |
... | ... | |
401 | 433 |
fail("General exception:\n" + e.getMessage()); |
402 | 434 |
} |
403 | 435 |
} |
404 |
|
|
436 |
|
|
405 | 437 |
/** |
406 | 438 |
* Test the to connect a wrong metacat url. Create a new metacat with a |
407 | 439 |
* inaccessible url. Then try to login it. If get a MetacatInaccessible |
... | ... | |
415 | 447 |
} catch (MetacatInaccessibleException mie) { |
416 | 448 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
417 | 449 |
} |
418 |
|
|
450 |
|
|
419 | 451 |
try { |
420 | 452 |
mWrong.login(username, password); |
421 | 453 |
} catch (MetacatInaccessibleException mie) { |
... | ... | |
457 | 489 |
System.err.println("SessionId (m2): " + m2.getSessionId()); |
458 | 490 |
m2.setSessionId(oldSessionId); |
459 | 491 |
System.err.println("SessionId (m2 after set): " + m2.getSessionId()); |
460 |
String response = m2.insert(identifier,
|
|
492 |
String response = m2.insert(identifier, |
|
461 | 493 |
new StringReader(testdocument), null); |
462 | 494 |
System.err.println("Reuse Insert response: " + response); |
463 | 495 |
assertTrue(response.indexOf("<success>") != -1); |
... | ... | |
474 | 506 |
|
475 | 507 |
/** |
476 | 508 |
* Try to perform an action that requires a session to be valid without |
477 |
* having logged in first, but use an invalid session identifier.
|
|
509 |
* having logged in first, but use an invalid session identifier. |
|
478 | 510 |
* Then insert a document, which should fail. |
479 | 511 |
*/ |
480 | 512 |
public void reuseInvalidSession() |
... | ... | |
495 | 527 |
System.err.println("SessionId (m3 after set): " + m3.getSessionId()); |
496 | 528 |
System.err.println("Performing resuseInvalidSession insert: " + |
497 | 529 |
identifier); |
498 |
String response = m3.insert(identifier,
|
|
530 |
String response = m3.insert(identifier, |
|
499 | 531 |
new StringReader(testdocument), null); |
500 | 532 |
System.err.println("ReuseInvalid Insert response: " + response); |
501 | 533 |
assertTrue(response.indexOf("<success>") == -1); |
... | ... | |
519 | 551 |
{ |
520 | 552 |
StringBuffer docid = new StringBuffer(prefix); |
521 | 553 |
docid.append("."); |
522 |
|
|
554 |
|
|
523 | 555 |
// Create a calendar to get the date formatted properly |
524 | 556 |
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); |
525 | 557 |
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); |
Also available in: Unified diff
Added code to test the upload functionality in the client