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: berkley $'
|
8
|
* '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
|
9
|
* '$Revision: 5027 $'
|
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 java.util.*;
|
29
|
import java.io.*;
|
30
|
|
31
|
import java.security.MessageDigest;
|
32
|
|
33
|
import edu.ucsb.nceas.MCTestCase;
|
34
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
35
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
36
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
37
|
import edu.ucsb.nceas.metacat.dataone.CrudService;
|
38
|
import junit.framework.Test;
|
39
|
import junit.framework.TestSuite;
|
40
|
|
41
|
import org.dataone.service.types.*;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
44
|
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
|
45
|
|
46
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
47
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
48
|
|
49
|
/**
|
50
|
* A JUnit test for testing the dataone CrudService class
|
51
|
*/
|
52
|
public class CrudServiceTest extends MCTestCase
|
53
|
{
|
54
|
/**
|
55
|
* consstructor for the test
|
56
|
*/
|
57
|
public CrudServiceTest(String name)
|
58
|
{
|
59
|
super(name);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Establish a testing framework by initializing appropriate objects
|
64
|
*/
|
65
|
public void setUp() throws Exception
|
66
|
{
|
67
|
super.setUp();
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Release any objects after tests are complete
|
72
|
*/
|
73
|
public void tearDown()
|
74
|
{
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Create a suite of tests to be run together
|
79
|
*/
|
80
|
public static Test suite()
|
81
|
{
|
82
|
TestSuite suite = new TestSuite();
|
83
|
suite.addTest(new CrudServiceTest("initialize"));
|
84
|
suite.addTest(new CrudServiceTest("testSingletonAccessor"));
|
85
|
suite.addTest(new CrudServiceTest("testCreateAndGet"));
|
86
|
suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
|
87
|
suite.addTest(new CrudServiceTest("testUpdate"));
|
88
|
//suite.addTest(new CrudServiceTest(""));
|
89
|
//suite.addTest(new CrudServiceTest(""));
|
90
|
return suite;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* public Identifier update(AuthToken token, Identifier guid,
|
95
|
* InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta)
|
96
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
97
|
* UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata,
|
98
|
* NotImplemented
|
99
|
*/
|
100
|
public void testUpdate()
|
101
|
{
|
102
|
printTestHeader("testUpdate");
|
103
|
try
|
104
|
{
|
105
|
CrudService cs = CrudService.getInstance();
|
106
|
AuthToken token = getToken();
|
107
|
//create a doc
|
108
|
Identifier id = createDoc(token, getTestDoc());
|
109
|
|
110
|
//get the doc and sysmetadata
|
111
|
String gotDoc = getDoc(token, id);
|
112
|
SystemMetadata sm = getSystemMetadata(token, id);
|
113
|
|
114
|
//update the doc
|
115
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
116
|
Identifier newid = new Identifier();
|
117
|
newid.setValue(generateDocumentId());
|
118
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
119
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
120
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
121
|
|
122
|
//get doc - check that it matches update
|
123
|
String newdoc = getDoc(token, newid);
|
124
|
assertTrue(gotDoc.equals(newdoc));
|
125
|
|
126
|
//get sysmeta - check that ids and other fields are updated
|
127
|
SystemMetadata newnewsm = getSystemMetadata(token, id);
|
128
|
assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
|
129
|
|
130
|
//get the new sysmeta and make sure the obsoletes field is set
|
131
|
SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
|
132
|
assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
|
133
|
}
|
134
|
catch(Exception e)
|
135
|
{
|
136
|
e.printStackTrace();
|
137
|
fail("Error in testUpdate: " + e.getMessage());
|
138
|
}
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
143
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
144
|
* InvalidRequest, NotImplemented
|
145
|
*/
|
146
|
public void testGetSystemMetadata()
|
147
|
{
|
148
|
printTestHeader("testGetSystemMetadata");
|
149
|
try
|
150
|
{
|
151
|
CrudService cs = CrudService.getInstance();
|
152
|
AuthToken token = getToken();
|
153
|
//run create
|
154
|
Identifier id = createDoc(token, getTestDoc());
|
155
|
//get the systemMetadata and make sure it is the correct object for this testDoc
|
156
|
SystemMetadata sm = getSystemMetadata(token, id);
|
157
|
assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
|
158
|
assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
|
159
|
|
160
|
try
|
161
|
{
|
162
|
Identifier fakeid = new Identifier();
|
163
|
fakeid.setValue("somethingfake.234234");
|
164
|
getSystemMetadata(token, fakeid);
|
165
|
fail("getSystemMetadata should have thrown an exception.");
|
166
|
}
|
167
|
catch(Exception e)
|
168
|
{
|
169
|
assertTrue(true);
|
170
|
}
|
171
|
}
|
172
|
catch(Exception e)
|
173
|
{
|
174
|
e.printStackTrace();
|
175
|
fail("Error testing system metadata: " + e.getMessage());
|
176
|
}
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta)
|
181
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
182
|
* InsufficientResources, InvalidSystemMetadata, NotImplemented
|
183
|
*
|
184
|
* public InputStream get(AuthToken token, Identifier guid)
|
185
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
186
|
* NotImplemented
|
187
|
*/
|
188
|
public void testCreateAndGet()
|
189
|
{
|
190
|
printTestHeader("testCreateAndGet");
|
191
|
try
|
192
|
{
|
193
|
CrudService cs = CrudService.getInstance();
|
194
|
AuthToken token = getToken();
|
195
|
//run create
|
196
|
Identifier id = createDoc(token, getTestDoc());
|
197
|
//compare the docs
|
198
|
String gotDoc = getDoc(token, id);
|
199
|
assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
|
200
|
|
201
|
try
|
202
|
{
|
203
|
Identifier fakeid = new Identifier();
|
204
|
fakeid.setValue("somethingfake.234234");
|
205
|
getDoc(token, fakeid);
|
206
|
fail("testCreateAndGet should have thrown an exception.");
|
207
|
}
|
208
|
catch(Exception e)
|
209
|
{
|
210
|
assertTrue(true);
|
211
|
}
|
212
|
}
|
213
|
catch(Exception e)
|
214
|
{
|
215
|
e.printStackTrace();
|
216
|
fail("Error in testCreate: " + e.getMessage());
|
217
|
}
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* getInstance()
|
222
|
*/
|
223
|
public void testSingletonAccessor()
|
224
|
{
|
225
|
printTestHeader("testSingletonAccessor");
|
226
|
CrudService cs = CrudService.getInstance();
|
227
|
assertNotNull(cs);
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Run an initial test that always passes to check that the test harness is
|
232
|
* working.
|
233
|
*/
|
234
|
public void initialize()
|
235
|
{
|
236
|
printTestHeader("initialize");
|
237
|
assertTrue(1 == 1);
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* return the systemMetadata object for id
|
242
|
*/
|
243
|
private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
|
244
|
throws Exception
|
245
|
{
|
246
|
CrudService cs = CrudService.getInstance();
|
247
|
return cs.getSystemMetadata(token, id);
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* get a doc from metacat using CrudService.get()
|
252
|
*/
|
253
|
private String getDoc(AuthToken token, Identifier id)
|
254
|
throws Exception
|
255
|
{
|
256
|
CrudService cs = CrudService.getInstance();
|
257
|
//try to get the same doc then compare them
|
258
|
InputStream gotDocStream = cs.get(token, id);
|
259
|
StringBuffer sb = new StringBuffer();
|
260
|
byte[] b = new byte[1024];
|
261
|
int numread = gotDocStream.read(b, 0, 1024);
|
262
|
while(numread != -1)
|
263
|
{
|
264
|
sb.append(new String(b, 0, numread));
|
265
|
numread = gotDocStream.read(b, 0, 1024);
|
266
|
}
|
267
|
return sb.toString();
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* return a test document
|
272
|
*/
|
273
|
private String getTestDoc()
|
274
|
{
|
275
|
return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* authenticate and return a token
|
280
|
*/
|
281
|
private AuthToken getToken()
|
282
|
throws Exception
|
283
|
{
|
284
|
CrudService cs = CrudService.getInstance();
|
285
|
//login and get a sessionid
|
286
|
MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
|
287
|
String username = PropertyService.getProperty("test.mcUser");
|
288
|
String password = PropertyService.getProperty("test.mcPassword");
|
289
|
String response = restClient.login(username, password);
|
290
|
String sessionid = restClient.getSessionId();
|
291
|
SessionService sessionService = SessionService.getInstance();
|
292
|
sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
|
293
|
AuthToken token = new AuthToken(sessionid);
|
294
|
return token;
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* create a doc using CrudService.create() and return its id
|
299
|
*/
|
300
|
private Identifier createDoc(AuthToken token, String testDoc) throws Exception
|
301
|
{
|
302
|
Identifier id;
|
303
|
CrudService cs = CrudService.getInstance();
|
304
|
|
305
|
id = new Identifier();
|
306
|
String docid = generateDocumentId();
|
307
|
id.setValue(docid);
|
308
|
|
309
|
//create the system metadata then run the create method
|
310
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
311
|
SystemMetadata sm = createSystemMetadata(id, testDoc);
|
312
|
//create the doc
|
313
|
cs.create(token, id, sbis, sm);
|
314
|
return id;
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* create a generic SystemMetadata object for testing
|
319
|
*/
|
320
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
|
321
|
throws Exception
|
322
|
{
|
323
|
SystemMetadata sm = new SystemMetadata();
|
324
|
//set the id
|
325
|
sm.setIdentifier(id);
|
326
|
sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
327
|
//create the checksum
|
328
|
String checksumS = checksum(testDoc);
|
329
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
330
|
Checksum checksum = new Checksum();
|
331
|
checksum.setValue(checksumS);
|
332
|
checksum.setAlgorithm(ca);
|
333
|
sm.setChecksum(checksum);
|
334
|
//set the size
|
335
|
sm.setSize(testDoc.getBytes().length);
|
336
|
//submitter
|
337
|
Principal p = new Principal();
|
338
|
p.setValue("joe");
|
339
|
sm.setSubmitter(p);
|
340
|
sm.setRightsHolder(p);
|
341
|
sm.setDateUploaded(new Date());
|
342
|
sm.setDateSysMetadataModified(new Date());
|
343
|
NodeReference nr = new NodeReference();
|
344
|
nr.setValue("metacat");
|
345
|
sm.setOriginMemberNode(nr);
|
346
|
sm.setAuthoritativeMemberNode(nr);
|
347
|
return sm;
|
348
|
}
|
349
|
|
350
|
/**
|
351
|
* print a header to start each test
|
352
|
*/
|
353
|
private void printTestHeader(String testName)
|
354
|
{
|
355
|
System.out.println();
|
356
|
System.out.println("********************************** " + testName + " **********************************");
|
357
|
}
|
358
|
|
359
|
/**
|
360
|
* produce an md5 checksum for item
|
361
|
*/
|
362
|
private String checksum(String item)
|
363
|
throws Exception
|
364
|
{
|
365
|
StringBufferInputStream fis = new StringBufferInputStream(item);
|
366
|
|
367
|
byte[] buffer = new byte[1024];
|
368
|
MessageDigest complete = MessageDigest.getInstance("MD5");
|
369
|
int numRead;
|
370
|
|
371
|
do
|
372
|
{
|
373
|
numRead = fis.read(buffer);
|
374
|
if (numRead > 0)
|
375
|
{
|
376
|
complete.update(buffer, 0, numRead);
|
377
|
}
|
378
|
} while (numRead != -1);
|
379
|
|
380
|
|
381
|
return getHex(complete.digest());
|
382
|
}
|
383
|
|
384
|
/**
|
385
|
* convert a byte array to a hex string
|
386
|
*/
|
387
|
private static String getHex( byte [] raw )
|
388
|
{
|
389
|
final String HEXES = "0123456789ABCDEF";
|
390
|
if ( raw == null ) {
|
391
|
return null;
|
392
|
}
|
393
|
final StringBuilder hex = new StringBuilder( 2 * raw.length );
|
394
|
for ( final byte b : raw ) {
|
395
|
hex.append(HEXES.charAt((b & 0xF0) >> 4))
|
396
|
.append(HEXES.charAt((b & 0x0F)));
|
397
|
}
|
398
|
return hex.toString();
|
399
|
}
|
400
|
}
|