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: leinfelder $'
|
8
|
* '$Date: 2014-07-23 16:19:48 -0700 (Wed, 23 Jul 2014) $'
|
9
|
* '$Revision: 8810 $'
|
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 java.io.ByteArrayInputStream;
|
30
|
import java.io.FileInputStream;
|
31
|
import java.io.FileNotFoundException;
|
32
|
import java.io.IOException;
|
33
|
import java.io.InputStream;
|
34
|
import java.util.HashMap;
|
35
|
|
36
|
import junit.framework.Test;
|
37
|
import junit.framework.TestSuite;
|
38
|
|
39
|
import org.apache.commons.io.IOUtils;
|
40
|
import org.dataone.client.v2.formats.ObjectFormatCache;
|
41
|
import org.dataone.configuration.Settings;
|
42
|
import org.dataone.service.types.v1.Identifier;
|
43
|
import org.dataone.service.types.v1.Session;
|
44
|
import org.dataone.service.types.v1.Subject;
|
45
|
import org.dataone.service.types.v2.SystemMetadata;
|
46
|
import org.junit.After;
|
47
|
import org.junit.Before;
|
48
|
|
49
|
import edu.ucsb.nceas.ezid.EZIDService;
|
50
|
import edu.ucsb.nceas.ezid.profile.DataCiteProfile;
|
51
|
import edu.ucsb.nceas.ezid.profile.InternalProfile;
|
52
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
53
|
|
54
|
/**
|
55
|
* A JUnit test to exercise the DOI registration for content added
|
56
|
* via the DataONE MN API
|
57
|
*
|
58
|
* @author leinfelder
|
59
|
*
|
60
|
*/
|
61
|
public class RegisterDOITest extends D1NodeServiceTest {
|
62
|
|
63
|
|
64
|
|
65
|
/**
|
66
|
* Set up the test fixtures
|
67
|
*
|
68
|
* @throws Exception
|
69
|
*/
|
70
|
@Before
|
71
|
public void setUp() throws Exception {
|
72
|
super.setUp();
|
73
|
// set up the configuration for d1client
|
74
|
Settings.getConfiguration().setProperty("D1Client.cnClassName",
|
75
|
MockCNode.class.getName());
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Remove the test fixtures
|
80
|
*/
|
81
|
@After
|
82
|
public void tearDown() {
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Build the test suite
|
87
|
*
|
88
|
* @return
|
89
|
*/
|
90
|
public static Test suite() {
|
91
|
|
92
|
TestSuite suite = new TestSuite();
|
93
|
suite.addTest(new RegisterDOITest("initialize"));
|
94
|
|
95
|
// DOI registration test
|
96
|
suite.addTest(new RegisterDOITest("testCreateDOI"));
|
97
|
suite.addTest(new RegisterDOITest("testMintAndCreateDOI"));
|
98
|
suite.addTest(new RegisterDOITest("testMintAndCreateForEML"));
|
99
|
|
100
|
// publish
|
101
|
suite.addTest(new RegisterDOITest("testPublishDOI"));
|
102
|
|
103
|
return suite;
|
104
|
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Constructor for the tests
|
109
|
*
|
110
|
* @param name
|
111
|
* - the name of the test
|
112
|
*/
|
113
|
public RegisterDOITest(String name) {
|
114
|
super(name);
|
115
|
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Initial blank test
|
120
|
*/
|
121
|
public void initialize() {
|
122
|
assertTrue(1 == 1);
|
123
|
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* constructs a "fake" session with a test subject
|
128
|
* @return
|
129
|
*/
|
130
|
@Override
|
131
|
public Session getTestSession() throws Exception {
|
132
|
Session session = new Session();
|
133
|
Subject subject = new Subject();
|
134
|
subject.setValue("CN=Benjamin Leinfelder A515,O=University of Chicago,C=US,DC=cilogon,DC=org");
|
135
|
session.setSubject(subject);
|
136
|
return session;
|
137
|
}
|
138
|
|
139
|
public void testMintAndCreateDOI() {
|
140
|
printTestHeader("testMintAndCreateDOI");
|
141
|
testMintAndCreateDOI(null);
|
142
|
}
|
143
|
|
144
|
public void testMintAndCreateForEML() {
|
145
|
printTestHeader("testMintAndCreateForEML");
|
146
|
String emlFile = "test/tao.14563.1.xml";
|
147
|
InputStream content = null;
|
148
|
try {
|
149
|
content = new FileInputStream(emlFile);
|
150
|
testMintAndCreateDOI(content);
|
151
|
content.close();
|
152
|
} catch (FileNotFoundException e) {
|
153
|
e.printStackTrace();
|
154
|
fail(e.getMessage());
|
155
|
} catch (IOException e) {
|
156
|
e.printStackTrace();
|
157
|
fail(e.getMessage());
|
158
|
} finally {
|
159
|
IOUtils.closeQuietly(content);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Test object creation
|
165
|
*/
|
166
|
private void testMintAndCreateDOI(InputStream inputStream) {
|
167
|
printTestHeader("testMintAndCreateDOI - common");
|
168
|
|
169
|
try {
|
170
|
// get ezid config properties
|
171
|
String ezidUsername = PropertyService.getProperty("guid.ezid.username");
|
172
|
String ezidPassword = PropertyService.getProperty("guid.ezid.password");
|
173
|
String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
|
174
|
|
175
|
EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
|
176
|
ezid.login(ezidUsername, ezidPassword);
|
177
|
|
178
|
// Mint a DOI
|
179
|
Session session = getTestSession();
|
180
|
Identifier guid = MNodeService.getInstance(request).generateIdentifier(session, "DOI", null);
|
181
|
|
182
|
// check that EZID knows about it
|
183
|
HashMap<String, String> metadata = null;
|
184
|
int count = 0;
|
185
|
do {
|
186
|
try {
|
187
|
metadata = ezid.getMetadata(guid.getValue());
|
188
|
} catch (Exception e) {
|
189
|
Thread.sleep(1000);
|
190
|
}
|
191
|
count++;
|
192
|
} while (metadata == null && count < 10);
|
193
|
assertNotNull(metadata);
|
194
|
|
195
|
// add the actual object for the newly-minted DOI
|
196
|
SystemMetadata sysmeta = null;
|
197
|
InputStream object = null;
|
198
|
boolean isMetadata = false;
|
199
|
if (inputStream != null) {
|
200
|
sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
201
|
object = inputStream;
|
202
|
sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
|
203
|
isMetadata = true;
|
204
|
} else {
|
205
|
object = new ByteArrayInputStream("test".getBytes("UTF-8"));
|
206
|
sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
207
|
}
|
208
|
|
209
|
Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
|
210
|
assertEquals(guid.getValue(), pid.getValue());
|
211
|
|
212
|
// check for the metadata for title element
|
213
|
count = 0;
|
214
|
metadata = null;
|
215
|
do {
|
216
|
try {
|
217
|
metadata = ezid.getMetadata(pid.getValue());
|
218
|
// check if the update thread finished yet, otherwise try again
|
219
|
if (metadata != null && isMetadata) {
|
220
|
String registeredTarget = metadata.get(InternalProfile.TARGET.toString());
|
221
|
if (!registeredTarget.endsWith("/#view/" + pid.getValue())) {
|
222
|
// try fetching it again
|
223
|
metadata = null;
|
224
|
}
|
225
|
}
|
226
|
} catch (Exception e) {
|
227
|
Thread.sleep(1000);
|
228
|
}
|
229
|
count++;
|
230
|
} while (metadata == null && count < 10);
|
231
|
assertNotNull(metadata);
|
232
|
assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
|
233
|
|
234
|
// check that the target URI was updated
|
235
|
if (isMetadata) {
|
236
|
String registeredTarget = metadata.get(InternalProfile.TARGET.toString());
|
237
|
assertTrue(registeredTarget.endsWith("/#view/" + pid.getValue()));
|
238
|
}
|
239
|
|
240
|
System.out.println("tested with DOI: " + pid.getValue());
|
241
|
|
242
|
} catch (Exception e) {
|
243
|
e.printStackTrace();
|
244
|
fail("Unexpected error: " + e.getMessage());
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Test object creation
|
250
|
*/
|
251
|
public void testCreateDOI() {
|
252
|
printTestHeader("testCreateDOI");
|
253
|
|
254
|
try {
|
255
|
// get ezid config properties
|
256
|
String shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
|
257
|
String ezidUsername = PropertyService.getProperty("guid.ezid.username");
|
258
|
String ezidPassword = PropertyService.getProperty("guid.ezid.password");
|
259
|
String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
|
260
|
|
261
|
Session session = getTestSession();
|
262
|
Identifier guid = new Identifier();
|
263
|
guid.setValue(shoulder + "/testCreateDOI." + System.currentTimeMillis());
|
264
|
InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
|
265
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
|
266
|
Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
|
267
|
assertEquals(guid.getValue(), pid.getValue());
|
268
|
|
269
|
// check for the metadata explicitly, using ezid service
|
270
|
EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
|
271
|
ezid.login(ezidUsername, ezidPassword);
|
272
|
int count = 0;
|
273
|
HashMap<String, String> metadata = null;
|
274
|
do {
|
275
|
try {
|
276
|
metadata = ezid.getMetadata(pid.getValue());
|
277
|
} catch (Exception e) {
|
278
|
Thread.sleep(1000);
|
279
|
}
|
280
|
count++;
|
281
|
} while (metadata == null && count < 10);
|
282
|
|
283
|
assertNotNull(metadata);
|
284
|
assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
|
285
|
|
286
|
} catch (Exception e) {
|
287
|
e.printStackTrace();
|
288
|
fail("Unexpected error: " + e.getMessage());
|
289
|
}
|
290
|
}
|
291
|
|
292
|
/**
|
293
|
* Test object publishing
|
294
|
*/
|
295
|
public void testPublishDOI() {
|
296
|
printTestHeader("testPublishDOI");
|
297
|
|
298
|
try {
|
299
|
// get ezid config properties
|
300
|
String ezidUsername = PropertyService.getProperty("guid.ezid.username");
|
301
|
String ezidPassword = PropertyService.getProperty("guid.ezid.password");
|
302
|
String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
|
303
|
|
304
|
Session session = getTestSession();
|
305
|
Identifier guid = new Identifier();
|
306
|
guid.setValue("testPublishDOI." + System.currentTimeMillis());
|
307
|
|
308
|
// use EML to test
|
309
|
// TODO: include an ORE to really exercise it
|
310
|
String emlFile = "test/tao.14563.1.xml";
|
311
|
InputStream content = null;
|
312
|
try {
|
313
|
content = new FileInputStream(emlFile);
|
314
|
|
315
|
// create the initial version without DOI
|
316
|
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
|
317
|
sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
|
318
|
Identifier pid = MNodeService.getInstance(request).create(session, guid, content, sysmeta);
|
319
|
assertEquals(guid.getValue(), pid.getValue());
|
320
|
|
321
|
// now publish it
|
322
|
Identifier publishedIdentifier = MNodeService.getInstance(request).publish(session, pid);
|
323
|
|
324
|
// check for the metadata explicitly, using ezid service
|
325
|
EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
|
326
|
ezid.login(ezidUsername, ezidPassword);
|
327
|
int count = 0;
|
328
|
HashMap<String, String> metadata = null;
|
329
|
do {
|
330
|
try {
|
331
|
metadata = ezid.getMetadata(publishedIdentifier.getValue());
|
332
|
} catch (Exception e) {
|
333
|
Thread.sleep(1000);
|
334
|
}
|
335
|
count++;
|
336
|
} while (metadata == null && count < 10);
|
337
|
|
338
|
assertNotNull(metadata);
|
339
|
assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
|
340
|
content.close();
|
341
|
} catch (FileNotFoundException e) {
|
342
|
e.printStackTrace();
|
343
|
fail(e.getMessage());
|
344
|
} finally {
|
345
|
IOUtils.closeQuietly(content);
|
346
|
}
|
347
|
|
348
|
|
349
|
} catch (Exception e) {
|
350
|
e.printStackTrace();
|
351
|
fail("Unexpected error: " + e.getMessage());
|
352
|
}
|
353
|
}
|
354
|
}
|