Project

General

Profile

« Previous | Next » 

Revision 5359

Added by berkley almost 14 years ago

setting up the framework for the listObjects api call

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
38 38
import junit.framework.Test;
39 39
import junit.framework.TestSuite;
40 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;
41 46
import org.dataone.service.types.*;
42 47

  
43 48
import edu.ucsb.nceas.metacat.properties.PropertyService;
......
85 90
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
86 91
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
87 92
		suite.addTest(new CrudServiceTest("testUpdate"));
93
		suite.addTest(new CrudServiceTest("testListObjects"));
88 94
		//suite.addTest(new CrudServiceTest(""));
89
		//suite.addTest(new CrudServiceTest(""));
90 95
		return suite;
91 96
	}
92 97
	
93 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
	/**
94 186
	 * public Identifier update(AuthToken token, Identifier guid, 
95 187
     *       InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
96 188
     *         throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
......
299 391
	 */
300 392
	private Identifier createDoc(AuthToken token, String testDoc) throws Exception
301 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
	{
302 402
	    Identifier id;
303 403
        CrudService cs = CrudService.getInstance();
304 404
        
......
308 408
        
309 409
        //create the system metadata then run the create method
310 410
        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
311
        SystemMetadata sm = createSystemMetadata(id, testDoc);
411
        SystemMetadata sm = createSystemMetadata(id, testDoc, format);
312 412
        //create the doc
313 413
        cs.create(token, id, sbis, sm);
314 414
        return id;
......
320 420
	private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
321 421
	  throws Exception
322 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
	{
323 433
	    SystemMetadata sm = new SystemMetadata();
324 434
        //set the id
325 435
        sm.setIdentifier(id);
326
        sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
436
        sm.setObjectFormat(format);
327 437
        //create the checksum
328 438
        String checksumS = checksum(testDoc);
329 439
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
60 60
import org.dataone.service.types.Identifier;
61 61
import org.dataone.service.types.LogRecordSet;
62 62
import org.dataone.service.types.SystemMetadata;
63
import org.dataone.service.types.ObjectList;
64
import org.dataone.service.types.ObjectFormat;
63 65
import org.jibx.runtime.BindingDirectory;
64 66
import org.jibx.runtime.IBindingFactory;
65 67
import org.jibx.runtime.IMarshallingContext;
......
278 280
            throw new ServiceFailure("1030", "Error updating document in CrudService: " + e.getMessage());
279 281
        }
280 282
    }
283
    
284
    /**
285
     *  Retrieve the list of objects present on the MN that match the calling 
286
     *  parameters. This method is required to support the process of Member 
287
     *  Node synchronization. At a minimum, this method should be able to 
288
     *  return a list of objects that match:
289
     *  startTime <= SystemMetadata.dateSysMetadataModified
290
     *  but is expected to also support date range (by also specifying endTime), 
291
     *  and should also support slicing of the matching set of records by 
292
     *  indicating the starting index of the response (where 0 is the index 
293
     *  of the first item) and the count of elements to be returned.
294
     *  
295
     * @see http://mule1.dataone.org/ArchitectureDocs/mn_api_replication.html#MN_replication.listObjects
296
     * @param token
297
     * @param startTime
298
     * @param endTime
299
     * @param objectFormat
300
     * @param replicaStatus
301
     * @param start
302
     * @param count
303
     * @return ObjectList
304
     * @throws NotAuthorized
305
     * @throws InvalidRequest
306
     * @throws NotImplemented
307
     * @throws ServiceFailure
308
     * @throws InvalidToken
309
     */
310
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
311
        ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
312
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
313
    {
314
      ObjectList ol = new ObjectList();
315
      //do an squery on metacat to return systemMetadata docs where 
316
      //sm.dateSysMetadataModified >= startTime
317
      //sm.dateSysMetadataModified <= endTime
318
      //further slice the returned dataset if replicaStatus is false.
319
      //if start != 0, return only the documents with index >= start
320
      //if the number of results is > count, return the first count docs
321
      return ol;
322
    }
323
    
324
    /**
325
     * Call listObjects with the default values for replicaStatus (true), start (0),
326
     * and count (1000).
327
     * @param token
328
     * @param startTime
329
     * @param endTime
330
     * @param objectFormat
331
     * @return
332
     * @throws NotAuthorized
333
     * @throws InvalidRequest
334
     * @throws NotImplemented
335
     * @throws ServiceFailure
336
     * @throws InvalidToken
337
     */
338
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
339
        ObjectFormat objectFormat)
340
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
341
    {
342
       return listObjects(token, startTime, endTime, objectFormat, true, 0, 1000);
343
    }
281 344

  
345
    /**
346
     * Delete a document.  NOT IMPLEMENTED
347
     */
282 348
    public Identifier delete(AuthToken token, Identifier guid)
283 349
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
284 350
            NotImplemented {
285 351
        throw new NotImplemented("1000", "This method not yet implemented.");
286 352
    }
287 353

  
354
    /**
355
     * describe a document.  NOT IMPLEMENTED
356
     */
288 357
    public DescribeResponse describe(AuthToken token, Identifier guid)
289 358
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
290 359
            NotImplemented {
291 360
        throw new NotImplemented("1000", "This method not yet implemented.");
292 361
    }
293 362
    
363
    /**
364
     * get a document with a specified guid.
365
     */
294 366
    public InputStream get(AuthToken token, Identifier guid)
295 367
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
296 368
            NotImplemented {
......
341 413
        }
342 414
    }
343 415

  
416
    /**
417
     * get the checksum for a document.  NOT IMPLEMENTED
418
     */
344 419
    public Checksum getChecksum(AuthToken token, Identifier guid)
345 420
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
346 421
            InvalidRequest, NotImplemented {
347 422
        throw new NotImplemented("1000", "This method not yet implemented.");
348 423
    }
349 424

  
425
    /**
426
     * get the checksum for a document.  NOT IMPLEMENTED
427
     */
350 428
    public Checksum getChecksum(AuthToken token, Identifier guid, 
351 429
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
352 430
            NotAuthorized, NotFound, InvalidRequest, NotImplemented {
353 431
        throw new NotImplemented("1000", "This method not yet implemented.");
354 432
    }
355 433

  
434
    /**
435
     * get log records.  NOT IMPLEMENTED
436
     */
356 437
    public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
357 438
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
358 439
            NotImplemented {
359 440
        throw new NotImplemented("1000", "This method not yet implemented.");
360 441
    }
361 442

  
443
    /**
444
     * get the system metadata for a document with a specified guid.
445
     */
362 446
    public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
363 447
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
364 448
            InvalidRequest, NotImplemented {
......
463 547
        return scimeta;
464 548
    }
465 549

  
550
    /**
551
     * insert a data doc
552
     * @param object
553
     * @param guid
554
     * @param sessionData
555
     * @throws ServiceFailure
556
     */
466 557
    private void insertDataObject(InputStream object, Identifier guid, 
467 558
            SessionData sessionData) throws ServiceFailure {
468 559
        
......
538 629
        }
539 630
    }
540 631

  
632
    /**
633
     * write a file to a stream
634
     * @param dir
635
     * @param fileName
636
     * @param data
637
     * @return
638
     * @throws ServiceFailure
639
     */
541 640
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
542 641
        throws ServiceFailure {
543 642
        
......
723 822
        return localId;
724 823
    }
725 824
    
825
    /**
826
     * serialize a system metadata doc
827
     * @param sysmeta
828
     * @return
829
     * @throws ServiceFailure
830
     */
726 831
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
727 832
        throws ServiceFailure {
728 833
        IBindingFactory bfact;
......
739 844
        return sysmetaOut;
740 845
    }
741 846
    
847
    /**
848
     * deserialize a system metadata doc
849
     * @param xml
850
     * @return
851
     * @throws ServiceFailure
852
     */
742 853
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
743 854
        throws ServiceFailure {
744 855
        try {

Also available in: Unified diff