Project

General

Profile

« Previous | Next » 

Revision 9167

Added by Jing Tao over 9 years ago

Add the junit test for testing the updateSystemMetadata method.

View differences:

test/edu/ucsb/nceas/metacat/dataone/CNodeServiceTest.java
28 28

  
29 29
import java.io.ByteArrayInputStream;
30 30
import java.io.InputStream;
31
import java.math.BigInteger;
31 32
import java.text.SimpleDateFormat;
32 33
import java.util.Calendar;
33 34
import java.util.Date;
......
39 40
import org.apache.commons.io.IOUtils;
40 41
import org.dataone.client.v2.itk.D1Client;
41 42
import org.dataone.service.exceptions.BaseException;
43
import org.dataone.service.exceptions.InvalidRequest;
42 44
import org.dataone.service.exceptions.InvalidSystemMetadata;
43 45
import org.dataone.service.exceptions.NotAuthorized;
44 46
import org.dataone.service.exceptions.NotFound;
......
113 115
		suite.addTest(new CNodeServiceTest("readDeletedObject"));
114 116
		suite.addTest(new CNodeServiceTest("testGetSID"));
115 117
		suite.addTest(new CNodeServiceTest("testListViews"));
118
		suite.addTest(new CNodeServiceTest("testUpdateSystemMetadata"));
116 119
	
117 120
		return suite;
118 121
	}
......
1150 1153
          System.out.println("It has the view named "+name);
1151 1154
      }
1152 1155
  }
1156
  
1157
  public void testUpdateSystemMetadata() throws Exception {
1158
          String str1 = "object1";
1159
          String str2 = "object2";
1160
          String str3 = "object3";
1161
  
1162
          //insert test documents with a series id
1163
          Session session = getTestSession();
1164
          Identifier guid = new Identifier();
1165
          guid.setValue(generateDocumentId());
1166
          InputStream object1 = new ByteArrayInputStream(str1.getBytes("UTF-8"));
1167
          SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object1);
1168
          String sid1= "sid."+System.nanoTime();
1169
          Identifier seriesId = new Identifier();
1170
          seriesId.setValue(sid1);
1171
          System.out.println("the first sid is "+seriesId.getValue());
1172
          sysmeta.setSeriesId(seriesId);
1173
          sysmeta.setArchived(false);
1174
          CNodeService.getInstance(request).create(session, guid, object1, sysmeta);
1175
          //Test the generating object succeeded. 
1176
          SystemMetadata metadata = CNodeService.getInstance(request).getSystemMetadata(session, guid);
1177
          assertTrue(metadata.getIdentifier().equals(guid));
1178
          assertTrue(metadata.getArchived().equals(false));
1179
          System.out.println("the checksum from request is "+metadata.getChecksum().getValue());
1180
          assertTrue(metadata.getSize().equals(sysmeta.getSize()));
1181
          
1182
          //update system metadata sucessfully
1183
          metadata.setArchived(true);
1184
          CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1185
          SystemMetadata metadata2 = CNodeService.getInstance(request).getSystemMetadata(session, seriesId);
1186
          assertTrue(metadata2.getIdentifier().equals(guid));
1187
          assertTrue(metadata2.getSeriesId().equals(seriesId));
1188
          assertTrue(metadata2.getArchived().equals(true));
1189
          assertTrue(metadata2.getChecksum().getValue().equals(metadata.getChecksum().getValue()));
1190
          
1191
          Identifier newId = new Identifier();
1192
          newId.setValue("newValue");
1193
          metadata.setIdentifier(newId);
1194
          try {
1195
              CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1196
              fail("We shouldn't get there");
1197
          } catch (Exception e) {
1198
              assertTrue(e instanceof InvalidRequest);
1199
          }
1200
          
1201
          newId.setValue("newValue");
1202
          metadata.setSeriesId(newId);
1203
          try {
1204
              CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1205
              fail("We shouldn't get there");
1206
          } catch (Exception e) {
1207
              assertTrue(e instanceof InvalidRequest);
1208
          }
1209
          
1210
          Date newDate = new Date();
1211
          metadata.setDateUploaded(newDate);
1212
          try {
1213
              CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1214
              fail("We shouldn't get there");
1215
          } catch (Exception e) {
1216
              assertTrue(e instanceof InvalidRequest);
1217
          }
1218
          
1219
          Checksum checkSum = new Checksum();
1220
          checkSum.setValue("12345");
1221
          metadata.setChecksum(checkSum);
1222
          try {
1223
              CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1224
              fail("We shouldn't get there");
1225
          } catch (Exception e) {
1226
              assertTrue(e instanceof InvalidRequest);
1227
          }
1228
          
1229
          BigInteger size = new BigInteger("4000");
1230
          metadata.setSize(size);
1231
          try {
1232
              CNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1233
              fail("We shouldn't get there");
1234
          } catch (Exception e) {
1235
              assertTrue(e instanceof InvalidRequest);
1236
          }
1237
  }
1153 1238
}
test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java
28 28

  
29 29

  
30 30

  
31
import edu.ucsb.nceas.metacat.dataone.CNodeService;
31 32
import edu.ucsb.nceas.metacat.dataone.MNodeService;
32 33
import edu.ucsb.nceas.metacat.properties.SkinPropertyService;
33 34
import edu.ucsb.nceas.metacat.service.ServiceService;
......
44 45
import java.io.IOException;
45 46
import java.io.InputStream;
46 47
import java.io.UnsupportedEncodingException;
48
import java.math.BigInteger;
47 49
import java.net.URL;
48 50
import java.text.SimpleDateFormat;
49 51
import java.util.ArrayList;
......
170 172
    suite.addTest(new MNodeServiceTest("testCreateAndUpdateXMLWithUnmatchingEncoding"));
171 173
    suite.addTest(new MNodeServiceTest("testGetSID"));
172 174
    suite.addTest(new MNodeServiceTest("testListViews"));
175
    suite.addTest(new MNodeServiceTest("testUpdateSystemMetadata"));
173 176
    
174 177
    return suite;
175 178
    
......
1939 1942
        }
1940 1943
    }
1941 1944
    
1945
    public void testUpdateSystemMetadata() throws Exception {
1946
        String str1 = "object1";
1947
        String str2 = "object2";
1948
        String str3 = "object3";
1949

  
1950
        //insert test documents with a series id
1951
        Session session = getTestSession();
1952
        Identifier guid = new Identifier();
1953
        guid.setValue(generateDocumentId());
1954
        InputStream object1 = new ByteArrayInputStream(str1.getBytes("UTF-8"));
1955
        SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object1);
1956
        String sid1= "sid."+System.nanoTime();
1957
        Identifier seriesId = new Identifier();
1958
        seriesId.setValue(sid1);
1959
        System.out.println("the first sid is "+seriesId.getValue());
1960
        sysmeta.setSeriesId(seriesId);
1961
        sysmeta.setArchived(false);
1962
        MNodeService.getInstance(request).create(session, guid, object1, sysmeta);
1963
        //Test the generating object succeeded. 
1964
        SystemMetadata metadata = MNodeService.getInstance(request).getSystemMetadata(session, guid);
1965
        assertTrue(metadata.getIdentifier().equals(guid));
1966
        assertTrue(metadata.getArchived().equals(false));
1967
        System.out.println("the checksum from request is "+metadata.getChecksum().getValue());
1968
        assertTrue(metadata.getSize().equals(sysmeta.getSize()));
1969
        
1970
        //update system metadata sucessfully
1971
        metadata.setArchived(true);
1972
        MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1973
        SystemMetadata metadata2 = MNodeService.getInstance(request).getSystemMetadata(session, seriesId);
1974
        assertTrue(metadata2.getIdentifier().equals(guid));
1975
        assertTrue(metadata2.getSeriesId().equals(seriesId));
1976
        assertTrue(metadata2.getArchived().equals(true));
1977
        assertTrue(metadata2.getChecksum().getValue().equals(metadata.getChecksum().getValue()));
1978
        
1979
        Identifier newId = new Identifier();
1980
        newId.setValue("newValue");
1981
        metadata.setIdentifier(newId);
1982
        try {
1983
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1984
            fail("We shouldn't get there");
1985
        } catch (Exception e) {
1986
            assertTrue(e instanceof InvalidRequest);
1987
        }
1988
        
1989
        newId.setValue("newValue");
1990
        metadata.setSeriesId(newId);
1991
        try {
1992
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
1993
            fail("We shouldn't get there");
1994
        } catch (Exception e) {
1995
            assertTrue(e instanceof InvalidRequest);
1996
        }
1997
        
1998
        Date newDate = new Date();
1999
        metadata.setDateUploaded(newDate);
2000
        try {
2001
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2002
            fail("We shouldn't get there");
2003
        } catch (Exception e) {
2004
            assertTrue(e instanceof InvalidRequest);
2005
        }
2006
        
2007
        Checksum checkSum = new Checksum();
2008
        checkSum.setValue("12345");
2009
        metadata.setChecksum(checkSum);
2010
        try {
2011
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2012
            fail("We shouldn't get there");
2013
        } catch (Exception e) {
2014
            assertTrue(e instanceof InvalidRequest);
2015
        }
2016
        
2017
        BigInteger size = new BigInteger("4000");
2018
        metadata.setSize(size);
2019
        try {
2020
            MNodeService.getInstance(request).updateSystemMetadata(session, guid, metadata);
2021
            fail("We shouldn't get there");
2022
        } catch (Exception e) {
2023
            assertTrue(e instanceof InvalidRequest);
2024
        }
1942 2025
}
2026
    
2027
}

Also available in: Unified diff