Revision 9185
Added by Jing Tao over 9 years ago
test/edu/ucsb/nceas/metacat/annotation/OrePackageTest.java | ||
---|---|---|
1 |
/** '$RCSfile$' |
|
2 |
* Copyright: 2010 Regents of the University of California and the |
|
3 |
* National Center for Ecological Analysis and Synthesis |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; either version 2 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 |
*/ |
|
19 |
package edu.ucsb.nceas.metacat.annotation; |
|
20 |
|
|
21 |
import java.io.ByteArrayInputStream; |
|
22 |
import java.io.InputStream; |
|
23 |
import java.net.URI; |
|
24 |
import java.util.Arrays; |
|
25 |
import java.util.HashMap; |
|
26 |
import java.util.List; |
|
27 |
import java.util.Map; |
|
28 |
|
|
29 |
import junit.framework.Test; |
|
30 |
import junit.framework.TestSuite; |
|
31 |
|
|
32 |
import org.dataone.ore.ResourceMapFactory; |
|
33 |
import org.dataone.service.types.v1.Identifier; |
|
34 |
import org.dataone.service.types.v1.ObjectFormatIdentifier; |
|
35 |
import org.dataone.service.types.v1.Session; |
|
36 |
import org.dataone.service.types.v2.SystemMetadata; |
|
37 |
import org.dspace.foresite.OREFactory; |
|
38 |
import org.dspace.foresite.Predicate; |
|
39 |
import org.dspace.foresite.ResourceMap; |
|
40 |
import org.dspace.foresite.Triple; |
|
41 |
import org.dspace.foresite.jena.TripleJena; |
|
42 |
|
|
43 |
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest; |
|
44 |
import edu.ucsb.nceas.metacat.dataone.MNodeService; |
|
45 |
|
|
46 |
public class OrePackageTest extends D1NodeServiceTest { |
|
47 |
|
|
48 |
|
|
49 |
private static final String ANNOTATION_TEST_DOC = "test/eml-sample-annotation.xml"; |
|
50 |
private static final String ANNOTATION_TEST_DATA = "test/onlineDataFile2"; |
|
51 |
|
|
52 |
/** |
|
53 |
* constructor for the test |
|
54 |
*/ |
|
55 |
public OrePackageTest(String name) { |
|
56 |
super(name); |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Establish a testing framework by initializing appropriate objects |
|
61 |
*/ |
|
62 |
public void setUp() throws Exception { |
|
63 |
super.setUp(); |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Release any objects after tests are complete |
|
68 |
*/ |
|
69 |
public void tearDown() { |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Create a suite of tests to be run together |
|
74 |
*/ |
|
75 |
public static Test suite() { |
|
76 |
TestSuite suite = new TestSuite(); |
|
77 |
suite.addTest(new OrePackageTest("testGenerateAnnotation")); |
|
78 |
return suite; |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
/** |
|
83 |
* Generate a single ORE+annotation |
|
84 |
* @throws Exception |
|
85 |
*/ |
|
86 |
public void testGenerateAnnotation() throws Exception { |
|
87 |
|
|
88 |
// add an EML package |
|
89 |
Identifier metadataPid = new Identifier(); |
|
90 |
metadataPid.setValue("testOre.eml." + System.currentTimeMillis()); |
|
91 |
Session session = getTestSession(); |
|
92 |
try { |
|
93 |
InputStream object = new ByteArrayInputStream(this.getTestDocFromFile(ANNOTATION_TEST_DOC).getBytes("UTF-8")); |
|
94 |
SystemMetadata sysmeta = createSystemMetadata(metadataPid, session.getSubject(), object); |
|
95 |
ObjectFormatIdentifier formatId = new ObjectFormatIdentifier(); |
|
96 |
formatId.setValue("eml://ecoinformatics.org/eml-2.0.0"); |
|
97 |
sysmeta.setFormatId(formatId); |
|
98 |
Identifier pid = MNodeService.getInstance(request).create(session, metadataPid, object, sysmeta); |
|
99 |
assertEquals(metadataPid.getValue(), pid.getValue()); |
|
100 |
} catch (Exception e) { |
|
101 |
e.printStackTrace(); |
|
102 |
fail("Could not add metadata test file: " + e.getMessage()); |
|
103 |
} |
|
104 |
|
|
105 |
// add a data file |
|
106 |
Identifier dataIdentifier = new Identifier(); |
|
107 |
dataIdentifier.setValue("derivedData." + System.currentTimeMillis()); |
|
108 |
try { |
|
109 |
InputStream object = new ByteArrayInputStream(this.getTestDocFromFile(ANNOTATION_TEST_DATA).getBytes("UTF-8")); |
|
110 |
SystemMetadata sysmeta = createSystemMetadata(dataIdentifier, session.getSubject(), object); |
|
111 |
ObjectFormatIdentifier formatId = new ObjectFormatIdentifier(); |
|
112 |
formatId.setValue("text/csv"); |
|
113 |
sysmeta.setFormatId(formatId); |
|
114 |
Identifier pid = MNodeService.getInstance(request).create(session, dataIdentifier, object, sysmeta); |
|
115 |
assertEquals(dataIdentifier.getValue(), pid.getValue()); |
|
116 |
} catch (Exception e) { |
|
117 |
e.printStackTrace(); |
|
118 |
fail("Could not add data test file: " + e.getMessage()); |
|
119 |
} |
|
120 |
|
|
121 |
// generate the ORE for the EML metadata + data |
|
122 |
Map<Identifier, List<Identifier>> resources = new HashMap<Identifier, List<Identifier>>(); |
|
123 |
Identifier resourceMapPid = new Identifier(); |
|
124 |
resourceMapPid.setValue("resourceMap_" + metadataPid.getValue()); |
|
125 |
List<Identifier> dataPids = Arrays.asList(dataIdentifier); |
|
126 |
resources.put(metadataPid, dataPids); |
|
127 |
|
|
128 |
ResourceMap resourceMap = ResourceMapFactory.getInstance().createResourceMap(resourceMapPid , resources); |
|
129 |
|
|
130 |
// augment the ORE package with triples describing the derivation |
|
131 |
Predicate predicate = new Predicate(new URI("http://www.w3.org/ns/prov#wasDerivedFrom")); |
|
132 |
Triple triple = |
|
133 |
OREFactory.createTriple( |
|
134 |
new URI("https://cn.dataone.org/cn/v1/resolve/" + dataIdentifier.getValue()), |
|
135 |
predicate, |
|
136 |
new URI("https://cn.dataone.org/cn/v1/resolve/data.1.1")); |
|
137 |
System.out.println("Triple: " + " " + triple.getPredicate().getURI() + " " + triple.getObjectURI()); |
|
138 |
resourceMap.addTriple(triple); |
|
139 |
|
|
140 |
// get the RDF content |
|
141 |
String rdfContent = ResourceMapFactory.getInstance().serializeResourceMap(resourceMap); |
|
142 |
|
|
143 |
// save the ORE+annotation (adjust formatId as needed) |
|
144 |
Identifier annotationPid = new Identifier(); |
|
145 |
annotationPid.setValue("http://annotation/" + metadataPid.getValue()); |
|
146 |
try { |
|
147 |
InputStream object = new ByteArrayInputStream(rdfContent.getBytes("UTF-8")); |
|
148 |
SystemMetadata sysmeta = createSystemMetadata(annotationPid, session.getSubject(), object); |
|
149 |
ObjectFormatIdentifier formatId = new ObjectFormatIdentifier(); |
|
150 |
formatId.setValue("http://www.w3.org/TR/rdf-syntax-grammar"); |
|
151 |
sysmeta.setFormatId(formatId); |
|
152 |
Identifier pid = MNodeService.getInstance(request).create(session, annotationPid, object, sysmeta); |
|
153 |
assertEquals(annotationPid.getValue(), pid.getValue()); |
|
154 |
} catch (Exception e) { |
|
155 |
e.printStackTrace(); |
|
156 |
fail("Could not add annotation test file: " + e.getMessage()); |
|
157 |
} |
|
158 |
|
|
159 |
// TODO: check that it was parsed by metacat-index |
|
160 |
} |
|
161 |
|
|
162 |
} |
|
163 | 0 |
Also available in: Unified diff
Remove the obsoleted test class.