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: tao $'
8
 *     '$Date: 2013-10-11 14:42:41 -0700 (Fri, 11 Oct 2013) $'
9
 * '$Revision: 8311 $'
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.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.v1.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 = ezid.getMetadata(guid.getValue());
184
			assertNotNull(metadata);
185

    
186
			// add the actual object for the newly-minted DOI
187
			SystemMetadata sysmeta = null;
188
			InputStream object = null;
189
			boolean isMetadata = false;
190
			if (inputStream != null) {
191
				sysmeta = createSystemMetadata(guid, session.getSubject(), null);
192
				object = inputStream;
193
		        sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
194
		        isMetadata = true;
195
			} else {
196
				object = new ByteArrayInputStream("test".getBytes("UTF-8"));
197
				sysmeta = createSystemMetadata(guid, session.getSubject(), object);
198
			}
199

    
200
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
201
			assertEquals(guid.getValue(), pid.getValue());
202

    
203
			// check for the metadata for title element
204
			metadata = ezid.getMetadata(pid.getValue());
205
			assertNotNull(metadata);
206
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
207
			
208
			// check that the target URI was updated
209
			if (isMetadata) {
210
				String registeredTarget = metadata.get(InternalProfile.TARGET.toString());
211
				assertTrue(registeredTarget.endsWith("/#view/" + pid.getValue()));
212
			}
213
			
214
			System.out.println("tested with DOI: " + pid.getValue());
215
			
216
		} catch (Exception e) {
217
			e.printStackTrace();
218
			fail("Unexpected error: " + e.getMessage());
219
		}
220
	}
221
	
222
	/**
223
	 * Test object creation
224
	 */
225
	public void testCreateDOI() {
226
		printTestHeader("testCreateDOI");
227

    
228
		try {
229
			// get ezid config properties
230
			String shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
231
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
232
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
233
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
234
			
235
			Session session = getTestSession();
236
			Identifier guid = new Identifier();
237
			guid.setValue(shoulder + "/testCreateDOI." + System.currentTimeMillis());
238
			InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
239
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
240
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
241
			assertEquals(guid.getValue(), pid.getValue());
242

    
243
			// check for the metadata explicitly, using ezid service
244
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
245
			ezid.login(ezidUsername, ezidPassword);
246
			HashMap<String, String> metadata = ezid.getMetadata(pid.getValue());
247
			assertNotNull(metadata);
248
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
249
			
250
		} catch (Exception e) {
251
			e.printStackTrace();
252
			fail("Unexpected error: " + e.getMessage());
253
		}
254
	}
255
	
256
	/**
257
	 * Test object publishing
258
	 */
259
	public void testPublishDOI() {
260
		printTestHeader("testPublishDOI");
261

    
262
		try {
263
			// get ezid config properties
264
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
265
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
266
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
267
			
268
			Session session = getTestSession();
269
			Identifier guid = new Identifier();
270
			guid.setValue("testPublishDOI." + System.currentTimeMillis());
271
			
272
			// use EML to test
273
			// TODO: include an ORE to really exercise it
274
			String emlFile = "test/tao.14563.1.xml";
275
			InputStream content = null;
276
			try {
277
				content = new FileInputStream(emlFile);
278
	            
279
	            // create the initial version without DOI
280
	            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
281
	            sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
282
	            Identifier pid = MNodeService.getInstance(request).create(session, guid, content, sysmeta);
283
	            assertEquals(guid.getValue(), pid.getValue());
284

    
285
	            // now publish it
286
	            Identifier publishedIdentifier = MNodeService.getInstance(request).publish(session, pid);
287
	            
288
	            // check for the metadata explicitly, using ezid service
289
	            EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
290
	            ezid.login(ezidUsername, ezidPassword);
291
	            HashMap<String, String> metadata = ezid.getMetadata(publishedIdentifier.getValue());
292
	            assertNotNull(metadata);
293
	            assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
294
	            content.close();
295
			} catch (FileNotFoundException e) {
296
				e.printStackTrace();
297
				fail(e.getMessage());
298
			} finally {
299
			    IOUtils.closeQuietly(content);
300
			}
301

    
302
			
303
		} catch (Exception e) {
304
			e.printStackTrace();
305
			fail("Unexpected error: " + e.getMessage());
306
		}
307
	}
308
}
(5-5/6)