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: jones $'
8
 *     '$Date: 2013-10-09 23:52:11 -0700 (Wed, 09 Oct 2013) $'
9
 * '$Revision: 8304 $'
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.InputStream;
33
import java.util.HashMap;
34

    
35
import junit.framework.Test;
36
import junit.framework.TestSuite;
37

    
38
import org.apache.commons.io.IOUtils;
39
import org.dataone.client.ObjectFormatCache;
40
import org.dataone.configuration.Settings;
41
import org.dataone.service.types.v1.Identifier;
42
import org.dataone.service.types.v1.Session;
43
import org.dataone.service.types.v1.Subject;
44
import org.dataone.service.types.v1.SystemMetadata;
45
import org.junit.After;
46
import org.junit.Before;
47

    
48
import edu.ucsb.nceas.ezid.EZIDService;
49
import edu.ucsb.nceas.ezid.profile.DataCiteProfile;
50
import edu.ucsb.nceas.ezid.profile.InternalProfile;
51
import edu.ucsb.nceas.metacat.properties.PropertyService;
52

    
53
/**
54
 * A JUnit test to exercise the DOI registration for content added
55
 * via the DataONE MN API
56
 * 
57
 * @author leinfelder
58
 *
59
 */
60
public class RegisterDOITest extends D1NodeServiceTest {
61

    
62
	
63
	
64
	/**
65
	 * Set up the test fixtures
66
	 * 
67
	 * @throws Exception
68
	 */
69
	@Before
70
	public void setUp() throws Exception {
71
		super.setUp();
72
		// set up the configuration for d1client
73
		Settings.getConfiguration().setProperty("D1Client.cnClassName",
74
				MockCNode.class.getName());
75
	}
76

    
77
	/**
78
	 * Remove the test fixtures
79
	 */
80
	@After
81
	public void tearDown() {
82
	}
83

    
84
	/**
85
	 * Build the test suite
86
	 * 
87
	 * @return
88
	 */
89
	public static Test suite() {
90

    
91
		TestSuite suite = new TestSuite();
92
		suite.addTest(new RegisterDOITest("initialize"));
93

    
94
		// DOI registration test
95
		suite.addTest(new RegisterDOITest("testCreateDOI"));
96
		suite.addTest(new RegisterDOITest("testMintAndCreateDOI"));
97
		suite.addTest(new RegisterDOITest("testMintAndCreateForEML"));
98
		
99
		// publish
100
		suite.addTest(new RegisterDOITest("testPublishDOI"));
101

    
102
		return suite;
103

    
104
	}
105

    
106
	/**
107
	 * Constructor for the tests
108
	 * 
109
	 * @param name
110
	 *            - the name of the test
111
	 */
112
	public RegisterDOITest(String name) {
113
		super(name);
114

    
115
	}
116

    
117
	/**
118
	 * Initial blank test
119
	 */
120
	public void initialize() {
121
		assertTrue(1 == 1);
122

    
123
	}
124
	
125
	/**
126
	 * constructs a "fake" session with a test subject
127
	 * @return
128
	 */
129
	@Override
130
	public Session getTestSession() throws Exception {
131
		Session session = new Session();
132
        Subject subject = new Subject();
133
        subject.setValue("CN=Benjamin Leinfelder A515,O=University of Chicago,C=US,DC=cilogon,DC=org");
134
        session.setSubject(subject);
135
        return session;
136
	}
137
  
138
	public void testMintAndCreateDOI() {
139
		printTestHeader("testMintAndCreateDOI");
140
		testMintAndCreateDOI(null);
141
	}
142
  	
143
	public void testMintAndCreateForEML() {
144
		printTestHeader("testMintAndCreateForEML");
145
		String emlFile = "test/tao.14563.1.xml";
146
		InputStream content = null;
147
		try {
148
		    content = new FileInputStream(emlFile);
149
		    testMintAndCreateDOI(content);
150
		    content.close(); 
151
		} catch (FileNotFoundException e) {
152
			e.printStackTrace();
153
			fail(e.getMessage());
154
		} finally {
155
		    IOUtils.closeQuietly(content);
156
		}
157
	}
158
	
159
	/**
160
	 * Test object creation
161
	 */
162
	private void testMintAndCreateDOI(InputStream inputStream) {
163
		printTestHeader("testMintAndCreateDOI - common");
164

    
165
		try {
166
			// get ezid config properties
167
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
168
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
169
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
170
			
171
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
172
			ezid.login(ezidUsername, ezidPassword);
173
			
174
			// Mint a DOI
175
			Session session = getTestSession();
176
			Identifier guid = MNodeService.getInstance(request).generateIdentifier(session, "DOI", null);
177
			
178
			// check that EZID knows about it
179
			HashMap<String, String> metadata = ezid.getMetadata(guid.getValue());
180
			assertNotNull(metadata);
181

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

    
196
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
197
			assertEquals(guid.getValue(), pid.getValue());
198

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

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

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

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

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

    
298
			
299
		} catch (Exception e) {
300
			e.printStackTrace();
301
			fail("Unexpected error: " + e.getMessage());
302
		}
303
	}
304
}
(5-5/6)