Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: berkley $'
8
 *     '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
9
 * '$Revision: 5027 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacat.dataone;
27

    
28
import java.util.*;
29
import java.io.*;
30

    
31
import java.security.MessageDigest;
32

    
33
import edu.ucsb.nceas.MCTestCase;
34
import edu.ucsb.nceas.metacat.IdentifierManager;
35
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
36
import edu.ucsb.nceas.metacat.client.MetacatException;
37
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
38
import edu.ucsb.nceas.metacat.dataone.CrudService;
39
import junit.framework.Test;
40
import junit.framework.TestSuite;
41

    
42
import org.apache.log.LogEvent;
43
import org.dataone.service.exceptions.InvalidRequest;
44
import org.dataone.service.exceptions.InvalidToken;
45
import org.dataone.service.exceptions.NotAuthorized;
46
import org.dataone.service.exceptions.NotImplemented;
47
import org.dataone.service.exceptions.ServiceFailure;
48
import org.dataone.service.types.*;
49

    
50
import edu.ucsb.nceas.metacat.properties.PropertyService;
51
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
52

    
53
import edu.ucsb.nceas.metacat.service.SessionService;
54
import edu.ucsb.nceas.metacat.util.SessionData;
55

    
56
/**
57
 * A JUnit test for testing the dataone CrudService class
58
 */
59
public class CrudServiceTest extends MCTestCase 
60
{   
61
    /**
62
    * consstructor for the test
63
    */
64
    public CrudServiceTest(String name)
65
    {
66
        super(name);
67
    }
68
  
69
    /**
70
	 * Establish a testing framework by initializing appropriate objects
71
	 */
72
	public void setUp() throws Exception 
73
	{
74
		super.setUp();
75
	}
76

    
77
	/**
78
	 * Release any objects after tests are complete
79
	 */
80
	public void tearDown() 
81
	{
82
	}
83

    
84
	/**
85
	 * Create a suite of tests to be run together
86
	 */
87
	public static Test suite() 
88
	{
89
		TestSuite suite = new TestSuite();
90
		/*suite.addTest(new CrudServiceTest("initialize"));
91
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
92
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
93
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
94
		suite.addTest(new CrudServiceTest("testUpdate"));
95
		suite.addTest(new CrudServiceTest("testListObjects"));
96
		suite.addTest(new CrudServiceTest("testAccessControl"));
97
		suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
98
		suite.addTest(new CrudServiceTest("testGetLogRecords"));
99
		suite.addTest(new CrudServiceTest("testChecksumError"));*/
100
		suite.addTest(new CrudServiceTest("testPublicAccess"));
101
		return suite;
102
	}
103
	
104
	/**
105
	 * test to make sure that get and listObjects methods are publicly accessible
106
	 */
107
	public void testPublicAccess()
108
	{
109
	    try
110
	    {
111
	        AuthToken token = new AuthToken("public");
112
	        //AuthToken token = getToken();
113
	        CrudService cs = CrudService.getInstance();
114
	        ObjectList ol = cs.listObjects(token, null, null, null);
115
	        System.out.println("ol: " + ol.sizeObjectInfoList());
116
	        assertTrue(ol.sizeObjectInfoList() > 0);
117
	    }
118
	    catch(Exception e)
119
	    {
120
	        fail("Error in testPublicAccess: " + e.getMessage());
121
	    }
122
	    
123
	}
124
	
125
	/**
126
	 * test for an error where the checksum algorithm gets appended onto the checksum.
127
	 */
128
	public void testChecksumError()
129
	{
130
	    printTestHeader("testChecksumError");
131
	    try
132
	    {
133
	        Date d1 = new Date();
134
	        CrudService cs = CrudService.getInstance();
135
	        AuthToken token = getToken();
136
	        ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
137
	        //create docs at different times
138

    
139
	        Identifier id = new Identifier();
140
	        String docid = generateDocumentId();
141
	        id.setValue(docid);
142

    
143
	        //create the system metadata then run the create method
144
	        String testDoc = getTestDoc();
145
	        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
146
	        SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
147
	        assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
148
	        //create the doc
149
	        Identifier idC = cs.create(token, id, sbis, sm);
150
	        assertTrue(idC.getValue().equals(id.getValue()));
151
	        SystemMetadata smC = getSystemMetadata(token, idC);
152
	        assertFalse(smC.getChecksum().getValue().startsWith("MD5"));
153
	        
154
	        Date d2 = new Date();
155
	        ObjectList ol = cs.listObjects(token, d1, d2, of1);
156
	        assertTrue(ol.sizeObjectInfoList() == 1);
157
	        ObjectInfo oi = ol.getObjectInfo(0);
158
	        //this will fail if the error state exists.
159
	        assertFalse(oi.getChecksum().getValue().startsWith("MD5"));
160
	    }
161
	    catch(Exception e)
162
	    {
163
	        fail("Unexpected exception: " + e.getMessage());
164
	    }
165
        
166
	}
167
	
168
	/**
169
	 * test CrudService.getLogRecords
170
	 */
171
	public void testGetLogRecords()
172
	{
173
	    printTestHeader("testGetLogRecords");
174
	    try
175
	    {
176
	        CrudService cs = CrudService.getInstance();
177
	        AuthToken token = getToken();
178
	        Date fromDate = new Date();
179
	        Identifier id = createDoc(token, getTestDoc());
180
	        Date toDate = new Date();
181
	        Log lrs = cs.getLogRecords(token, fromDate, toDate, null);
182
	        assertNotNull(lrs);
183
	        assertTrue(lrs.sizeLogEntryList() == 1);
184
	        LogEntry lrLogEvent = lrs.getLogEntry(0);
185
	        assertTrue(lrLogEvent.getEvent().name().equals("CREATE"));
186
	    }
187
	    catch(Exception e)
188
	    {
189
	        e.printStackTrace();
190
	        fail("testGetLogRecords threw an unexpected exception: " + e.getMessage());
191
	    }
192
	}
193
	
194
	/**
195
	 * test the generation of system metadata for docs that don't already 
196
	 * have it.  This will be used for migration of existing object stores
197
	 * to dataone.
198
	 */
199
	public void testGenerateMissingSystemMetadata()
200
	{
201
	    printTestHeader("testGenerateMissingSystemMetadata");
202
	    try
203
	    {
204
	        CrudService cs = CrudService.getInstance();
205
	        AuthToken token = getToken();
206
	        //create a document with no system metadata
207
	        String testDoc = getTestDoc();
208
	        Identifier id = new Identifier();
209
	        String docid = generateDocumentId();
210
	        id.setValue(docid);
211
	        
212
	        cs.insertOrUpdateDocument(testDoc, id, cs.getSessionData(token), "insert");
213
	        //try to get its system metadata, should fail
214
	        try
215
	        {
216
	            getSystemMetadata(token, id);
217
	            fail("call to getSystemMetadata should have failed.");
218
	        }
219
	        catch(Exception e)
220
	        {}
221
	        
222
	        //generate missing system metadata
223
	        cs.generateMissingSystemMetadata(token);
224
	        //try to get system metadata again, should succeed
225
	        getSystemMetadata(token, id);
226
	    }
227
	    catch(Exception e)
228
	    {
229
	        fail("Unexpected error generating missing system metadata: " + e.getMessage());
230
	    }
231
	}
232
	
233
	/**
234
	 * make sure that only valid sessions can update/delete
235
	 */
236
	public void testAccessControl()
237
	{
238
	    printTestHeader("testAccessControl");
239
        try
240
        {
241
            CrudService cs = CrudService.getInstance();
242
            AuthToken token = getToken();
243
            //create a doc
244
            Identifier id = createDoc(token, getTestDoc());
245
            
246
            //get the doc and sysmetadata
247
            String gotDoc = getDoc(token, id);
248
            SystemMetadata sm = getSystemMetadata(token, id);
249
            
250
            //break the session id
251
            String sessionid = "somefakesessionid";
252
            token = new AuthToken(sessionid);
253
            
254
            //update the doc
255
            gotDoc = gotDoc.replaceAll("XXX", "YYY");
256
            Identifier newid = new Identifier();
257
            newid.setValue(generateDocumentId());
258
            StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
259
            SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
260
            Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
261
            fail("exception should have been thrown.");
262
        }
263
        catch(Exception e)
264
        {
265
        }
266
        
267
        try
268
        {
269
            CrudService cs = CrudService.getInstance();
270
            AuthToken token = new AuthToken("somefakesessionid");
271
            //create a doc
272
            Identifier id = createDoc(token, getTestDoc());
273
            fail("exception should have been thrown.");
274
        }
275
        catch(Exception e)
276
        {
277
        }
278
	}
279
	
280
	/**
281
	 * public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
282
     *     ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
283
     *       throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
284
	 */
285
	public void testListObjects()
286
	{
287
	    printTestHeader("testListObjects");
288
	    try
289
	    {
290
	        CrudService cs = CrudService.getInstance();
291
	        AuthToken token = getToken();
292
	        ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
293
	        ObjectFormat of2 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.0.0");
294
	        //create docs at different times
295
	        Date d1 = new Date();
296
	        Identifier id1 = createDoc(token, getTestDoc(), of1);
297
            SystemMetadata sm1 = getSystemMetadata(token, id1);
298
            
299
            Date d2 = new Date();
300
            Identifier id2 = createDoc(token, getTestDoc(), of2);
301
            SystemMetadata sm2 = getSystemMetadata(token, id2);
302
            
303
            Date d3 = new Date();
304
            Identifier id3 = createDoc(token, getTestDoc(), of1);
305
            SystemMetadata sm3 = getSystemMetadata(token, id3);
306
              
307
            Date d4 = new Date();
308
            Identifier id4 = createDoc(token, getTestDoc(), of2);
309
            SystemMetadata sm4 = getSystemMetadata(token, id4);
310
            
311
            Date d5 = new Date();
312
            Identifier id5 = createDoc(token, getTestDoc(), of1);
313
            SystemMetadata sm5 = getSystemMetadata(token, id5);  
314
             
315
            Date d6 = new Date();
316
            
317
            //now get the objects for specific time ranges and test that it returns
318
            //the correct objects
319
            
320
            ObjectList list = cs.listObjects(token, null, null, null);
321
            //System.out.println("list size: " + list.sizeObjectInfoList());
322
           
323
            //should return sm1 and sm2
324
            ObjectList list1 = cs.listObjects(token, d1, d3, null);
325
            assertTrue(list1.sizeObjectInfoList() == 2);
326
            assertTrue(idInObjectList(id1, list1));
327
            assertTrue(idInObjectList(id2, list1));
328
            
329
            ObjectInfo info = list1.getObjectInfo(0);
330
            
331
            
332
            //should only return sm1
333
            ObjectList list2 = cs.listObjects(token, d1, d3, of1);
334
            assertTrue(list2.sizeObjectInfoList() == 1);
335
            assertTrue(idInObjectList(id1, list2));
336
            
337
            //should return sm1-sm4
338
            ObjectList list3 = cs.listObjects(token, d1, d5, null);
339
            assertTrue(list3.sizeObjectInfoList() == 4);
340
            ObjectInfo oi4 = list3.getObjectInfo(0);
341
            assertTrue(idInObjectList(id1, list3));
342
            assertTrue(idInObjectList(id2, list3));
343
            assertTrue(idInObjectList(id3, list3));
344
            assertTrue(idInObjectList(id4, list3));
345
            
346
            //should only return sm2 and sm4
347
            ObjectList list4 = cs.listObjects(token, d1, d5, of2);
348
            assertTrue(list4.sizeObjectInfoList() == 2);
349
            assertTrue(idInObjectList(id2, list4));
350
            assertTrue(idInObjectList(id4, list4));
351
            
352
            //should return all
353
            ObjectList list5 = cs.listObjects(token, d1, d6, null);
354
            assertTrue(list5.sizeObjectInfoList() == 5);
355
            assertTrue(idInObjectList(id1, list5));
356
            assertTrue(idInObjectList(id2, list5));
357
            assertTrue(idInObjectList(id3, list5));
358
            assertTrue(idInObjectList(id4, list5));
359
            assertTrue(idInObjectList(id5, list5));
360
            
361
            //should return 1, 3, 5
362
            ObjectList list6 = cs.listObjects(token, d1, d6, of1);
363
            assertTrue(list6.sizeObjectInfoList() == 3);
364
            assertTrue(idInObjectList(id1, list6));
365
            assertTrue(idInObjectList(id3, list6));
366
            assertTrue(idInObjectList(id5, list6));
367
            
368
	    }
369
	    catch(Exception e)
370
	    {
371
	        //e.printStackTrace();
372
	        fail("Error in listObjects: " + e.getMessage());
373
	    }
374
	}
375
	
376
	private boolean idInObjectList(Identifier id, ObjectList list)
377
	{
378
	    for(int i=0; i<list.sizeObjectInfoList(); i++)
379
	    {
380
	        ObjectInfo oi = list.getObjectInfo(i);
381
	        if(id.getValue().equals(oi.getIdentifier().getValue()))
382
	            return true;
383
	    }
384
	    return false;
385
	}
386
	
387
	/**
388
	 * public Identifier update(AuthToken token, Identifier guid, 
389
     *       InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
390
     *         throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
391
     *           UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
392
     *           NotImplemented
393
	 */
394
	public void testUpdate()
395
	{
396
	    printTestHeader("testUpdate");
397
	    try
398
	    {
399
	        CrudService cs = CrudService.getInstance();
400
	        AuthToken token = getToken();
401
            //create a doc
402
            Identifier id = createDoc(token, getTestDoc());
403
            
404
            //get the doc and sysmetadata
405
            String gotDoc = getDoc(token, id);
406
            SystemMetadata sm = getSystemMetadata(token, id);
407
            
408
            //update the doc
409
            gotDoc = gotDoc.replaceAll("XXX", "YYY");
410
            Identifier newid = new Identifier();
411
            newid.setValue(generateDocumentId());
412
            StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
413
            SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
414
            Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
415
            
416
            //get doc - check that it matches update
417
            String newdoc = getDoc(token, newid);
418
            assertTrue(gotDoc.equals(newdoc));
419
            
420
            //get sysmeta - check that ids and other fields are updated
421
            SystemMetadata newnewsm = getSystemMetadata(token, id);
422
            assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
423
            
424
            //get the new sysmeta and make sure the obsoletes field is set
425
            SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
426
            assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
427
        }
428
        catch(Exception e)
429
        {
430
            e.printStackTrace();
431
            fail("Error in testUpdate: " + e.getMessage());
432
        }
433
	}
434
	
435
	/**
436
	 * public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
437
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
438
     *       InvalidRequest, NotImplemented
439
	 */
440
	public void testGetSystemMetadata()
441
	{
442
	    printTestHeader("testGetSystemMetadata");
443
	    try
444
	    {
445
            CrudService cs = CrudService.getInstance();
446
            AuthToken token = getToken();
447
            //run create
448
            Identifier id = createDoc(token, getTestDoc());
449
            //get the systemMetadata and make sure it is the correct object for this testDoc
450
            SystemMetadata sm = getSystemMetadata(token, id);
451
            assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
452
            assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
453
            
454
            try
455
            {
456
                Identifier fakeid = new Identifier();
457
                fakeid.setValue("somethingfake.234234");
458
                getSystemMetadata(token, fakeid);
459
                fail("getSystemMetadata should have thrown an exception.");
460
            }
461
            catch(Exception e)
462
            {
463
                assertTrue(true);
464
            }
465
        }
466
        catch(Exception e)
467
        {
468
            e.printStackTrace();
469
            fail("Error testing system metadata: " + e.getMessage());
470
        }
471
	}
472
	
473
	/**
474
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
475
     *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
476
     *       InsufficientResources, InvalidSystemMetadata, NotImplemented
477
     *
478
     * public InputStream get(AuthToken token, Identifier guid)
479
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
480
     *       NotImplemented
481
	 */
482
	public void testCreateAndGet()
483
	{
484
	    printTestHeader("testCreateAndGet");
485
	    try
486
	    {
487
	        CrudService cs = CrudService.getInstance();
488
	        AuthToken token = getToken();
489
	        //run create
490
	        Identifier id = createDoc(token, getTestDoc());
491
	        //make these docs public for debugging purposes.
492
	        makeDocPublic(id, true);
493
            //compare the docs
494
            String gotDoc = getDoc(token, id);
495
            assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
496
            
497
            try
498
            {
499
                Identifier fakeid = new Identifier();
500
                fakeid.setValue("somethingfake.234234");
501
                getDoc(token, fakeid);
502
                fail("testCreateAndGet should have thrown an exception.");
503
            }
504
            catch(Exception e)
505
            {
506
                assertTrue(true);
507
            }
508
        }
509
        catch(Exception e)
510
        {
511
            e.printStackTrace();
512
            fail("Error in testCreate: " + e.getMessage());
513
        }
514
	}
515

    
516
	/**
517
	 * getInstance()
518
	 */
519
	public void testSingletonAccessor()
520
	{
521
	    printTestHeader("testSingletonAccessor");
522
	    CrudService cs = CrudService.getInstance();
523
	    assertNotNull(cs);
524
	}
525
	
526
	/**
527
	 * Run an initial test that always passes to check that the test harness is
528
	 * working.
529
	 */
530
	public void initialize() 
531
	{
532
	    printTestHeader("initialize");
533
		assertTrue(1 == 1);
534
	}
535
	
536
	/**
537
	 * return the systemMetadata object for id
538
	 */
539
	private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
540
	  throws Exception
541
	{
542
	    CrudService cs = CrudService.getInstance();
543
	    return cs.getSystemMetadata(token, id);
544
	}
545
	
546
	/**
547
	 * get a doc from metacat using CrudService.get()
548
	 */
549
	private String getDoc(AuthToken token, Identifier id)
550
	  throws Exception
551
	{
552
	    CrudService cs = CrudService.getInstance();
553
	    //try to get the same doc then compare them
554
        InputStream gotDocStream = cs.get(token, id);
555
        StringBuffer sb = new StringBuffer();
556
        byte[] b = new byte[1024];
557
        int numread = gotDocStream.read(b, 0, 1024);
558
        while(numread != -1)
559
        {
560
            sb.append(new String(b, 0, numread));
561
            numread = gotDocStream.read(b, 0, 1024);
562
        }
563
        return sb.toString();
564
	}
565
	
566
	/**
567
	 * return a test document
568
	 */
569
	private String getTestDoc()
570
	{
571
	    return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
572
	}
573
	
574
	/**
575
	 * authenticate and return a token
576
	 */
577
	private AuthToken getToken()
578
	  throws Exception
579
	{
580
	    CrudService cs = CrudService.getInstance();
581
        //login and get a sessionid
582
        MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
583
        String username = PropertyService.getProperty("test.mcUser");
584
        String password = PropertyService.getProperty("test.mcPassword");
585
        String response = restClient.login(username, password);
586
        String sessionid = restClient.getSessionId();
587
        SessionService sessionService = SessionService.getInstance();
588
        sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
589
        AuthToken token = new AuthToken(sessionid);
590
        return token;
591
	}
592
	
593
	/**
594
	 * create a doc using CrudService.create() and return its id
595
	 */
596
	private Identifier createDoc(AuthToken token, String testDoc) throws Exception
597
	{
598
	    return createDoc(token, testDoc, ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
599
	}
600
	
601
	/**
602
	 * create a doc using CrudService.create() and return its id
603
	 */
604
	private Identifier createDoc(AuthToken token, String testDoc, ObjectFormat format) throws Exception
605
	{
606
	    Identifier id;
607
        CrudService cs = CrudService.getInstance();
608
        
609
        id = new Identifier();
610
        String docid = generateDocumentId();
611
        id.setValue(docid);
612
        
613
        //create the system metadata then run the create method
614
        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
615
        SystemMetadata sm = createSystemMetadata(id, testDoc, format);
616
        //create the doc
617
        cs.create(token, id, sbis, sm);
618
        return id;
619
	}
620
	
621
	/**
622
	 * create a generic SystemMetadata object for testing
623
	 */
624
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
625
	  throws Exception
626
	{
627
	    return createSystemMetadata(id, testDoc, 
628
	            ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
629
	}
630
	
631
	/**
632
	 * create system metadata with a specified id, doc and format
633
	 */
634
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc, ObjectFormat format)
635
	  throws Exception
636
	{
637
	    SystemMetadata sm = new SystemMetadata();
638
        //set the id
639
        sm.setIdentifier(id);
640
        sm.setObjectFormat(format);
641
        //create the checksum
642
        String checksumS = checksum(testDoc);
643
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
644
        Checksum checksum = new Checksum();
645
        checksum.setValue(checksumS);
646
        checksum.setAlgorithm(ca);
647
        sm.setChecksum(checksum);
648
        //set the size
649
        sm.setSize(testDoc.getBytes().length);
650
        //submitter
651
        Principal p = new Principal();
652
        p.setValue("joe");
653
        sm.setSubmitter(p);
654
        sm.setRightsHolder(p);
655
        sm.setDateUploaded(new Date());
656
        sm.setDateSysMetadataModified(new Date());
657
        NodeReference nr = new NodeReference();
658
        nr.setValue("metacat");
659
        sm.setOriginMemberNode(nr);
660
        sm.setAuthoritativeMemberNode(nr);
661
        return sm;
662
	}
663
	
664
	/**
665
	 *  make a document public in metacat by inserting an access document
666
	 * @param id
667
	 */
668
	private void makeDocPublic(Identifier id, boolean systemMetadataToo)
669
	  throws Exception
670
	{
671
	    CrudService cs = CrudService.getInstance();
672
	    IdentifierManager im = IdentifierManager.getInstance();
673
	    cs.setAccess(getToken(), id, "public", "read", "allow", "allowFirst");
674
	    if(systemMetadataToo)
675
	    {
676
	        String smidS = im.getSystemMetadataId(id.getValue());
677
	        Identifier smid = new Identifier();
678
	        smid.setValue(smidS);
679
	        cs.setAccess(getToken(), smid, "public", "read", "allow", "allowFirst");
680
	    }
681
	}
682
	
683
	/**
684
	 * print a header to start each test
685
	 */
686
	private void printTestHeader(String testName)
687
	{
688
	    System.out.println();
689
	    System.out.println("*************** " + testName + " ***************");
690
	}
691
  
692
	/**
693
	 * produce an md5 checksum for item
694
	 */
695
	private String checksum(String item)
696
	  throws Exception
697
	{
698
        StringBufferInputStream fis =  new StringBufferInputStream(item);
699
        
700
        byte[] buffer = new byte[1024];
701
        MessageDigest complete = MessageDigest.getInstance("MD5");
702
        int numRead;
703
        
704
        do 
705
        {
706
          numRead = fis.read(buffer);
707
          if (numRead > 0) 
708
          {
709
            complete.update(buffer, 0, numRead);
710
          }
711
        } while (numRead != -1);
712
        
713
        
714
        return getHex(complete.digest());
715
	}
716
	
717
	/**
718
	 * convert a byte array to a hex string
719
	 */
720
	private static String getHex( byte [] raw ) 
721
	{
722
	    final String HEXES = "0123456789ABCDEF";
723
        if ( raw == null ) {
724
          return null;
725
        }
726
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
727
        for ( final byte b : raw ) {
728
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
729
             .append(HEXES.charAt((b & 0x0F)));
730
        }
731
        return hex.toString();
732
    }
733
}
(1-1/2)