Project

General

Profile

« Previous | Next » 

Revision 7510

Correctly mint and register DOIs in teh MN API implementation. Add tests to exercise minting and creating. http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5513

View differences:

test/edu/ucsb/nceas/metacat/dataone/RegisterDOITest.java
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$'
8
 *     '$Date$'
9
 * '$Revision$'
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.InputStream;
31
import java.util.HashMap;
32

  
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35

  
36
import org.dataone.configuration.Settings;
37
import org.dataone.service.types.v1.Identifier;
38
import org.dataone.service.types.v1.Session;
39
import org.dataone.service.types.v1.SystemMetadata;
40
import org.junit.After;
41
import org.junit.Before;
42

  
43
import edu.ucsb.nceas.ezid.EZIDService;
44
import edu.ucsb.nceas.ezid.profile.DataCiteProfile;
45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46

  
47
/**
48
 * A JUnit test to exercise the DOI registration for content added
49
 * via the DataONE MN API
50
 * 
51
 * @author leinfelder
52
 *
53
 */
54
public class RegisterDOITest extends D1NodeServiceTest {
55

  
56
  /**
57
   * Set up the test fixtures
58
   * 
59
   * @throws Exception
60
   */
61
  @Before
62
  public void setUp() throws Exception {
63
    super.setUp();
64
    // set up the configuration for d1client
65
    Settings.getConfiguration().setProperty("D1Client.cnClassName", MockCNode.class.getName());
66
  }
67

  
68
  /**
69
   * Remove the test fixtures
70
   */
71
  @After
72
  public void tearDown() {
73
  }
74
  
75
  /**
76
   * Build the test suite
77
   * @return
78
   */
79
  public static Test suite() {
80
    
81
    TestSuite suite = new TestSuite();
82
    suite.addTest(new RegisterDOITest("initialize"));
83
    
84
    // DOI registration test
85
    //suite.addTest(new RegisterDOITest("testCreateDOI"));
86
    suite.addTest(new RegisterDOITest("testMintAndCreateDOI"));
87

  
88
    return suite;
89
    
90
  }
91
  
92
  /**
93
   * Constructor for the tests
94
   * 
95
   * @param name - the name of the test
96
   */
97
  public RegisterDOITest(String name) {
98
    super(name);
99
    
100
  }
101

  
102
  /**
103
   * Initial blank test
104
   */
105
  public void initialize() {
106
    assertTrue(1 == 1);
107
    
108
  }
109
  
110
  	/**
111
	 * Test object creation
112
	 */
113
	public void testMintAndCreateDOI() {
114
		printTestHeader("testMintAndCreateDOI");
115

  
116
		try {
117
			// get ezid config properties
118
			String ezidUsername = PropertyService.getProperty("guid.ezid.username");
119
			String ezidPassword = PropertyService.getProperty("guid.ezid.password");
120
			String ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
121
			
122
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
123
			ezid.login(ezidUsername, ezidPassword);
124
			
125
			// Mint a DOI
126
			Session session = getTestSession();
127
			Identifier guid = MNodeService.getInstance(request).generateIdentifier(session, "DOI", null);
128
			
129
			// check that EZID knows about it
130
			HashMap<String, String> metadata = ezid.getMetadata(guid.getValue());
131
			assertNotNull(metadata);
132

  
133
			// add the actual object for the newly-minted DOI
134
			InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
135
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
136
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
137
			assertEquals(guid.getValue(), pid.getValue());
138

  
139
			// check for the metadata for title element
140
			metadata = ezid.getMetadata(pid.getValue());
141
			assertNotNull(metadata);
142
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
143
			
144
		} catch (Exception e) {
145
			e.printStackTrace();
146
			fail("Unexpected error: " + e.getMessage());
147
		}
148
	}
149
	
150
	/**
151
	 * Test object creation
152
	 */
153
	public void testCreateDOI() {
154
		printTestHeader("testCreateDOI");
155

  
156
		try {
157
			// get ezid config properties
158
			String shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
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
			Session session = getTestSession();
164
			Identifier guid = new Identifier();
165
			guid.setValue(shoulder + "/testCreateDOI." + System.currentTimeMillis());
166
			InputStream object = new ByteArrayInputStream( "test".getBytes("UTF-8"));
167
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
168
			Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta);
169
			assertEquals(guid.getValue(), pid.getValue());
170

  
171
			// check for the metadata explicitly, using ezid service
172
			EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
173
			ezid.login(ezidUsername, ezidPassword);
174
			HashMap<String, String> metadata = ezid.getMetadata(pid.getValue());
175
			assertNotNull(metadata);
176
			assertTrue(metadata.containsKey(DataCiteProfile.TITLE.toString()));
177
			
178
		} catch (Exception e) {
179
			e.printStackTrace();
180
			fail("Unexpected error: " + e.getMessage());
181
		}
182
	}
183
}
0 184

  
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
28 28
import java.io.InputStream;
29 29
import java.math.BigInteger;
30 30
import java.security.NoSuchAlgorithmException;
31
import java.text.SimpleDateFormat;
31 32
import java.util.ArrayList;
32 33
import java.util.Calendar;
33 34
import java.util.Date;
......
96 97
import org.dataone.service.types.v1_1.QueryEngineList;
97 98
import org.dataone.service.types.v1_1.QueryField;
98 99
import org.dataone.service.util.Constants;
99
import org.dataone.service.util.DateTimeMarshaller;
100 100

  
101 101
import edu.ucsb.nceas.ezid.EZIDException;
102 102
import edu.ucsb.nceas.ezid.EZIDService;
103 103
import edu.ucsb.nceas.ezid.profile.DataCiteProfile;
104
import edu.ucsb.nceas.ezid.profile.ErcMissingValueCode;
104 105
import edu.ucsb.nceas.ezid.profile.InternalProfile;
105 106
import edu.ucsb.nceas.metacat.DBQuery;
106 107
import edu.ucsb.nceas.metacat.EventLog;
......
413 414
        try {
414 415
			registerDOI(sysmeta);
415 416
		} catch (EZIDException e) {
416
            throw new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
417
			ServiceFailure sf = new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
418
			sf.initCause(e);
419
            throw sf;
417 420
		}
418 421
        
419 422
        // return 
......
1268 1271
				throw new InvalidRequest("2193", "DOI scheme is not enabled at this node.");
1269 1272
			}
1270 1273
			
1271
			// do not enter metadata about this identifier since we do not know anything about it
1272
			HashMap<String, String> metadata = null;
1274
			// add only the minimal metadata required for this DOI
1275
			HashMap<String, String> metadata = new HashMap<String, String>();
1276
			metadata.put(DataCiteProfile.TITLE.toString(), ErcMissingValueCode.UNKNOWN.toString());
1277
			metadata.put(DataCiteProfile.CREATOR.toString(), ErcMissingValueCode.UNKNOWN.toString());
1278
			metadata.put(DataCiteProfile.PUBLISHER.toString(), ErcMissingValueCode.UNKNOWN.toString());
1279
			metadata.put(DataCiteProfile.PUBLICATION_YEAR.toString(), ErcMissingValueCode.UNKNOWN.toString());
1273 1280
			try {
1274 1281
				// call the EZID service
1275 1282
				String ezidServiceBaseUrl = null;
......
1556 1563
				
1557 1564
				// check for existing metadata
1558 1565
				boolean create = false;
1559
				metadata = ezid.getMetadata(identifier);
1566
				try {
1567
					metadata = ezid.getMetadata(identifier);
1568
				} catch (EZIDException e) {
1569
					// expected much of the time
1570
					logMetacat.warn("No metadata found for given identifier: " + e.getMessage());
1571
				}
1560 1572
				if (metadata == null) {
1561 1573
					create = true;
1562
					metadata = new HashMap<String, String>();
1563 1574
				}
1575
				// confuses the API if we send the original metadata that it gave us, so start from scratch
1576
				metadata = new HashMap<String, String>();
1564 1577
				
1565
				// TODO: set values in the metadata with better values
1578
				// TODO: set title and creator to better values
1566 1579
				Node node = getCapabilities();
1567
				metadata.put(DataCiteProfile.TITLE.toString(), "TBD");
1580
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
1581
				String year = sdf .format(sysMeta.getDateUploaded());
1582
				metadata.put(DataCiteProfile.TITLE.toString(), ErcMissingValueCode.UNKNOWN.toString());
1568 1583
				metadata.put(DataCiteProfile.CREATOR.toString(), sysMeta.getRightsHolder().getValue());
1569 1584
				metadata.put(DataCiteProfile.PUBLISHER.toString(), node.getName());
1570
				metadata.put(DataCiteProfile.PUBLICATION_YEAR.toString(), DateTimeMarshaller.serializeDateToUTC(sysMeta.getDateUploaded()));
1585
				metadata.put(DataCiteProfile.PUBLICATION_YEAR.toString(), year);
1571 1586
				metadata.put(InternalProfile.TARGET.toString(), node.getBaseURL() + "/v1/object/" + identifier);
1572
				metadata.put(InternalProfile.PROFILE.toString(), "datacite");
1573 1587
	
1574 1588
				// set using the API
1575 1589
				if (create) {

Also available in: Unified diff