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