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