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-10-12 11:17:30 -0700 (Wed, 12 Oct 2011) $'
|
9
|
* '$Revision: 6532 $'
|
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.InputStream;
|
30
|
import java.text.SimpleDateFormat;
|
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.util.Constants;
|
38
|
import org.dataone.service.exceptions.InsufficientResources;
|
39
|
import org.dataone.service.exceptions.InvalidRequest;
|
40
|
import org.dataone.service.exceptions.NotAuthorized;
|
41
|
import org.dataone.service.exceptions.NotFound;
|
42
|
import org.dataone.service.exceptions.NotImplemented;
|
43
|
import org.dataone.service.exceptions.ServiceFailure;
|
44
|
import org.dataone.service.types.v1.AccessPolicy;
|
45
|
import org.dataone.service.types.v1.AccessRule;
|
46
|
import org.dataone.service.types.v1.Checksum;
|
47
|
import org.dataone.service.types.v1.Event;
|
48
|
import org.dataone.service.types.v1.Identifier;
|
49
|
import org.dataone.service.types.v1.Log;
|
50
|
import org.dataone.service.types.v1.NodeReference;
|
51
|
import org.dataone.service.types.v1.ObjectFormat;
|
52
|
import org.dataone.service.types.v1.ObjectFormatIdentifier;
|
53
|
import org.dataone.service.types.v1.ObjectFormatList;
|
54
|
import org.dataone.service.types.v1.ObjectInfo;
|
55
|
import org.dataone.service.types.v1.ObjectList;
|
56
|
import org.dataone.service.types.v1.Permission;
|
57
|
import org.dataone.service.types.v1.Replica;
|
58
|
import org.dataone.service.types.v1.ReplicationPolicy;
|
59
|
import org.dataone.service.types.v1.ReplicationStatus;
|
60
|
import org.dataone.service.types.v1.Session;
|
61
|
import org.dataone.service.types.v1.Subject;
|
62
|
import org.dataone.service.types.v1.SystemMetadata;
|
63
|
|
64
|
/**
|
65
|
* A JUnit test for testing the dataone CNCore implementation
|
66
|
*/
|
67
|
public class CNodeServiceTest extends D1NodeServiceTest {
|
68
|
|
69
|
/**
|
70
|
* constructor for the test
|
71
|
*/
|
72
|
public CNodeServiceTest(String name)
|
73
|
{
|
74
|
super(name);
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Create a suite of tests to be run together
|
79
|
*/
|
80
|
public static Test suite()
|
81
|
{
|
82
|
TestSuite suite = new TestSuite();
|
83
|
suite.addTest(new CNodeServiceTest("initialize"));
|
84
|
|
85
|
suite.addTest(new CNodeServiceTest("testAssertRelation"));
|
86
|
suite.addTest(new CNodeServiceTest("testChecksum"));
|
87
|
suite.addTest(new CNodeServiceTest("testCreate"));
|
88
|
suite.addTest(new CNodeServiceTest("testGet"));
|
89
|
suite.addTest(new CNodeServiceTest("testGetFormat"));
|
90
|
suite.addTest(new CNodeServiceTest("testGetLogRecords"));
|
91
|
suite.addTest(new CNodeServiceTest("testGetSystemMetadata"));
|
92
|
suite.addTest(new CNodeServiceTest("testIsAuthorized"));
|
93
|
suite.addTest(new CNodeServiceTest("testListFormats"));
|
94
|
suite.addTest(new CNodeServiceTest("testListNodes"));
|
95
|
suite.addTest(new CNodeServiceTest("testObjectFormatNotFoundException"));
|
96
|
suite.addTest(new CNodeServiceTest("testRegisterSystemMetadata"));
|
97
|
suite.addTest(new CNodeServiceTest("testReplicationPolicy"));
|
98
|
suite.addTest(new CNodeServiceTest("testReplicationStatus"));
|
99
|
suite.addTest(new CNodeServiceTest("testReserveIdentifier"));
|
100
|
suite.addTest(new CNodeServiceTest("testSearch"));
|
101
|
suite.addTest(new CNodeServiceTest("testSetAccessPolicy"));
|
102
|
suite.addTest(new CNodeServiceTest("testSetOwner"));
|
103
|
|
104
|
return suite;
|
105
|
}
|
106
|
|
107
|
|
108
|
/**
|
109
|
* test for registering standalone system metadata
|
110
|
*/
|
111
|
public Identifier testRegisterSystemMetadata() {
|
112
|
printTestHeader("testRegisterSystemMetadata");
|
113
|
|
114
|
try {
|
115
|
Session session = getTestSession();
|
116
|
Identifier guid = new Identifier();
|
117
|
guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
|
118
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
119
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
120
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
121
|
assertEquals(guid.getValue(), retGuid.getValue());
|
122
|
return retGuid;
|
123
|
} catch(Exception e) {
|
124
|
fail("Unexpected error: " + e.getMessage());
|
125
|
}
|
126
|
return null;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* test for getting system metadata
|
131
|
*/
|
132
|
public void testGetSystemMetadata() {
|
133
|
printTestHeader("testGetSystemMetadata");
|
134
|
|
135
|
try {
|
136
|
Session session = getTestSession();
|
137
|
Identifier guid = new Identifier();
|
138
|
guid.setValue("testGetSystemMetadata." + System.currentTimeMillis());
|
139
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
140
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
141
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
142
|
assertEquals(guid.getValue(), retGuid.getValue());
|
143
|
// get it
|
144
|
SystemMetadata retSysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
145
|
// check it
|
146
|
assertEquals(sysmeta.getIdentifier().getValue(), retSysmeta.getIdentifier().getValue());
|
147
|
} catch(Exception e) {
|
148
|
fail("Unexpected error: " + e.getMessage());
|
149
|
}
|
150
|
}
|
151
|
|
152
|
public void testGetLogRecords() {
|
153
|
printTestHeader("testGetLogRecords");
|
154
|
try {
|
155
|
|
156
|
Session session = getTestSession();
|
157
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
158
|
Date fromDate = sdf.parse("2010-01-01");
|
159
|
Date toDate = new Date();
|
160
|
Event event = Event.CREATE;
|
161
|
int start = 0;
|
162
|
int count = 1;
|
163
|
|
164
|
Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate,
|
165
|
event, start, count);
|
166
|
assertNotNull(log);
|
167
|
assertTrue(log.getCount() == count);
|
168
|
assertTrue(log.getStart() == start);
|
169
|
assertTrue(log.getTotal() > 0);
|
170
|
} catch (Exception e) {
|
171
|
e.printStackTrace();
|
172
|
fail("Unexpected error: " + e.getMessage());
|
173
|
}
|
174
|
}
|
175
|
|
176
|
public void testCreate() {
|
177
|
printTestHeader("testCreate");
|
178
|
|
179
|
try {
|
180
|
Session session = getTestSession();
|
181
|
Identifier guid = new Identifier();
|
182
|
guid.setValue("testCreate." + System.currentTimeMillis());
|
183
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
184
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
185
|
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
|
186
|
assertEquals(guid, pid);
|
187
|
} catch(Exception e) {
|
188
|
e.printStackTrace();
|
189
|
fail("Unexpected error: " + e.getMessage());
|
190
|
}
|
191
|
}
|
192
|
|
193
|
public void testGet() {
|
194
|
printTestHeader("testGet");
|
195
|
|
196
|
try {
|
197
|
Session session = getTestSession();
|
198
|
Identifier guid = new Identifier();
|
199
|
guid.setValue("testGet." + System.currentTimeMillis());
|
200
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
201
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
202
|
Identifier pid = CNodeService.getInstance().create(session, guid, object, sysmeta);
|
203
|
assertEquals(guid.getValue(), pid.getValue());
|
204
|
// get it
|
205
|
InputStream retObject = CNodeService.getInstance().get(session, pid);
|
206
|
// check it
|
207
|
object.reset();
|
208
|
assertTrue(IOUtils.contentEquals(object, retObject));
|
209
|
} catch(Exception e) {
|
210
|
e.printStackTrace();
|
211
|
fail("Unexpected error: " + e.getMessage());
|
212
|
}
|
213
|
}
|
214
|
|
215
|
public void testAssertRelation() {
|
216
|
printTestHeader("testAssertRelation");
|
217
|
|
218
|
try {
|
219
|
Session session = getTestSession();
|
220
|
Identifier guid = new Identifier();
|
221
|
guid.setValue("testAssertRelation." + System.currentTimeMillis());
|
222
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
223
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
224
|
Identifier describePid = new Identifier();
|
225
|
describePid.setValue("describePid." + System.currentTimeMillis());
|
226
|
sysmeta.setObsoletes(describePid);
|
227
|
// save it
|
228
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
229
|
assertEquals(guid.getValue(), retGuid.getValue());
|
230
|
// save the other
|
231
|
InputStream object2 = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
232
|
SystemMetadata describeSysmeta = createSystemMetadata(describePid, session.getSubject(), object2);
|
233
|
Identifier retDescribePid = CNodeService.getInstance().registerSystemMetadata(session, describePid, describeSysmeta);
|
234
|
assertEquals(describePid.getValue(), retDescribePid.getValue());
|
235
|
// check it
|
236
|
boolean result = CNodeService.getInstance().assertRelation(session, guid, "obsoletes", describePid);
|
237
|
assertTrue(result);
|
238
|
} catch(Exception e) {
|
239
|
e.printStackTrace();
|
240
|
fail("Unexpected error: " + e.getMessage());
|
241
|
}
|
242
|
}
|
243
|
|
244
|
public void testChecksum() {
|
245
|
printTestHeader("testChecksum");
|
246
|
|
247
|
try {
|
248
|
Session session = getTestSession();
|
249
|
Identifier guid = new Identifier();
|
250
|
guid.setValue("testChecksum." + System.currentTimeMillis());
|
251
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
252
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
253
|
// save it
|
254
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
255
|
assertEquals(guid.getValue(), retGuid.getValue());
|
256
|
// check it
|
257
|
Checksum checksum = CNodeService.getInstance().getChecksum(session, guid);
|
258
|
assertEquals(sysmeta.getChecksum().getValue(), checksum.getValue());
|
259
|
} catch(Exception e) {
|
260
|
fail("Unexpected error: " + e.getMessage());
|
261
|
}
|
262
|
}
|
263
|
|
264
|
public void testListNodes() {
|
265
|
printTestHeader("testListNodes");
|
266
|
|
267
|
try {
|
268
|
CNodeService.getInstance().listNodes();
|
269
|
} catch(NotImplemented e) {
|
270
|
// expecting not implemented
|
271
|
assertTrue(true);
|
272
|
} catch(Exception e) {
|
273
|
fail("Unexpected error: " + e.getMessage());
|
274
|
}
|
275
|
}
|
276
|
|
277
|
public void testReserveIdentifier() {
|
278
|
printTestHeader("testReserveIdentifier");
|
279
|
|
280
|
try {
|
281
|
Session session = getTestSession();
|
282
|
Identifier guid = new Identifier();
|
283
|
guid.setValue("testReserveIdentifier." + System.currentTimeMillis());
|
284
|
// reserve it
|
285
|
boolean result = CNodeService.getInstance().reserveIdentifier(session, guid);
|
286
|
assertTrue(result);
|
287
|
} catch(NotImplemented ni) {
|
288
|
// this is not implemented in Metacat
|
289
|
assertTrue(true);
|
290
|
} catch(Exception e) {
|
291
|
e.printStackTrace();
|
292
|
fail("Unexpected error: " + e.getMessage());
|
293
|
}
|
294
|
}
|
295
|
|
296
|
public void testSearch() {
|
297
|
printTestHeader("testSearch");
|
298
|
|
299
|
try {
|
300
|
Session session = getTestSession();
|
301
|
Identifier guid = new Identifier();
|
302
|
guid.setValue("testSearch." + System.currentTimeMillis());
|
303
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
304
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
305
|
|
306
|
// save it
|
307
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
308
|
assertEquals(guid.getValue(), retGuid.getValue());
|
309
|
// search for it
|
310
|
ObjectList objectList = CNodeService.getInstance().search(session, null, null);
|
311
|
// check it
|
312
|
assertNotNull(objectList);
|
313
|
boolean result = false;
|
314
|
for (ObjectInfo objectInfo: objectList.getObjectInfoList()) {
|
315
|
Identifier pid = objectInfo.getIdentifier();
|
316
|
if (pid.getValue().equals(guid.getValue())) {
|
317
|
result = true;
|
318
|
break;
|
319
|
}
|
320
|
}
|
321
|
assertTrue(result);
|
322
|
} catch(Exception e) {
|
323
|
fail("Unexpected error: " + e.getMessage());
|
324
|
}
|
325
|
}
|
326
|
|
327
|
public void testSetOwner() {
|
328
|
printTestHeader("testSetOwner");
|
329
|
|
330
|
try {
|
331
|
Session session = getTestSession();
|
332
|
Identifier guid = new Identifier();
|
333
|
guid.setValue("testSetOwner." + System.currentTimeMillis());
|
334
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
335
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
336
|
// save it
|
337
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
338
|
assertEquals(guid.getValue(), retGuid.getValue());
|
339
|
Subject rightsHolder = new Subject();
|
340
|
rightsHolder.setValue("newUser");
|
341
|
// set it
|
342
|
Identifier retPid = CNodeService.getInstance().setOwner(session, guid, rightsHolder);
|
343
|
assertEquals(guid, retPid);
|
344
|
// get it
|
345
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
346
|
assertNotNull(sysmeta);
|
347
|
// check it
|
348
|
assertEquals(rightsHolder.getValue(), sysmeta.getRightsHolder().getValue());
|
349
|
|
350
|
} catch(Exception e) {
|
351
|
fail("Unexpected error: " + e.getMessage());
|
352
|
}
|
353
|
}
|
354
|
|
355
|
public void testSetAccessPolicy() {
|
356
|
printTestHeader("testSetAccessPolicy");
|
357
|
|
358
|
try {
|
359
|
Session session = getTestSession();
|
360
|
Identifier guid = new Identifier();
|
361
|
guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
|
362
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
363
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
364
|
// save it
|
365
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
366
|
assertEquals(guid.getValue(), retGuid.getValue());
|
367
|
AccessPolicy accessPolicy = new AccessPolicy();
|
368
|
AccessRule accessRule = new AccessRule();
|
369
|
accessRule.addPermission(Permission.WRITE);
|
370
|
Subject publicSubject = new Subject();
|
371
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
372
|
accessRule.addSubject(publicSubject);
|
373
|
accessPolicy.addAllow(accessRule);
|
374
|
// set it
|
375
|
boolean result = CNodeService.getInstance().setAccessPolicy(session, guid, accessPolicy );
|
376
|
assertTrue(result);
|
377
|
// check it
|
378
|
result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
|
379
|
assertTrue(result);
|
380
|
} catch(Exception e) {
|
381
|
fail("Unexpected error: " + e.getMessage());
|
382
|
}
|
383
|
}
|
384
|
|
385
|
public void testIsAuthorized() {
|
386
|
printTestHeader("testIsAuthorized");
|
387
|
|
388
|
try {
|
389
|
Session session = getTestSession();
|
390
|
Identifier guid = new Identifier();
|
391
|
guid.setValue("testIsAuthorized." + System.currentTimeMillis());
|
392
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
393
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
394
|
// save it
|
395
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
396
|
assertEquals(guid.getValue(), retGuid.getValue());
|
397
|
// check it
|
398
|
Subject publicSubject = new Subject();
|
399
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
400
|
session.setSubject(publicSubject);
|
401
|
// public read
|
402
|
boolean result = CNodeService.getInstance().isAuthorized(session, guid, Permission.READ);
|
403
|
assertTrue(result);
|
404
|
// not public write
|
405
|
try {
|
406
|
result = false;
|
407
|
result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
|
408
|
fail("Public WRITE should be denied");
|
409
|
} catch (NotAuthorized nae) {
|
410
|
result = true;
|
411
|
}
|
412
|
assertTrue(result);
|
413
|
} catch(Exception e) {
|
414
|
fail("Unexpected error: " + e.getMessage());
|
415
|
}
|
416
|
}
|
417
|
|
418
|
public void testReplicationPolicy() {
|
419
|
printTestHeader("testReplicationPolicy");
|
420
|
|
421
|
try {
|
422
|
Session session = getTestSession();
|
423
|
Identifier guid = new Identifier();
|
424
|
guid.setValue("testReplicationPolicy." + System.currentTimeMillis());
|
425
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
426
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
427
|
// save it
|
428
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
429
|
assertEquals(guid.getValue(), retGuid.getValue());
|
430
|
|
431
|
ReplicationPolicy policy = new ReplicationPolicy();
|
432
|
NodeReference node = new NodeReference();
|
433
|
node.setValue("testNode");
|
434
|
policy.addPreferredMemberNode(node );
|
435
|
// set it
|
436
|
boolean result = CNodeService.getInstance().setReplicationPolicy(session, guid, policy);
|
437
|
assertTrue(result);
|
438
|
// get it
|
439
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
440
|
assertNotNull(sysmeta);
|
441
|
// check it
|
442
|
assertEquals(policy.getPreferredMemberNode(0).getValue(), sysmeta.getReplicationPolicy().getPreferredMemberNode(0).getValue());
|
443
|
|
444
|
} catch(Exception e) {
|
445
|
fail("Unexpected error: " + e.getMessage());
|
446
|
}
|
447
|
}
|
448
|
|
449
|
public void testReplicationStatus() {
|
450
|
printTestHeader("testReplicationStatus");
|
451
|
|
452
|
try {
|
453
|
Session session = getTestSession();
|
454
|
Identifier guid = new Identifier();
|
455
|
guid.setValue("testReplicationStatus." + System.currentTimeMillis());
|
456
|
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
457
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
458
|
Replica replica = new Replica();
|
459
|
NodeReference replicaMemberNode = new NodeReference();
|
460
|
replicaMemberNode.setValue("testNode");
|
461
|
replica.setReplicationStatus(ReplicationStatus.REQUESTED);
|
462
|
replica.setReplicaMemberNode(replicaMemberNode);
|
463
|
replica.setReplicaVerified(new Date());
|
464
|
sysmeta.addReplica(replica );
|
465
|
// save it
|
466
|
Identifier retGuid = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
467
|
assertEquals(guid.getValue(), retGuid.getValue());
|
468
|
// set it
|
469
|
ReplicationStatus status = ReplicationStatus.COMPLETED;
|
470
|
boolean result = CNodeService.getInstance().setReplicationStatus(session, guid, replicaMemberNode, status);
|
471
|
assertTrue(result);
|
472
|
// get it
|
473
|
sysmeta = CNodeService.getInstance().getSystemMetadata(session, guid);
|
474
|
assertNotNull(sysmeta);
|
475
|
// check it
|
476
|
assertEquals(status, sysmeta.getReplica(0).getReplicationStatus());
|
477
|
|
478
|
} catch(Exception e) {
|
479
|
fail("Unexpected error: " + e.getMessage());
|
480
|
}
|
481
|
}
|
482
|
|
483
|
/**
|
484
|
* Run an initial test that always passes to check that the test harness is
|
485
|
* working.
|
486
|
*/
|
487
|
public void initialize()
|
488
|
{
|
489
|
printTestHeader("initialize");
|
490
|
assertTrue(1 == 1);
|
491
|
}
|
492
|
|
493
|
|
494
|
/**
|
495
|
* test to list the object formats registered in metacat
|
496
|
*/
|
497
|
public void testListFormats() {
|
498
|
|
499
|
printTestHeader("testListFormats");
|
500
|
|
501
|
// make sure we are set up
|
502
|
setUpFormats();
|
503
|
|
504
|
// there should be at least 59 formats in the list
|
505
|
int formatsCount = 59;
|
506
|
ObjectFormatList objectFormatList;
|
507
|
|
508
|
try {
|
509
|
objectFormatList = CNodeService.getInstance().listFormats();
|
510
|
assertTrue(objectFormatList.getTotal() >= formatsCount);
|
511
|
|
512
|
} catch (InvalidRequest e) {
|
513
|
fail("Could not get the object format list: " + e.getMessage());
|
514
|
|
515
|
} catch (ServiceFailure e) {
|
516
|
fail("Could not get the object format list: " + e.getMessage());
|
517
|
|
518
|
} catch (NotFound e) {
|
519
|
fail("Could not get the object format list: " + e.getMessage());
|
520
|
|
521
|
} catch (InsufficientResources e) {
|
522
|
fail("Could not get the object format list: " + e.getMessage());
|
523
|
|
524
|
} catch (NotImplemented e) {
|
525
|
fail("Could not get the object format list: " + e.getMessage());
|
526
|
|
527
|
}
|
528
|
|
529
|
}
|
530
|
|
531
|
/**
|
532
|
* Test getting a single object format from the registered list
|
533
|
*/
|
534
|
public void testGetFormat() {
|
535
|
|
536
|
printTestHeader("testGetFormat");
|
537
|
|
538
|
// make sure we are set up
|
539
|
setUpFormats();
|
540
|
|
541
|
String knownFormat = "text/plain";
|
542
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
543
|
fmtid.setValue(knownFormat);
|
544
|
|
545
|
try {
|
546
|
|
547
|
String result =
|
548
|
CNodeService.getInstance().getFormat(fmtid).getFmtid().getValue();
|
549
|
System.out.println("Expected result: " + knownFormat);
|
550
|
System.out.println("Found result: " + result);
|
551
|
assertTrue(result.equals(knownFormat));
|
552
|
|
553
|
} catch (NullPointerException npe) {
|
554
|
fail("The returned format was null: " + npe.getMessage());
|
555
|
|
556
|
} catch (NotFound nfe) {
|
557
|
fail("The format " + knownFormat + " was not found: " + nfe.getMessage());
|
558
|
|
559
|
} catch (InvalidRequest ire) {
|
560
|
fail("The format " + knownFormat + " was not found: " + ire.getMessage());
|
561
|
|
562
|
} catch (ServiceFailure sfe) {
|
563
|
fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
|
564
|
|
565
|
} catch (InsufficientResources ise) {
|
566
|
fail("The format " + knownFormat + " was not found: " + ise.getMessage());
|
567
|
|
568
|
} catch (NotImplemented nie) {
|
569
|
fail("The getFormat() method has not been implemented: " + nie.getMessage());
|
570
|
|
571
|
}
|
572
|
|
573
|
}
|
574
|
|
575
|
/**
|
576
|
* Test getting a non-existent object format, returning NotFound
|
577
|
*/
|
578
|
public void testObjectFormatNotFoundException() {
|
579
|
|
580
|
printTestHeader("testObjectFormatNotFoundException");
|
581
|
|
582
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
583
|
String badFormat = "text/bad-format";
|
584
|
fmtid.setValue(badFormat);
|
585
|
|
586
|
try {
|
587
|
|
588
|
ObjectFormat objectFormat =
|
589
|
CNodeService.getInstance().getFormat(fmtid);
|
590
|
|
591
|
} catch (Exception e) {
|
592
|
|
593
|
assertTrue(e instanceof NotFound);
|
594
|
}
|
595
|
|
596
|
}
|
597
|
|
598
|
}
|