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: 2011-07-05 11:54:57 -0700 (Tue, 05 Jul 2011) $'
|
9
|
* '$Revision: 6287 $'
|
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
|
import static org.junit.Assert.assertTrue;
|
29
|
import static org.junit.Assert.fail;
|
30
|
|
31
|
import java.util.Date;
|
32
|
|
33
|
import junit.framework.Test;
|
34
|
import junit.framework.TestSuite;
|
35
|
|
36
|
import org.dataone.client.ObjectFormatCache;
|
37
|
import org.dataone.service.exceptions.InsufficientResources;
|
38
|
import org.dataone.service.exceptions.InvalidRequest;
|
39
|
import org.dataone.service.exceptions.NotFound;
|
40
|
import org.dataone.service.exceptions.NotImplemented;
|
41
|
import org.dataone.service.exceptions.ServiceFailure;
|
42
|
import org.dataone.service.types.Checksum;
|
43
|
import org.dataone.service.types.ChecksumAlgorithm;
|
44
|
import org.dataone.service.types.Identifier;
|
45
|
import org.dataone.service.types.NodeReference;
|
46
|
import org.dataone.service.types.ObjectFormat;
|
47
|
import org.dataone.service.types.ObjectFormatIdentifier;
|
48
|
import org.dataone.service.types.ObjectFormatList;
|
49
|
import org.dataone.service.types.Session;
|
50
|
import org.dataone.service.types.Subject;
|
51
|
import org.dataone.service.types.SystemMetadata;
|
52
|
|
53
|
import edu.ucsb.nceas.MCTestCase;
|
54
|
|
55
|
/**
|
56
|
* A JUnit test for testing the dataone CNCore implementation
|
57
|
*/
|
58
|
public class CNodeServiceTest extends MCTestCase {
|
59
|
|
60
|
/**
|
61
|
* constructor for the test
|
62
|
*/
|
63
|
public CNodeServiceTest(String name)
|
64
|
{
|
65
|
super(name);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Establish a testing framework by initializing appropriate objects
|
70
|
*/
|
71
|
public void setUp() throws Exception
|
72
|
{
|
73
|
super.setUp();
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Release any objects after tests are complete
|
78
|
*/
|
79
|
public void tearDown()
|
80
|
{
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Create a suite of tests to be run together
|
85
|
*/
|
86
|
public static Test suite()
|
87
|
{
|
88
|
TestSuite suite = new TestSuite();
|
89
|
suite.addTest(new CNodeServiceTest("initialize"));
|
90
|
|
91
|
suite.addTest(new CNodeServiceTest("testListFormats"));
|
92
|
suite.addTest(new CNodeServiceTest("testGetFormat"));
|
93
|
suite.addTest(new CNodeServiceTest("testRegisterSystemMetadata"));
|
94
|
|
95
|
return suite;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* test for registering standalone system metadata
|
100
|
*/
|
101
|
public void testRegisterSystemMetadata()
|
102
|
{
|
103
|
printTestHeader("testRegisterSystemMetadata");
|
104
|
|
105
|
try {
|
106
|
Session session = new Session();
|
107
|
Subject subject = new Subject();
|
108
|
subject.setValue("cn=test,dc=dataone,dc=org");
|
109
|
session.setSubject(subject);
|
110
|
Identifier guid = new Identifier();
|
111
|
guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
|
112
|
SystemMetadata sysmeta = createSystemMetadata(guid, subject);
|
113
|
CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
|
114
|
}
|
115
|
catch(Exception e)
|
116
|
{
|
117
|
fail("Unexpected error in testDescribe: " + e.getMessage());
|
118
|
}
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Run an initial test that always passes to check that the test harness is
|
123
|
* working.
|
124
|
*/
|
125
|
public void initialize()
|
126
|
{
|
127
|
printTestHeader("initialize");
|
128
|
assertTrue(1 == 1);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* create system metadata with a specified id
|
133
|
*/
|
134
|
private SystemMetadata createSystemMetadata(Identifier id, Subject owner)
|
135
|
throws Exception
|
136
|
{
|
137
|
SystemMetadata sm = new SystemMetadata();
|
138
|
//set the id
|
139
|
sm.setIdentifier(id);
|
140
|
sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
|
141
|
//create the checksum
|
142
|
String checksumS = "test";
|
143
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
144
|
Checksum checksum = new Checksum();
|
145
|
checksum.setValue(checksumS);
|
146
|
checksum.setAlgorithm(ca);
|
147
|
sm.setChecksum(checksum);
|
148
|
//set the size
|
149
|
sm.setSize(0);
|
150
|
sm.setSubmitter(owner);
|
151
|
sm.setRightsHolder(owner);
|
152
|
sm.setDateUploaded(new Date());
|
153
|
sm.setDateSysMetadataModified(new Date());
|
154
|
NodeReference nr = new NodeReference();
|
155
|
nr.setValue("metacat");
|
156
|
sm.setOriginMemberNode(nr);
|
157
|
sm.setAuthoritativeMemberNode(nr);
|
158
|
return sm;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* test to list the object formats registered in metacat
|
163
|
*/
|
164
|
public void testListFormats() {
|
165
|
|
166
|
printTestHeader("testListFormats");
|
167
|
|
168
|
// there should be at least 59 formats in the list
|
169
|
int formatsCount = 59;
|
170
|
ObjectFormatList objectFormatList;
|
171
|
|
172
|
try {
|
173
|
objectFormatList = CNodeService.getInstance().listFormats();
|
174
|
assertTrue(objectFormatList.getTotal() >= formatsCount);
|
175
|
|
176
|
} catch (InvalidRequest e) {
|
177
|
fail("Could not get the object format list: " + e.getMessage());
|
178
|
|
179
|
} catch (ServiceFailure e) {
|
180
|
fail("Could not get the object format list: " + e.getMessage());
|
181
|
|
182
|
} catch (NotFound e) {
|
183
|
fail("Could not get the object format list: " + e.getMessage());
|
184
|
|
185
|
} catch (InsufficientResources e) {
|
186
|
fail("Could not get the object format list: " + e.getMessage());
|
187
|
|
188
|
} catch (NotImplemented e) {
|
189
|
fail("Could not get the object format list: " + e.getMessage());
|
190
|
|
191
|
}
|
192
|
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Test getting a single object format from the registered list
|
197
|
*/
|
198
|
public void testGetFormat() {
|
199
|
|
200
|
printTestHeader("testGetFormat");
|
201
|
|
202
|
String knownFormat = "text/plain";
|
203
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
204
|
fmtid.setValue(knownFormat);
|
205
|
|
206
|
try {
|
207
|
|
208
|
String result =
|
209
|
CNodeService.getInstance().getFormat(fmtid).getFmtid().getValue();
|
210
|
System.out.println("Expected result: " + knownFormat);
|
211
|
System.out.println("Found result: " + result);
|
212
|
assertTrue(result.equals(knownFormat));
|
213
|
|
214
|
} catch (NullPointerException npe) {
|
215
|
fail("The returned format was null: " + npe.getMessage());
|
216
|
|
217
|
} catch (NotFound nfe) {
|
218
|
fail("The format " + knownFormat + " was not found: " + nfe.getMessage());
|
219
|
|
220
|
} catch (InvalidRequest ire) {
|
221
|
fail("The format " + knownFormat + " was not found: " + ire.getMessage());
|
222
|
|
223
|
} catch (ServiceFailure sfe) {
|
224
|
fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
|
225
|
|
226
|
} catch (InsufficientResources ise) {
|
227
|
fail("The format " + knownFormat + " was not found: " + ise.getMessage());
|
228
|
|
229
|
} catch (NotImplemented nie) {
|
230
|
fail("The getFormat() method has not been implemented: " + nie.getMessage());
|
231
|
|
232
|
}
|
233
|
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Test getting a non-existent object format, returning NotFound
|
238
|
*/
|
239
|
public void testObjectFormatNotFoundException() {
|
240
|
|
241
|
printTestHeader("testObjectFormatNotFoundException");
|
242
|
|
243
|
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
244
|
String badFormat = "text/bad-format";
|
245
|
fmtid.setValue(badFormat);
|
246
|
|
247
|
try {
|
248
|
|
249
|
ObjectFormat objectFormat =
|
250
|
CNodeService.getInstance().getFormat(fmtid);
|
251
|
|
252
|
} catch (Exception e) {
|
253
|
|
254
|
assertTrue(e instanceof NotFound);
|
255
|
}
|
256
|
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* print a header to start each test
|
261
|
*/
|
262
|
private void printTestHeader(String testName)
|
263
|
{
|
264
|
System.out.println();
|
265
|
System.out.println("*************** " + testName + " ***************");
|
266
|
}
|
267
|
|
268
|
}
|