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-03-13 17:11:53 -0700 (Wed, 13 Mar 2013) $'
9
 * '$Revision: 7516 $'
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
		return suite;
98

    
99
	}
100

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

    
110
	}
111

    
112
	/**
113
	 * Initial blank test
114
	 */
115
	public void initialize() {
116
		assertTrue(1 == 1);
117

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

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

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

    
186
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
187
			assertEquals(guid.getValue(), pid.getValue());
188

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

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

    
223
			// check for the metadata explicitly, using ezid service
224
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
225
			ezid.login(ezidUsername, ezidPassword);
226
			HashMap<String, String> metadata = ezid.getMetadata(pid.getValue());
227
			assertNotNull(metadata);
228
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
229
			
230
		} catch (Exception e) {
231
			e.printStackTrace();
232
			fail("Unexpected error: " + e.getMessage());
233
		}
234
	}
235
}
(5-5/6)