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.client.MetacatAuthException;
35
import edu.ucsb.nceas.metacat.client.MetacatException;
36
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
37
import edu.ucsb.nceas.metacat.dataone.CrudService;
38
import junit.framework.Test;
39
import junit.framework.TestSuite;
40

    
41
import org.dataone.service.exceptions.InvalidRequest;
42
import org.dataone.service.exceptions.InvalidToken;
43
import org.dataone.service.exceptions.NotAuthorized;
44
import org.dataone.service.exceptions.NotImplemented;
45
import org.dataone.service.exceptions.ServiceFailure;
46
import org.dataone.service.types.*;
47

    
48
import edu.ucsb.nceas.metacat.properties.PropertyService;
49
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
50

    
51
import edu.ucsb.nceas.metacat.service.SessionService;
52
import edu.ucsb.nceas.metacat.util.SessionData;
53

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

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

    
82
	/**
83
	 * Create a suite of tests to be run together
84
	 */
85
	public static Test suite() 
86
	{
87
		TestSuite suite = new TestSuite();
88
		suite.addTest(new CrudServiceTest("initialize"));
89
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
90
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
91
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
92
		suite.addTest(new CrudServiceTest("testUpdate"));
93
		suite.addTest(new CrudServiceTest("testListObjects"));
94
		//suite.addTest(new CrudServiceTest(""));
95
		return suite;
96
	}
97
	
98
	/**
99
	 * public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
100
     *     ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
101
     *       throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
102
	 */
103
	public void testListObjects()
104
	{
105
	    printTestHeader("testListObjects");
106
	    try
107
	    {
108
	        CrudService cs = CrudService.getInstance();
109
	        AuthToken token = getToken();
110
	        ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
111
	        ObjectFormat of2 = ObjectFormat.convert("text/plain");
112
	        //create docs at different times
113
	        Date d1 = new Date();
114
	        Identifier id1 = createDoc(token, getTestDoc(), of1);
115
            SystemMetadata sm1 = getSystemMetadata(token, id1);
116
            
117
            Date d2 = new Date();
118
            Identifier id2 = createDoc(token, getTestDoc(), of2);
119
            SystemMetadata sm2 = getSystemMetadata(token, id2);
120
            
121
            Date d3 = new Date();
122
            Identifier id3 = createDoc(token, getTestDoc(), of1);
123
            SystemMetadata sm3 = getSystemMetadata(token, id3);
124
              
125
            Date d4 = new Date();
126
            Identifier id4 = createDoc(token, getTestDoc(), of2);
127
            SystemMetadata sm4 = getSystemMetadata(token, id4);
128
            
129
            Date d5 = new Date();
130
            Identifier id5 = createDoc(token, getTestDoc(), of1);
131
            SystemMetadata sm5 = getSystemMetadata(token, id5);  
132
              
133
            //now get the objects for specific time ranges and test that it returns
134
            //the correct objects
135
	        
136
            //should return sm1 and sm2
137
            ObjectList list1 = cs.listObjects(token, d1, d2, null);
138
            assertTrue(list1.sizeObjectInfoList() == 2);
139
            ObjectInfo oi1 = list1.getObjectInfo(0);
140
            assertTrue(oi1.getIdentifier().getValue().equals(id1.getValue()));
141
            ObjectInfo oi2 = list1.getObjectInfo(1);
142
            assertTrue(oi2.getIdentifier().getValue().equals(id2.getValue()));
143
            
144
            //should only return sm1
145
            ObjectList list2 = cs.listObjects(token, d1, d2, of1);
146
            assertTrue(list2.sizeObjectInfoList() == 1);
147
            ObjectInfo oi3 = list2.getObjectInfo(0);
148
            assertTrue(oi3.getIdentifier().getValue().equals(id1.getValue()));
149
            
150
            //should return sm1-sm4
151
            ObjectList list3 = cs.listObjects(token, d1, d4, null);
152
            assertTrue(list3.sizeObjectInfoList() == 4);
153
            ObjectInfo oi4 = list3.getObjectInfo(0);
154
            assertTrue(oi4.getIdentifier().getValue().equals(id1.getValue()));
155
            ObjectInfo oi5 = list3.getObjectInfo(1);
156
            assertTrue(oi5.getIdentifier().getValue().equals(id2.getValue()));
157
            ObjectInfo oi6 = list3.getObjectInfo(2);
158
            assertTrue(oi6.getIdentifier().getValue().equals(id3.getValue()));
159
            ObjectInfo oi7 = list3.getObjectInfo(3);
160
            assertTrue(oi7.getIdentifier().getValue().equals(id4.getValue()));
161
            
162
            //should only return sm2 and sm4
163
            ObjectList list4 = cs.listObjects(token, d1, d2, of2);
164
            assertTrue(list4.sizeObjectInfoList() == 2);
165
            ObjectInfo oi8 = list4.getObjectInfo(0);
166
            assertTrue(oi8.getIdentifier().getValue().equals(id2.getValue()));
167
            ObjectInfo oi9 = list4.getObjectInfo(1);
168
            assertTrue(oi9.getIdentifier().getValue().equals(id4.getValue()));
169
            
170
            //should return all
171
            ObjectList list5 = cs.listObjects(token, d1, d5, null);
172
            assertTrue(list5.sizeObjectInfoList() == 5);
173
            //should return 1, 3, 5
174
            ObjectList list6 = cs.listObjects(token, d1, d2, of1);
175
            assertTrue(list5.sizeObjectInfoList() == 3);
176
            
177
            
178
	    }
179
	    catch(Exception e)
180
	    {
181
	        fail("Error in listObjects: " + e.getMessage());
182
	    }
183
	}
184
	
185
	/**
186
	 * public Identifier update(AuthToken token, Identifier guid, 
187
     *       InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
188
     *         throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
189
     *           UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
190
     *           NotImplemented
191
	 */
192
	public void testUpdate()
193
	{
194
	    printTestHeader("testUpdate");
195
	    try
196
	    {
197
	        CrudService cs = CrudService.getInstance();
198
	        AuthToken token = getToken();
199
            //create a doc
200
            Identifier id = createDoc(token, getTestDoc());
201
            
202
            //get the doc and sysmetadata
203
            String gotDoc = getDoc(token, id);
204
            SystemMetadata sm = getSystemMetadata(token, id);
205
            
206
            //update the doc
207
            gotDoc = gotDoc.replaceAll("XXX", "YYY");
208
            Identifier newid = new Identifier();
209
            newid.setValue(generateDocumentId());
210
            StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
211
            SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
212
            Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
213
            
214
            //get doc - check that it matches update
215
            String newdoc = getDoc(token, newid);
216
            assertTrue(gotDoc.equals(newdoc));
217
            
218
            //get sysmeta - check that ids and other fields are updated
219
            SystemMetadata newnewsm = getSystemMetadata(token, id);
220
            assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
221
            
222
            //get the new sysmeta and make sure the obsoletes field is set
223
            SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
224
            assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
225
        }
226
        catch(Exception e)
227
        {
228
            e.printStackTrace();
229
            fail("Error in testUpdate: " + e.getMessage());
230
        }
231
	}
232
	
233
	/**
234
	 * public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
235
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
236
     *       InvalidRequest, NotImplemented
237
	 */
238
	public void testGetSystemMetadata()
239
	{
240
	    printTestHeader("testGetSystemMetadata");
241
	    try
242
	    {
243
            CrudService cs = CrudService.getInstance();
244
            AuthToken token = getToken();
245
            //run create
246
            Identifier id = createDoc(token, getTestDoc());
247
            //get the systemMetadata and make sure it is the correct object for this testDoc
248
            SystemMetadata sm = getSystemMetadata(token, id);
249
            assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
250
            assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
251
            
252
            try
253
            {
254
                Identifier fakeid = new Identifier();
255
                fakeid.setValue("somethingfake.234234");
256
                getSystemMetadata(token, fakeid);
257
                fail("getSystemMetadata should have thrown an exception.");
258
            }
259
            catch(Exception e)
260
            {
261
                assertTrue(true);
262
            }
263
        }
264
        catch(Exception e)
265
        {
266
            e.printStackTrace();
267
            fail("Error testing system metadata: " + e.getMessage());
268
        }
269
	}
270
	
271
	/**
272
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
273
     *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
274
     *       InsufficientResources, InvalidSystemMetadata, NotImplemented
275
     *
276
     * public InputStream get(AuthToken token, Identifier guid)
277
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
278
     *       NotImplemented
279
	 */
280
	public void testCreateAndGet()
281
	{
282
	    printTestHeader("testCreateAndGet");
283
	    try
284
	    {
285
	        CrudService cs = CrudService.getInstance();
286
	        AuthToken token = getToken();
287
	        //run create
288
	        Identifier id = createDoc(token, getTestDoc());
289
            //compare the docs
290
            String gotDoc = getDoc(token, id);
291
            assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
292
            
293
            try
294
            {
295
                Identifier fakeid = new Identifier();
296
                fakeid.setValue("somethingfake.234234");
297
                getDoc(token, fakeid);
298
                fail("testCreateAndGet should have thrown an exception.");
299
            }
300
            catch(Exception e)
301
            {
302
                assertTrue(true);
303
            }
304
        }
305
        catch(Exception e)
306
        {
307
            e.printStackTrace();
308
            fail("Error in testCreate: " + e.getMessage());
309
        }
310
	}
311

    
312
	/**
313
	 * getInstance()
314
	 */
315
	public void testSingletonAccessor()
316
	{
317
	    printTestHeader("testSingletonAccessor");
318
	    CrudService cs = CrudService.getInstance();
319
	    assertNotNull(cs);
320
	}
321
	
322
	/**
323
	 * Run an initial test that always passes to check that the test harness is
324
	 * working.
325
	 */
326
	public void initialize() 
327
	{
328
	    printTestHeader("initialize");
329
		assertTrue(1 == 1);
330
	}
331
	
332
	/**
333
	 * return the systemMetadata object for id
334
	 */
335
	private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
336
	  throws Exception
337
	{
338
	    CrudService cs = CrudService.getInstance();
339
	    return cs.getSystemMetadata(token, id);
340
	}
341
	
342
	/**
343
	 * get a doc from metacat using CrudService.get()
344
	 */
345
	private String getDoc(AuthToken token, Identifier id)
346
	  throws Exception
347
	{
348
	    CrudService cs = CrudService.getInstance();
349
	    //try to get the same doc then compare them
350
        InputStream gotDocStream = cs.get(token, id);
351
        StringBuffer sb = new StringBuffer();
352
        byte[] b = new byte[1024];
353
        int numread = gotDocStream.read(b, 0, 1024);
354
        while(numread != -1)
355
        {
356
            sb.append(new String(b, 0, numread));
357
            numread = gotDocStream.read(b, 0, 1024);
358
        }
359
        return sb.toString();
360
	}
361
	
362
	/**
363
	 * return a test document
364
	 */
365
	private String getTestDoc()
366
	{
367
	    return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
368
	}
369
	
370
	/**
371
	 * authenticate and return a token
372
	 */
373
	private AuthToken getToken()
374
	  throws Exception
375
	{
376
	    CrudService cs = CrudService.getInstance();
377
        //login and get a sessionid
378
        MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
379
        String username = PropertyService.getProperty("test.mcUser");
380
        String password = PropertyService.getProperty("test.mcPassword");
381
        String response = restClient.login(username, password);
382
        String sessionid = restClient.getSessionId();
383
        SessionService sessionService = SessionService.getInstance();
384
        sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
385
        AuthToken token = new AuthToken(sessionid);
386
        return token;
387
	}
388
	
389
	/**
390
	 * create a doc using CrudService.create() and return its id
391
	 */
392
	private Identifier createDoc(AuthToken token, String testDoc) throws Exception
393
	{
394
	    return createDoc(token, testDoc, ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
395
	}
396
	
397
	/**
398
	 * create a doc using CrudService.create() and return its id
399
	 */
400
	private Identifier createDoc(AuthToken token, String testDoc, ObjectFormat format) throws Exception
401
	{
402
	    Identifier id;
403
        CrudService cs = CrudService.getInstance();
404
        
405
        id = new Identifier();
406
        String docid = generateDocumentId();
407
        id.setValue(docid);
408
        
409
        //create the system metadata then run the create method
410
        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
411
        SystemMetadata sm = createSystemMetadata(id, testDoc, format);
412
        //create the doc
413
        cs.create(token, id, sbis, sm);
414
        return id;
415
	}
416
	
417
	/**
418
	 * create a generic SystemMetadata object for testing
419
	 */
420
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
421
	  throws Exception
422
	{
423
	    return createSystemMetadata(id, testDoc, 
424
	            ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
425
	}
426
	
427
	/**
428
	 * create system metadata with a specified id, doc and format
429
	 */
430
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc, ObjectFormat format)
431
	  throws Exception
432
	{
433
	    SystemMetadata sm = new SystemMetadata();
434
        //set the id
435
        sm.setIdentifier(id);
436
        sm.setObjectFormat(format);
437
        //create the checksum
438
        String checksumS = checksum(testDoc);
439
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
440
        Checksum checksum = new Checksum();
441
        checksum.setValue(checksumS);
442
        checksum.setAlgorithm(ca);
443
        sm.setChecksum(checksum);
444
        //set the size
445
        sm.setSize(testDoc.getBytes().length);
446
        //submitter
447
        Principal p = new Principal();
448
        p.setValue("joe");
449
        sm.setSubmitter(p);
450
        sm.setRightsHolder(p);
451
        sm.setDateUploaded(new Date());
452
        sm.setDateSysMetadataModified(new Date());
453
        NodeReference nr = new NodeReference();
454
        nr.setValue("metacat");
455
        sm.setOriginMemberNode(nr);
456
        sm.setAuthoritativeMemberNode(nr);
457
        return sm;
458
	}
459
	
460
	/**
461
	 * print a header to start each test
462
	 */
463
	private void printTestHeader(String testName)
464
	{
465
	    System.out.println();
466
	    System.out.println("********************************** " + testName + " **********************************");
467
	}
468
  
469
	/**
470
	 * produce an md5 checksum for item
471
	 */
472
	private String checksum(String item)
473
	  throws Exception
474
	{
475
        StringBufferInputStream fis =  new StringBufferInputStream(item);
476
        
477
        byte[] buffer = new byte[1024];
478
        MessageDigest complete = MessageDigest.getInstance("MD5");
479
        int numRead;
480
        
481
        do 
482
        {
483
          numRead = fis.read(buffer);
484
          if (numRead > 0) 
485
          {
486
            complete.update(buffer, 0, numRead);
487
          }
488
        } while (numRead != -1);
489
        
490
        
491
        return getHex(complete.digest());
492
	}
493
	
494
	/**
495
	 * convert a byte array to a hex string
496
	 */
497
	private static String getHex( byte [] raw ) 
498
	{
499
	    final String HEXES = "0123456789ABCDEF";
500
        if ( raw == null ) {
501
          return null;
502
        }
503
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
504
        for ( final byte b : raw ) {
505
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
506
             .append(HEXES.charAt((b & 0x0F)));
507
        }
508
        return hex.toString();
509
    }
510
}
    (1-1/1)