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: 2013-07-03 21:58:38 -0700 (Wed, 03 Jul 2013) $'
9
 * '$Revision: 7849 $'
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.dataone.client.ObjectFormatCache;
39
import org.dataone.configuration.Settings;
40
import org.dataone.service.types.v1.Identifier;
41
import org.dataone.service.types.v1.Session;
42
import org.dataone.service.types.v1.Subject;
43
import org.dataone.service.types.v1.SystemMetadata;
44
import org.junit.After;
45
import org.junit.Before;
46

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

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

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

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

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

    
89
		TestSuite suite = new TestSuite();
90
		suite.addTest(new RegisterDOITest("initialize"));
91

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

    
100
		return suite;
101

    
102
	}
103

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

    
113
	}
114

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

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

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

    
177
			// add the actual object for the newly-minted DOI
178
			SystemMetadata sysmeta = null;
179
			InputStream object = null;
180
			if (inputStream != null) {
181
				sysmeta = createSystemMetadata(guid, session.getSubject(), null);
182
				object = inputStream;
183
		        sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
184
			} else {
185
				object = new ByteArrayInputStream("test".getBytes("UTF-8"));
186
				sysmeta = createSystemMetadata(guid, session.getSubject(), object);
187
			}
188

    
189
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
190
			assertEquals(guid.getValue(), pid.getValue());
191

    
192
			// check for the metadata for title element
193
			metadata = ezid.getMetadata(pid.getValue());
194
			assertNotNull(metadata);
195
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
196
			
197
			System.out.println("tested with DOI: " + pid.getValue());
198
			
199
		} catch (Exception e) {
200
			e.printStackTrace();
201
			fail("Unexpected error: " + e.getMessage());
202
		}
203
	}
204
	
205
	/**
206
	 * Test object creation
207
	 */
208
	public void testCreateDOI() {
209
		printTestHeader("testCreateDOI");
210

    
211
		try {
212
			// get ezid config properties
213
			String shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
214
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
215
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
216
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
217
			
218
			Session session = getTestSession();
219
			Identifier guid = new Identifier();
220
			guid.setValue(shoulder + "/testCreateDOI." + System.currentTimeMillis());
221
			InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
222
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
223
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
224
			assertEquals(guid.getValue(), pid.getValue());
225

    
226
			// check for the metadata explicitly, using ezid service
227
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
228
			ezid.login(ezidUsername, ezidPassword);
229
			HashMap<String, String> metadata = ezid.getMetadata(pid.getValue());
230
			assertNotNull(metadata);
231
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
232
			
233
		} catch (Exception e) {
234
			e.printStackTrace();
235
			fail("Unexpected error: " + e.getMessage());
236
		}
237
	}
238
	
239
	/**
240
	 * Test object publishing
241
	 */
242
	public void testPublishDOI() {
243
		printTestHeader("testPublishDOI");
244

    
245
		try {
246
			// get ezid config properties
247
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
248
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
249
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
250
			
251
			Session session = getTestSession();
252
			Identifier guid = new Identifier();
253
			guid.setValue("testPublishDOI." + System.currentTimeMillis());
254
			
255
			// use EML to test
256
			// TODO: include an ORE to really exercise it
257
			String emlFile = "test/tao.14563.1.xml";
258
			InputStream content = null;
259
			try {
260
				content = new FileInputStream(emlFile);
261
			} catch (FileNotFoundException e) {
262
				e.printStackTrace();
263
				fail(e.getMessage());
264
			}
265
			
266
			// create the initial version without DOI
267
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), null);
268
	        sysmeta.setFormatId(ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0").getFormatId());
269
			Identifier pid = MNodeService.getInstance(request).create(session, guid, content, sysmeta);
270
			assertEquals(guid.getValue(), pid.getValue());
271

    
272
			// now publish it
273
			Identifier publishedIdentifier = MNodeService.getInstance(request).publish(session, pid);
274
			
275
			// check for the metadata explicitly, using ezid service
276
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
277
			ezid.login(ezidUsername, ezidPassword);
278
			HashMap<String, String> metadata = ezid.getMetadata(publishedIdentifier.getValue());
279
			assertNotNull(metadata);
280
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
281
			
282
		} catch (Exception e) {
283
			e.printStackTrace();
284
			fail("Unexpected error: " + e.getMessage());
285
		}
286
	}
287
}
(5-5/6)