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-12 16:45:01 -0700 (Tue, 12 Mar 2013) $'
9
 * '$Revision: 7513 $'
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
}
(5-5/5)