Project

General

Profile

« Previous | Next » 

Revision 10298

Added by Jing Tao almost 7 years ago

Add tests to test the miss-matched checkup in the update method.

View differences:

test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java
149 149
    suite.addTest(new MNodeServiceTest("testCreate"));
150 150
    suite.addTest(new MNodeServiceTest("testCreateInvalidIdentifier"));
151 151
    suite.addTest(new MNodeServiceTest("testUpdate"));
152
    suite.addTest(new MNodeServiceTest("testMissMatchedCheckSumUpdate"));
153
    suite.addTest(new MNodeServiceTest("testMissMatchedChecksumUpdateSciMetadata"));
152 154
    suite.addTest(new MNodeServiceTest("testUpdateSystemMetadata"));
153 155
    suite.addTest(new MNodeServiceTest("testUpdateObsoletesAndObsoletedBy"));
154 156
    suite.addTest(new MNodeServiceTest("testArchive"));
......
705 707
    }
706 708
  }
707 709
  
710
  /**
711
   * Test object updating
712
   */
713
  public void testMissMatchedCheckSumUpdate() {
714
    printTestHeader("testMissMatchedCheckSumUpdate");
715
    
716
    try {
717
      Session session = getTestSession();
718
      Identifier guid = new Identifier();
719
      guid.setValue("testUpdate." + System.currentTimeMillis());
720
      InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
721
      SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
722
      System.out.println("========= the old pid is "+guid.getValue());
723
      Identifier newPid = new Identifier();
724
      newPid.setValue("testUpdate." + (System.currentTimeMillis() + 1)); // ensure it is different from original
725
      Identifier pid = 
726
        MNodeService.getInstance(request).create(session, guid, object, sysmeta);
727
      
728
      object = new ByteArrayInputStream("test".getBytes("UTF-8"));
729
      SystemMetadata newSysMeta = createSystemMetadata(newPid, session.getSubject(), object);
730
      Checksum checksum = newSysMeta.getChecksum();
731
      checksum.setValue("foo-checksum");
732
      newSysMeta.setChecksum(checksum);
733
      System.out.println("========= the new pid is "+newPid.getValue());
734
      // do the update and it should fail
735
      try {
736
          MNodeService.getInstance(request).update(session, pid, object, newPid, newSysMeta);
737
          fail("we shouldn't get here since the checksum is wrong");
738
      } catch (InvalidSystemMetadata ee) {
739
          try {
740
              MNodeService.getInstance(request).getSystemMetadata(session, newPid);
741
              fail("we shouldn't get here since the newPid "+newPid+" shouldn't be create.");
742
          } catch (NotFound eeee) {
743
              
744
          }
745
          
746
      }catch (Exception eee) {
747
          eee.printStackTrace();
748
          fail("Unexpected error in the update: " + eee.getMessage());
749
      }
750
        
751
    } catch (Exception e) {
752
      e.printStackTrace();
753
      fail("Unexpected error: " + e.getMessage());
754

  
755
    }
756
  }
708 757
  
758
  
709 759
  /**
710 760
   * Test object updating
711 761
   */
762
  public void testMissMatchedChecksumUpdateSciMetadata() {
763
    printTestHeader("testMissMatchedChecksumUpdateSciMetadata");
764
    
765
    try {
766
      String st1="<eml:eml xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" packageId=\"tao.13397.1\" system=\"knb\" xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.1 eml.xsd\">"
767
                  +"<access authSystem=\"knb\" order=\"allowFirst\">"
768
                  +"<allow><principal>public</principal><permission>read</permission></allow></access>"
769
                  +"<dataset><title>test</title><creator id=\"1445543475577\"><individualName><surName>test</surName></individualName></creator>"
770
                  +"<contact id=\"1445543479900\"><individualName><surName>test</surName></individualName></contact></dataset></eml:eml>";
771
      Session session = getTestSession();
772
      Identifier guid = new Identifier();
773
      guid.setValue("testUpdate." + System.currentTimeMillis());
774
      InputStream object = new ByteArrayInputStream(st1.getBytes("UTF-8"));
775
      SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
776
      sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.1").getFormatId());
777
      MNodeService.getInstance(request).create(session, guid, object, sysmeta);
778
      System.out.println("=================the old pid is "+guid.getValue());
779
      
780
      String st2="<eml:eml xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" packageId=\"tao.13397.1\" system=\"knb\" xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.1 eml.xsd\">"
781
              +"<access authSystem=\"knb\" order=\"allowFirst\">"
782
              +"<allow><principal>public</principal><permission>read</permission></allow></access>"
783
              +"<dataset><title>test2</title><creator id=\"1445543475577\"><individualName><surName>test</surName></individualName></creator>"
784
              +"<contact id=\"1445543479900\"><individualName><surName>test</surName></individualName></contact></dataset></eml:eml>";
785
      Identifier newPid = new Identifier();
786
      newPid.setValue("testUpdate." + (System.currentTimeMillis() + 1)); // ensure it is different from original
787
      System.out.println("=================the new pid is "+newPid.getValue());
788
      object = new ByteArrayInputStream(st2.getBytes("UTF-8"));
789
      SystemMetadata sysmeta2 = createSystemMetadata(newPid, session.getSubject(), object);
790
      sysmeta2.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.1").getFormatId());
791
      sysmeta2.setObsoletes(guid);
792
      Checksum sum1= sysmeta2.getChecksum();
793
      sum1.setValue("foo checksum");
794
      sysmeta2.setChecksum(sum1);
795
      object = new ByteArrayInputStream(st2.getBytes("UTF-8"));
796
      // do the update and it should fail
797
      try {
798
          MNodeService.getInstance(request).update(session, guid, object, newPid, sysmeta2);
799
          fail("we shouldn't get here since the checksum is wrong");
800
      } catch (ServiceFailure ee) {
801
          try {
802
              MNodeService.getInstance(request).getSystemMetadata(session, newPid);
803
              fail("we shouldn't get here since the newPid "+newPid+" shouldn't be create.");
804
          } catch (NotFound eeee) {
805
              
806
          }
807
          
808
      }catch (Exception eee) {
809
          eee.printStackTrace();
810
          fail("Unexpected error in the update: " + eee.getMessage());
811
      }
812
    
813

  
814
    } catch (Exception e) {
815
      e.printStackTrace();
816
      fail("Unexpected error: " + e.getMessage());
817

  
818
    }
819
  }
820

  
821
  
822
  /**
823
   * Test object updating
824
   */
712 825
  public void testUpdateSciMetadata() {
713 826
    printTestHeader("testUpdate");
714 827
    

Also available in: Unified diff