1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2010 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the Access Controls in metacat by JUnit
|
6
|
*
|
7
|
* '$Author: berkley $'
|
8
|
* '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
|
9
|
* '$Revision: 5027 $'
|
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.metacat.dataone;
|
27
|
|
28
|
import java.util.*;
|
29
|
import java.io.*;
|
30
|
|
31
|
import java.security.MessageDigest;
|
32
|
|
33
|
import edu.ucsb.nceas.MCTestCase;
|
34
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
35
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
36
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
37
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
38
|
import edu.ucsb.nceas.metacat.dataone.CrudService;
|
39
|
import junit.framework.Test;
|
40
|
import junit.framework.TestSuite;
|
41
|
|
42
|
import org.apache.commons.io.IOUtils;
|
43
|
import org.apache.log.LogEvent;
|
44
|
import org.dataone.service.exceptions.InvalidRequest;
|
45
|
import org.dataone.service.exceptions.InvalidToken;
|
46
|
import org.dataone.service.exceptions.NotAuthorized;
|
47
|
import org.dataone.service.exceptions.NotImplemented;
|
48
|
import org.dataone.service.exceptions.ServiceFailure;
|
49
|
import org.dataone.service.types.*;
|
50
|
|
51
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
52
|
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
|
53
|
|
54
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
55
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
56
|
|
57
|
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
|
58
|
|
59
|
/**
|
60
|
* A JUnit test for testing the dataone CrudService class
|
61
|
*/
|
62
|
public class CrudServiceTest extends MCTestCase
|
63
|
{
|
64
|
private int createCount = 0;
|
65
|
|
66
|
/**
|
67
|
* consstructor for the test
|
68
|
*/
|
69
|
public CrudServiceTest(String name)
|
70
|
{
|
71
|
super(name);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Establish a testing framework by initializing appropriate objects
|
76
|
*/
|
77
|
public void setUp() throws Exception
|
78
|
{
|
79
|
super.setUp();
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Release any objects after tests are complete
|
84
|
*/
|
85
|
public void tearDown()
|
86
|
{
|
87
|
System.out.println(createCount + " docs created in this test session.");
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Create a suite of tests to be run together
|
92
|
*/
|
93
|
public static Test suite()
|
94
|
{
|
95
|
TestSuite suite = new TestSuite();
|
96
|
suite.addTest(new CrudServiceTest("initialize"));
|
97
|
suite.addTest(new CrudServiceTest("testSingletonAccessor"));
|
98
|
suite.addTest(new CrudServiceTest("testCreateAndGet"));
|
99
|
suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
|
100
|
suite.addTest(new CrudServiceTest("testUpdate"));
|
101
|
suite.addTest(new CrudServiceTest("testListObjects"));
|
102
|
suite.addTest(new CrudServiceTest("testAccessControl"));
|
103
|
//suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
|
104
|
suite.addTest(new CrudServiceTest("testGetLogRecords"));
|
105
|
suite.addTest(new CrudServiceTest("testChecksumError"));
|
106
|
suite.addTest(new CrudServiceTest("testPublicAccess"));
|
107
|
suite.addTest(new CrudServiceTest("testFailedCreate"));
|
108
|
suite.addTest(new CrudServiceTest("testChecksum"));
|
109
|
suite.addTest(new CrudServiceTest("testDescribe"));
|
110
|
suite.addTest(new CrudServiceTest("testDelete"));
|
111
|
return suite;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* test the delete function
|
116
|
*/
|
117
|
public void testDelete()
|
118
|
{
|
119
|
try
|
120
|
{
|
121
|
CrudService cs = CrudService.getInstance();
|
122
|
AuthToken token = getToken();
|
123
|
String doc = getTestDoc();
|
124
|
Identifier id = createDoc(token, doc);
|
125
|
System.out.println("ID: " + id.getValue());
|
126
|
makeDocPublic(token, id, true);
|
127
|
cs.get(token, id);
|
128
|
cs.delete(token, id);
|
129
|
//there should be a try/catch around the next statement
|
130
|
//to make sure that the document is no longer readable,
|
131
|
//but because the data is output in a 2nd thread, the
|
132
|
//exception does not get caught here. See
|
133
|
//https://redmine.dataone.org/issues/1079
|
134
|
//cs.get(token, id);
|
135
|
}
|
136
|
catch(Exception e)
|
137
|
{
|
138
|
fail("Unexpected error in testDescribe: " + e.getMessage());
|
139
|
}
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* test the describe crud function
|
144
|
*/
|
145
|
public void testDescribe()
|
146
|
{
|
147
|
try
|
148
|
{
|
149
|
CrudService cs = CrudService.getInstance();
|
150
|
AuthToken token = getToken();
|
151
|
|
152
|
String doc = getTestDoc();
|
153
|
Identifier id = createDoc(token, doc);
|
154
|
System.out.println("ID: " + id.getValue());
|
155
|
makeDocPublic(token, id, true);
|
156
|
|
157
|
SystemMetadata sm = cs.getSystemMetadata(token, id);
|
158
|
|
159
|
DescribeResponse dr = cs.describe(token, id);
|
160
|
|
161
|
assertTrue(dr.getDataONE_Checksum().getValue().equals(sm.getChecksum().getValue()));
|
162
|
assertTrue(dr.getDataONE_ObjectFormat().toString().equals(sm.getObjectFormat().toString()));
|
163
|
assertTrue(dr.getLast_Modified().getTime() == sm.getDateSysMetadataModified().getTime());
|
164
|
assertTrue(dr.getContent_Length() == sm.getSize());
|
165
|
}
|
166
|
catch(Exception e)
|
167
|
{
|
168
|
fail("Unexpected error in testDescribe: " + e.getMessage());
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* test the checksum creation
|
174
|
*/
|
175
|
public void testChecksum()
|
176
|
{
|
177
|
try
|
178
|
{
|
179
|
CrudService cs = CrudService.getInstance();
|
180
|
AuthToken token = getToken();
|
181
|
System.out.println("token: " + token.getToken());
|
182
|
String doc = getTestDoc();
|
183
|
Identifier id = createDoc(token, doc);
|
184
|
System.out.println("ID: " + id.getValue());
|
185
|
makeDocPublic(token, id, true);
|
186
|
String doc2 = getDoc(token, id);
|
187
|
|
188
|
String checksum = checksum(doc);
|
189
|
Checksum checksum2 = cs.getChecksum(token, id, "MD5");
|
190
|
System.out.println("doc: \"" + doc + "\"");
|
191
|
System.out.println("doc2: \"" + doc2 + "\"");
|
192
|
System.out.println("checksum1: " + checksum);
|
193
|
System.out.println("checksum2: " + checksum2.getValue());
|
194
|
assertTrue(checksum.equals(checksum2.getValue()));
|
195
|
|
196
|
}
|
197
|
catch(Exception e)
|
198
|
{
|
199
|
fail("Unexpected error in testChecksum: " + e.getMessage());
|
200
|
}
|
201
|
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* insert an invalid document and make sure create fails and does not
|
206
|
* leave any artifacts behind
|
207
|
*/
|
208
|
public void testFailedCreate()
|
209
|
{
|
210
|
try
|
211
|
{
|
212
|
String invalidDoc = "<xxx></yyy>";
|
213
|
System.out.println("trying to insert doc " + invalidDoc);
|
214
|
CrudService cs = CrudService.getInstance();
|
215
|
AuthToken token = getToken();
|
216
|
//run create
|
217
|
try
|
218
|
{
|
219
|
Identifier id = createDoc(token, invalidDoc);
|
220
|
fail("Should have thrown an exception.");
|
221
|
}
|
222
|
catch(Exception e)
|
223
|
{
|
224
|
//e.printStackTrace();
|
225
|
}
|
226
|
|
227
|
}
|
228
|
catch(Exception e)
|
229
|
{
|
230
|
fail("unexpected exception in testFailedCreate: " + e.getMessage());
|
231
|
}
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* test to make sure that get and listObjects methods are publicly accessible
|
236
|
*/
|
237
|
public void testPublicAccess()
|
238
|
{
|
239
|
try
|
240
|
{
|
241
|
AuthToken token = new AuthToken("public");
|
242
|
//AuthToken token = getToken();
|
243
|
CrudService cs = CrudService.getInstance();
|
244
|
ObjectList ol = cs.listObjects(token, null, null, null);
|
245
|
System.out.println("ol: " + ol.sizeObjectInfoList());
|
246
|
assertTrue(ol.sizeObjectInfoList() > 0);
|
247
|
|
248
|
ObjectInfo oi = ol.getObjectInfo(0);
|
249
|
String s = getDoc(token, oi.getIdentifier());
|
250
|
assertNotNull(s);
|
251
|
|
252
|
try
|
253
|
{ //try a create with the public auth. should fail
|
254
|
Identifier id = new Identifier();
|
255
|
String docid = generateDocumentId();
|
256
|
id.setValue(docid);
|
257
|
String testDoc = getTestDoc();
|
258
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
259
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
260
|
SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
|
261
|
assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
|
262
|
//create the doc
|
263
|
Identifier idC = cs.create(token, id, sbis, sm);
|
264
|
fail("Should have thrown an auth exception");
|
265
|
}
|
266
|
catch(Exception e)
|
267
|
{
|
268
|
|
269
|
}
|
270
|
|
271
|
//create a legit doc
|
272
|
token = getToken();
|
273
|
Identifier id = new Identifier();
|
274
|
String docid = generateDocumentId();
|
275
|
id.setValue(docid);
|
276
|
String testDoc = getTestDoc();
|
277
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
278
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
279
|
SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
|
280
|
assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
|
281
|
//create the doc
|
282
|
Identifier idC = cs.create(token, id, sbis, sm);
|
283
|
//make it public, then use a public token to try to get it
|
284
|
InputStream data = null;
|
285
|
try
|
286
|
{
|
287
|
AuthToken publicToken = new AuthToken("public");
|
288
|
data = cs.get(publicToken, id);
|
289
|
data.read();
|
290
|
|
291
|
fail("should have thrown an exception");
|
292
|
}
|
293
|
catch(Exception e)
|
294
|
{
|
295
|
/*System.out.println("%%%%%%%%%%%%%%%%%%%%%%%getting result");
|
296
|
Object o = ((InputStreamFromOutputStream)data).getResult();
|
297
|
System.out.println("result: " + o);
|
298
|
System.out.println("exception thrown!");
|
299
|
System.out.println("exception thrown: " + e.getClass().getName());*/
|
300
|
}
|
301
|
|
302
|
|
303
|
|
304
|
makeDocPublic(token, id, true);
|
305
|
token = new AuthToken("public");
|
306
|
data = cs.get(token, id);
|
307
|
}
|
308
|
catch(Exception e)
|
309
|
{
|
310
|
fail("Error in testPublicAccess: " + e.getMessage());
|
311
|
}
|
312
|
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* test for an error where the checksum algorithm gets appended onto the checksum.
|
317
|
*/
|
318
|
public void testChecksumError()
|
319
|
{
|
320
|
printTestHeader("testChecksumError");
|
321
|
try
|
322
|
{
|
323
|
Date d1 = new Date(new Date().getTime() - 5000);
|
324
|
CrudService cs = CrudService.getInstance();
|
325
|
AuthToken token = getToken();
|
326
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
327
|
//create docs at different times
|
328
|
|
329
|
Identifier id = new Identifier();
|
330
|
String docid = "test." + new Date().getTime();
|
331
|
id.setValue(docid);
|
332
|
|
333
|
//create the system metadata then run the create method
|
334
|
String testDoc = getTestDoc();
|
335
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
336
|
SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
|
337
|
assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
|
338
|
//create the doc
|
339
|
Identifier idC = cs.create(token, id, sbis, sm);
|
340
|
makeDocPublic(token, id, true);
|
341
|
assertTrue(idC.getValue().equals(id.getValue()));
|
342
|
SystemMetadata smC = getSystemMetadata(token, idC);
|
343
|
assertFalse(smC.getChecksum().getValue().startsWith("MD5"));
|
344
|
|
345
|
Date d2 = new Date(new Date().getTime() + 5000);
|
346
|
|
347
|
ObjectList ol = cs.listObjects(token, d1, d2, of1);
|
348
|
assertTrue(ol.sizeObjectInfoList() > 0);
|
349
|
ObjectInfo oi = ol.getObjectInfo(0);
|
350
|
//this will fail if the error state exists.
|
351
|
assertFalse(oi.getChecksum().getValue().startsWith("MD5"));
|
352
|
}
|
353
|
catch(Exception e)
|
354
|
{
|
355
|
fail("Unexpected exception: " + e.getMessage());
|
356
|
}
|
357
|
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* test CrudService.getLogRecords
|
362
|
*/
|
363
|
public void testGetLogRecords()
|
364
|
{
|
365
|
printTestHeader("testGetLogRecords");
|
366
|
try
|
367
|
{
|
368
|
CrudService cs = CrudService.getInstance();
|
369
|
AuthToken token = getToken();
|
370
|
Date fromDate = new Date();
|
371
|
Identifier id = createDoc(token, getTestDoc());
|
372
|
Date toDate = new Date();
|
373
|
Log lrs = cs.getLogRecords(token, fromDate, toDate, null);
|
374
|
assertNotNull(lrs);
|
375
|
assertTrue(lrs.sizeLogEntryList() == 1);
|
376
|
LogEntry lrLogEvent = lrs.getLogEntry(0);
|
377
|
assertTrue(lrLogEvent.getEvent().name().equals("CREATE"));
|
378
|
assertTrue(lrLogEvent.getIdentifier().getValue().equals(id.getValue()));
|
379
|
}
|
380
|
catch(Exception e)
|
381
|
{
|
382
|
e.printStackTrace();
|
383
|
fail("testGetLogRecords threw an unexpected exception: " + e.getMessage());
|
384
|
}
|
385
|
}
|
386
|
|
387
|
/**
|
388
|
* test the generation of system metadata for docs that don't already
|
389
|
* have it. This will be used for migration of existing object stores
|
390
|
* to dataone.
|
391
|
*/
|
392
|
public void testGenerateMissingSystemMetadata()
|
393
|
{
|
394
|
printTestHeader("testGenerateMissingSystemMetadata");
|
395
|
try
|
396
|
{
|
397
|
CrudService cs = CrudService.getInstance();
|
398
|
AuthToken token = getToken();
|
399
|
//create a document with no system metadata
|
400
|
String testDoc = getTestDoc();
|
401
|
Identifier id = new Identifier();
|
402
|
String docid = generateDocumentId();
|
403
|
id.setValue(docid);
|
404
|
|
405
|
cs.insertOrUpdateDocument(testDoc, id, cs.getSessionData(token), "insert", false);
|
406
|
//try to get its system metadata, should fail
|
407
|
try
|
408
|
{
|
409
|
getSystemMetadata(token, id);
|
410
|
fail("call to getSystemMetadata should have failed.");
|
411
|
}
|
412
|
catch(Exception e)
|
413
|
{}
|
414
|
|
415
|
//generate missing system metadata
|
416
|
cs.generateMissingSystemMetadata(token);
|
417
|
//try to get system metadata again, should succeed
|
418
|
getSystemMetadata(token, id);
|
419
|
}
|
420
|
catch(Exception e)
|
421
|
{
|
422
|
fail("Unexpected error generating missing system metadata: " + e.getMessage());
|
423
|
}
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* make sure that only valid sessions can update/delete
|
428
|
*/
|
429
|
public void testAccessControl()
|
430
|
{
|
431
|
printTestHeader("testAccessControl");
|
432
|
try
|
433
|
{
|
434
|
CrudService cs = CrudService.getInstance();
|
435
|
AuthToken token = getToken();
|
436
|
//create a doc
|
437
|
Identifier id = createDoc(token, getTestDoc());
|
438
|
|
439
|
//get the doc and sysmetadata
|
440
|
String gotDoc = getDoc(token, id);
|
441
|
SystemMetadata sm = getSystemMetadata(token, id);
|
442
|
|
443
|
//break the session id
|
444
|
String sessionid = "somefakesessionid";
|
445
|
token = new AuthToken(sessionid);
|
446
|
|
447
|
//update the doc
|
448
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
449
|
Identifier newid = new Identifier();
|
450
|
newid.setValue(generateDocumentId());
|
451
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
452
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
453
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
454
|
fail("exception should have been thrown.");
|
455
|
}
|
456
|
catch(Exception e)
|
457
|
{
|
458
|
}
|
459
|
|
460
|
try
|
461
|
{
|
462
|
CrudService cs = CrudService.getInstance();
|
463
|
AuthToken token = new AuthToken("somefakesessionid");
|
464
|
//create a doc
|
465
|
Identifier id = createDoc(token, getTestDoc());
|
466
|
fail("exception should have been thrown.");
|
467
|
}
|
468
|
catch(Exception e)
|
469
|
{
|
470
|
}
|
471
|
}
|
472
|
|
473
|
/**
|
474
|
* public ObjectList listObjects(AuthToken token, Date startTime, Date endTime,
|
475
|
* ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
|
476
|
* throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
|
477
|
*/
|
478
|
public void testListObjects()
|
479
|
{
|
480
|
printTestHeader("testListObjects");
|
481
|
try
|
482
|
{
|
483
|
CrudService cs = CrudService.getInstance();
|
484
|
AuthToken token = getToken();
|
485
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
486
|
ObjectFormat of2 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.0.0");
|
487
|
//create docs at different times
|
488
|
Date d1 = new Date();
|
489
|
Identifier id1 = createDoc(token, getTestDoc(), of1);
|
490
|
SystemMetadata sm1 = getSystemMetadata(token, id1);
|
491
|
|
492
|
Date d2 = new Date();
|
493
|
Identifier id2 = createDoc(token, getTestDoc(), of2);
|
494
|
makeDocPublic(token, id2, true);
|
495
|
SystemMetadata sm2 = getSystemMetadata(token, id2);
|
496
|
|
497
|
Date d3 = new Date();
|
498
|
Identifier id3 = createDoc(token, getTestDoc(), of1);
|
499
|
makeDocPublic(token, id3, true);
|
500
|
SystemMetadata sm3 = getSystemMetadata(token, id3);
|
501
|
|
502
|
Date d4 = new Date();
|
503
|
Identifier id4 = createDoc(token, getTestDoc(), of2);
|
504
|
makeDocPublic(token, id4, true);
|
505
|
SystemMetadata sm4 = getSystemMetadata(token, id4);
|
506
|
|
507
|
Date d5 = new Date();
|
508
|
Identifier id5 = createDoc(token, getTestDoc(), of1);
|
509
|
makeDocPublic(token, id5, true);
|
510
|
SystemMetadata sm5 = getSystemMetadata(token, id5);
|
511
|
|
512
|
Date d6 = new Date();
|
513
|
|
514
|
//now get the objects for specific time ranges and test that it returns
|
515
|
//the correct objects
|
516
|
|
517
|
//ObjectList list = cs.listObjects(token, null, null, null);
|
518
|
//System.out.println("list size: " + list.sizeObjectInfoList());
|
519
|
|
520
|
//should return sm1 and sm2
|
521
|
ObjectList list1 = cs.listObjects(token, d1, d3, null);
|
522
|
assertTrue(list1.sizeObjectInfoList() == 2);
|
523
|
assertTrue(idInObjectList(id1, list1));
|
524
|
assertTrue(idInObjectList(id2, list1));
|
525
|
|
526
|
ObjectInfo info = list1.getObjectInfo(0);
|
527
|
|
528
|
|
529
|
//should only return sm1
|
530
|
ObjectList list2 = cs.listObjects(token, d1, d3, of1);
|
531
|
assertTrue(list2.sizeObjectInfoList() == 1);
|
532
|
assertTrue(idInObjectList(id1, list2));
|
533
|
|
534
|
//should return sm1-sm4
|
535
|
ObjectList list3 = cs.listObjects(token, d1, d5, null);
|
536
|
assertTrue(list3.sizeObjectInfoList() == 4);
|
537
|
ObjectInfo oi4 = list3.getObjectInfo(0);
|
538
|
assertTrue(idInObjectList(id1, list3));
|
539
|
assertTrue(idInObjectList(id2, list3));
|
540
|
assertTrue(idInObjectList(id3, list3));
|
541
|
assertTrue(idInObjectList(id4, list3));
|
542
|
|
543
|
//should only return sm2 and sm4
|
544
|
ObjectList list4 = cs.listObjects(token, d1, d5, of2);
|
545
|
assertTrue(list4.sizeObjectInfoList() == 2);
|
546
|
assertTrue(idInObjectList(id2, list4));
|
547
|
assertTrue(idInObjectList(id4, list4));
|
548
|
|
549
|
//should return all
|
550
|
ObjectList list5 = cs.listObjects(token, d1, d6, null);
|
551
|
assertTrue(list5.sizeObjectInfoList() == 5);
|
552
|
assertTrue(idInObjectList(id1, list5));
|
553
|
assertTrue(idInObjectList(id2, list5));
|
554
|
assertTrue(idInObjectList(id3, list5));
|
555
|
assertTrue(idInObjectList(id4, list5));
|
556
|
assertTrue(idInObjectList(id5, list5));
|
557
|
|
558
|
//should return 1, 3, 5
|
559
|
ObjectList list6 = cs.listObjects(token, d1, d6, of1);
|
560
|
assertTrue(list6.sizeObjectInfoList() == 3);
|
561
|
assertTrue(idInObjectList(id1, list6));
|
562
|
assertTrue(idInObjectList(id3, list6));
|
563
|
assertTrue(idInObjectList(id5, list6));
|
564
|
|
565
|
//should return 4 (id1 is not public)
|
566
|
token = new AuthToken("public");
|
567
|
ObjectList list7 = cs.listObjects(token, d1, d6, null);
|
568
|
//System.out.println("list7 size: " + list7.sizeObjectInfoList());
|
569
|
assertTrue(list7.sizeObjectInfoList() == 4);
|
570
|
|
571
|
//test paging
|
572
|
ObjectList list8 = cs.listObjects(token, d1, d6, null, false, 2, 2);
|
573
|
assertTrue(list8.getCount() == 2);
|
574
|
assertTrue(list8.getStart() == 2);
|
575
|
assertTrue(list8.getTotal() == 4);
|
576
|
|
577
|
}
|
578
|
catch(Exception e)
|
579
|
{
|
580
|
//e.printStackTrace();
|
581
|
fail("Error in listObjects: " + e.getMessage());
|
582
|
}
|
583
|
}
|
584
|
|
585
|
private boolean idInObjectList(Identifier id, ObjectList list)
|
586
|
{
|
587
|
for(int i=0; i<list.sizeObjectInfoList(); i++)
|
588
|
{
|
589
|
ObjectInfo oi = list.getObjectInfo(i);
|
590
|
if(id.getValue().equals(oi.getIdentifier().getValue()))
|
591
|
return true;
|
592
|
}
|
593
|
return false;
|
594
|
}
|
595
|
|
596
|
/**
|
597
|
* public Identifier update(AuthToken token, Identifier guid,
|
598
|
* InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta)
|
599
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
600
|
* UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata,
|
601
|
* NotImplemented
|
602
|
*/
|
603
|
public void testUpdate()
|
604
|
{
|
605
|
printTestHeader("testUpdate");
|
606
|
try
|
607
|
{
|
608
|
CrudService cs = CrudService.getInstance();
|
609
|
AuthToken token = getToken();
|
610
|
//create a doc
|
611
|
Identifier id = createDoc(token, getTestDoc());
|
612
|
|
613
|
//get the doc and sysmetadata
|
614
|
String gotDoc = getDoc(token, id);
|
615
|
SystemMetadata sm = getSystemMetadata(token, id);
|
616
|
|
617
|
//update the doc
|
618
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
619
|
Identifier newid = new Identifier();
|
620
|
newid.setValue(generateDocumentId());
|
621
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
622
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
623
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
624
|
|
625
|
//get doc - check that it matches update
|
626
|
String newdoc = getDoc(token, newid);
|
627
|
assertTrue(gotDoc.equals(newdoc));
|
628
|
|
629
|
//get sysmeta - check that ids and other fields are updated
|
630
|
SystemMetadata newnewsm = getSystemMetadata(token, id);
|
631
|
assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
|
632
|
|
633
|
//get the new sysmeta and make sure the obsoletes field is set
|
634
|
SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
|
635
|
assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
|
636
|
}
|
637
|
catch(Exception e)
|
638
|
{
|
639
|
e.printStackTrace();
|
640
|
fail("Error in testUpdate: " + e.getMessage());
|
641
|
}
|
642
|
}
|
643
|
|
644
|
/**
|
645
|
* public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
646
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
647
|
* InvalidRequest, NotImplemented
|
648
|
*/
|
649
|
public void testGetSystemMetadata()
|
650
|
{
|
651
|
printTestHeader("testGetSystemMetadata");
|
652
|
try
|
653
|
{
|
654
|
CrudService cs = CrudService.getInstance();
|
655
|
AuthToken token = getToken();
|
656
|
//run create
|
657
|
Identifier id = createDoc(token, getTestDoc());
|
658
|
//get the systemMetadata and make sure it is the correct object for this testDoc
|
659
|
SystemMetadata sm = getSystemMetadata(token, id);
|
660
|
assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
|
661
|
assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
|
662
|
|
663
|
try
|
664
|
{
|
665
|
Identifier fakeid = new Identifier();
|
666
|
fakeid.setValue("somethingfake.234234");
|
667
|
getSystemMetadata(token, fakeid);
|
668
|
fail("getSystemMetadata should have thrown an exception.");
|
669
|
}
|
670
|
catch(Exception e)
|
671
|
{
|
672
|
assertTrue(true);
|
673
|
}
|
674
|
}
|
675
|
catch(Exception e)
|
676
|
{
|
677
|
e.printStackTrace();
|
678
|
fail("Error testing system metadata: " + e.getMessage());
|
679
|
}
|
680
|
}
|
681
|
|
682
|
/**
|
683
|
* create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta)
|
684
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
685
|
* InsufficientResources, InvalidSystemMetadata, NotImplemented
|
686
|
*
|
687
|
* public InputStream get(AuthToken token, Identifier guid)
|
688
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
689
|
* NotImplemented
|
690
|
*/
|
691
|
public void testCreateAndGet()
|
692
|
{
|
693
|
printTestHeader("testCreateAndGet");
|
694
|
try
|
695
|
{
|
696
|
CrudService cs = CrudService.getInstance();
|
697
|
AuthToken token = getToken();
|
698
|
//run create
|
699
|
Identifier id = createDoc(token, getTestDoc());
|
700
|
//make these docs public for debugging purposes.
|
701
|
makeDocPublic(token, id, true);
|
702
|
//compare the docs
|
703
|
String gotDoc = getDoc(token, id);
|
704
|
assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
|
705
|
|
706
|
try
|
707
|
{
|
708
|
Identifier fakeid = new Identifier();
|
709
|
fakeid.setValue("somethingfake.234234");
|
710
|
getDoc(token, fakeid);
|
711
|
fail("testCreateAndGet should have thrown an exception.");
|
712
|
}
|
713
|
catch(Exception e)
|
714
|
{
|
715
|
assertTrue(true);
|
716
|
}
|
717
|
}
|
718
|
catch(Exception e)
|
719
|
{
|
720
|
e.printStackTrace();
|
721
|
fail("Error in testCreate: " + e.getMessage());
|
722
|
}
|
723
|
}
|
724
|
|
725
|
/**
|
726
|
* getInstance()
|
727
|
*/
|
728
|
public void testSingletonAccessor()
|
729
|
{
|
730
|
printTestHeader("testSingletonAccessor");
|
731
|
CrudService cs = CrudService.getInstance();
|
732
|
assertNotNull(cs);
|
733
|
}
|
734
|
|
735
|
/**
|
736
|
* Run an initial test that always passes to check that the test harness is
|
737
|
* working.
|
738
|
*/
|
739
|
public void initialize()
|
740
|
{
|
741
|
printTestHeader("initialize");
|
742
|
assertTrue(1 == 1);
|
743
|
}
|
744
|
|
745
|
/**
|
746
|
* return the systemMetadata object for id
|
747
|
*/
|
748
|
private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
|
749
|
throws Exception
|
750
|
{
|
751
|
CrudService cs = CrudService.getInstance();
|
752
|
return cs.getSystemMetadata(token, id);
|
753
|
}
|
754
|
|
755
|
/**
|
756
|
* get a doc from metacat using CrudService.get()
|
757
|
*/
|
758
|
private String getDoc(AuthToken token, Identifier id)
|
759
|
throws Exception
|
760
|
{
|
761
|
CrudService cs = CrudService.getInstance();
|
762
|
InputStream gotDocStream = cs.get(token, id);
|
763
|
return IOUtils.toString(gotDocStream);
|
764
|
}
|
765
|
|
766
|
/**
|
767
|
* return a test document
|
768
|
*/
|
769
|
private String getTestDoc()
|
770
|
{
|
771
|
return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
|
772
|
}
|
773
|
|
774
|
/**
|
775
|
* authenticate and return a token
|
776
|
*/
|
777
|
private AuthToken getToken()
|
778
|
throws Exception
|
779
|
{
|
780
|
CrudService cs = CrudService.getInstance();
|
781
|
//login and get a sessionid
|
782
|
MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
|
783
|
String username = PropertyService.getProperty("test.mcUser");
|
784
|
String password = PropertyService.getProperty("test.mcPassword");
|
785
|
String response = restClient.login(username, password);
|
786
|
String sessionid = restClient.getSessionId();
|
787
|
SessionService sessionService = SessionService.getInstance();
|
788
|
sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
|
789
|
AuthToken token = new AuthToken(sessionid);
|
790
|
return token;
|
791
|
}
|
792
|
|
793
|
/**
|
794
|
* create a doc using CrudService.create() and return its id
|
795
|
*/
|
796
|
private Identifier createDoc(AuthToken token, String testDoc) throws Exception
|
797
|
{
|
798
|
return createDoc(token, testDoc, ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
799
|
}
|
800
|
|
801
|
/**
|
802
|
* create a doc using CrudService.create() and return its id
|
803
|
*/
|
804
|
private Identifier createDoc(AuthToken token, String testDoc, ObjectFormat format) throws Exception
|
805
|
{
|
806
|
Identifier id;
|
807
|
CrudService cs = CrudService.getInstance();
|
808
|
|
809
|
id = new Identifier();
|
810
|
String docid = generateDocumentId();
|
811
|
id.setValue(docid);
|
812
|
|
813
|
//create the system metadata then run the create method
|
814
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
815
|
SystemMetadata sm = createSystemMetadata(id, testDoc, format);
|
816
|
//create the doc
|
817
|
cs.create(token, id, sbis, sm);
|
818
|
createCount++;
|
819
|
return id;
|
820
|
}
|
821
|
|
822
|
/**
|
823
|
* create a generic SystemMetadata object for testing
|
824
|
*/
|
825
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
|
826
|
throws Exception
|
827
|
{
|
828
|
return createSystemMetadata(id, testDoc,
|
829
|
ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
830
|
}
|
831
|
|
832
|
/**
|
833
|
* create system metadata with a specified id, doc and format
|
834
|
*/
|
835
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc, ObjectFormat format)
|
836
|
throws Exception
|
837
|
{
|
838
|
SystemMetadata sm = new SystemMetadata();
|
839
|
//set the id
|
840
|
sm.setIdentifier(id);
|
841
|
sm.setObjectFormat(format);
|
842
|
//create the checksum
|
843
|
String checksumS = checksum(testDoc);
|
844
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
845
|
Checksum checksum = new Checksum();
|
846
|
checksum.setValue(checksumS);
|
847
|
checksum.setAlgorithm(ca);
|
848
|
sm.setChecksum(checksum);
|
849
|
//set the size
|
850
|
sm.setSize(testDoc.getBytes().length);
|
851
|
//submitter
|
852
|
Principal p = new Principal();
|
853
|
p.setValue("joe");
|
854
|
sm.setSubmitter(p);
|
855
|
sm.setRightsHolder(p);
|
856
|
sm.setDateUploaded(new Date());
|
857
|
sm.setDateSysMetadataModified(new Date());
|
858
|
NodeReference nr = new NodeReference();
|
859
|
nr.setValue("metacat");
|
860
|
sm.setOriginMemberNode(nr);
|
861
|
sm.setAuthoritativeMemberNode(nr);
|
862
|
return sm;
|
863
|
}
|
864
|
|
865
|
/**
|
866
|
* make a document public in metacat by inserting an access document
|
867
|
* @param id
|
868
|
*/
|
869
|
private void makeDocPublic(AuthToken token, Identifier id, boolean systemMetadataToo)
|
870
|
throws Exception
|
871
|
{
|
872
|
CrudService cs = CrudService.getInstance();
|
873
|
cs.setAccess(token, id, "public", "read", "allow", "allowFirst", systemMetadataToo);
|
874
|
}
|
875
|
|
876
|
/**
|
877
|
* print a header to start each test
|
878
|
*/
|
879
|
private void printTestHeader(String testName)
|
880
|
{
|
881
|
System.out.println();
|
882
|
System.out.println("*************** " + testName + " ***************");
|
883
|
}
|
884
|
|
885
|
/**
|
886
|
* produce an md5 checksum for item
|
887
|
*/
|
888
|
private String checksum(String item)
|
889
|
throws Exception
|
890
|
{
|
891
|
StringBufferInputStream fis = new StringBufferInputStream(item);
|
892
|
|
893
|
byte[] buffer = new byte[1024];
|
894
|
MessageDigest complete = MessageDigest.getInstance("MD5");
|
895
|
int numRead;
|
896
|
|
897
|
do
|
898
|
{
|
899
|
numRead = fis.read(buffer);
|
900
|
if (numRead > 0)
|
901
|
{
|
902
|
complete.update(buffer, 0, numRead);
|
903
|
}
|
904
|
} while (numRead != -1);
|
905
|
|
906
|
|
907
|
return getHex(complete.digest());
|
908
|
}
|
909
|
|
910
|
/**
|
911
|
* convert a byte array to a hex string
|
912
|
*/
|
913
|
private static String getHex( byte [] raw )
|
914
|
{
|
915
|
final String HEXES = "0123456789ABCDEF";
|
916
|
if ( raw == null ) {
|
917
|
return null;
|
918
|
}
|
919
|
final StringBuilder hex = new StringBuilder( 2 * raw.length );
|
920
|
for ( final byte b : raw ) {
|
921
|
hex.append(HEXES.charAt((b & 0xF0) >> 4))
|
922
|
.append(HEXES.charAt((b & 0x0F)));
|
923
|
}
|
924
|
return hex.toString();
|
925
|
}
|
926
|
}
|