Project

General

Profile

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: 2017-03-06 16:58:11 -0800 (Mon, 06 Mar 2017) $'
9
 * '$Revision: 10189 $'
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
			if (isMetadata) {
240
				String creator = metadata.get(DataCiteProfile.CREATOR.toString());
241
				assertTrue(creator.equals("adfasd"));				
242
			}
243
			
244
			System.out.println("tested with DOI: " + pid.getValue());
245
			
246
		} catch (Exception e) {
247
			e.printStackTrace();
248
			fail("Unexpected error: " + e.getMessage());
249
		}
250
	}
251
	
252
	/**
253
	 * Test object creation
254
	 */
255
	public void testCreateDOI() {
256
		printTestHeader("testCreateDOI");
257

    
258
		try {
259
			// get ezid config properties
260
			String shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
261
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
262
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
263
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
264
			
265
			Session session = getTestSession();
266
			Identifier guid = new Identifier();
267
			guid.setValue(shoulder + "/testCreateDOI." + System.currentTimeMillis());
268
			InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
269
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
270
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
271
			assertEquals(guid.getValue(), pid.getValue());
272

    
273
			// check for the metadata explicitly, using ezid service
274
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
275
			ezid.login(ezidUsername, ezidPassword);
276
			int count = 0;
277
			HashMap<String, String> metadata = null;
278
			do {
279
				try {
280
					metadata = ezid.getMetadata(pid.getValue());
281
				} catch (Exception e) {
282
					Thread.sleep(1000);
283
				}
284
				count++;
285
			} while (metadata == null && count < 10);
286
			
287
			assertNotNull(metadata);
288
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
289
			
290
		} catch (Exception e) {
291
			e.printStackTrace();
292
			fail("Unexpected error: " + e.getMessage());
293
		}
294
	}
295
	
296
	/**
297
	 * Test object publishing
298
	 */
299
	public void testPublishDOI() {
300
		printTestHeader("testPublishDOI");
301

    
302
		try {
303
			// get ezid config properties
304
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
305
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
306
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
307
			
308
			Session session = getTestSession();
309
			Identifier guid = new Identifier();
310
			guid.setValue("testPublishDOI." + System.currentTimeMillis());
311
			
312
			// use EML to test
313
			// TODO: include an ORE to really exercise it
314
			String emlFile = "test/tao.14563.1.xml";
315
			InputStream content = null;
316
			try {
317
				content = new FileInputStream(emlFile);
318
	            
319
	            // create the initial version without DOI
320
	            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
321
	            sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
322
	            Identifier pid = MNodeService.getInstance(request).create(session, guid, content, sysmeta);
323
	            assertEquals(guid.getValue(), pid.getValue());
324

    
325
	            // now publish it
326
	            Identifier publishedIdentifier = MNodeService.getInstance(request).publish(session, pid);
327
	            
328
	            // check for the metadata explicitly, using ezid service
329
	            EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
330
	            ezid.login(ezidUsername, ezidPassword);
331
	            int count = 0;
332
				HashMap<String, String> metadata = null;
333
				do {
334
					try {
335
						metadata = ezid.getMetadata(publishedIdentifier.getValue());
336
					} catch (Exception e) {
337
						Thread.sleep(1000);
338
					}
339
					count++;
340
				} while (metadata == null && count < 10);
341
	            
342
	            assertNotNull(metadata);
343
	            assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
344
	            content.close();
345
			} catch (FileNotFoundException e) {
346
				e.printStackTrace();
347
				fail(e.getMessage());
348
			} finally {
349
			    IOUtils.closeQuietly(content);
350
			}
351

    
352
			
353
		} catch (Exception e) {
354
			e.printStackTrace();
355
			fail("Unexpected error: " + e.getMessage());
356
		}
357
	}
358
}
(6-6/9)