Project

General

Profile

« Previous | Next » 

Revision 6309

Added by Chris Jones over 13 years ago

Add MNodeServiceTest tests for MNCore, MNRead, MNStorage, MNAuthorization implementations. MNReplication remains to be tested, as does MNCore.getCapabilities() and getOperationStatistics().

View differences:

test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java
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:$'
8
 *     '$Date:$'
9
 * '$Revision:$'
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 static org.junit.Assert.*;
29

  
30
import java.io.ByteArrayInputStream;
31
import java.io.InputStream;
32
import java.io.StringBufferInputStream;
33
import java.io.UnsupportedEncodingException;
34
import java.security.MessageDigest;
35
import java.util.Date;
36

  
37
import junit.framework.Test;
38
import junit.framework.TestSuite;
39

  
40
import org.dataone.client.ObjectFormatCache;
41
import org.dataone.service.Constants;
42
import org.dataone.service.exceptions.IdentifierNotUnique;
43
import org.dataone.service.exceptions.InsufficientResources;
44
import org.dataone.service.exceptions.InvalidRequest;
45
import org.dataone.service.exceptions.InvalidSystemMetadata;
46
import org.dataone.service.exceptions.InvalidToken;
47
import org.dataone.service.exceptions.NotAuthorized;
48
import org.dataone.service.exceptions.NotFound;
49
import org.dataone.service.exceptions.NotImplemented;
50
import org.dataone.service.exceptions.ServiceFailure;
51
import org.dataone.service.exceptions.UnsupportedType;
52
import org.dataone.service.types.AccessPolicy;
53
import org.dataone.service.types.AccessRule;
54
import org.dataone.service.types.Checksum;
55
import org.dataone.service.types.ChecksumAlgorithm;
56
import org.dataone.service.types.DescribeResponse;
57
import org.dataone.service.types.Event;
58
import org.dataone.service.types.Identifier;
59
import org.dataone.service.types.Log;
60
import org.dataone.service.types.NodeReference;
61
import org.dataone.service.types.ObjectFormat;
62
import org.dataone.service.types.ObjectList;
63
import org.dataone.service.types.Permission;
64
import org.dataone.service.types.Session;
65
import org.dataone.service.types.Subject;
66
import org.dataone.service.types.SystemMetadata;
67
import org.junit.After;
68
import org.junit.Before;
69

  
70
import edu.ucsb.nceas.MCTestCase;
71

  
72
/**
73
 * A JUnit test to exercise the Metacat Member Node service implementation.
74
 * This also tests a few of the D1NodeService superclass methods
75
 * 
76
 * @author cjones
77
 *
78
 */
79
public class MNodeServiceTest extends MCTestCase {
80

  
81
	/**
82
	 * Set up the test fixtures
83
	 * 
84
	 * @throws Exception
85
	 */
86
	@Before
87
	public void setUp() throws Exception {
88
		super.setUp();
89
		
90
	}
91

  
92
	/**
93
	 * Remove the test fixtures
94
	 * 
95
	 * @throws Exception
96
	 */
97
	@After
98
	public void tearDown() throws Exception {
99
	}
100
	
101
	/**
102
	 * Build the test suite
103
	 * @return
104
	 */
105
	public static Test suite() {
106
		
107
		TestSuite suite = new TestSuite();
108
    suite.addTest(new MNodeServiceTest("initialize"));
109
    // MNStorage tests
110
    suite.addTest(new MNodeServiceTest("testCreate"));
111
    suite.addTest(new MNodeServiceTest("testUpdate"));
112
    suite.addTest(new MNodeServiceTest("testDelete"));
113
    // MNRead tests
114
    suite.addTest(new MNodeServiceTest("testGet"));
115
    suite.addTest(new MNodeServiceTest("testGetChecksum"));
116
    suite.addTest(new MNodeServiceTest("testGetSystemMetadata"));
117
    suite.addTest(new MNodeServiceTest("testDescribe"));
118
    // MNCore tests
119
    suite.addTest(new MNodeServiceTest("testPing"));
120
    suite.addTest(new MNodeServiceTest("testGetLogRecords"));
121
    // suite.addTest(new MNodeServiceTest("testGetOperationStatistics"));
122
    // suite.addTest(new MNodeServiceTest("testGetCapabilities"));
123
    // MNAuthorization tests
124
    suite.addTest(new MNodeServiceTest("testIsAuthorized"));
125
     suite.addTest(new MNodeServiceTest("testSetAccessPolicy"));
126
    // MNreplication tests
127
    // suite.addTest(new MNodeServiceTest("testReplicate"));
128
    // suite.addTest(new MNodeServiceTest("testSynchronizationFailed"));
129
    
130
		
131
		return suite;
132
		
133
	}
134
	
135
	/**
136
	 * Constructor for the tests
137
	 * 
138
	 * @param name - the name of the test
139
	 */
140
	public MNodeServiceTest(String name) {
141
		super(name);
142
		
143
	}
144

  
145
	/**
146
	 * Initial blank test
147
	 */
148
	public void initialize() {
149
		assertTrue(1 == 1);
150
		
151
	}
152
	
153
	/**
154
	 * Test getting a known object
155
	 */
156
	public void testGet() {
157
    printTestHeader("testGet");
158

  
159
		try {
160
	    Session session = getTestSession();
161
	    Identifier guid = new Identifier();
162
	    guid.setValue("testGet." + System.currentTimeMillis());
163
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
164
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
165
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
166
	    InputStream result = MNodeService.getInstance().get(session, guid);
167
	    assertEquals(result, object);
168
	    
169
    } catch (UnsupportedEncodingException e) {
170
	    e.printStackTrace();
171
	    fail("Unexpected error: " + e.getMessage());
172

  
173
    } catch (InvalidToken e) {
174
	    e.printStackTrace();
175
	    fail("Unexpected error: " + e.getMessage());
176
	    
177
    } catch (ServiceFailure e) {
178
	    e.printStackTrace();
179
	    fail("Unexpected error: " + e.getMessage());
180
	    
181
    } catch (NotAuthorized e) {
182
	    e.printStackTrace();
183
	    fail("Unexpected error: " + e.getMessage());
184
	    
185
    } catch (IdentifierNotUnique e) {
186
	    e.printStackTrace();
187
	    fail("Unexpected error: " + e.getMessage());
188
	    
189
    } catch (UnsupportedType e) {
190
	    e.printStackTrace();
191
	    fail("Unexpected error: " + e.getMessage());
192
	    
193
    } catch (InsufficientResources e) {
194
	    e.printStackTrace();
195
	    fail("Unexpected error: " + e.getMessage());
196
	    
197
    } catch (InvalidSystemMetadata e) {
198
	    e.printStackTrace();
199
	    fail("Unexpected error: " + e.getMessage());
200
	    
201
    } catch (NotImplemented e) {
202
	    e.printStackTrace();
203
	    fail("Unexpected error: " + e.getMessage());
204
	    
205
    } catch (InvalidRequest e) {
206
	    e.printStackTrace();
207
	    fail("Unexpected error: " + e.getMessage());
208
	    
209
    } catch (NotFound e) {
210
	    e.printStackTrace();
211
	    fail("Unexpected error: " + e.getMessage());
212
	    
213
    } catch (Exception e) {
214
	    e.printStackTrace();
215
	    fail("Unexpected error: " + e.getMessage());
216
	    
217
    }
218

  
219
	}
220

  
221
	/**
222
	 * Test getting the system metadata of an object
223
	 */
224
	public void testGetSystemMetadata() {    
225
    printTestHeader("testGetSystemMetadata");
226

  
227
    try {
228
	    Session session = getTestSession();
229
	    Identifier guid = new Identifier();
230
	    guid.setValue("testGetSystemMetadata." + System.currentTimeMillis());
231
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
232
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
233
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
234
	    SystemMetadata newsysmeta = MNodeService.getInstance().getSystemMetadata(session, pid);
235
	    assertEquals(newsysmeta, sysmeta);
236
	    
237
    } catch (UnsupportedEncodingException e) {
238
	    e.printStackTrace();
239
	    fail("Unexpected error: " + e.getMessage());
240

  
241
    } catch (InvalidToken e) {
242
	    e.printStackTrace();	    
243
	    fail("Unexpected error: " + e.getMessage());
244

  
245
    } catch (ServiceFailure e) {
246
	    e.printStackTrace();
247
	    fail("Unexpected error: " + e.getMessage());
248

  
249
    } catch (NotAuthorized e) {
250
	    e.printStackTrace();
251
	    fail("Unexpected error: " + e.getMessage());
252

  
253
    } catch (IdentifierNotUnique e) {
254
	    e.printStackTrace();
255
	    fail("Unexpected error: " + e.getMessage());
256
	    
257
    } catch (UnsupportedType e) {
258
	    e.printStackTrace();
259
	    fail("Unexpected error: " + e.getMessage());
260

  
261
    } catch (InsufficientResources e) {
262
	    e.printStackTrace();
263
	    fail("Unexpected error: " + e.getMessage());
264

  
265
    } catch (InvalidSystemMetadata e) {
266
	    e.printStackTrace();
267
	    fail("Unexpected error: " + e.getMessage());
268

  
269
    } catch (NotImplemented e) {
270
	    e.printStackTrace();
271
	    fail("Unexpected error: " + e.getMessage());
272

  
273
    } catch (InvalidRequest e) {
274
	    e.printStackTrace();
275
	    fail("Unexpected error: " + e.getMessage());
276

  
277
    } catch (NotFound e) {
278
	    e.printStackTrace();
279
	    fail("Unexpected error: " + e.getMessage());
280

  
281
    } catch (Exception e) {
282
	    e.printStackTrace();
283
	    fail("Unexpected error: " + e.getMessage());
284

  
285
    }
286
    
287
	}
288

  
289
	/**
290
	 * Test object creation
291
	 */
292
	public void testCreate() {
293
    printTestHeader("testCreate");
294
    
295
		try {
296
	    Session session = getTestSession();
297
	    Identifier guid = new Identifier();
298
	    guid.setValue("testCreate." + System.currentTimeMillis());
299
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
300
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
301
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
302
	    assertEquals(guid, pid);
303
    } catch (UnsupportedEncodingException e) {
304
	    e.printStackTrace();
305
	    fail("Unexpected error: " + e.getMessage());
306

  
307
    } catch (InvalidToken e) {
308
	    e.printStackTrace();
309
	    fail("Unexpected error: " + e.getMessage());
310

  
311
    } catch (ServiceFailure e) {
312
	    e.printStackTrace();
313
	    fail("Unexpected error: " + e.getMessage());
314

  
315
    } catch (NotAuthorized e) {
316
	    e.printStackTrace();
317
	    fail("Unexpected error: " + e.getMessage());
318

  
319
    } catch (IdentifierNotUnique e) {
320
	    e.printStackTrace();
321
	    fail("Unexpected error: " + e.getMessage());
322

  
323
    } catch (UnsupportedType e) {
324
	    e.printStackTrace();
325
	    fail("Unexpected error: " + e.getMessage());
326

  
327
    } catch (InsufficientResources e) {
328
	    e.printStackTrace();
329
	    fail("Unexpected error: " + e.getMessage());
330

  
331
    } catch (InvalidSystemMetadata e) {
332
	    e.printStackTrace();
333
	    fail("Unexpected error: " + e.getMessage());
334

  
335
    } catch (NotImplemented e) {
336
	    e.printStackTrace();
337
	    fail("Unexpected error: " + e.getMessage());
338

  
339
    } catch (InvalidRequest e) {
340
	    e.printStackTrace();
341
	    fail("Unexpected error: " + e.getMessage());
342

  
343
    } catch (Exception e) {
344
	    e.printStackTrace();
345
	    fail("Unexpected error: " + e.getMessage());
346

  
347
    }
348
      
349
  }
350

  
351
	/**
352
	 * test object deletion
353
	 */
354
	public void testDelete() {
355
    printTestHeader("testDelete");
356

  
357
		try {
358
	    Session session = getTestSession();
359
	    Identifier guid = new Identifier();
360
	    guid.setValue("testDelete." + System.currentTimeMillis());
361
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
362
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
363
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
364
      Identifier deletedPid = MNodeService.getInstance().delete(session, pid);
365
      assertEquals(pid, deletedPid);
366
      
367
		} catch (UnsupportedEncodingException e) {
368
	    e.printStackTrace();
369
	    
370
    } catch (InvalidToken e) {
371
	    e.printStackTrace();
372
	    
373
    } catch (ServiceFailure e) {
374
	    e.printStackTrace();
375
	    fail("Unexpected error: " + e.getMessage());
376

  
377
    } catch (NotAuthorized e) {
378
	    e.printStackTrace();
379
	    fail("Unexpected error: " + e.getMessage());
380
	    
381
    } catch (IdentifierNotUnique e) {
382
	    e.printStackTrace();
383
	    fail("Unexpected error: " + e.getMessage());
384
	    
385
    } catch (UnsupportedType e) {
386
	    e.printStackTrace();
387
	    fail("Unexpected error: " + e.getMessage());
388
	    
389
    } catch (InsufficientResources e) {
390
	    e.printStackTrace();
391
	    fail("Unexpected error: " + e.getMessage());
392
	    
393
    } catch (InvalidSystemMetadata e) {
394
	    e.printStackTrace();
395
	    fail("Unexpected error: " + e.getMessage());
396
	    
397
    } catch (NotImplemented e) {
398
	    e.printStackTrace();
399
	    fail("Unexpected error: " + e.getMessage());
400
	    
401
    } catch (InvalidRequest e) {
402
	    e.printStackTrace();
403
	    fail("Unexpected error: " + e.getMessage());
404
	    
405
    } catch (Exception e) {
406
	    e.printStackTrace();
407
	    fail("Unexpected error: " + e.getMessage());
408
	    
409
    }
410

  
411
	}
412

  
413
	/**
414
	 * Test object updating
415
	 */
416
	public void testUpdate() {
417
    printTestHeader("testUpdate");
418
    
419
		try {
420
	    Session session = getTestSession();
421
	    Identifier guid = new Identifier();
422
	    guid.setValue("testUpdate." + System.currentTimeMillis());
423
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
424
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
425
	    Identifier newPid = new Identifier();
426
	    newPid.setValue("testUpdate." + System.currentTimeMillis());
427
	    Identifier pid = 
428
	    	MNodeService.getInstance().create(session, guid, object, sysmeta);
429
	    Identifier updatedPid = 
430
	    	MNodeService.getInstance().update(session, guid, object, newPid, sysmeta);
431
	    assertEquals(updatedPid, newPid);
432
	    
433
    } catch (UnsupportedEncodingException e) {
434
	    e.printStackTrace();
435
	    fail("Unexpected error: " + e.getMessage());
436

  
437
    } catch (InvalidToken e) {
438
	    e.printStackTrace();
439
	    fail("Unexpected error: " + e.getMessage());
440

  
441
    } catch (ServiceFailure e) {
442
	    e.printStackTrace();
443
	    fail("Unexpected error: " + e.getMessage());
444

  
445
    } catch (NotAuthorized e) {
446
	    e.printStackTrace();
447
	    fail("Unexpected error: " + e.getMessage());
448

  
449
    } catch (IdentifierNotUnique e) {
450
	    e.printStackTrace();
451
	    fail("Unexpected error: " + e.getMessage());
452

  
453
    } catch (UnsupportedType e) {
454
	    e.printStackTrace();
455
	    fail("Unexpected error: " + e.getMessage());
456

  
457
    } catch (InsufficientResources e) {
458
	    e.printStackTrace();
459
	    fail("Unexpected error: " + e.getMessage());
460

  
461
    } catch (InvalidSystemMetadata e) {
462
	    e.printStackTrace();
463
	    fail("Unexpected error: " + e.getMessage());
464

  
465
    } catch (NotImplemented e) {
466
	    e.printStackTrace();
467
	    fail("Unexpected error: " + e.getMessage());
468

  
469
    } catch (InvalidRequest e) {
470
	    e.printStackTrace();
471
	    fail("Unexpected error: " + e.getMessage());
472

  
473
    } catch (Exception e) {
474
	    e.printStackTrace();
475
	    fail("Unexpected error: " + e.getMessage());
476

  
477
    }
478
	}
479

  
480
	public void testReplicate() {
481
		fail("Not yet implemented");
482
	}
483

  
484
	/**
485
	 * Test describing an object
486
	 */
487
	public void testDescribe() {
488
    printTestHeader("testGetSystemMetadata");
489

  
490
    try {
491
	    Session session = getTestSession();
492
	    Identifier guid = new Identifier();
493
	    guid.setValue("testGetSystemMetadata." + System.currentTimeMillis());
494
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
495
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
496
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
497
	    DescribeResponse describeResponse = MNodeService.getInstance().describe(session, pid);
498
	    assertEquals(describeResponse.getDataONE_Checksum(), sysmeta.getChecksum());
499
	    assertEquals(describeResponse.getDataONE_ObjectFormat(), sysmeta.getObjectFormat());
500
	    
501
    } catch (UnsupportedEncodingException e) {
502
	    e.printStackTrace();
503
	    fail("Unexpected error: " + e.getMessage());
504

  
505
    } catch (InvalidToken e) {
506
	    e.printStackTrace();	    
507
	    fail("Unexpected error: " + e.getMessage());
508

  
509
    } catch (ServiceFailure e) {
510
	    e.printStackTrace();
511
	    fail("Unexpected error: " + e.getMessage());
512

  
513
    } catch (NotAuthorized e) {
514
	    e.printStackTrace();
515
	    fail("Unexpected error: " + e.getMessage());
516

  
517
    } catch (IdentifierNotUnique e) {
518
	    e.printStackTrace();
519
	    fail("Unexpected error: " + e.getMessage());
520
	    
521
    } catch (UnsupportedType e) {
522
	    e.printStackTrace();
523
	    fail("Unexpected error: " + e.getMessage());
524

  
525
    } catch (InsufficientResources e) {
526
	    e.printStackTrace();
527
	    fail("Unexpected error: " + e.getMessage());
528

  
529
    } catch (InvalidSystemMetadata e) {
530
	    e.printStackTrace();
531
	    fail("Unexpected error: " + e.getMessage());
532

  
533
    } catch (NotImplemented e) {
534
	    e.printStackTrace();
535
	    fail("Unexpected error: " + e.getMessage());
536

  
537
    } catch (InvalidRequest e) {
538
	    e.printStackTrace();
539
	    fail("Unexpected error: " + e.getMessage());
540

  
541
    } catch (NotFound e) {
542
	    e.printStackTrace();
543
	    fail("Unexpected error: " + e.getMessage());
544

  
545
    } catch (Exception e) {
546
	    e.printStackTrace();
547
	    fail("Unexpected error: " + e.getMessage());
548

  
549
    }
550
	}
551

  
552
	/**
553
	 * Test getting the checksum of an object
554
	 */
555
	public void testGetChecksum() {
556
    printTestHeader("testGetChecksum");
557

  
558
	  try {
559
	    Session session = getTestSession();
560
	    Identifier guid = new Identifier();
561
	    guid.setValue("testGetChecksum." + System.currentTimeMillis());
562
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
563
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
564
	    Identifier pid = MNodeService.getInstance().create(session, guid, object, sysmeta);
565
	    Checksum checksum = MNodeService.getInstance().getChecksum(session, pid, "MD5");
566
	    assertEquals(checksum.getValue(), sysmeta.getChecksum().getValue());
567
    
568
	  } catch (UnsupportedEncodingException e) {
569
	    e.printStackTrace();
570
	    fail("Unexpected error: " + e.getMessage());
571

  
572
    } catch (InvalidToken e) {
573
	    e.printStackTrace();
574
	    fail("Unexpected error: " + e.getMessage());
575

  
576
    } catch (ServiceFailure e) {
577
	    e.printStackTrace();
578
	    fail("Unexpected error: " + e.getMessage());
579

  
580
    } catch (NotAuthorized e) {
581
	    e.printStackTrace();
582
	    fail("Unexpected error: " + e.getMessage());
583

  
584
    } catch (IdentifierNotUnique e) {
585
	    e.printStackTrace();
586
	    fail("Unexpected error: " + e.getMessage());
587

  
588
    } catch (UnsupportedType e) {
589
	    e.printStackTrace();
590
	    fail("Unexpected error: " + e.getMessage());
591

  
592
    } catch (InsufficientResources e) {
593
	    e.printStackTrace();
594
	    fail("Unexpected error: " + e.getMessage());
595

  
596
    } catch (InvalidSystemMetadata e) {
597
	    e.printStackTrace();
598
	    fail("Unexpected error: " + e.getMessage());
599

  
600
    } catch (NotImplemented e) {
601
	    e.printStackTrace();
602
	    fail("Unexpected error: " + e.getMessage());
603

  
604
    } catch (InvalidRequest e) {
605
	    e.printStackTrace();
606
	    fail("Unexpected error: " + e.getMessage());
607

  
608
    } catch (NotFound e) {
609
	    e.printStackTrace();
610
	    fail("Unexpected error: " + e.getMessage());
611

  
612
    } catch (Exception e) {
613
	    e.printStackTrace();
614
	    fail("Unexpected error: " + e.getMessage());
615

  
616
    }
617
	  
618
	}
619

  
620
	/**
621
	 * Testing listing objects on the Member Node
622
	 */
623
	public void testListObjects() {
624
    printTestHeader("testListObjects");
625

  
626
    ObjectList objectList = null;
627
    Session session = getTestSession();
628
    Date startTime = new Date("2010-01-01");
629
    Date endTime = new Date();
630
    ObjectFormat objectFormat = null;
631
    boolean replicaStatus = false;
632
    int start = 0;
633
    int count = 1000;
634
    
635
    try {
636
	    objectList = 
637
	    	MNodeService.getInstance().listObjects(session, startTime, endTime, 
638
	    		objectFormat, replicaStatus, start, count);
639
	    assertNotNull(objectList);
640
	    assertTrue(objectList.getCount() == 1000);
641
	    assertTrue(objectList.getStart() == 0);
642
	    assertTrue(objectList.getTotal() > 1);
643
	    
644
    } catch (NotAuthorized e) {
645
	    e.printStackTrace();
646
	    fail("Unexpected error: " + e.getMessage());
647

  
648
    } catch (InvalidRequest e) {
649
	    e.printStackTrace();
650
	    fail("Unexpected error: " + e.getMessage());
651
	    
652
    } catch (NotImplemented e) {
653
	    e.printStackTrace();
654
	    fail("Unexpected error: " + e.getMessage());
655
	    
656
    } catch (ServiceFailure e) {
657
	    e.printStackTrace();
658
	    fail("Unexpected error: " + e.getMessage());
659
	    
660
    } catch (InvalidToken e) {
661
	    e.printStackTrace();
662
	    fail("Unexpected error: " + e.getMessage());
663
	    
664
    }
665
	}
666

  
667
	public void testGetCapabilities() {
668
		fail("Not yet implemented");
669
	}
670

  
671
	public void testGetOperationStatistics() {
672
		fail("Not yet implemented");
673
	}
674

  
675
	public void testPing() {
676

  
677
		try {
678
	    boolean pingable = MNodeService.getInstance().ping();
679
	    assertTrue(pingable);
680
	    
681
    } catch (NotImplemented e) {
682
	    e.printStackTrace();
683
	    fail("Unexpected error: " + e.getMessage());
684

  
685
    } catch (ServiceFailure e) {
686
	    e.printStackTrace();
687
	    fail("Unexpected error: " + e.getMessage());
688

  
689
    } catch (NotAuthorized e) {
690
	    e.printStackTrace();
691
	    fail("Unexpected error: " + e.getMessage());
692

  
693
    } catch (InvalidRequest e) {
694
	    e.printStackTrace();
695
	    fail("Unexpected error: " + e.getMessage());
696

  
697
    } catch (InsufficientResources e) {
698
	    e.printStackTrace();
699
	    fail("Unexpected error: " + e.getMessage());
700

  
701
    } catch (UnsupportedType e) {
702
	    e.printStackTrace();
703
	    fail("Unexpected error: " + e.getMessage());
704

  
705
    }
706
		
707
	}
708

  
709
	public void testSynchronizationFailed() {
710
		fail("Not yet implemented");
711
	}
712

  
713
	public void testGetLogRecords() {
714
    printTestHeader("testListObjects");
715

  
716
    Log log = null;
717
    Session session = getTestSession();
718
    Date fromDate = new Date("2010-01-01");
719
    Date toDate = new Date();
720
    Event event = Event.CREATE;
721
    int start = 0;
722
    int count = 1000;
723
    
724
	  try {
725
	    log = MNodeService.getInstance().getLogRecords(session, fromDate, toDate, 
726
	    	event, start, count);
727
	    assertNotNull(log);
728
	    assertTrue(log.getCount() == 1000);
729
	    assertTrue(log.getStart() == 0);
730
	    assertTrue(log.getTotal() > 1);
731
    } catch (InvalidToken e) {
732
	    e.printStackTrace();
733
	    fail("Unexpected error: " + e.getMessage());
734

  
735
    } catch (ServiceFailure e) {
736
	    e.printStackTrace();
737
	    fail("Unexpected error: " + e.getMessage());
738
	    
739
    } catch (NotAuthorized e) {
740
	    e.printStackTrace();
741
	    fail("Unexpected error: " + e.getMessage());
742
	    
743
    } catch (InvalidRequest e) {
744
	    e.printStackTrace();
745
	    fail("Unexpected error: " + e.getMessage());
746
	    
747
    } catch (NotImplemented e) {
748
	    e.printStackTrace();
749
	    fail("Unexpected error: " + e.getMessage());
750
	    
751
    }
752
	}
753

  
754
	/**
755
	 * Testing setting access on a known object
756
	 */
757
	public void testSetAccessPolicy() {
758
    printTestHeader("testSetAccess");
759
    
760
    boolean accessWasSet = false;
761
    
762
    try {
763
	    // create an object to set access on
764
	    Session session = getTestSession();
765
	    Identifier guid = new Identifier();
766
	    guid.setValue("testSetAccess." + System.currentTimeMillis());
767
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
768
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
769
	    Identifier pid = 
770
	    	MNodeService.getInstance().create(session, guid, object, sysmeta);
771
	    // set the access
772
	    AccessPolicy accessPolicy = new AccessPolicy();
773
	    AccessRule allow = new AccessRule();
774
	    allow.addPermission(Permission.WRITE);
775
	    Subject publicSubject = new Subject();
776
	    publicSubject.setValue(Constants.PUBLIC_SUBJECT);
777
	    allow.addSubject(publicSubject);
778
	    accessPolicy.addAllow(allow);
779
	    
780
	    accessWasSet = 
781
	    	MNodeService.getInstance().setAccessPolicy(session, pid, accessPolicy);
782
	    assertTrue(accessWasSet);
783
	    
784
    } catch (UnsupportedEncodingException e) {
785
	    e.printStackTrace();
786
	    
787
    } catch (InvalidToken e) {
788
	    e.printStackTrace();
789
	    fail("Unexpected error: " + e.getMessage());
790
	    
791
    } catch (ServiceFailure e) {
792
	    e.printStackTrace();
793
	    fail("Unexpected error: " + e.getMessage());
794
	    
795
    } catch (NotAuthorized e) {
796
	    e.printStackTrace();
797
	    fail("Unexpected error: " + e.getMessage());
798
	    
799
    } catch (IdentifierNotUnique e) {
800
	    e.printStackTrace();
801
	    fail("Unexpected error: " + e.getMessage());
802
	    
803
    } catch (UnsupportedType e) {
804
	    e.printStackTrace();
805
	    fail("Unexpected error: " + e.getMessage());
806
	    
807
    } catch (InsufficientResources e) {
808
	    e.printStackTrace();
809
	    fail("Unexpected error: " + e.getMessage());
810
	    
811
    } catch (InvalidSystemMetadata e) {
812
	    e.printStackTrace();
813
	    fail("Unexpected error: " + e.getMessage());
814
	    
815
    } catch (NotImplemented e) {
816
	    e.printStackTrace();
817
	    fail("Unexpected error: " + e.getMessage());
818
	    
819
    } catch (InvalidRequest e) {
820
	    e.printStackTrace();
821
	    fail("Unexpected error: " + e.getMessage());
822
	    
823
    } catch (NotFound e) {
824
	    e.printStackTrace();
825
	    fail("Unexpected error: " + e.getMessage());
826
	    
827
    } catch (Exception e) {
828
	    e.printStackTrace();
829
	    fail("Unexpected error: " + e.getMessage());
830
	    
831
    }
832
	    
833
	}
834

  
835
	/**
836
	 * Test if a subject is authorized to read a known object
837
	 */
838
	public void testIsAuthorized() {
839
    printTestHeader("testIsAuthorized");
840
    
841
		try {
842
	    Session session = getTestSession();
843
	    Identifier guid = new Identifier();
844
	    guid.setValue("testIsAuthorized." + System.currentTimeMillis());
845
	    SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
846
	    InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
847
	    Identifier pid = 
848
	    	MNodeService.getInstance().create(session, guid, object, sysmeta);
849
	    boolean isAuthorized = 
850
	    	MNodeService.getInstance().isAuthorized(session, pid, Permission.READ);
851
	    assertEquals(isAuthorized, true);
852
	    
853
    } catch (UnsupportedEncodingException e) {
854
	    e.printStackTrace();
855
	    fail("Unexpected error: " + e.getMessage());
856

  
857
    } catch (InvalidToken e) {
858
	    e.printStackTrace();
859
	    fail("Unexpected error: " + e.getMessage());
860

  
861
    } catch (ServiceFailure e) {
862
	    e.printStackTrace();
863
	    fail("Unexpected error: " + e.getMessage());
864

  
865
    } catch (NotAuthorized e) {
866
	    e.printStackTrace();
867
	    fail("Unexpected error: " + e.getMessage());
868

  
869
    } catch (IdentifierNotUnique e) {
870
	    e.printStackTrace();
871
	    fail("Unexpected error: " + e.getMessage());
872

  
873
    } catch (UnsupportedType e) {
874
	    e.printStackTrace();
875
	    fail("Unexpected error: " + e.getMessage());
876

  
877
    } catch (InsufficientResources e) {
878
	    e.printStackTrace();
879
	    fail("Unexpected error: " + e.getMessage());
880

  
881
    } catch (InvalidSystemMetadata e) {
882
	    e.printStackTrace();
883
	    fail("Unexpected error: " + e.getMessage());
884

  
885
    } catch (NotImplemented e) {
886
	    e.printStackTrace();
887
	    fail("Unexpected error: " + e.getMessage());
888

  
889
    } catch (InvalidRequest e) {
890
	    e.printStackTrace();
891
	    fail("Unexpected error: " + e.getMessage());
892

  
893
    } catch (Exception e) {
894
	    e.printStackTrace();
895
	    fail("Unexpected error: " + e.getMessage());
896

  
897
    }
898
	}
899

  
900
	/**
901
	 * print a header to start each test
902
	 */
903
	private void printTestHeader(String testName)
904
	{
905
	    System.out.println();
906
	    System.out.println("*************** " + testName + " ***************");
907
	}
908
  
909
	/**
910
	 * produce an md5 checksum for item
911
	 */
912
	private String checksum(String item)
913
	  throws Exception {
914
    StringBufferInputStream fis =  new StringBufferInputStream(item);
915
    
916
    byte[] buffer = new byte[1024];
917
    MessageDigest complete = MessageDigest.getInstance("MD5");
918
    int numRead;
919
    
920
    do {
921
      numRead = fis.read(buffer);
922
      if (numRead > 0) {
923
        complete.update(buffer, 0, numRead);
924
      }
925
    } while (numRead != -1);
926
    
927
    return getHex(complete.digest());
928
	}
929
	
930
	/**
931
	 * convert a byte array to a hex string
932
	 */
933
	private static String getHex( byte [] raw ) {
934
	  final String HEXES = "0123456789ABCDEF";
935
      if ( raw == null ) {
936
        return null;
937
      }
938
      final StringBuilder hex = new StringBuilder( 2 * raw.length );
939
      for ( final byte b : raw ) {
940
        hex.append(HEXES.charAt((b & 0xF0) >> 4))
941
           .append(HEXES.charAt((b & 0x0F)));
942
      }
943
      return hex.toString();
944
  }
945
	
946
	/**
947
	 * constructs a "fake" session with a test subject
948
	 * @return
949
	 */
950
	private Session getTestSession() {
951
		Session session = new Session();
952
        Subject subject = new Subject();
953
        subject.setValue("cn=test,dc=dataone,dc=org");
954
        session.setSubject(subject);
955
        return session;
956
	}
957

  
958
	/**
959
	 * create system metadata with a specified id
960
	 */
961
	private SystemMetadata createSystemMetadata(Identifier id, Subject owner)
962
	  throws Exception {
963
	  SystemMetadata sm = new SystemMetadata();
964
    // set the id
965
    sm.setIdentifier(id);
966
    sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
967
    // create the checksum
968
    String checksumS = "test";
969
    ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
970
    Checksum checksum = new Checksum();
971
    checksum.setValue(checksumS);
972
    checksum.setAlgorithm(ca);
973
    sm.setChecksum(checksum);
974
    // set the size
975
    sm.setSize(0);
976
    sm.setSubmitter(owner);
977
    sm.setRightsHolder(owner);
978
    sm.setDateUploaded(new Date());
979
    sm.setDateSysMetadataModified(new Date());
980
    NodeReference nr = new NodeReference();
981
    nr.setValue("metacat");
982
    sm.setOriginMemberNode(nr);
983
    sm.setAuthoritativeMemberNode(nr);
984
		// set the access to public read
985
    AccessPolicy accessPolicy = new AccessPolicy();
986
    AccessRule allow = new AccessRule();
987
    allow.addPermission(Permission.READ);
988
    Subject publicSubject = new Subject();
989
    publicSubject.setValue(Constants.PUBLIC_SUBJECT);
990
		allow.addSubject(publicSubject);
991
		accessPolicy.addAllow(allow);
992
    sm.setAccessPolicy(accessPolicy);
993
      
994
    return sm;
995
	}
996

  
997
}
0 998

  

Also available in: Unified diff