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: leinfelder $'
|
8
|
* '$Date: 2011-07-07 15:16:56 -0700 (Thu, 07 Jul 2011) $'
|
9
|
* '$Revision: 6322 $'
|
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.ByteArrayInputStream;
|
29
|
import java.io.ByteArrayOutputStream;
|
30
|
import java.io.InputStream;
|
31
|
import java.io.InputStreamReader;
|
32
|
import java.io.Reader;
|
33
|
import java.text.SimpleDateFormat;
|
34
|
import java.util.Date;
|
35
|
|
36
|
import junit.framework.Test;
|
37
|
import junit.framework.TestSuite;
|
38
|
|
39
|
import org.apache.commons.io.IOUtils;
|
40
|
import org.dataone.client.ObjectFormatCache;
|
41
|
import org.dataone.service.Constants;
|
42
|
import org.dataone.service.exceptions.InsufficientResources;
|
43
|
import org.dataone.service.exceptions.InvalidRequest;
|
44
|
import org.dataone.service.exceptions.NotAuthorized;
|
45
|
import org.dataone.service.exceptions.NotFound;
|
46
|
import org.dataone.service.exceptions.NotImplemented;
|
47
|
import org.dataone.service.exceptions.ServiceFailure;
|
48
|
import org.dataone.service.impl.ObjectFormatServiceImpl;
|
49
|
import org.dataone.service.types.AccessPolicy;
|
50
|
import org.dataone.service.types.AccessRule;
|
51
|
import org.dataone.service.types.Checksum;
|
52
|
import org.dataone.service.types.ChecksumAlgorithm;
|
53
|
import org.dataone.service.types.Event;
|
54
|
import org.dataone.service.types.Identifier;
|
55
|
import org.dataone.service.types.Log;
|
56
|
import org.dataone.service.types.NodeReference;
|
57
|
import org.dataone.service.types.ObjectFormat;
|
58
|
import org.dataone.service.types.ObjectFormatIdentifier;
|
59
|
import org.dataone.service.types.ObjectFormatList;
|
60
|
import org.dataone.service.types.ObjectInfo;
|
61
|
import org.dataone.service.types.ObjectList;
|
62
|
import org.dataone.service.types.Permission;
|
63
|
import org.dataone.service.types.Replica;
|
64
|
import org.dataone.service.types.ReplicationPolicy;
|
65
|
import org.dataone.service.types.ReplicationStatus;
|
66
|
import org.dataone.service.types.Session;
|
67
|
import org.dataone.service.types.Subject;
|
68
|
import org.dataone.service.types.SystemMetadata;
|
69
|
import org.dataone.service.types.util.ServiceTypeUtil;
|
70
|
|
71
|
import edu.ucsb.nceas.MCTestCase;
|
72
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
73
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
74
|
|
75
|
/**
|
76
|
* A JUnit test for testing the dataone CNCore implementation
|
77
|
*/
|
78
|
public class CNodeServiceTest extends MCTestCase {
|
79
|
|
80
|
/**
|
81
|
* constructor for the test
|
82
|
*/
|
83
|
public CNodeServiceTest(String name)
|
84
|
{
|
85
|
super(name);
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Establish a testing framework by initializing appropriate objects
|
90
|
*/
|
91
|
public void setUp() throws Exception
|
92
|
{
|
93
|
super.setUp();
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Release any objects after tests are complete
|
98
|
*/
|
99
|
public void tearDown()
|
100
|
{
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Create a suite of tests to be run together
|
105
|
*/
|
106
|
public static Test suite()
|
107
|
{
|
108
|
TestSuite suite = new TestSuite();
|
109
|
suite.addTest(new CNodeServiceTest("initialize"));
|
110
|
|
111
|
suite.addTest(new CNodeServiceTest("testAssertRelation"));
|
112
|
suite.addTest(new CNodeServiceTest("testChecksum"));
|
113
|
suite.addTest(new CNodeServiceTest("testCreate"));
|
114
|
suite.addTest(new CNodeServiceTest("testGet"));
|
115
|
suite.addTest(new CNodeServiceTest("testGetFormat"));
|
116
|
suite.addTest(new CNodeServiceTest("testGetLogRecords"));
|
117
|
suite.addTest(new CNodeServiceTest("testGetSystemMetadata"));
|
118
|
suite.addTest(new CNodeServiceTest("testIsAuthorized"));
|
119
|
suite.addTest(new CNodeServiceTest("testListFormats"));
|
120
|
suite.addTest(new CNodeServiceTest("testListNodes"));
|
121
|
suite.addTest(new CNodeServiceTest("testObjectFormatNotFoundException"));
|
122
|
suite.addTest(new CNodeServiceTest("testRegisterSystemMetadata"));
|
123
|
suite.addTest(new CNodeServiceTest("testReplicationPolicy"));
|
124
|
suite.addTest(new CNodeServiceTest("testReplicationStatus"));
|
125
|
suite.addTest(new CNodeServiceTest("testReserveIdentifier"));
|
126
|
suite.addTest(new CNodeServiceTest("testSearch"));
|
127
|
suite.addTest(new CNodeServiceTest("testSetAccessPolicy"));
|
128
|
suite.addTest(new CNodeServiceTest("testSetOwner"));
|
129
|
|
130
|
return suite;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* constructs a "fake" session with a test subject
|
135
|
* @return
|
136
|
*/
|
137
|
private Session getTestSession() {
|
138
|
Session session = new Session();
|
139
|
Subject subject = new Subject();
|
140
|
subject.setValue("cn=test,dc=dataone,dc=org");
|
141
|
session.setSubject(subject);
|
142
|
return session;
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* test for registering standalone system metadata
|
147
|
*/
|
148
|
public void testRegisterSystemMetadata() {
|
149
|
printTestHeader("testRegisterSystemMetadata");
|
150
|
|
151
|
try {
|
152
|
Session session = getTestSession();
|
153
|
Identifier guid = new Identifier();
|
154
|
guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
|
155
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
156
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
157
|
assertTrue(result);
|
158
|
} catch(Exception e) {
|
159
|
fail("Unexpected error: " + e.getMessage());
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* test for getting system metadata
|
165
|
*/
|
166
|
public void testGetSystemMetadata() {
|
167
|
printTestHeader("testGetSystemMetadata");
|
168
|
|
169
|
try {
|
170
|
Session session = getTestSession();
|
171
|
Identifier guid = new Identifier();
|
172
|
guid.setValue("testGetSystemMetadata." + System.currentTimeMillis());
|
173
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
174
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
175
|
assertTrue(result);
|
176
|
// get it
|
177
|
SystemMetadata retSysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
178
|
// check it
|
179
|
assertEquals(sysmeta.getIdentifier().getValue(), retSysmeta.getIdentifier().getValue());
|
180
|
} catch(Exception e) {
|
181
|
fail("Unexpected error: " + e.getMessage());
|
182
|
}
|
183
|
}
|
184
|
|
185
|
public void testGetLogRecords() {
|
186
|
printTestHeader("testGetLogRecords");
|
187
|
try {
|
188
|
|
189
|
Session session = getTestSession();
|
190
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
191
|
Date fromDate = sdf.parse("2010-01-01");
|
192
|
Date toDate = new Date();
|
193
|
Event event = Event.CREATE;
|
194
|
int start = 0;
|
195
|
int count = 1;
|
196
|
|
197
|
Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate,
|
198
|
event, start, count);
|
199
|
assertNotNull(log);
|
200
|
assertTrue(log.getCount() == count);
|
201
|
assertTrue(log.getStart() == start);
|
202
|
assertTrue(log.getTotal() > 0);
|
203
|
} catch (Exception e) {
|
204
|
e.printStackTrace();
|
205
|
fail("Unexpected error: " + e.getMessage());
|
206
|
}
|
207
|
}
|
208
|
|
209
|
public void testCreate() {
|
210
|
printTestHeader("testCreate");
|
211
|
|
212
|
try {
|
213
|
Session session = getTestSession();
|
214
|
Identifier guid = new Identifier();
|
215
|
guid.setValue("testCreate." + System.currentTimeMillis());
|
216
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
217
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
218
|
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
|
219
|
assertEquals(guid, pid);
|
220
|
} catch(Exception e) {
|
221
|
e.printStackTrace();
|
222
|
fail("Unexpected error: " + e.getMessage());
|
223
|
}
|
224
|
}
|
225
|
|
226
|
public void testGet() {
|
227
|
printTestHeader("testGet");
|
228
|
|
229
|
try {
|
230
|
Session session = getTestSession();
|
231
|
Identifier guid = new Identifier();
|
232
|
guid.setValue("testGet." + System.currentTimeMillis());
|
233
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
234
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
235
|
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
|
236
|
assertEquals(guid, pid);
|
237
|
// get it
|
238
|
InputStream retObject = CNodeService.getInstance().get(session, pid);
|
239
|
// check it
|
240
|
assertTrue(IOUtils.contentEquals(object, retObject));
|
241
|
} catch(Exception e) {
|
242
|
e.printStackTrace();
|
243
|
fail("Unexpected error: " + e.getMessage());
|
244
|
}
|
245
|
}
|
246
|
|
247
|
public void testAssertRelation() {
|
248
|
printTestHeader("testAssertRelation");
|
249
|
|
250
|
try {
|
251
|
Session session = getTestSession();
|
252
|
Identifier guid = new Identifier();
|
253
|
guid.setValue("testAssertRelation." + System.currentTimeMillis());
|
254
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
255
|
Identifier describePid = new Identifier();
|
256
|
describePid.setValue("describePid." + System.currentTimeMillis());
|
257
|
sysmeta.addDescribe(describePid);
|
258
|
// save it
|
259
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
260
|
assertTrue(result);
|
261
|
// save the other
|
262
|
SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject());
|
263
|
result = CNodeService.getInstance().registerSystemMetadata(session, describePid, describeSysmeta);
|
264
|
assertTrue(result);
|
265
|
// check it
|
266
|
result = CNodeService.getInstance().assertRelation(session, guid, "describes", describePid);
|
267
|
assertTrue(result);
|
268
|
} catch(Exception e) {
|
269
|
e.printStackTrace();
|
270
|
fail("Unexpected error: " + e.getMessage());
|
271
|
}
|
272
|
}
|
273
|
|
274
|
public void testChecksum() {
|
275
|
printTestHeader("testChecksum");
|
276
|
|
277
|
try {
|
278
|
Session session = getTestSession();
|
279
|
Identifier guid = new Identifier();
|
280
|
guid.setValue("testChecksum." + System.currentTimeMillis());
|
281
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
282
|
// save it
|
283
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
284
|
assertTrue(result);
|
285
|
// check it
|
286
|
Checksum checksum = CNodeService.getInstance().getChecksum(session, guid);
|
287
|
assertEquals(sysmeta.getChecksum().getValue(), checksum.getValue());
|
288
|
} catch(Exception e) {
|
289
|
fail("Unexpected error: " + e.getMessage());
|
290
|
}
|
291
|
}
|
292
|
|
293
|
public void testListNodes() {
|
294
|
printTestHeader("testListNodes");
|
295
|
|
296
|
try {
|
297
|
CNodeService.getInstance().listNodes();
|
298
|
} catch(NotImplemented e) {
|
299
|
// expecting not implemented
|
300
|
assertTrue(true);
|
301
|
} catch(Exception e) {
|
302
|
fail("Unexpected error: " + e.getMessage());
|
303
|
}
|
304
|
}
|
305
|
|
306
|
public void testReserveIdentifier() {
|
307
|
printTestHeader("testReserveIdentifier");
|
308
|
|
309
|
try {
|
310
|
Session session = getTestSession();
|
311
|
Identifier guid = new Identifier();
|
312
|
guid.setValue("testReserveIdentifier." + System.currentTimeMillis());
|
313
|
// reserve it
|
314
|
Identifier retPid = CNodeService.getInstance().reserveIdentifier(session, guid, null, null);
|
315
|
assertEquals(guid, retPid);
|
316
|
} catch(Exception e) {
|
317
|
e.printStackTrace();
|
318
|
fail("Unexpected error: " + e.getMessage());
|
319
|
}
|
320
|
}
|
321
|
|
322
|
public void testSearch() {
|
323
|
printTestHeader("testSearch");
|
324
|
|
325
|
try {
|
326
|
Session session = getTestSession();
|
327
|
Identifier guid = new Identifier();
|
328
|
guid.setValue("testSearch." + System.currentTimeMillis());
|
329
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
330
|
|
331
|
// save it
|
332
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
333
|
assertTrue(result);
|
334
|
// search for it
|
335
|
ObjectList objectList = CNodeService.getInstance().search(session, null, null);
|
336
|
// check it
|
337
|
assertNotNull(objectList);
|
338
|
result = false;
|
339
|
for (ObjectInfo objectInfo: objectList.getObjectInfoList()) {
|
340
|
Identifier pid = objectInfo.getIdentifier();
|
341
|
if (pid.getValue().equals(guid.getValue())) {
|
342
|
result = true;
|
343
|
break;
|
344
|
}
|
345
|
}
|
346
|
assertTrue(result);
|
347
|
} catch(Exception e) {
|
348
|
fail("Unexpected error: " + e.getMessage());
|
349
|
}
|
350
|
}
|
351
|
|
352
|
public void testSetOwner() {
|
353
|
printTestHeader("testSetOwner");
|
354
|
|
355
|
try {
|
356
|
Session session = getTestSession();
|
357
|
Identifier guid = new Identifier();
|
358
|
guid.setValue("testSetOwner." + System.currentTimeMillis());
|
359
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
360
|
// save it
|
361
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
362
|
assertTrue(result);
|
363
|
Subject rightsHolder = new Subject();
|
364
|
rightsHolder.setValue("newUser");
|
365
|
// set it
|
366
|
Identifier retPid = CNodeService.getInstance().setOwner(session, guid, rightsHolder);
|
367
|
assertEquals(guid, retPid);
|
368
|
// get it
|
369
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
370
|
assertNotNull(sysmeta);
|
371
|
// check it
|
372
|
assertEquals(rightsHolder.getValue(), sysmeta.getRightsHolder().getValue());
|
373
|
|
374
|
} catch(Exception e) {
|
375
|
fail("Unexpected error: " + e.getMessage());
|
376
|
}
|
377
|
}
|
378
|
|
379
|
public void testSetAccessPolicy() {
|
380
|
printTestHeader("testSetAccessPolicy");
|
381
|
|
382
|
try {
|
383
|
Session session = getTestSession();
|
384
|
Identifier guid = new Identifier();
|
385
|
guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
|
386
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
387
|
// save it
|
388
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
389
|
assertTrue(result);
|
390
|
AccessPolicy accessPolicy = new AccessPolicy();
|
391
|
AccessRule accessRule = new AccessRule();
|
392
|
accessRule.addPermission(Permission.WRITE);
|
393
|
Subject publicSubject = new Subject();
|
394
|
publicSubject.setValue(Constants.PUBLIC_SUBJECT);
|
395
|
accessRule.addSubject(publicSubject);
|
396
|
accessPolicy.addAllow(accessRule);
|
397
|
// set it
|
398
|
result = CNodeService.getInstance().setAccessPolicy(session, guid, accessPolicy );
|
399
|
assertTrue(result);
|
400
|
// check it
|
401
|
result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
|
402
|
assertTrue(result);
|
403
|
} catch(Exception e) {
|
404
|
fail("Unexpected error: " + e.getMessage());
|
405
|
}
|
406
|
}
|
407
|
|
408
|
public void testIsAuthorized() {
|
409
|
printTestHeader("testIsAuthorized");
|
410
|
|
411
|
try {
|
412
|
Session session = getTestSession();
|
413
|
Identifier guid = new Identifier();
|
414
|
guid.setValue("testIsAuthorized." + System.currentTimeMillis());
|
415
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
416
|
// save it
|
417
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
418
|
assertTrue(result);
|
419
|
// check it
|
420
|
Subject publicSubject = new Subject();
|
421
|
publicSubject.setValue(Constants.PUBLIC_SUBJECT);
|
422
|
session.setSubject(publicSubject);
|
423
|
// public read
|
424
|
result = CNodeService.getInstance().isAuthorized(session, guid, Permission.READ);
|
425
|
assertTrue(result);
|
426
|
// not public write
|
427
|
try {
|
428
|
result = false;
|
429
|
result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
|
430
|
fail("Public WRITE should be denied");
|
431
|
} catch (NotAuthorized nae) {
|
432
|
result = true;
|
433
|
}
|
434
|
assertTrue(result);
|
435
|
} catch(Exception e) {
|
436
|
fail("Unexpected error: " + e.getMessage());
|
437
|
}
|
438
|
}
|
439
|
|
440
|
public void testReplicationPolicy() {
|
441
|
printTestHeader("testReplicationPolicy");
|
442
|
|
443
|
try {
|
444
|
Session session = getTestSession();
|
445
|
Identifier guid = new Identifier();
|
446
|
guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
|
447
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
448
|
// save it
|
449
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
450
|
assertTrue(result);
|
451
|
|
452
|
ReplicationPolicy policy = new ReplicationPolicy();
|
453
|
NodeReference node = new NodeReference();
|
454
|
node.setValue("testNode");
|
455
|
policy.addPreferredMemberNode(node );
|
456
|
// set it
|
457
|
result = CNodeService.getInstance().setReplicationPolicy(session, guid, policy);
|
458
|
assertTrue(result);
|
459
|
// get it
|
460
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
461
|
assertNotNull(sysmeta);
|
462
|
// check it
|
463
|
assertEquals(policy.getPreferredMemberNode(0).getValue(), sysmeta.getReplicationPolicy().getPreferredMemberNode(0).getValue());
|
464
|
|
465
|
} catch(Exception e) {
|
466
|
fail("Unexpected error: " + e.getMessage());
|
467
|
}
|
468
|
}
|
469
|
|
470
|
public void testReplicationStatus() {
|
471
|
printTestHeader("testReplicationStatus");
|
472
|
|
473
|
try {
|
474
|
Session session = getTestSession();
|
475
|
Identifier guid = new Identifier();
|
476
|
guid.setValue("testReplicationStatus." + System.currentTimeMillis());
|
477
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
|
478
|
Replica replica = new Replica();
|
479
|
NodeReference replicaMemberNode = new NodeReference();
|
480
|
replicaMemberNode.setValue("testNode");
|
481
|
replica.setReplicationStatus(ReplicationStatus.REQUESTED);
|
482
|
replica.setReplicaMemberNode(replicaMemberNode);
|
483
|
replica.setReplicaVerified(new Date());
|
484
|
sysmeta.addReplica(replica );
|
485
|
// save it
|
486
|
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
487
|
assertTrue(result);
|
488
|
// set it
|
489
|
ReplicationStatus status = ReplicationStatus.COMPLETED;
|
490
|
result = CNodeService.getInstance().setReplicationStatus(session, guid, status);
|
491
|
assertTrue(result);
|
492
|
// get it
|
493
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
494
|
assertNotNull(sysmeta);
|
495
|
// check it
|
496
|
assertEquals(status, sysmeta.getReplica(0).getReplicationStatus());
|
497
|
|
498
|
} catch(Exception e) {
|
499
|
fail("Unexpected error: " + e.getMessage());
|
500
|
}
|
501
|
}
|
502
|
|
503
|
/**
|
504
|
* Run an initial test that always passes to check that the test harness is
|
505
|
* working.
|
506
|
*/
|
507
|
public void initialize()
|
508
|
{
|
509
|
printTestHeader("initialize");
|
510
|
assertTrue(1 == 1);
|
511
|
}
|
512
|
|
513
|
/**
|
514
|
* create system metadata with a specified id
|
515
|
*/
|
516
|
private SystemMetadata createSystemMetadata(Identifier id, Subject owner)
|
517
|
throws Exception
|
518
|
{
|
519
|
SystemMetadata sm = new SystemMetadata();
|
520
|
// set the id
|
521
|
sm.setIdentifier(id);
|
522
|
sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
|
523
|
// create the checksum
|
524
|
String checksumS = "test";
|
525
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
526
|
Checksum checksum = new Checksum();
|
527
|
checksum.setValue(checksumS);
|
528
|
checksum.setAlgorithm(ca);
|
529
|
sm.setChecksum(checksum);
|
530
|
// set the size
|
531
|
sm.setSize(0);
|
532
|
sm.setSubmitter(owner);
|
533
|
sm.setRightsHolder(owner);
|
534
|
sm.setDateUploaded(new Date());
|
535
|
sm.setDateSysMetadataModified(new Date());
|
536
|
NodeReference nr = new NodeReference();
|
537
|
nr.setValue("metacat");
|
538
|
sm.setOriginMemberNode(nr);
|
539
|
sm.setAuthoritativeMemberNode(nr);
|
540
|
// set the access to public read
|
541
|
AccessPolicy accessPolicy = new AccessPolicy();
|
542
|
AccessRule allow = new AccessRule();
|
543
|
allow.addPermission(Permission.READ);
|
544
|
Subject publicSubject = new Subject();
|
545
|
publicSubject.setValue(Constants.PUBLIC_SUBJECT);
|
546
|
allow.addSubject(publicSubject);
|
547
|
accessPolicy.addAllow(allow);
|
548
|
sm.setAccessPolicy(accessPolicy);
|
549
|
|
550
|
return sm;
|
551
|
}
|
552
|
|
553
|
/**
|
554
|
* For fresh Metacat installations without the Object Format List
|
555
|
* we insert the default version from d1_common.jar
|
556
|
*/
|
557
|
private void setUpFormats() {
|
558
|
try {
|
559
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
560
|
m.login(username, password);
|
561
|
// check if it exists already
|
562
|
InputStream is = null;
|
563
|
try {
|
564
|
is = m.read(ObjectFormatService.OBJECT_FORMAT_DOCID);
|
565
|
} catch (Exception e) {
|
566
|
// probably missing the doc
|
567
|
}
|
568
|
if (is == null) {
|
569
|
ObjectFormatList formats = ObjectFormatServiceImpl.getInstance().listFormats();
|
570
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
571
|
ServiceTypeUtil.serializeServiceType(ObjectFormatList.class, formats, out);
|
572
|
Reader xmlDocument = new InputStreamReader(new ByteArrayInputStream(out.toByteArray()));
|
573
|
m.insert(ObjectFormatService.OBJECT_FORMAT_DOCID + ".1", xmlDocument, null);
|
574
|
}
|
575
|
m.logout();
|
576
|
} catch (Exception e) {
|
577
|
// any number of things could go wrong
|
578
|
e.printStackTrace();
|
579
|
}
|
580
|
}
|
581
|
|
582
|
/**
|
583
|
* test to list the object formats registered in metacat
|
584
|
*/
|
585
|
public void testListFormats() {
|
586
|
|
587
|
printTestHeader("testListFormats");
|
588
|
|
589
|
// make sure we are set up
|
590
|
setUpFormats();
|
591
|
|
592
|
// there should be at least 59 formats in the list
|
593
|
int formatsCount = 59;
|
594
|
ObjectFormatList objectFormatList;
|
595
|
|
596
|
try {
|
597
|
objectFormatList = CNodeService.getInstance().listFormats();
|
598
|
assertTrue(objectFormatList.getTotal() >= formatsCount);
|
599
|
|
600
|
} catch (InvalidRequest e) {
|
601
|
fail("Could not get the object format list: " + e.getMessage());
|
602
|
|
603
|
} catch (ServiceFailure e) {
|
604
|
fail("Could not get the object format list: " + e.getMessage());
|
605
|
|
606
|
} catch (NotFound e) {
|
607
|
fail("Could not get the object format list: " + e.getMessage());
|
608
|
|
609
|
} catch (InsufficientResources e) {
|
610
|
fail("Could not get the object format list: " + e.getMessage());
|
611
|
|
612
|
} catch (NotImplemented e) {
|
613
|
fail("Could not get the object format list: " + e.getMessage());
|
614
|
|
615
|
}
|
616
|
|
617
|
}
|
618
|
|
619
|
/**
|
620
|
* Test getting a single object format from the registered list
|
621
|
*/
|
622
|
public void testGetFormat() {
|
623
|
|
624
|
printTestHeader("testGetFormat");
|
625
|
|
626
|
// make sure we are set up
|
627
|
setUpFormats();
|
628
|
|
629
|
String knownFormat = "text/plain";
|
630
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
631
|
fmtid.setValue(knownFormat);
|
632
|
|
633
|
try {
|
634
|
|
635
|
String result =
|
636
|
CNodeService.getInstance().getFormat(fmtid).getFmtid().getValue();
|
637
|
System.out.println("Expected result: " + knownFormat);
|
638
|
System.out.println("Found result: " + result);
|
639
|
assertTrue(result.equals(knownFormat));
|
640
|
|
641
|
} catch (NullPointerException npe) {
|
642
|
fail("The returned format was null: " + npe.getMessage());
|
643
|
|
644
|
} catch (NotFound nfe) {
|
645
|
fail("The format " + knownFormat + " was not found: " + nfe.getMessage());
|
646
|
|
647
|
} catch (InvalidRequest ire) {
|
648
|
fail("The format " + knownFormat + " was not found: " + ire.getMessage());
|
649
|
|
650
|
} catch (ServiceFailure sfe) {
|
651
|
fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
|
652
|
|
653
|
} catch (InsufficientResources ise) {
|
654
|
fail("The format " + knownFormat + " was not found: " + ise.getMessage());
|
655
|
|
656
|
} catch (NotImplemented nie) {
|
657
|
fail("The getFormat() method has not been implemented: " + nie.getMessage());
|
658
|
|
659
|
}
|
660
|
|
661
|
}
|
662
|
|
663
|
/**
|
664
|
* Test getting a non-existent object format, returning NotFound
|
665
|
*/
|
666
|
public void testObjectFormatNotFoundException() {
|
667
|
|
668
|
printTestHeader("testObjectFormatNotFoundException");
|
669
|
|
670
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
671
|
String badFormat = "text/bad-format";
|
672
|
fmtid.setValue(badFormat);
|
673
|
|
674
|
try {
|
675
|
|
676
|
ObjectFormat objectFormat =
|
677
|
CNodeService.getInstance().getFormat(fmtid);
|
678
|
|
679
|
} catch (Exception e) {
|
680
|
|
681
|
assertTrue(e instanceof NotFound);
|
682
|
}
|
683
|
|
684
|
}
|
685
|
|
686
|
/**
|
687
|
* print a header to start each test
|
688
|
*/
|
689
|
private void printTestHeader(String testName)
|
690
|
{
|
691
|
System.out.println();
|
692
|
System.out.println("*************** " + testName + " ***************");
|
693
|
}
|
694
|
|
695
|
}
|