1 |
8835
|
walker
|
/**
|
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 |
8848
|
walker
|
import java.io.FileReader;
|
38 |
|
|
import java.io.FileInputStream;
|
39 |
8835
|
walker
|
import java.io.FileOutputStream;
|
40 |
|
|
import java.io.IOException;
|
41 |
|
|
import java.io.InputStream;
|
42 |
|
|
import java.io.UnsupportedEncodingException;
|
43 |
|
|
import java.text.SimpleDateFormat;
|
44 |
|
|
import java.util.ArrayList;
|
45 |
|
|
import java.util.Calendar;
|
46 |
|
|
import java.util.Date;
|
47 |
|
|
import java.util.HashMap;
|
48 |
|
|
import java.util.Iterator;
|
49 |
|
|
import java.util.List;
|
50 |
|
|
import java.util.Map;
|
51 |
|
|
|
52 |
|
|
import junit.framework.Test;
|
53 |
|
|
import junit.framework.TestSuite;
|
54 |
|
|
|
55 |
|
|
import org.apache.commons.io.IOUtils;
|
56 |
|
|
import org.dataone.client.v1.itk.DataPackage;
|
57 |
|
|
import org.dataone.client.v2.formats.ObjectFormatCache;
|
58 |
|
|
import org.dataone.configuration.Settings;
|
59 |
|
|
import org.dataone.ore.ResourceMapFactory;
|
60 |
|
|
import org.dataone.service.util.TypeMarshaller;
|
61 |
|
|
import org.dataone.service.exceptions.IdentifierNotUnique;
|
62 |
|
|
import org.dataone.service.exceptions.InsufficientResources;
|
63 |
|
|
import org.dataone.service.exceptions.InvalidRequest;
|
64 |
|
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
65 |
|
|
import org.dataone.service.exceptions.InvalidToken;
|
66 |
|
|
import org.dataone.service.exceptions.NotAuthorized;
|
67 |
|
|
import org.dataone.service.exceptions.NotFound;
|
68 |
|
|
import org.dataone.service.exceptions.NotImplemented;
|
69 |
|
|
import org.dataone.service.exceptions.ServiceFailure;
|
70 |
|
|
import org.dataone.service.exceptions.SynchronizationFailed;
|
71 |
|
|
import org.dataone.service.exceptions.UnsupportedType;
|
72 |
|
|
import org.dataone.service.types.v1.AccessPolicy;
|
73 |
|
|
import org.dataone.service.types.v1.AccessRule;
|
74 |
|
|
import org.dataone.service.types.v1.Checksum;
|
75 |
|
|
import org.dataone.service.types.v1.DescribeResponse;
|
76 |
|
|
import org.dataone.service.types.v1.Event;
|
77 |
|
|
import org.dataone.service.types.v1.Identifier;
|
78 |
|
|
import org.dataone.service.types.v2.Log;
|
79 |
|
|
import org.dataone.service.types.v2.Node;
|
80 |
|
|
import org.dataone.service.types.v1.NodeReference;
|
81 |
|
|
import org.dataone.service.types.v1.ObjectFormatIdentifier;
|
82 |
|
|
import org.dataone.service.types.v1.ObjectList;
|
83 |
|
|
import org.dataone.service.types.v1.Permission;
|
84 |
|
|
import org.dataone.service.types.v1.Person;
|
85 |
|
|
import org.dataone.service.types.v1.Session;
|
86 |
|
|
import org.dataone.service.types.v1.Subject;
|
87 |
|
|
import org.dataone.service.types.v1.SubjectInfo;
|
88 |
|
|
import org.dataone.service.types.v2.SystemMetadata;
|
89 |
|
|
import org.dspace.foresite.ResourceMap;
|
90 |
|
|
import org.jibx.runtime.JiBXException;
|
91 |
|
|
import org.junit.After;
|
92 |
|
|
import org.junit.Before;
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* A JUnit test to insert a resource map into metacat with PROV relationships
|
96 |
|
|
*
|
97 |
|
|
* @author lwalker 08-15-2014
|
98 |
|
|
*
|
99 |
|
|
*/
|
100 |
|
|
public class InsertORETest extends D1NodeServiceTest {
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* Set up the test fixtures
|
104 |
|
|
*
|
105 |
|
|
* @throws Exception
|
106 |
|
|
*/
|
107 |
|
|
@Before
|
108 |
|
|
public void setUp() throws Exception {
|
109 |
|
|
super.setUp();
|
110 |
|
|
// set up the configuration for d1client
|
111 |
|
|
Settings.getConfiguration().setProperty("D1Client.cnClassName", MockCNode.class.getName());
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/**
|
115 |
|
|
* Remove the test fixtures
|
116 |
|
|
*/
|
117 |
|
|
@After
|
118 |
|
|
public void tearDown() {
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* Build the test suite
|
123 |
|
|
* @return
|
124 |
|
|
*/
|
125 |
|
|
public static Test suite() {
|
126 |
|
|
|
127 |
|
|
TestSuite suite = new TestSuite();
|
128 |
|
|
suite.addTest(new InsertORETest("initialize"));
|
129 |
|
|
|
130 |
|
|
// Insert test
|
131 |
|
|
suite.addTest(new InsertORETest("testInsertORE"));
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
return suite;
|
135 |
|
|
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/**
|
139 |
|
|
* Constructor for the tests
|
140 |
|
|
*
|
141 |
|
|
* @param name - the name of the test
|
142 |
|
|
*/
|
143 |
|
|
public InsertORETest(String name) {
|
144 |
|
|
super(name);
|
145 |
|
|
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Initial blank test
|
150 |
|
|
*/
|
151 |
|
|
public void initialize() {
|
152 |
|
|
assertTrue(1 == 1);
|
153 |
|
|
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
public void testInsertORE() {
|
157 |
|
|
printTestHeader("testInsertORE");
|
158 |
|
|
|
159 |
|
|
try {
|
160 |
|
|
//Create the predicate URIs and NS
|
161 |
|
|
String provNS = "http://www.w3.org/ns/prov#";
|
162 |
|
|
String wasDerivedFromURI = provNS + "wasDerivedFrom";
|
163 |
|
|
String usedURI = provNS + "used";
|
164 |
|
|
String wasGeneratedByURI = provNS + "wasGeneratedBy";
|
165 |
|
|
String generatedURI = provNS + "generated";
|
166 |
|
|
String wasInformedByURI = provNS + "wasInformedBy";
|
167 |
|
|
Long uniqueNum = System.currentTimeMillis();
|
168 |
|
|
|
169 |
|
|
// construct the ORE package
|
170 |
|
|
Identifier resourceMapId = new Identifier();
|
171 |
|
|
resourceMapId.setValue("package." + uniqueNum + ".1");
|
172 |
|
|
DataPackage dataPackage = new DataPackage(resourceMapId);
|
173 |
|
|
|
174 |
|
|
//Metadata
|
175 |
|
|
Identifier metadataId = new Identifier();
|
176 |
|
|
metadataId.setValue("meta." + uniqueNum + ".1");
|
177 |
|
|
|
178 |
|
|
//Data
|
179 |
|
|
List<Identifier> dataIds = new ArrayList<Identifier>();
|
180 |
|
|
Identifier dataId = new Identifier();
|
181 |
|
|
dataId.setValue("data." + uniqueNum + ".1");
|
182 |
|
|
Identifier imgId = new Identifier();
|
183 |
|
|
imgId.setValue("img." + uniqueNum + ".1");
|
184 |
|
|
Identifier drawActivityId = new Identifier();
|
185 |
|
|
drawActivityId.setValue("drawActivity." + uniqueNum + ".1");
|
186 |
|
|
Identifier composeActivityId = new Identifier();
|
187 |
|
|
composeActivityId.setValue("composeActivity." + uniqueNum + ".1");
|
188 |
|
|
|
189 |
|
|
//Create the primary (external) resources identifiers
|
190 |
|
|
Identifier primaryDataId = new Identifier();
|
191 |
|
|
primaryDataId.setValue("primaryData.1.1");
|
192 |
|
|
Identifier primaryDataId2 = new Identifier();
|
193 |
|
|
primaryDataId2.setValue("primaryData.2.1");
|
194 |
|
|
|
195 |
|
|
dataIds.add(dataId);
|
196 |
|
|
dataIds.add(imgId);
|
197 |
|
|
dataIds.add(drawActivityId);
|
198 |
|
|
dataIds.add(composeActivityId);
|
199 |
|
|
|
200 |
|
|
//Create lists of the items to use in the triples
|
201 |
|
|
List<Identifier> dataIdList = new ArrayList<Identifier>();
|
202 |
|
|
dataIdList.add(dataId);
|
203 |
|
|
List<Identifier> activityIdList = new ArrayList<Identifier>();
|
204 |
|
|
activityIdList.add(drawActivityId);
|
205 |
|
|
List<Identifier> composeActivityIdList = new ArrayList<Identifier>();
|
206 |
|
|
composeActivityIdList.add(composeActivityId);
|
207 |
|
|
List<Identifier> primaryDataIdList = new ArrayList<Identifier>();
|
208 |
|
|
primaryDataIdList.add(primaryDataId);
|
209 |
|
|
primaryDataIdList.add(primaryDataId2);
|
210 |
|
|
|
211 |
|
|
//---- isDocumentedBy/documents ----
|
212 |
|
|
dataPackage.insertRelationship(metadataId, dataIds);
|
213 |
|
|
|
214 |
|
|
//---- wasDerivedFrom ----
|
215 |
|
|
dataPackage.insertRelationship(dataId, primaryDataIdList, provNS, wasDerivedFromURI);
|
216 |
|
|
dataPackage.insertRelationship(imgId, dataIdList, provNS, wasDerivedFromURI);
|
217 |
|
|
|
218 |
|
|
//---- wasGeneratedBy ----
|
219 |
|
|
dataPackage.insertRelationship(imgId, activityIdList, provNS, wasGeneratedByURI);
|
220 |
|
|
dataPackage.insertRelationship(dataId, composeActivityIdList, provNS, wasGeneratedByURI);
|
221 |
|
|
|
222 |
|
|
//---- wasInformedBy ----
|
223 |
|
|
dataPackage.insertRelationship(drawActivityId, composeActivityIdList, provNS, wasInformedByURI);
|
224 |
|
|
|
225 |
|
|
//---- used ----
|
226 |
|
|
dataPackage.insertRelationship(drawActivityId, dataIdList, provNS, usedURI);
|
227 |
|
|
dataPackage.insertRelationship(composeActivityId, primaryDataIdList, provNS, usedURI);
|
228 |
|
|
|
229 |
|
|
//Create the resourceMap
|
230 |
|
|
ResourceMap resourceMap = dataPackage.getMap();
|
231 |
|
|
assertNotNull(resourceMap);
|
232 |
|
|
|
233 |
|
|
String rdfXml = ResourceMapFactory.getInstance().serializeResourceMap(resourceMap);
|
234 |
|
|
assertNotNull(rdfXml);
|
235 |
|
|
|
236 |
|
|
Session session = getTestSession();
|
237 |
|
|
InputStream object = null;
|
238 |
|
|
SystemMetadata sysmeta = null;
|
239 |
|
|
|
240 |
|
|
// save the data objects (data just contains their ID)
|
241 |
|
|
InputStream dataObject1 = new ByteArrayInputStream(dataId.getValue().getBytes("UTF-8"));
|
242 |
|
|
sysmeta = createSystemMetadata(dataId, session.getSubject(), dataObject1);
|
243 |
|
|
MNodeService.getInstance(request).create(session, dataId, dataObject1, sysmeta);
|
244 |
|
|
// second data file
|
245 |
|
|
InputStream dataObject2 = new ByteArrayInputStream(imgId.getValue().getBytes("UTF-8"));
|
246 |
|
|
sysmeta = createSystemMetadata(imgId, session.getSubject(), dataObject2);
|
247 |
|
|
MNodeService.getInstance(request).create(session, imgId, dataObject2, sysmeta);
|
248 |
|
|
// third data file
|
249 |
|
|
InputStream dataObject3 = new ByteArrayInputStream(drawActivityId.getValue().getBytes("UTF-8"));
|
250 |
|
|
sysmeta = createSystemMetadata(drawActivityId, session.getSubject(), dataObject3);
|
251 |
|
|
MNodeService.getInstance(request).create(session, drawActivityId, dataObject3, sysmeta);
|
252 |
|
|
// fourth data file
|
253 |
|
|
InputStream dataObject4 = new ByteArrayInputStream(composeActivityId.getValue().getBytes("UTF-8"));
|
254 |
|
|
sysmeta = createSystemMetadata(composeActivityId, session.getSubject(), dataObject4);
|
255 |
|
|
MNodeService.getInstance(request).create(session, composeActivityId, dataObject4, sysmeta);
|
256 |
|
|
// metadata file
|
257 |
8848
|
walker
|
String testfile = "test/eml-sample.xml";
|
258 |
|
|
InputStream metadataObject = new FileInputStream(testfile);
|
259 |
|
|
sysmeta = createSystemMetadata(metadataId, session.getSubject(), null);
|
260 |
|
|
sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
|
261 |
8835
|
walker
|
MNodeService.getInstance(request).create(session, metadataId, metadataObject, sysmeta);
|
262 |
|
|
|
263 |
|
|
// save the ORE object
|
264 |
|
|
object = new ByteArrayInputStream(rdfXml.getBytes("UTF-8"));
|
265 |
|
|
sysmeta = createSystemMetadata(resourceMapId, session.getSubject(), object);
|
266 |
|
|
sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("http://www.openarchives.org/ore/terms").getFormatId());
|
267 |
|
|
Identifier pid = MNodeService.getInstance(request).create(session, resourceMapId, object, sysmeta);
|
268 |
|
|
|
269 |
|
|
// get the package we uploaded
|
270 |
|
|
InputStream bagStream = MNodeService.getInstance(request).getPackage(session, null, pid);
|
271 |
|
|
File bagFile = File.createTempFile("bagit.", ".zip");
|
272 |
|
|
IOUtils.copy(bagStream, new FileOutputStream(bagFile));
|
273 |
|
|
BagFactory bagFactory = new BagFactory();
|
274 |
|
|
Bag bag = bagFactory.createBag(bagFile);
|
275 |
|
|
Iterator<Manifest> manifestIter = bag.getTagManifests().iterator();
|
276 |
|
|
while (manifestIter.hasNext()) {
|
277 |
|
|
String filepath = manifestIter.next().getFilepath();
|
278 |
|
|
BagFile entryFile = bag.getBagFile(filepath);
|
279 |
|
|
InputStream result = entryFile.newInputStream();
|
280 |
|
|
// check ORE
|
281 |
|
|
if (filepath.contains(resourceMapId.getValue())) {
|
282 |
|
|
object.reset();
|
283 |
|
|
assertTrue(object.available() > 0);
|
284 |
|
|
assertTrue(result.available() > 0);
|
285 |
|
|
assertTrue(IOUtils.contentEquals(result, object));
|
286 |
|
|
}
|
287 |
|
|
// check metadata
|
288 |
|
|
if (filepath.contains(metadataId.getValue())) {
|
289 |
|
|
metadataObject.reset();
|
290 |
|
|
assertTrue(metadataObject.available() > 0);
|
291 |
|
|
assertTrue(result.available() > 0);
|
292 |
|
|
assertTrue(IOUtils.contentEquals(result, metadataObject));
|
293 |
|
|
}
|
294 |
|
|
if (filepath.contains(dataId.getValue())) {
|
295 |
|
|
dataObject1.reset();
|
296 |
|
|
assertTrue(dataObject1.available() > 0);
|
297 |
|
|
assertTrue(result.available() > 0);
|
298 |
|
|
assertTrue(IOUtils.contentEquals(result, dataObject1));
|
299 |
|
|
}
|
300 |
|
|
if (filepath.contains(imgId.getValue())) {
|
301 |
|
|
dataObject2.reset();
|
302 |
|
|
assertTrue(dataObject2.available() > 0);
|
303 |
|
|
assertTrue(result.available() > 0);
|
304 |
|
|
assertTrue(IOUtils.contentEquals(result, dataObject2));
|
305 |
|
|
}
|
306 |
|
|
if (filepath.contains(drawActivityId.getValue())) {
|
307 |
|
|
dataObject3.reset();
|
308 |
|
|
assertTrue(dataObject3.available() > 0);
|
309 |
|
|
assertTrue(result.available() > 0);
|
310 |
|
|
assertTrue(IOUtils.contentEquals(result, dataObject3));
|
311 |
|
|
}
|
312 |
|
|
if (filepath.contains(composeActivityId.getValue())) {
|
313 |
|
|
dataObject4.reset();
|
314 |
|
|
assertTrue(dataObject4.available() > 0);
|
315 |
|
|
assertTrue(result.available() > 0);
|
316 |
|
|
assertTrue(IOUtils.contentEquals(result, dataObject4));
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
// clean up
|
323 |
|
|
bagFile.delete();
|
324 |
|
|
|
325 |
|
|
// test the ORE lookup
|
326 |
|
|
List<Identifier> oreIds = MNodeService.getInstance(request).lookupOreFor(metadataId, true);
|
327 |
|
|
assertTrue(oreIds.contains(resourceMapId));
|
328 |
|
|
|
329 |
|
|
System.out.println("************** The package is: ***********");
|
330 |
|
|
for(Identifier objectId : oreIds){
|
331 |
|
|
System.out.println(objectId.getValue());
|
332 |
|
|
}
|
333 |
|
|
System.out.println("***********************************************");
|
334 |
|
|
|
335 |
|
|
} catch (Exception e) {
|
336 |
|
|
e.printStackTrace();
|
337 |
|
|
fail("Unexpected error: " + e.getMessage());
|
338 |
|
|
}
|
339 |
|
|
}
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
}
|