Project

General

Profile

« Previous | Next » 

Revision 7853

MN.getPackage() - test with ORE that includes 2 data files and a "metadata" file (still should be using EML for that test). https://projects.ecoinformatics.org/ecoinfo/issues/6026

View differences:

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

  
29 29
import gov.loc.repository.bagit.Bag;
30 30
import gov.loc.repository.bagit.BagFactory;
31
import gov.loc.repository.bagit.BagFile;
32
import gov.loc.repository.bagit.Manifest;
31 33

  
32 34
import java.io.ByteArrayInputStream;
33 35
import java.io.ByteArrayOutputStream;
......
40 42
import java.util.ArrayList;
41 43
import java.util.Calendar;
42 44
import java.util.Date;
45
import java.util.HashMap;
46
import java.util.Iterator;
43 47
import java.util.List;
48
import java.util.Map;
44 49

  
45 50
import junit.framework.Test;
46 51
import junit.framework.TestSuite;
47 52

  
48 53
import org.apache.commons.io.IOUtils;
54
import org.dataone.client.ObjectFormatCache;
49 55
import org.dataone.configuration.Settings;
56
import org.dataone.ore.ResourceMapFactory;
50 57
import org.dataone.service.util.TypeMarshaller;
51 58
import org.dataone.service.exceptions.IdentifierNotUnique;
52 59
import org.dataone.service.exceptions.InsufficientResources;
......
77 84
import org.dataone.service.types.v1.Subject;
78 85
import org.dataone.service.types.v1.SubjectInfo;
79 86
import org.dataone.service.types.v1.SystemMetadata;
87
import org.dspace.foresite.ResourceMap;
80 88
import org.jibx.runtime.JiBXException;
81 89
import org.junit.After;
82 90
import org.junit.Before;
......
149 157
    suite.addTest(new MNodeServiceTest("testReplicate"));
150 158
    // MN packaging tests
151 159
    suite.addTest(new MNodeServiceTest("testGetPackage"));
160
    suite.addTest(new MNodeServiceTest("testGetOREPackage"));
152 161
    
153 162
    
154 163
    return suite;
......
1112 1121
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
1113 1122
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
1114 1123
			InputStream bagStream = MNodeService.getInstance(request).getPackage(session, pid);
1115
			File bagFile = File.createTempFile("bagit", ".zip");
1124
			File bagFile = File.createTempFile("bagit.", ".zip");
1116 1125
			IOUtils.copy(bagStream, new FileOutputStream(bagFile));
1117 1126
			BagFactory bagFactory = new BagFactory();
1118 1127
			Bag bag = bagFactory.createBag(bagFile);
......
1133 1142
			fail("Unexpected error: " + e.getMessage());
1134 1143
		}
1135 1144
	}
1145
	
1146
	
1147
	/**
1148
	 * Test getting a known object
1149
	 */
1150
	public void testGetOREPackage() {
1151
		printTestHeader("testGetOREPackage");
1152

  
1153
		try {
1154
			
1155
			// construct the ORE package
1156
			Identifier resourceMapId = new Identifier();
1157
			//resourceMapId.setValue("doi://1234/AA/map.1.1");
1158
			resourceMapId.setValue("testGetOREPackage." + System.currentTimeMillis());
1159
			Identifier metadataId = new Identifier();
1160
			metadataId.setValue("doi://1234/AA/meta.1." + + System.currentTimeMillis());
1161
			List<Identifier> dataIds = new ArrayList<Identifier>();
1162
			Identifier dataId = new Identifier();
1163
			dataId.setValue("doi://1234/AA/data.1." + System.currentTimeMillis());
1164
			Identifier dataId2 = new Identifier();
1165
			dataId2.setValue("doi://1234/AA/data.2." + System.currentTimeMillis());
1166
			dataIds.add(dataId);
1167
			dataIds.add(dataId2);
1168
			Map<Identifier, List<Identifier>> idMap = new HashMap<Identifier, List<Identifier>>();
1169
			idMap.put(metadataId, dataIds);
1170
			ResourceMapFactory rmf = ResourceMapFactory.getInstance();
1171
			ResourceMap resourceMap = rmf.createResourceMap(resourceMapId, idMap);
1172
			assertNotNull(resourceMap);
1173
			String rdfXml = ResourceMapFactory.getInstance().serializeResourceMap(resourceMap);
1174
			assertNotNull(rdfXml);
1175
			
1176
			Session session = getTestSession();
1177
			InputStream object = null;
1178
			SystemMetadata sysmeta = null;
1179
			
1180
			// save the data objects (data just contains their ID)
1181
			InputStream dataObject1 = new ByteArrayInputStream(dataId.getValue().getBytes("UTF-8"));
1182
			sysmeta = createSystemMetadata(dataId, session.getSubject(), dataObject1);
1183
			MNodeService.getInstance(request).create(session, dataId, dataObject1, sysmeta);
1184
			// second data file
1185
			InputStream dataObject2 = new ByteArrayInputStream(dataId2.getValue().getBytes("UTF-8"));
1186
			sysmeta = createSystemMetadata(dataId2, session.getSubject(), dataObject2);
1187
			MNodeService.getInstance(request).create(session, dataId2, dataObject2, sysmeta);
1188
			// metadata file
1189
			InputStream metadataObject = new ByteArrayInputStream(metadataId.getValue().getBytes("UTF-8"));
1190
			sysmeta = createSystemMetadata(metadataId, session.getSubject(), metadataObject);
1191
			MNodeService.getInstance(request).create(session, metadataId, metadataObject, sysmeta);
1192
						
1193
			// save the ORE object
1194
			object = new ByteArrayInputStream(rdfXml.getBytes("UTF-8"));
1195
			sysmeta = createSystemMetadata(resourceMapId, session.getSubject(), object);
1196
			sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("http://www.openarchives.org/ore/terms").getFormatId());
1197
			Identifier pid = MNodeService.getInstance(request).create(session, resourceMapId, object, sysmeta);
1198
			
1199
			// get the package we uploaded
1200
			InputStream bagStream = MNodeService.getInstance(request).getPackage(session, pid);
1201
			File bagFile = File.createTempFile("bagit.", ".zip");
1202
			IOUtils.copy(bagStream, new FileOutputStream(bagFile));
1203
			BagFactory bagFactory = new BagFactory();
1204
			Bag bag = bagFactory.createBag(bagFile);
1205
			Iterator<Manifest> manifestIter = bag.getTagManifests().iterator();
1206
			while (manifestIter.hasNext()) {
1207
				String filepath = manifestIter.next().getFilepath();
1208
				BagFile entryFile = bag.getBagFile(filepath);
1209
				InputStream result = entryFile.newInputStream();
1210
				// check ORE
1211
				if (filepath.contains(resourceMapId.getValue())) {
1212
					object.reset();
1213
					assertTrue(object.available() > 0);
1214
					assertTrue(result.available() > 0);
1215
					assertTrue(IOUtils.contentEquals(result, object));
1216
				}
1217
				// check metadata
1218
				if (filepath.contains(metadataId.getValue())) {
1219
					metadataObject.reset();
1220
					assertTrue(metadataObject.available() > 0);
1221
					assertTrue(result.available() > 0);
1222
					assertTrue(IOUtils.contentEquals(result, metadataObject));
1223
				}
1224
				if (filepath.contains(dataId.getValue())) {
1225
					dataObject1.reset();
1226
					assertTrue(dataObject1.available() > 0);
1227
					assertTrue(result.available() > 0);
1228
					assertTrue(IOUtils.contentEquals(result, dataObject1));
1229
				}
1230
				if (filepath.contains(dataId2.getValue())) {
1231
					dataObject2.reset();
1232
					assertTrue(dataObject2.available() > 0);
1233
					assertTrue(result.available() > 0);
1234
					assertTrue(IOUtils.contentEquals(result, dataObject2));
1235
				}
1236
				
1237
				
1238
			}
1239
			
1240
			// clean up
1241
			bagFile.delete();
1242

  
1243
		} catch (Exception e) {
1244
			e.printStackTrace();
1245
			fail("Unexpected error: " + e.getMessage());
1246
		}
1247
	}
1248
	
1136 1249
  
1137
  
1138
  
1139 1250
}
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
1673 1673
			packagePids.add(pid);
1674 1674
		}
1675 1675

  
1676
		// track the pid-to-file mapping
1677
		StringBuffer pidMapping = new StringBuffer();
1676 1678
		// loop through the package contents
1677 1679
		for (Identifier entryPid: packagePids) {
1678 1680
			SystemMetadata entrySysMeta = this.getSystemMetadata(session, entryPid);
1679 1681
			String extension = ObjectFormatInfo.instance().getExtension(entrySysMeta.getFormatId().getValue());
1680 1682
	        String prefix = entryPid.getValue();
1681
			File tempFile = File.createTempFile(prefix + "_bagit", extension);
1683
	        prefix = "entry";
1684
			File tempFile = File.createTempFile(prefix + ".", extension);
1682 1685
			tempFiles.add(tempFile);
1683 1686
			InputStream entryInputStream = this.get(session, entryPid);
1684 1687
			IOUtils.copy(entryInputStream, new FileOutputStream(tempFile));
1685 1688
			bag.addFileToPayload(tempFile);
1689
			pidMapping.append(entryPid.getValue() + "\t" + "data/" + tempFile.getName() + "\n");
1686 1690
		}
1687 1691
		
1692
		//add the the pid to data file map
1693
		File pidMappingFile = File.createTempFile("pid-mapping.", ".txt");
1694
		IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
1695
		bag.addFileAsTag(pidMappingFile);
1696

  
1688 1697
		bag = bag.makeComplete();
1689
		File bagFile = File.createTempFile("bagit", ".zip");
1698
		File bagFile = File.createTempFile("bag.", ".zip");
1690 1699
		// TODO: delete more confidently
1691 1700
		bagFile.deleteOnExit();
1692 1701
		bag.setFile(bagFile);

Also available in: Unified diff