Project

General

Profile

« Previous | Next » 

Revision 9027

Added by Jing Tao about 10 years ago

Add a test method to the method to get head version of a sid.

View differences:

test/edu/ucsb/nceas/metacattest/IdentifierManagerTest.java
24 24

  
25 25
package edu.ucsb.nceas.metacattest;
26 26

  
27
import java.io.ByteArrayInputStream;
28
import java.io.InputStream;
27 29
import java.sql.SQLException;
28 30
import java.util.*;
29 31

  
32
import org.dataone.service.types.v1.Identifier;
33
import org.dataone.service.types.v1.Session;
34
import org.dataone.service.types.v2.SystemMetadata;
35

  
30 36
import edu.ucsb.nceas.MCTestCase;
31 37
import edu.ucsb.nceas.metacat.AccessionNumber;
32 38
import edu.ucsb.nceas.metacat.AccessionNumberException;
......
35 41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
36 42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
37 43
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
44
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
45
import edu.ucsb.nceas.metacat.dataone.MNodeService;
38 46

  
39
public class IdentifierManagerTest extends MCTestCase {
47
public class IdentifierManagerTest extends D1NodeServiceTest {
40 48
    private String badGuid = "test:testIdThatDoesNotExist";
41 49
    
50
    public IdentifierManagerTest(String name) {
51
        super(name);
52
    }
42 53
    /**
43 54
     * Initialize the connection to metacat, and insert a document to be 
44 55
     * used for testing with a known docid.
......
223 234
      System.out.println("localid: " + localid);
224 235
      assertTrue(localid != null);
225 236
    }
237
    
238
    /**
239
     * Test the method - getHeadPID for a speicified SID
240
     */
241
    public void testGetHeadPID() {
242
        
243
        try {
244
            //insert test documents with a series id
245
            Session session = getTestSession();
246
            Identifier guid = new Identifier();
247
            guid.setValue(generateDocumentId());
248
            InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
249
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
250
            String sid1= "sid."+System.nanoTime();
251
            Identifier seriesId = new Identifier();
252
            seriesId.setValue(sid1);
253
            System.out.println("the first sid is "+seriesId.getValue());
254
            sysmeta.setSeriesId(seriesId);
255
            MNodeService.getInstance(request).create(session, guid, object, sysmeta);
256
            System.out.println("the first pid is "+guid.getValue());
257
            Identifier head = IdentifierManager.getInstance().getHeadPID(seriesId);
258
            System.out.println("the head 1 is "+head.getValue());
259
            assertTrue(head.getValue().equals(guid.getValue()));
260
            
261
            //do a update with the same series id
262
            Thread.sleep(1000);
263
            Identifier newPid = new Identifier();
264
            newPid.setValue(generateDocumentId()+"1");
265
            System.out.println("the second pid is "+newPid.getValue());
266
            SystemMetadata newSysMeta = createSystemMetadata(newPid, session.getSubject(), object);
267
            newSysMeta.setObsoletes(guid);
268
            newSysMeta.setSeriesId(seriesId);
269
            MNodeService.getInstance(request).update(session, guid, object, newPid, newSysMeta);
270
            // the pid should be the newPid when we try to get the sid1
271
            head = IdentifierManager.getInstance().getHeadPID(seriesId);
272
            System.out.println("the head 2 is "+head.getValue());
273
            assertTrue(head.getValue().equals(newPid.getValue()));
274
            
275
            //do another update with different series id
276
            Thread.sleep(1000);
277
            String sid2 = "sid."+System.nanoTime();
278
            Identifier seriesId2= new Identifier();
279
            seriesId2.setValue(sid2);
280
            System.out.println("the second sid is "+seriesId2.getValue());
281
            Identifier newPid2 = new Identifier();
282
            newPid2.setValue(generateDocumentId()+"2");
283
            System.out.println("the third pid is "+newPid2.getValue());
284
            SystemMetadata sysmeta3 = createSystemMetadata(newPid2, session.getSubject(), object);
285
            sysmeta3.setObsoletes(newPid);
286
            sysmeta3.setSeriesId(seriesId2);
287
            MNodeService.getInstance(request).update(session, newPid, object, newPid2, sysmeta3);
288
            
289
            // the pid should be the newPid when we try to get the sid1
290
            head =IdentifierManager.getInstance().getHeadPID(seriesId);
291
            System.out.println("the head 3 is "+head.getValue());
292
            assertTrue(head.getValue().equals(newPid.getValue()));
293
            
294
            // the pid should be the newPid2 when we try to get the sid2
295
            head = IdentifierManager.getInstance().getHeadPID(seriesId2);
296
            System.out.println("the head 4 is "+head.getValue());
297
            assertTrue(head.getValue().equals(newPid2.getValue()));
298
            
299
            // the pid should be null when we try to get a no-exist sid
300
            Identifier non_exist_sid = new Identifier();
301
            non_exist_sid.setValue("no-sid-exist-123qwe");
302
            assertTrue(IdentifierManager.getInstance().getHeadPID(non_exist_sid) == null);
303
            
304
        } catch (Exception e) {
305
            fail(e.getMessage());
306
        }
307
        
308
    }
226 309

  
227 310
    /** 
228 311
     * Insert a test document, returning the docid that was used. 

Also available in: Unified diff