Project

General

Profile

« Previous | Next » 

Revision 9034

Added by Jing Tao almost 10 years ago

Add the code to test getting test series ids.

View differences:

test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java
27 27

  
28 28

  
29 29

  
30

  
31
import edu.ucsb.nceas.metacat.dataone.MNodeService;
30 32
import gov.loc.repository.bagit.Bag;
31 33
import gov.loc.repository.bagit.BagFactory;
32 34
import gov.loc.repository.bagit.BagFile;
......
160 162
    suite.addTest(new MNodeServiceTest("testGetOREPackage"));
161 163
    suite.addTest(new MNodeServiceTest("testReadDeletedObject"));
162 164
    suite.addTest(new MNodeServiceTest("testCreateAndUpdateXMLWithUnmatchingEncoding"));
165
    suite.addTest(new MNodeServiceTest("testGetSID"));
163 166
    return suite;
164 167
    
165 168
  }
......
1383 1386
          
1384 1387
    }
1385 1388
  
1389
    /**
1390
     * Test the method - get api  for a speicified SID
1391
     */
1392
    public void testGetSID() {
1393
        String str1 = "object1";
1394
        String str2 = "object2";
1395
        String str3 = "object3";
1396
        try {
1397
            //insert test documents with a series id
1398
            Session session = getTestSession();
1399
            Identifier guid = new Identifier();
1400
            guid.setValue(generateDocumentId());
1401
            InputStream object1 = new ByteArrayInputStream(str1.getBytes("UTF-8"));
1402
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object1);
1403
            String sid1= "sid."+System.nanoTime();
1404
            Identifier seriesId = new Identifier();
1405
            seriesId.setValue(sid1);
1406
            System.out.println("the first sid is "+seriesId.getValue());
1407
            sysmeta.setSeriesId(seriesId);
1408
            MNodeService.getInstance(request).create(session, guid, object1, sysmeta);
1409
            System.out.println("the first pid is "+guid.getValue());
1410
            //test the get(pid) for v2
1411
            InputStream result = MNodeService.getInstance(request).get(session, guid);
1412
            // go back to beginning of original stream
1413
            object1.reset();
1414
            // check
1415
            assertTrue(object1.available() > 0);
1416
            assertTrue(result.available() > 0);
1417
            assertTrue(IOUtils.contentEquals(result, object1));
1418
            // test the get(id) for v2
1419
            InputStream result1 = MNodeService.getInstance(request).get(session, seriesId);
1420
            object1.reset();
1421
            // check
1422
            assertTrue(object1.available() > 0);
1423
            assertTrue(result1.available() > 0);
1424
            assertTrue(IOUtils.contentEquals(result1, object1));
1425
            //test the get(pid) for v1
1426
            InputStream result2 = edu.ucsb.nceas.metacat.dataone.v1.MNodeService.getInstance(request).get(session, guid);
1427
            object1.reset();
1428
            // check
1429
            assertTrue(object1.available() > 0);
1430
            assertTrue(result2.available() > 0);
1431
            assertTrue(IOUtils.contentEquals(result2, object1));
1432
            //test the get(sid) for v1
1433
            try {
1434
                InputStream result3 = edu.ucsb.nceas.metacat.dataone.v1.MNodeService.getInstance(request).get(session, seriesId);
1435
                fail("the get(sid) methoud should throw a not found exception for the sid "+seriesId.getValue());
1436
            } catch (NotFound ee) {
1437
                
1438
            }
1439
            
1440
            //do a update with the same series id
1441
            /*Thread.sleep(1000);
1442
            Identifier newPid = new Identifier();
1443
            newPid.setValue(generateDocumentId()+"1");
1444
            System.out.println("the second pid is "+newPid.getValue());
1445
            SystemMetadata newSysMeta = createSystemMetadata(newPid, session.getSubject(), object);
1446
            newSysMeta.setObsoletes(guid);
1447
            newSysMeta.setSeriesId(seriesId);
1448
            MNodeService.getInstance(request).update(session, guid, object, newPid, newSysMeta);
1449
            // the pid should be the newPid when we try to get the sid1
1450
            head = IdentifierManager.getInstance().getHeadPID(seriesId);
1451
            System.out.println("the head 2 is "+head.getValue());
1452
            assertTrue(head.getValue().equals(newPid.getValue()));
1453
            
1454
            //do another update with different series id
1455
            Thread.sleep(1000);
1456
            String sid2 = "sid."+System.nanoTime();
1457
            Identifier seriesId2= new Identifier();
1458
            seriesId2.setValue(sid2);
1459
            System.out.println("the second sid is "+seriesId2.getValue());
1460
            Identifier newPid2 = new Identifier();
1461
            newPid2.setValue(generateDocumentId()+"2");
1462
            System.out.println("the third pid is "+newPid2.getValue());
1463
            SystemMetadata sysmeta3 = createSystemMetadata(newPid2, session.getSubject(), object);
1464
            sysmeta3.setObsoletes(newPid);
1465
            sysmeta3.setSeriesId(seriesId2);
1466
            MNodeService.getInstance(request).update(session, newPid, object, newPid2, sysmeta3);
1467
            
1468
            // the pid should be the newPid when we try to get the sid1
1469
            head =IdentifierManager.getInstance().getHeadPID(seriesId);
1470
            System.out.println("the head 3 is "+head.getValue());
1471
            assertTrue(head.getValue().equals(newPid.getValue()));
1472
            
1473
            // the pid should be the newPid2 when we try to get the sid2
1474
            head = IdentifierManager.getInstance().getHeadPID(seriesId2);
1475
            System.out.println("the head 4 is "+head.getValue());
1476
            assertTrue(head.getValue().equals(newPid2.getValue()));
1477
            
1478
            // the pid should be null when we try to get a no-exist sid
1479
            Identifier non_exist_sid = new Identifier();
1480
            non_exist_sid.setValue("no-sid-exist-123qwe");
1481
            assertTrue(IdentifierManager.getInstance().getHeadPID(non_exist_sid) == null);*/
1482
            
1483
        } catch (Exception e) {
1484
            fail(e.getMessage());
1485
        }
1486
        
1487
    }
1386 1488
}

Also available in: Unified diff