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.io.InputStream;
29
import java.io.StringBufferInputStream;
30
import java.security.MessageDigest;
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.types.AuthToken;
38
import org.dataone.service.types.Checksum;
39
import org.dataone.service.types.ChecksumAlgorithm;
40
import org.dataone.service.types.DescribeResponse;
41
import org.dataone.service.types.Identifier;
42
import org.dataone.service.types.Log;
43
import org.dataone.service.types.LogEntry;
44
import org.dataone.service.types.NodeReference;
45
import org.dataone.service.types.ObjectFormat;
46
import org.dataone.service.types.ObjectInfo;
47
import org.dataone.service.types.ObjectList;
48
import org.dataone.service.types.Principal;
49
import org.dataone.service.types.SystemMetadata;
50

    
51
import edu.ucsb.nceas.MCTestCase;
52
import edu.ucsb.nceas.metacat.MetaCatServlet;
53
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
54
import edu.ucsb.nceas.metacat.properties.PropertyService;
55
import edu.ucsb.nceas.metacat.service.SessionService;
56
import edu.ucsb.nceas.metacat.util.SessionData;
57

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

    
81
	/**
82
	 * Release any objects after tests are complete
83
	 */
84
	public void tearDown() 
85
	{
86
	    System.out.println(createCount + " docs created in this test session.");
87
	}
88

    
89
	/**
90
	 * Create a suite of tests to be run together
91
	 */
92
	public static Test suite() 
93
	{
94
		TestSuite suite = new TestSuite();
95
		suite.addTest(new CrudServiceTest("initialize"));
96
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
97
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
98
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
99
		suite.addTest(new CrudServiceTest("testUpdate"));
100
		suite.addTest(new CrudServiceTest("testListObjects"));
101
		suite.addTest(new CrudServiceTest("testAccessControl"));
102
		suite.addTest(new CrudServiceTest("testGetLogRecords"));
103
		suite.addTest(new CrudServiceTest("testChecksumError"));
104
		suite.addTest(new CrudServiceTest("testPublicAccess"));
105
		suite.addTest(new CrudServiceTest("testFailedCreate"));
106
		suite.addTest(new CrudServiceTest("testChecksum"));
107
		suite.addTest(new CrudServiceTest("testDescribe"));
108
		suite.addTest(new CrudServiceTest("testDelete"));
109
		suite.addTest(new CrudServiceTest("testSemiColonsInIdentifiers"));
110
		suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
111
		return suite;
112
	}
113
	
114
	/**
115
	 * test for the use of semi colons in identifiers
116
	 * in response to https://redmine.dataone.org/issues/1143
117
	 */
118
	public void testSemiColonsInIdentifiers()
119
	{
120
	    try
121
        {
122
            CrudService cs = CrudService.getInstance();
123
            AuthToken token = getToken();
124
            String testDoc = getTestDoc();
125
            
126
            Identifier id = new Identifier();
127
            id.setValue("someid;id;with;semi;colons;in;it." + new Date().getTime());
128
            String docid = generateDocumentId();
129
            
130
            //create the system metadata then run the create method
131
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
132
            SystemMetadata sm = createSystemMetadata(id, testDoc, 
133
                    ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
134
            //create the doc
135
            Identifier rGuid = cs.create(token, id, sbis, sm);
136
            
137
            System.out.println("ID: " + rGuid.getValue());
138
            assertTrue(rGuid.toString().equals(id.toString())); 
139
            System.out.println("created doc with id " + id.getValue());
140
        }
141
        catch(Exception e)
142
        {
143
            fail("Unexpected error in testDescribe: " + e.getMessage());
144
        }
145
	}
146
	
147
	/**
148
	 * test the delete function
149
	 */
150
	public void testDelete()
151
	{
152
	    try
153
        {
154
            CrudService cs = CrudService.getInstance();
155
            AuthToken token = getToken();
156
            String doc = getTestDoc();
157
            Identifier id = createDoc(token, doc);
158
            System.out.println("ID: " + id.getValue());
159
            makeDocPublic(token, id, true);
160
            cs.get(token, id);
161
            cs.delete(token, id);
162
            //there should be a try/catch around the next statement
163
            //to make sure that the document is no longer readable,
164
            //but because the data is output in a 2nd thread, the
165
            //exception does not get caught here.  See 
166
            //https://redmine.dataone.org/issues/1079
167
            //cs.get(token, id);
168
        }
169
        catch(Exception e)
170
        {
171
            fail("Unexpected error in testDescribe: " + e.getMessage());
172
        }
173
	}
174
	
175
	/**
176
	 * test the describe crud function
177
	 */
178
	public void testDescribe()
179
	{
180
	    try
181
	    {
182
	        CrudService cs = CrudService.getInstance();
183
	        AuthToken token = getToken();
184
	        
185
	        String doc = getTestDoc();
186
	        Identifier id = createDoc(token, doc);
187
	        System.out.println("ID: " + id.getValue());
188
	        makeDocPublic(token, id, true);
189
	        
190
	        SystemMetadata sm = cs.getSystemMetadata(token, id);
191
	        
192
	        DescribeResponse dr = cs.describe(token, id);
193
	        
194
	        assertTrue(dr.getDataONE_Checksum().getValue().equals(sm.getChecksum().getValue()));
195
	        assertTrue(dr.getDataONE_ObjectFormat().toString().equals(sm.getObjectFormat().toString()));
196
	        assertTrue(dr.getLast_Modified().getTime() == sm.getDateSysMetadataModified().getTime());
197
	        assertTrue(dr.getContent_Length() == sm.getSize());
198
	    }
199
	    catch(Exception e)
200
	    {
201
	        fail("Unexpected error in testDescribe: " + e.getMessage());
202
	    }
203
	}
204
	
205
	/**
206
	 * test the checksum creation
207
	 */
208
	public void testChecksum()
209
	{
210
	    try
211
	    {
212
	        CrudService cs = CrudService.getInstance();
213
	        AuthToken token = getToken();
214
	        System.out.println("token: " + token.getToken());
215
	        String doc = getTestDoc();
216
	        Identifier id = createDoc(token, doc);
217
	        System.out.println("ID: " + id.getValue());
218
            makeDocPublic(token, id, true);
219
	        String doc2 = getDoc(token, id);
220
            
221
	        String checksum = checksum(doc);
222
            Checksum checksum2 = cs.getChecksum(token, id, "MD5");
223
            System.out.println("doc: \"" + doc + "\"");
224
            System.out.println("doc2: \"" + doc2 + "\"");
225
            System.out.println("checksum1: " + checksum);
226
            System.out.println("checksum2: " + checksum2.getValue());
227
            assertTrue(checksum.equals(checksum2.getValue()));
228
            
229
	    }
230
	    catch(Exception e)
231
	    {
232
	        fail("Unexpected error in testChecksum: " + e.getMessage());
233
	    }
234
	    
235
	}
236
	
237
	/**
238
	 * insert an invalid document and make sure create fails and does not
239
	 * leave any artifacts behind
240
	 */
241
	public void testFailedCreate()
242
	{
243
	    try
244
	    {
245
	        String invalidDoc = "<xxx></yyy>";
246
	        System.out.println("trying to insert doc " + invalidDoc);
247
	        CrudService cs = CrudService.getInstance();
248
            AuthToken token = getToken();
249
            //run create
250
            try
251
            {
252
                Identifier id = createDoc(token, invalidDoc);
253
                fail("Should have thrown an exception.");
254
            }
255
            catch(Exception e)
256
            {
257
                //e.printStackTrace();
258
            }
259
            
260
	    }
261
	    catch(Exception e)
262
	    {
263
	        fail("unexpected exception in testFailedCreate: " + e.getMessage());
264
	    }
265
	}
266
	
267
	/**
268
	 * test to make sure that get and listObjects methods are publicly accessible
269
	 */
270
	public void testPublicAccess()
271
	{
272
	    try
273
	    {
274
	        System.out.println("********************* testPublicAccess *********************");
275
	        AuthToken token = new AuthToken("public");
276
	        //AuthToken token = getToken();
277
	        CrudService cs = CrudService.getInstance();
278
	        ObjectList ol = cs.listObjects(token, null, null, null);
279
	        System.out.println("ol: " + ol.sizeObjectInfoList());
280
	        assertTrue(ol.sizeObjectInfoList() > 0);
281
	        
282
	        ObjectInfo oi = ol.getObjectInfo(0);
283
	        String s = getDoc(token, oi.getIdentifier());
284
	        assertNotNull(s);
285
	        
286
	        try
287
	        { //try a create with the public auth.  should fail
288
	            Identifier id = new Identifier();
289
	            String docid = generateDocumentId();
290
	            id.setValue(docid);
291
	            String testDoc = getTestDoc();
292
	            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
293
	            ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
294
	            SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
295
	            assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
296
	            //create the doc
297
	            Identifier idC = cs.create(token, id, sbis, sm);
298
	            fail("Should have thrown an auth exception");
299
	        }
300
	        catch(Exception e)
301
	        {
302
	            
303
	        }
304
	        
305
	        //create a legit doc
306
	        token = getToken();
307
	        Identifier id = new Identifier();
308
            String docid = generateDocumentId();
309
            id.setValue(docid);
310
            String testDoc = getTestDoc();
311
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
312
            ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
313
            SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
314
            assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
315
            //create the doc
316
            Identifier idC = cs.create(token, id, sbis, sm);
317
            //make it public, then use a public token to try to get it
318
            InputStream data = null;
319
            try
320
            {
321
                AuthToken publicToken = new AuthToken("public");
322
                data = cs.get(publicToken, id);
323
                data.read();
324
                
325
                fail("should have thrown an exception");
326
            }
327
            catch(Exception e)
328
            {
329
                /*System.out.println("%%%%%%%%%%%%%%%%%%%%%%%getting result");
330
                Object o = ((InputStreamFromOutputStream)data).getResult();
331
                System.out.println("result: " + o);
332
                System.out.println("exception thrown!");
333
                System.out.println("exception thrown: " + e.getClass().getName());*/
334
            }
335
            
336
            
337
            
338
            makeDocPublic(token, id, true);
339
            token = new AuthToken("public");
340
	        data = cs.get(token, id);
341
	    }
342
	    catch(Exception e)
343
	    {
344
	        fail("Error in testPublicAccess: " + e.getMessage());
345
	    }
346
	    
347
	}
348
	
349
	/**
350
	 * test for an error where the checksum algorithm gets appended onto the checksum.
351
	 */
352
	public void testChecksumError()
353
	{
354
	    printTestHeader("testChecksumError");
355
	    try
356
	    {
357
	        Date d1 = new Date(new Date().getTime() - 5000);
358
	        CrudService cs = CrudService.getInstance();
359
	        AuthToken token = getToken();
360
	        ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
361
	        //create docs at different times
362

    
363
	        Identifier id = new Identifier();
364
	        String docid = "test." + new Date().getTime();
365
	        id.setValue(docid);
366

    
367
	        //create the system metadata then run the create method
368
	        String testDoc = getTestDoc();
369
	        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
370
	        SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
371
	        assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
372
	        //create the doc
373
	        Identifier idC = cs.create(token, id, sbis, sm);
374
	        makeDocPublic(token, id, true);
375
	        assertTrue(idC.getValue().equals(id.getValue()));
376
	        SystemMetadata smC = getSystemMetadata(token, idC);
377
	        assertFalse(smC.getChecksum().getValue().startsWith("MD5"));
378
	        
379
	        Date d2 = new Date(new Date().getTime() + 5000);
380
	        
381
	        ObjectList ol = cs.listObjects(token, d1, d2, of1);
382
	        assertTrue(ol.sizeObjectInfoList() > 0);
383
	        ObjectInfo oi = ol.getObjectInfo(0);
384
	        //this will fail if the error state exists.
385
	        assertFalse(oi.getChecksum().getValue().startsWith("MD5"));
386
	    }
387
	    catch(Exception e)
388
	    {
389
	        fail("Unexpected exception: " + e.getMessage());
390
	    }
391
        
392
	}
393
	
394
	/**
395
	 * test CrudService.getLogRecords
396
	 */
397
	public void testGetLogRecords()
398
	{
399
	    printTestHeader("testGetLogRecords");
400
	    try
401
	    {
402
	        CrudService cs = CrudService.getInstance();
403
	        AuthToken token = getToken();
404
	        Date fromDate = new Date();
405
	        Identifier id = createDoc(token, getTestDoc());
406
	        Date toDate = new Date();
407
	        Log lrs = cs.getLogRecords(token, fromDate, toDate, null);
408
	        assertNotNull(lrs);
409
	        System.out.println("log entry size: " + lrs.sizeLogEntryList());
410
	        assertTrue(lrs.sizeLogEntryList() == 1);
411
	        LogEntry lrLogEvent = lrs.getLogEntry(0);
412
	        assertTrue(lrLogEvent.getEvent().name().equals("CREATE"));
413
	        assertTrue(lrLogEvent.getIdentifier().getValue().equals(id.getValue()));
414
	    }
415
	    catch(Exception e)
416
	    {
417
	        e.printStackTrace();
418
	        fail("testGetLogRecords threw an unexpected exception: " + e.getMessage());
419
	    }
420
	}
421
	
422
	/**
423
	 * test the generation of system metadata for docs that don't already 
424
	 * have it.  This will be used for migration of existing object stores
425
	 * to dataone.
426
	 */
427
	public void testGenerateMissingSystemMetadata()
428
	{
429
	    printTestHeader("testGenerateMissingSystemMetadata");
430
	    try
431
	    {
432
	        
433
	        CrudService cs = CrudService.getInstance();
434
	        AuthToken token = getToken();
435
	        //create a document with no system metadata
436
	        String testDoc = getTestDoc();
437
	        Identifier id = new Identifier();
438
	        String docid = generateDocumentId();
439
	        id.setValue(docid);
440
	        
441
	        cs.insertOrUpdateDocument(testDoc, id, cs.getSessionData(token), "insert", false); 
442
	        //try to get its system metadata, should fail
443
	        try
444
	        {
445
	            getSystemMetadata(token, id);
446
	            fail("call to getSystemMetadata should have failed.");
447
	        }
448
	        catch(Exception e)
449
	        {}
450
	        
451
	        //generate missing system metadata
452
	        cs.generateMissingSystemMetadata(token);
453
	        //try to get system metadata again, should succeed
454
	        getSystemMetadata(token, id);
455
	    }
456
	    catch(Exception e)
457
	    {
458
	        fail("Unexpected error generating missing system metadata: " + e.getMessage());
459
	    }
460
	}
461
	
462
	/**
463
	 * make sure that only valid sessions can update/delete
464
	 */
465
	public void testAccessControl()
466
	{
467
	    printTestHeader("testAccessControl");
468
        try
469
        {
470
            CrudService cs = CrudService.getInstance();
471
            AuthToken token = getToken();
472
            //create a doc
473
            Identifier id = createDoc(token, getTestDoc());
474
            
475
            //get the doc and sysmetadata
476
            String gotDoc = getDoc(token, id);
477
            SystemMetadata sm = getSystemMetadata(token, id);
478
            
479
            //break the session id
480
            String sessionid = "somefakesessionid";
481
            token = new AuthToken(sessionid);
482
            
483
            //update the doc
484
            gotDoc = gotDoc.replaceAll("XXX", "YYY");
485
            Identifier newid = new Identifier();
486
            newid.setValue(generateDocumentId());
487
            StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
488
            SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
489
            Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
490
            fail("exception should have been thrown.");
491
        }
492
        catch(Exception e)
493
        {
494
        }
495
        
496
        try
497
        {
498
            CrudService cs = CrudService.getInstance();
499
            AuthToken token = new AuthToken("somefakesessionid");
500
            //create a doc
501
            Identifier id = createDoc(token, getTestDoc());
502
            fail("exception should have been thrown.");
503
        }
504
        catch(Exception e)
505
        {
506
        }
507
	}
508
	
509
	/**
510
	 * public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
511
     *     ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
512
     *       throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
513
	 */
514
	public void testListObjects()
515
	{
516
	    printTestHeader("testListObjects");
517
	    try
518
	    {
519
	        CrudService cs = CrudService.getInstance();
520
	        AuthToken token = getToken();
521
	        ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
522
	        ObjectFormat of2 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.0.0");
523
	        //create docs at different times
524
	        Date d1 = new Date();
525
	        Identifier id1 = createDoc(token, getTestDoc(EML2_1_0), of1);
526
            SystemMetadata sm1 = getSystemMetadata(token, id1);
527
            
528
            Date d2 = new Date();
529
            Identifier id2 = createDoc(token, getTestDoc(EML2_0_0), of2);
530
            makeDocPublic(token, id2, true);
531
            SystemMetadata sm2 = getSystemMetadata(token, id2);
532
            
533
            Date d3 = new Date();
534
            Identifier id3 = createDoc(token, getTestDoc(EML2_1_0), of1);
535
            makeDocPublic(token, id3, true);
536
            SystemMetadata sm3 = getSystemMetadata(token, id3);
537
              
538
            Date d4 = new Date();
539
            Identifier id4 = createDoc(token, getTestDoc(EML2_0_0), of2);
540
            makeDocPublic(token, id4, true);
541
            SystemMetadata sm4 = getSystemMetadata(token, id4);
542
            
543
            Date d5 = new Date();
544
            Identifier id5 = createDoc(token, getTestDoc(EML2_1_0), of1);
545
            makeDocPublic(token, id5, true);
546
            SystemMetadata sm5 = getSystemMetadata(token, id5);  
547
             
548
            Date d6 = new Date();
549
            
550
            //now get the objects for specific time ranges and test that it returns
551
            //the correct objects
552
            
553
            //ObjectList list = cs.listObjects(token, null, null, null);
554
            //System.out.println("list size: " + list.sizeObjectInfoList());
555
           
556
            //should return sm1 and sm2
557
            ObjectList list1 = cs.listObjects(token, d1, d3, null);
558
            assertTrue(list1.sizeObjectInfoList() == 2);
559
            assertTrue(idInObjectList(id1, list1));
560
            assertTrue(idInObjectList(id2, list1));
561
            
562
            ObjectInfo info = list1.getObjectInfo(0);
563
            
564
            
565
            //should only return sm1
566
            ObjectList list2 = cs.listObjects(token, d1, d3, of1);
567
            assertTrue(list2.sizeObjectInfoList() == 1);
568
            assertTrue(idInObjectList(id1, list2));
569
            
570
            //should return sm1-sm4
571
            ObjectList list3 = cs.listObjects(token, d1, d5, null);
572
            assertTrue(list3.sizeObjectInfoList() == 4);
573
            ObjectInfo oi4 = list3.getObjectInfo(0);
574
            assertTrue(idInObjectList(id1, list3));
575
            assertTrue(idInObjectList(id2, list3));
576
            assertTrue(idInObjectList(id3, list3));
577
            assertTrue(idInObjectList(id4, list3));
578
            
579
            //should only return sm2 and sm4
580
            ObjectList list4 = cs.listObjects(token, d1, d5, of2);
581
            assertTrue(list4.sizeObjectInfoList() == 2);
582
            assertTrue(idInObjectList(id2, list4));
583
            assertTrue(idInObjectList(id4, list4));
584
            
585
            //should return all
586
            ObjectList list5 = cs.listObjects(token, d1, d6, null);
587
            assertTrue(list5.sizeObjectInfoList() == 5);
588
            assertTrue(idInObjectList(id1, list5));
589
            assertTrue(idInObjectList(id2, list5));
590
            assertTrue(idInObjectList(id3, list5));
591
            assertTrue(idInObjectList(id4, list5));
592
            assertTrue(idInObjectList(id5, list5));
593
            
594
            //should return 1, 3, 5
595
            ObjectList list6 = cs.listObjects(token, d1, d6, of1);
596
            assertTrue(list6.sizeObjectInfoList() == 3);
597
            assertTrue(idInObjectList(id1, list6));
598
            assertTrue(idInObjectList(id3, list6));
599
            assertTrue(idInObjectList(id5, list6));
600
            
601
            //should return 4 (id1 is not public)
602
            token = new AuthToken("public");
603
            ObjectList list7 = cs.listObjects(token, d1, d6, null);
604
            //System.out.println("list7 size: " + list7.sizeObjectInfoList());
605
            assertTrue(list7.sizeObjectInfoList() == 5);
606
            
607
            //test paging
608
            ObjectList list8 = cs.listObjects(token, d1, d6, null, false, 2, 2);
609
            assertTrue(list8.getCount() == 2);
610
            assertTrue(list8.getStart() == 2);
611
            assertTrue(list8.getTotal() == 5);
612
            
613
            
614
	    }
615
	    catch(Exception e)
616
	    {
617
	        //e.printStackTrace();
618
	        fail("Error in listObjects: " + e.getMessage());
619
	    }
620
	}
621
	
622
	private boolean idInObjectList(Identifier id, ObjectList list)
623
	{
624
	    for(int i=0; i<list.sizeObjectInfoList(); i++)
625
	    {
626
	        ObjectInfo oi = list.getObjectInfo(i);
627
	        if(id.getValue().equals(oi.getIdentifier().getValue()))
628
	            return true;
629
	    }
630
	    return false;
631
	}
632
	
633
	/**
634
	 * public Identifier update(AuthToken token, Identifier guid, 
635
     *       InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
636
     *         throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
637
     *           UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
638
     *           NotImplemented
639
	 */
640
	public void testUpdate()
641
	{
642
	    printTestHeader("testUpdate");
643
	    try
644
	    {
645
	        CrudService cs = CrudService.getInstance();
646
	        AuthToken token = getToken();
647
            //create a doc
648
            Identifier id = createDoc(token, getTestDoc());
649
            
650
            //get the doc and sysmetadata
651
            String gotDoc = getDoc(token, id);
652
            SystemMetadata sm = getSystemMetadata(token, id);
653
            
654
            //update the doc
655
            gotDoc = gotDoc.replaceAll("XXX", "YYY");
656
            Identifier newid = new Identifier();
657
            newid.setValue(generateDocumentId());
658
            StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
659
            SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
660
            Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
661
            
662
            //get doc - check that it matches update
663
            String newdoc = getDoc(token, newid);
664
            assertTrue(gotDoc.equals(newdoc));
665
            
666
            //get sysmeta - check that ids and other fields are updated
667
            SystemMetadata newnewsm = getSystemMetadata(token, id);
668
            assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
669
            
670
            //get the new sysmeta and make sure the obsoletes field is set
671
            SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
672
            assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
673
        }
674
        catch(Exception e)
675
        {
676
            e.printStackTrace();
677
            fail("Error in testUpdate: " + e.getMessage());
678
        }
679
	}
680
	
681
	/**
682
	 * public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
683
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
684
     *       InvalidRequest, NotImplemented
685
	 */
686
	public void testGetSystemMetadata()
687
	{
688
	    printTestHeader("testGetSystemMetadata");
689
	    try
690
	    {
691
            CrudService cs = CrudService.getInstance();
692
            AuthToken token = getToken();
693
            //run create
694
            Identifier id = createDoc(token, getTestDoc());
695
            //get the systemMetadata and make sure it is the correct object for this testDoc
696
            SystemMetadata sm = getSystemMetadata(token, id);
697
            assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
698
            assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
699
            
700
            try
701
            {
702
                Identifier fakeid = new Identifier();
703
                fakeid.setValue("somethingfake.234234");
704
                getSystemMetadata(token, fakeid);
705
                fail("getSystemMetadata should have thrown an exception.");
706
            }
707
            catch(Exception e)
708
            {
709
                assertTrue(true);
710
            }
711
        }
712
        catch(Exception e)
713
        {
714
            e.printStackTrace();
715
            fail("Error testing system metadata: " + e.getMessage());
716
        }
717
	}
718
	
719
	/**
720
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
721
   *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
722
   *       InsufficientResources, InvalidSystemMetadata, NotImplemented
723
   *
724
   * public InputStream get(AuthToken token, Identifier guid)
725
   *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
726
   *       NotImplemented
727
	 */
728
	public void testCreateAndGet()
729
	{
730
	    printTestHeader("testCreateAndGet");
731
	    try
732
	    {
733
	        CrudService cs = CrudService.getInstance();
734
	        AuthToken token = getToken();
735
	        //run create
736
	        Identifier id = createDoc(token, getTestDoc());
737
	        //make these docs public for debugging purposes.
738
	        makeDocPublic(token, id, true);
739
            //compare the docs
740
            String gotDoc = getDoc(token, id);
741
            assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
742
            
743
            try
744
            {
745
                Identifier fakeid = new Identifier();
746
                fakeid.setValue("somethingfake.234234");
747
                getDoc(token, fakeid);
748
                fail("testCreateAndGet should have thrown an exception.");
749
            }
750
            catch(Exception e)
751
            {
752
                assertTrue(true);
753
            }
754
        }
755
        catch(Exception e)
756
        {
757
            e.printStackTrace();
758
            fail("Error in testCreate: " + e.getMessage());
759
        }
760
	}
761
	
762
	/**
763
	 * getInstance()
764
	 */
765
	public void testSingletonAccessor()
766
	{
767
	    printTestHeader("testSingletonAccessor");
768
	    CrudService cs = CrudService.getInstance();
769
	    assertNotNull(cs);
770
	}
771
	
772
	/**
773
	 * Run an initial test that always passes to check that the test harness is
774
	 * working.
775
	 */
776
	public void initialize() 
777
	{
778
	    printTestHeader("initialize");
779
		assertTrue(1 == 1);
780
	}
781
	
782
	/**
783
	 * return the systemMetadata object for id
784
	 */
785
	private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
786
	  throws Exception
787
	{
788
	    CrudService cs = CrudService.getInstance();
789
	    return cs.getSystemMetadata(token, id);
790
	}
791
	
792
	/**
793
	 * get a doc from metacat using CrudService.get()
794
	 */
795
	private String getDoc(AuthToken token, Identifier id)
796
	  throws Exception
797
	{
798
	    CrudService cs = CrudService.getInstance();
799
        InputStream gotDocStream = cs.get(token, id);
800
        return IOUtils.toString(gotDocStream);
801
	}
802
	
803
	public String getTestDoc()
804
	{
805
	    return getTestDoc(null);
806
	}
807
	
808
	/**
809
	 * return a test document.  objectFormat should come from MCTestCase
810
	 */
811
	public String getTestDoc(String objectFormat)
812
	{
813
	    if(objectFormat == null)
814
	    {
815
	        return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
816
	    }
817
	    
818
	    String accessBlock = getAccessBlock("public", true, true,
819
                false, false, false);
820
	    
821
	    
822
	    return getTestEmlDoc("Test identifier manager", objectFormat, null,
823
	                null, "http://fake.example.com/somedata", null,
824
	                accessBlock, null, null,
825
	                null, null);
826
	}
827
	
828
	/**
829
	 * authenticate and return a token
830
	 * use the test.mcUser and test.mcPassword username/password combo
831
	 */
832
	public AuthToken getToken()
833
	  throws Exception
834
	{
835
        String username = PropertyService.getProperty("test.mcUser");
836
        String password = PropertyService.getProperty("test.mcPassword");
837
        return getToken(username, password);
838
	}
839
	
840
	/**
841
	 * authenticate and return a token using the given credentials
842
	 */
843
	public AuthToken getToken(String username, String password)
844
	  throws Exception
845
	{
846
	    CrudService cs = CrudService.getInstance();
847
        //login and get a sessionid
848
        MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
849
        String response = restClient.login(username, password);
850
        String sessionid = restClient.getSessionId();
851
        SessionService sessionService = SessionService.getInstance();
852
        sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
853
        AuthToken token = new AuthToken(sessionid);
854
        return token;
855
	}
856
	
857
	/**
858
	 * create a doc using CrudService.create() and return its id
859
	 */
860
	public Identifier createDoc(AuthToken token, String testDoc) throws Exception
861
	{
862
	    return createDoc(token, testDoc, ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
863
	}
864
	
865
	/**
866
	 * create a doc using CrudService.create() and return its id
867
	 */
868
	public Identifier createDoc(AuthToken token, String testDoc, ObjectFormat format) throws Exception
869
	{
870
	    Identifier id;
871
        CrudService cs = CrudService.getInstance();
872
        
873
        id = new Identifier();
874
        String docid = generateDocumentId();
875
        id.setValue(docid);
876
        
877
        //create the system metadata then run the create method
878
        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
879
        SystemMetadata sm = createSystemMetadata(id, testDoc, format);
880
        //create the doc
881
        cs.create(token, id, sbis, sm);
882
        createCount++;
883
        return id;
884
	}
885
	
886
	/**
887
	 * create a generic SystemMetadata object for testing
888
	 */
889
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
890
	  throws Exception
891
	{
892
	    return createSystemMetadata(id, testDoc, 
893
	            ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
894
	}
895
	
896
	/**
897
	 * create system metadata with a specified id, doc and format
898
	 */
899
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc, ObjectFormat format)
900
	  throws Exception
901
	{
902
	    SystemMetadata sm = new SystemMetadata();
903
        //set the id
904
        sm.setIdentifier(id);
905
        sm.setObjectFormat(format);
906
        //create the checksum
907
        String checksumS = checksum(testDoc);
908
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
909
        Checksum checksum = new Checksum();
910
        checksum.setValue(checksumS);
911
        checksum.setAlgorithm(ca);
912
        sm.setChecksum(checksum);
913
        //set the size
914
        sm.setSize(testDoc.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
915
        //submitter
916
        Principal p = new Principal();
917
        p.setValue("joe");
918
        sm.setSubmitter(p);
919
        sm.setRightsHolder(p);
920
        sm.setDateUploaded(new Date());
921
        sm.setDateSysMetadataModified(new Date());
922
        NodeReference nr = new NodeReference();
923
        nr.setValue("metacat");
924
        sm.setOriginMemberNode(nr);
925
        sm.setAuthoritativeMemberNode(nr);
926
        return sm;
927
	}
928
	
929
	/**
930
	 *  make a document public in metacat by inserting an access document
931
	 * @param id
932
	 */
933
	private void makeDocPublic(AuthToken token, Identifier id, boolean systemMetadataToo)
934
	  throws Exception
935
	{
936
	    CrudService cs = CrudService.getInstance();
937
	    cs.setAccess(token, id, "public", "read", "allow", "allowFirst", systemMetadataToo);
938
	}
939
	
940
	/**
941
	 * print a header to start each test
942
	 */
943
	private void printTestHeader(String testName)
944
	{
945
	    System.out.println();
946
	    System.out.println("*************** " + testName + " ***************");
947
	}
948
  
949
	/**
950
	 * produce an md5 checksum for item
951
	 */
952
	private String checksum(String item)
953
	  throws Exception
954
	{
955
        StringBufferInputStream fis =  new StringBufferInputStream(item);
956
        
957
        byte[] buffer = new byte[1024];
958
        MessageDigest complete = MessageDigest.getInstance("MD5");
959
        int numRead;
960
        
961
        do 
962
        {
963
          numRead = fis.read(buffer);
964
          if (numRead > 0) 
965
          {
966
            complete.update(buffer, 0, numRead);
967
          }
968
        } while (numRead != -1);
969
        
970
        
971
        return getHex(complete.digest());
972
	}
973
	
974
	/**
975
	 * convert a byte array to a hex string
976
	 */
977
	private static String getHex( byte [] raw ) 
978
	{
979
	    final String HEXES = "0123456789ABCDEF";
980
        if ( raw == null ) {
981
          return null;
982
        }
983
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
984
        for ( final byte b : raw ) {
985
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
986
             .append(HEXES.charAt((b & 0x0F)));
987
        }
988
        return hex.toString();
989
  }
990
}
(1-1/3)