Project

General

Profile

« Previous | Next » 

Revision 8835

Add a test class that inserts an ORE with PROV relationships

View differences:

test/edu/ucsb/nceas/metacat/dataone/InsertORETest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author:$'
8
 *     '$Date:$'
9
 * '$Revision:$'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

  
26
package edu.ucsb.nceas.metacat.dataone;
27

  
28

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

  
34
import java.io.ByteArrayInputStream;
35
import java.io.ByteArrayOutputStream;
36
import java.io.File;
37
import java.io.FileOutputStream;
38
import java.io.IOException;
39
import java.io.InputStream;
40
import java.io.UnsupportedEncodingException;
41
import java.text.SimpleDateFormat;
42
import java.util.ArrayList;
43
import java.util.Calendar;
44
import java.util.Date;
45
import java.util.HashMap;
46
import java.util.Iterator;
47
import java.util.List;
48
import java.util.Map;
49

  
50
import junit.framework.Test;
51
import junit.framework.TestSuite;
52

  
53
import org.apache.commons.io.IOUtils;
54
import org.dataone.client.v1.itk.DataPackage;
55
import org.dataone.client.v2.formats.ObjectFormatCache;
56
import org.dataone.configuration.Settings;
57
import org.dataone.ore.ResourceMapFactory;
58
import org.dataone.service.util.TypeMarshaller;
59
import org.dataone.service.exceptions.IdentifierNotUnique;
60
import org.dataone.service.exceptions.InsufficientResources;
61
import org.dataone.service.exceptions.InvalidRequest;
62
import org.dataone.service.exceptions.InvalidSystemMetadata;
63
import org.dataone.service.exceptions.InvalidToken;
64
import org.dataone.service.exceptions.NotAuthorized;
65
import org.dataone.service.exceptions.NotFound;
66
import org.dataone.service.exceptions.NotImplemented;
67
import org.dataone.service.exceptions.ServiceFailure;
68
import org.dataone.service.exceptions.SynchronizationFailed;
69
import org.dataone.service.exceptions.UnsupportedType;
70
import org.dataone.service.types.v1.AccessPolicy;
71
import org.dataone.service.types.v1.AccessRule;
72
import org.dataone.service.types.v1.Checksum;
73
import org.dataone.service.types.v1.DescribeResponse;
74
import org.dataone.service.types.v1.Event;
75
import org.dataone.service.types.v1.Identifier;
76
import org.dataone.service.types.v2.Log;
77
import org.dataone.service.types.v2.Node;
78
import org.dataone.service.types.v1.NodeReference;
79
import org.dataone.service.types.v1.ObjectFormatIdentifier;
80
import org.dataone.service.types.v1.ObjectList;
81
import org.dataone.service.types.v1.Permission;
82
import org.dataone.service.types.v1.Person;
83
import org.dataone.service.types.v1.Session;
84
import org.dataone.service.types.v1.Subject;
85
import org.dataone.service.types.v1.SubjectInfo;
86
import org.dataone.service.types.v2.SystemMetadata;
87
import org.dspace.foresite.ResourceMap;
88
import org.jibx.runtime.JiBXException;
89
import org.junit.After;
90
import org.junit.Before;
91

  
92
/**
93
 * A JUnit test to insert a resource map into metacat with PROV relationships
94
 * 
95
 * @author lwalker 08-15-2014
96
 *
97
 */
98
public class InsertORETest extends D1NodeServiceTest {
99

  
100
  /**
101
   * Set up the test fixtures
102
   * 
103
   * @throws Exception
104
   */
105
  @Before
106
  public void setUp() throws Exception {
107
    super.setUp();
108
    // set up the configuration for d1client
109
    Settings.getConfiguration().setProperty("D1Client.cnClassName", MockCNode.class.getName());
110
  }
111

  
112
  /**
113
   * Remove the test fixtures
114
   */
115
  @After
116
  public void tearDown() {
117
  }
118
  
119
  /**
120
   * Build the test suite
121
   * @return
122
   */
123
  public static Test suite() {
124
    
125
    TestSuite suite = new TestSuite();
126
    suite.addTest(new InsertORETest("initialize"));
127
    
128
    // Insert test
129
    suite.addTest(new InsertORETest("testInsertORE"));
130
    
131
    
132
    return suite;
133
    
134
  }
135
  
136
  /**
137
   * Constructor for the tests
138
   * 
139
   * @param name - the name of the test
140
   */
141
  public InsertORETest(String name) {
142
    super(name);
143
    
144
  }
145

  
146
  /**
147
   * Initial blank test
148
   */
149
  public void initialize() {
150
    assertTrue(1 == 1);
151
    
152
  }
153
  
154
  public void testInsertORE() {
155
	  printTestHeader("testInsertORE");
156
	  
157
		  try {
158
			  	//Create the predicate URIs and NS
159
				String provNS = "http://www.w3.org/ns/prov#";
160
				String wasDerivedFromURI = provNS + "wasDerivedFrom";
161
				String usedURI = provNS + "used";
162
				String wasGeneratedByURI = provNS + "wasGeneratedBy";
163
				String generatedURI = provNS + "generated";
164
				String wasInformedByURI = provNS + "wasInformedBy";
165
				Long uniqueNum = System.currentTimeMillis();
166
				
167
				// construct the ORE package
168
				Identifier resourceMapId = new Identifier();
169
				resourceMapId.setValue("package." + uniqueNum + ".1");
170
				DataPackage dataPackage = new DataPackage(resourceMapId);
171
				
172
				//Metadata
173
				Identifier metadataId = new Identifier();
174
				metadataId.setValue("meta." + uniqueNum + ".1");
175
				
176
				//Data
177
				List<Identifier> dataIds = new ArrayList<Identifier>();
178
				Identifier dataId = new Identifier();
179
				dataId.setValue("data." + uniqueNum + ".1");
180
				Identifier imgId = new Identifier();
181
				imgId.setValue("img." + uniqueNum + ".1");
182
				Identifier drawActivityId = new Identifier();
183
				drawActivityId.setValue("drawActivity." + uniqueNum + ".1");
184
				Identifier composeActivityId = new Identifier();
185
				composeActivityId.setValue("composeActivity." + uniqueNum + ".1");
186
				
187
				//Create the primary (external) resources identifiers
188
				Identifier primaryDataId = new Identifier();
189
				primaryDataId.setValue("primaryData.1.1");
190
				Identifier primaryDataId2 = new Identifier();
191
				primaryDataId2.setValue("primaryData.2.1"); 
192
				
193
				dataIds.add(dataId);
194
				dataIds.add(imgId);
195
				dataIds.add(drawActivityId);
196
				dataIds.add(composeActivityId);
197
				
198
				//Create lists of the items to use in the triples
199
				List<Identifier> dataIdList = new ArrayList<Identifier>();
200
				dataIdList.add(dataId);
201
				List<Identifier> activityIdList = new ArrayList<Identifier>();
202
				activityIdList.add(drawActivityId);
203
				List<Identifier> composeActivityIdList = new ArrayList<Identifier>();
204
				composeActivityIdList.add(composeActivityId);
205
				List<Identifier> primaryDataIdList = new ArrayList<Identifier>();
206
				primaryDataIdList.add(primaryDataId);
207
				primaryDataIdList.add(primaryDataId2);
208
				
209
				//---- isDocumentedBy/documents ----
210
				dataPackage.insertRelationship(metadataId, dataIds);
211
				
212
				//---- wasDerivedFrom ----
213
				dataPackage.insertRelationship(dataId, primaryDataIdList, provNS, wasDerivedFromURI);
214
				dataPackage.insertRelationship(imgId, dataIdList, provNS, wasDerivedFromURI);
215
				
216
				//---- wasGeneratedBy ----
217
				dataPackage.insertRelationship(imgId, activityIdList, provNS, wasGeneratedByURI);
218
				dataPackage.insertRelationship(dataId, composeActivityIdList, provNS, wasGeneratedByURI);
219
				
220
				//---- wasInformedBy ----
221
				dataPackage.insertRelationship(drawActivityId, composeActivityIdList, provNS, wasInformedByURI);
222
				
223
				//---- used ----
224
				dataPackage.insertRelationship(drawActivityId, dataIdList, provNS, usedURI);
225
				dataPackage.insertRelationship(composeActivityId, primaryDataIdList, provNS, usedURI);
226
				
227
				//Create the resourceMap
228
				ResourceMap resourceMap = dataPackage.getMap();
229
				assertNotNull(resourceMap);
230
				
231
				String rdfXml = ResourceMapFactory.getInstance().serializeResourceMap(resourceMap);
232
				assertNotNull(rdfXml);
233
				
234
				Session session = getTestSession();
235
				InputStream object = null;
236
				SystemMetadata sysmeta = null;
237
				
238
				// save the data objects (data just contains their ID)
239
				InputStream dataObject1 = new ByteArrayInputStream(dataId.getValue().getBytes("UTF-8"));
240
				sysmeta = createSystemMetadata(dataId, session.getSubject(), dataObject1);
241
				MNodeService.getInstance(request).create(session, dataId, dataObject1, sysmeta);
242
				// second data file
243
				InputStream dataObject2 = new ByteArrayInputStream(imgId.getValue().getBytes("UTF-8"));
244
				sysmeta = createSystemMetadata(imgId, session.getSubject(), dataObject2);
245
				MNodeService.getInstance(request).create(session, imgId, dataObject2, sysmeta);
246
				// third data file
247
				InputStream dataObject3 = new ByteArrayInputStream(drawActivityId.getValue().getBytes("UTF-8"));
248
				sysmeta = createSystemMetadata(drawActivityId, session.getSubject(), dataObject3);
249
				MNodeService.getInstance(request).create(session, drawActivityId, dataObject3, sysmeta);
250
				// fourth data file
251
				InputStream dataObject4 = new ByteArrayInputStream(composeActivityId.getValue().getBytes("UTF-8"));
252
				sysmeta = createSystemMetadata(composeActivityId, session.getSubject(), dataObject4);
253
				MNodeService.getInstance(request).create(session, composeActivityId, dataObject4, sysmeta);
254
				// metadata file
255
				InputStream metadataObject = new ByteArrayInputStream(metadataId.getValue().getBytes("UTF-8"));
256
				sysmeta = createSystemMetadata(metadataId, session.getSubject(), metadataObject);
257
				MNodeService.getInstance(request).create(session, metadataId, metadataObject, sysmeta);
258
							
259
				// save the ORE object
260
				object = new ByteArrayInputStream(rdfXml.getBytes("UTF-8"));
261
				sysmeta = createSystemMetadata(resourceMapId, session.getSubject(), object);
262
				sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("http://www.openarchives.org/ore/terms").getFormatId());
263
				Identifier pid = MNodeService.getInstance(request).create(session, resourceMapId, object, sysmeta);
264
				
265
				// get the package we uploaded
266
				InputStream bagStream = MNodeService.getInstance(request).getPackage(session, null, pid);
267
				File bagFile = File.createTempFile("bagit.", ".zip");
268
				IOUtils.copy(bagStream, new FileOutputStream(bagFile));
269
				BagFactory bagFactory = new BagFactory();
270
				Bag bag = bagFactory.createBag(bagFile);
271
				Iterator<Manifest> manifestIter = bag.getTagManifests().iterator();
272
				while (manifestIter.hasNext()) {
273
					String filepath = manifestIter.next().getFilepath();
274
					BagFile entryFile = bag.getBagFile(filepath);
275
					InputStream result = entryFile.newInputStream();
276
					// check ORE
277
					if (filepath.contains(resourceMapId.getValue())) {
278
						object.reset();
279
						assertTrue(object.available() > 0);
280
						assertTrue(result.available() > 0);
281
						assertTrue(IOUtils.contentEquals(result, object));
282
					}
283
					// check metadata
284
					if (filepath.contains(metadataId.getValue())) {
285
						metadataObject.reset();
286
						assertTrue(metadataObject.available() > 0);
287
						assertTrue(result.available() > 0);
288
						assertTrue(IOUtils.contentEquals(result, metadataObject));
289
					}
290
					if (filepath.contains(dataId.getValue())) {
291
						dataObject1.reset();
292
						assertTrue(dataObject1.available() > 0);
293
						assertTrue(result.available() > 0);
294
						assertTrue(IOUtils.contentEquals(result, dataObject1));
295
					}
296
					if (filepath.contains(imgId.getValue())) {
297
						dataObject2.reset();
298
						assertTrue(dataObject2.available() > 0);
299
						assertTrue(result.available() > 0);
300
						assertTrue(IOUtils.contentEquals(result, dataObject2));
301
					}
302
					if (filepath.contains(drawActivityId.getValue())) {
303
						dataObject3.reset();
304
						assertTrue(dataObject3.available() > 0);
305
						assertTrue(result.available() > 0);
306
						assertTrue(IOUtils.contentEquals(result, dataObject3));
307
					}
308
					if (filepath.contains(composeActivityId.getValue())) {
309
						dataObject4.reset();
310
						assertTrue(dataObject4.available() > 0);
311
						assertTrue(result.available() > 0);
312
						assertTrue(IOUtils.contentEquals(result, dataObject4));
313
					}
314
					
315
					
316
				}
317
				
318
				// clean up
319
				bagFile.delete();
320
				
321
				// test the ORE lookup
322
				List<Identifier> oreIds = MNodeService.getInstance(request).lookupOreFor(metadataId, true);
323
				assertTrue(oreIds.contains(resourceMapId));
324
				
325
				System.out.println("************** The package is: ***********");
326
				for(Identifier objectId : oreIds){
327
					System.out.println(objectId.getValue());
328
				}
329
				System.out.println("***********************************************");
330
	  
331
  } catch (Exception e) {
332
		e.printStackTrace();
333
		fail("Unexpected error: " + e.getMessage());
334
	}
335
  }
336
		  
337
	
338
  
339
}
0 340

  

Also available in: Unified diff