Project

General

Profile

« Previous | Next » 

Revision 10108

Added by Jing Tao almost 8 years ago

Add junit test methods for testing circular obsoletes/obsoletedBy chain in the mn.updateSystemmetadata method.

View differences:

test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java
183 183
    
184 184
    suite.addTest(new MNodeServiceTest("testPermissionOfUpdateSystemmeta"));
185 185
    
186
   
186
    suite.addTest(new MNodeServiceTest("testUpdateSystemMetadataWithCircularObsoletesChain"));
187 187
    
188
    suite.addTest(new MNodeServiceTest("testUpdateSystemMetadataWithCircularObsoletedByChain"));
188 189
    
189 190
    return suite;
190 191
    
......
2529 2530
        }
2530 2531
       
2531 2532
    }
2533
    
2534
    
2535
    /**
2536
     * Test if the updateSystemmetadata method can catch the circular obsoletes fields chain
2537
     */
2538
    public void testUpdateSystemMetadataWithCircularObsoletesChain() throws Exception{
2539
        
2540
        Date date = new Date();
2541
        Thread.sleep(1000);
2542
        String str = "object1";
2543
        //insert a test document
2544
        Session session = getTestSession();
2545
        Identifier guid = new Identifier();
2546
        guid.setValue(generateDocumentId());
2547
        InputStream object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2548
        SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object1);
2549
        MNodeService.getInstance(request).create(session, guid, object1, sysmeta);
2550
        
2551
        //Test the generating object succeeded. 
2552
        SystemMetadata metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2553
        Thread.sleep(1000);
2554
        Identifier guid1 = new Identifier();
2555
        guid1.setValue(generateDocumentId());
2556
        object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2557
        sysmeta = createSystemMetadata(guid1, session.getSubject(), object1);
2558
        MNodeService.getInstance(request).create(session, guid1, object1, sysmeta);
2559
        //Test the generating object succeeded. 
2560
        metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid1);
2561
        
2562
        Thread.sleep(1000);
2563
        Identifier guid2 = new Identifier();
2564
        guid2.setValue(generateDocumentId());
2565
        object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2566
        sysmeta = createSystemMetadata(guid2, session.getSubject(), object1);
2567
        MNodeService.getInstance(request).create(session, guid2, object1, sysmeta);
2568
        //Test the generating object succeeded. 
2569
        metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid2);
2570
        
2571
        // test to create a circular obsoletes chain: guid obsoletes guid
2572
        metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2573
        metadata.setObsoletes(guid);
2574
        try {
2575
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2576
             fail("We can't update the system metadata which the obsoletes field is itself");
2577
       } catch (InvalidRequest e)  {
2578
           assertTrue("The update system metadata should fail and the error message should have the wording - circular chain", e.getMessage().contains("a circular chain"));
2579
       }
2580
       
2581
       // guid obsolete guid1
2582
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2583
       metadata.setObsoletes(guid1);
2584
       MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2585
       
2586
       // guid1 obsoletedBy guid
2587
       // guid1  obsoletes  guid2
2588
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid1);
2589
       metadata.setObsoletes(guid2);
2590
       metadata.setObsoletedBy(guid);
2591
       MNodeService.getInstance(request).updateSystemMetadata(session, guid1, metadata);
2592
       
2593
       // crete a circular obsolete chain:
2594
       // guid2 obsoletes guid
2595
       //guid2 obsoletedBy guid1
2596
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid2);
2597
       metadata.setObsoletes(guid);
2598
       metadata.setObsoletedBy(guid1);
2599
       
2600
       try {
2601
           MNodeService.getInstance(request).updateSystemMetadata(session, guid2, metadata);
2602
           fail("We can't update the system metadata which has a circular obsoletes chain");
2603
      } catch (InvalidRequest e)  {
2604
          assertTrue("The update system metadata should fail and the error message should have the wording - circular chain", e.getMessage().contains("a circular chain"));
2605
      }
2606
       
2607
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid2);
2608
       metadata.setObsoletedBy(guid1);
2609
       MNodeService.getInstance(request).updateSystemMetadata(session, guid2, metadata);
2610
    }
2611
    
2612
    
2613
    
2614
    
2615
    /**
2616
     * Test if the updateSystemmetadata method can catch the circular obsoletedBy chain
2617
     */
2618
    public void testUpdateSystemMetadataWithCircularObsoletedByChain() throws Exception{
2619
        
2620
        Date date = new Date();
2621
        Thread.sleep(1000);
2622
        String str = "object1";
2623
        //insert a test document
2624
        Session session = getTestSession();
2625
        Identifier guid = new Identifier();
2626
        guid.setValue(generateDocumentId());
2627
        InputStream object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2628
        SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object1);
2629
        MNodeService.getInstance(request).create(session, guid, object1, sysmeta);
2630
        
2631
        //Test the generating object succeeded. 
2632
        SystemMetadata metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2633
        Thread.sleep(1000);
2634
        Identifier guid1 = new Identifier();
2635
        guid1.setValue(generateDocumentId());
2636
        object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2637
        sysmeta = createSystemMetadata(guid1, session.getSubject(), object1);
2638
        MNodeService.getInstance(request).create(session, guid1, object1, sysmeta);
2639
        //Test the generating object succeeded. 
2640
        metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid1);
2641
        
2642
        Thread.sleep(1000);
2643
        Identifier guid2 = new Identifier();
2644
        guid2.setValue(generateDocumentId());
2645
        object1 = new ByteArrayInputStream(str.getBytes("UTF-8"));
2646
        sysmeta = createSystemMetadata(guid2, session.getSubject(), object1);
2647
        MNodeService.getInstance(request).create(session, guid2, object1, sysmeta);
2648
        //Test the generating object succeeded. 
2649
        metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid2);
2650
        
2651
      
2652
       // guid obsolete guid1
2653
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2654
       metadata.setObsoletes(guid1);
2655
       MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2656
       
2657
       // guid1  obsoletes  guid2
2658
       // guid1 obsoletedBy guid1 (a circular chain)
2659
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid1);
2660
       metadata.setObsoletes(guid2);
2661
       metadata.setObsoletedBy(guid1);
2662
       try {
2663
           MNodeService.getInstance(request).updateSystemMetadata(session, guid1, metadata);
2664
            fail("We can't update the system metadata which the obsoletes field is itself");
2665
      } catch (InvalidRequest e)  {
2666
          assertTrue("The update system metadata should fail and the error message should have the wording - circular chain", e.getMessage().contains("a circular chain"));
2667
      }
2668
       
2669
       
2670
       // guid1  obsoletes  guid2
2671
       // guid1 obsoletedBy guid 
2672
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid1);
2673
       metadata.setObsoletes(guid2);
2674
       metadata.setObsoletedBy(guid);
2675
       MNodeService.getInstance(request).updateSystemMetadata(session, guid1, metadata);
2676
      
2677
       
2678
       //guid2 obsoletedBy guid1
2679
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid2);
2680
       metadata.setObsoletedBy(guid1);
2681
       MNodeService.getInstance(request).updateSystemMetadata(session, guid2, metadata);
2682
       
2683
       
2684
       //guid2 obsoletedBy guid1
2685
       //guid1 obsoletedBy guid
2686
       //guid  obsoletedBy guid2 (created a circle)
2687
       metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
2688
       metadata.setObsoletedBy(guid2);
2689
       try {
2690
           MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2691
           fail("We can't update the system metadata which has a circular obsoletes chain");
2692
      } catch (InvalidRequest e)  {
2693
          assertTrue("The update system metadata should fail and the error message should have the wording - circular chain", e.getMessage().contains("a circular chain"));
2694
      }
2695
       
2696
   
2697
    }
2532 2698
}

Also available in: Unified diff