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
|
System.out.println("CST: creating document");
|
109
|
Identifier id = createDoc(token, getTestDoc());
|
110
|
System.out.println("CST: document created. Id: " + id.getValue());
|
111
|
|
112
|
//get the doc and sysmetadata
|
113
|
System.out.println("CST: getting document with id " + id.getValue());
|
114
|
String gotDoc = getDoc(token, id);
|
115
|
System.out.println("CST: getting sysmeta for doc with id " + id.getValue());
|
116
|
SystemMetadata sm = getSystemMetadata(token, id);
|
117
|
|
118
|
//update the doc
|
119
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
120
|
System.out.println("CST: Changed test doc to " + gotDoc);
|
121
|
Identifier newid = new Identifier();
|
122
|
newid.setValue(generateDocumentId());
|
123
|
System.out.println("CST: Generated newid " + newid.getValue());
|
124
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
125
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
126
|
System.out.println("CST: Created system metadata for new doc...updating newid: " + newid.getValue() + " obsId: " + id.getValue());
|
127
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
128
|
System.out.println("CST: Called update. Returned id " + updatedid.getValue());
|
129
|
|
130
|
//get doc - check that it matches update
|
131
|
String newdoc = getDoc(token, newid);
|
132
|
assertTrue(gotDoc.equals(newdoc));
|
133
|
|
134
|
//get sysmeta - check that ids and other fields are updated
|
135
|
System.out.println("CST: confirming obsoletedBy fields for sm doc with id " + id.getValue());
|
136
|
SystemMetadata newnewsm = getSystemMetadata(token, id);
|
137
|
List obsoletedByList = newnewsm.getObsoletedByList();
|
138
|
assertTrue(obsoletedByList.size() == 1);
|
139
|
String obsBy = ((Identifier)obsoletedByList.get(0)).getValue();
|
140
|
System.out.println("CST: obsBy: " + obsBy);
|
141
|
System.out.println("CST: id: " + id.getValue());
|
142
|
assertTrue(obsBy.equals(newid.getValue()));
|
143
|
}
|
144
|
catch(Exception e)
|
145
|
{
|
146
|
e.printStackTrace();
|
147
|
fail("Error in testUpdate: " + e.getMessage());
|
148
|
}
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
153
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
154
|
* InvalidRequest, NotImplemented
|
155
|
*/
|
156
|
public void testGetSystemMetadata()
|
157
|
{
|
158
|
printTestHeader("testGetSystemMetadata");
|
159
|
try
|
160
|
{
|
161
|
CrudService cs = CrudService.getInstance();
|
162
|
AuthToken token = getToken();
|
163
|
//run create
|
164
|
Identifier id = createDoc(token, getTestDoc());
|
165
|
//get the systemMetadata and make sure it is the correct object for this testDoc
|
166
|
SystemMetadata sm = getSystemMetadata(token, id);
|
167
|
assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
|
168
|
assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
|
169
|
|
170
|
try
|
171
|
{
|
172
|
Identifier fakeid = new Identifier();
|
173
|
fakeid.setValue("somethingfake.234234");
|
174
|
getSystemMetadata(token, fakeid);
|
175
|
fail("getSystemMetadata should have thrown an exception.");
|
176
|
}
|
177
|
catch(Exception e)
|
178
|
{
|
179
|
assertTrue(true);
|
180
|
}
|
181
|
}
|
182
|
catch(Exception e)
|
183
|
{
|
184
|
e.printStackTrace();
|
185
|
fail("Error testing system metadata: " + e.getMessage());
|
186
|
}
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta)
|
191
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
192
|
* InsufficientResources, InvalidSystemMetadata, NotImplemented
|
193
|
*
|
194
|
* public InputStream get(AuthToken token, Identifier guid)
|
195
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
196
|
* NotImplemented
|
197
|
*/
|
198
|
public void testCreateAndGet()
|
199
|
{
|
200
|
printTestHeader("testCreateAndGet");
|
201
|
try
|
202
|
{
|
203
|
CrudService cs = CrudService.getInstance();
|
204
|
AuthToken token = getToken();
|
205
|
//run create
|
206
|
Identifier id = createDoc(token, getTestDoc());
|
207
|
//run get
|
208
|
String gotDoc = getDoc(token, id);
|
209
|
System.out.println("got doc: " + gotDoc);
|
210
|
//compare the docs
|
211
|
assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
|
212
|
|
213
|
try
|
214
|
{
|
215
|
Identifier fakeid = new Identifier();
|
216
|
fakeid.setValue("somethingfake.234234");
|
217
|
getDoc(token, fakeid);
|
218
|
fail("testCreateAndGet should have thrown an exception.");
|
219
|
}
|
220
|
catch(Exception e)
|
221
|
{
|
222
|
assertTrue(true);
|
223
|
}
|
224
|
}
|
225
|
catch(Exception e)
|
226
|
{
|
227
|
e.printStackTrace();
|
228
|
fail("Error in testCreate: " + e.getMessage());
|
229
|
}
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* getInstance()
|
234
|
*/
|
235
|
public void testSingletonAccessor()
|
236
|
{
|
237
|
printTestHeader("testSingletonAccessor");
|
238
|
CrudService cs = CrudService.getInstance();
|
239
|
assertNotNull(cs);
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Run an initial test that always passes to check that the test harness is
|
244
|
* working.
|
245
|
*/
|
246
|
public void initialize()
|
247
|
{
|
248
|
printTestHeader("initialize");
|
249
|
assertTrue(1 == 1);
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* return the systemMetadata object for id
|
254
|
*/
|
255
|
private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
|
256
|
throws Exception
|
257
|
{
|
258
|
System.out.println("getting system metadata for id: " + id.getValue());
|
259
|
CrudService cs = CrudService.getInstance();
|
260
|
return cs.getSystemMetadata(token, id);
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* get a doc from metacat using CrudService.get()
|
265
|
*/
|
266
|
private String getDoc(AuthToken token, Identifier id)
|
267
|
throws Exception
|
268
|
{
|
269
|
System.out.println("getting doc for id " + id.getValue());
|
270
|
CrudService cs = CrudService.getInstance();
|
271
|
//try to get the same doc then compare them
|
272
|
InputStream gotDocStream = cs.get(token, id);
|
273
|
StringBuffer sb = new StringBuffer();
|
274
|
byte[] b = new byte[1024];
|
275
|
int numread = gotDocStream.read(b, 0, 1024);
|
276
|
while(numread != -1)
|
277
|
{
|
278
|
sb.append(new String(b, 0, numread));
|
279
|
numread = gotDocStream.read(b, 0, 1024);
|
280
|
}
|
281
|
System.out.println("returning doc for id " + id.getValue());
|
282
|
return sb.toString();
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* return a test document
|
287
|
*/
|
288
|
private String getTestDoc()
|
289
|
{
|
290
|
return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* authenticate and return a token
|
295
|
*/
|
296
|
private AuthToken getToken()
|
297
|
throws Exception
|
298
|
{
|
299
|
CrudService cs = CrudService.getInstance();
|
300
|
//login and get a sessionid
|
301
|
System.out.println("creating MetacatRestClient with url " + cs.getContextUrl());
|
302
|
MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
|
303
|
String username = PropertyService.getProperty("test.mcUser");
|
304
|
String password = PropertyService.getProperty("test.mcPassword");
|
305
|
System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
|
306
|
String response = restClient.login(username, password);
|
307
|
//System.out.println("response to login: " + response);
|
308
|
String sessionid = restClient.getSessionId();
|
309
|
SessionService sessionService = SessionService.getInstance();
|
310
|
sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
|
311
|
System.out.println("sessionid: " + sessionid);
|
312
|
AuthToken token = new AuthToken(sessionid);
|
313
|
return token;
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* create a doc using CrudService.create() and return its id
|
318
|
*/
|
319
|
private Identifier createDoc(AuthToken token, String testDoc) throws Exception
|
320
|
{
|
321
|
Identifier id;
|
322
|
CrudService cs = CrudService.getInstance();
|
323
|
|
324
|
id = new Identifier();
|
325
|
String docid = generateDocumentId();
|
326
|
id.setValue(docid);
|
327
|
System.out.println("creating doc with id " + id.getValue());
|
328
|
|
329
|
//create the system metadata then run the create method
|
330
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
331
|
SystemMetadata sm = createSystemMetadata(id, testDoc);
|
332
|
//create the doc
|
333
|
cs.create(token, id, sbis, sm);
|
334
|
System.out.println("document with id " + id.getValue() + " created.");
|
335
|
return id;
|
336
|
}
|
337
|
|
338
|
/**
|
339
|
* create a generic SystemMetadata object for testing
|
340
|
*/
|
341
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
|
342
|
throws Exception
|
343
|
{
|
344
|
SystemMetadata sm = new SystemMetadata();
|
345
|
//set the id
|
346
|
System.out.println("creating system metadata object for document with id " + id.getValue());
|
347
|
sm.setIdentifier(id);
|
348
|
System.out.println("sm id is " + id.getValue());
|
349
|
sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
350
|
System.out.println("sm objectformat: " + sm.getObjectFormat());
|
351
|
//create the checksum
|
352
|
String checksumS = checksum(testDoc);
|
353
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
354
|
Checksum checksum = new Checksum();
|
355
|
checksum.setValue(checksumS);
|
356
|
checksum.setAlgorithm(ca);
|
357
|
sm.setChecksum(checksum);
|
358
|
System.out.println("sm checksum is " + checksumS);
|
359
|
//set the size
|
360
|
sm.setSize(testDoc.getBytes().length);
|
361
|
System.out.println("sm size: " + testDoc.getBytes().length);
|
362
|
//submitter
|
363
|
Principal p = new Principal();
|
364
|
p.setValue("joe");
|
365
|
sm.setSubmitter(p);
|
366
|
sm.setRightsHolder(p);
|
367
|
sm.setDateUploaded(new Date());
|
368
|
sm.setDateSysMetadataModified(new Date());
|
369
|
NodeReference nr = new NodeReference();
|
370
|
nr.setValue("metacat");
|
371
|
sm.setOriginMemberNode(nr);
|
372
|
sm.setAuthoritativeMemberNode(nr);
|
373
|
return sm;
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* print a header to start each test
|
378
|
*/
|
379
|
private void printTestHeader(String testName)
|
380
|
{
|
381
|
System.out.println();
|
382
|
System.out.println("********************************** " + testName + " **********************************");
|
383
|
}
|
384
|
|
385
|
/**
|
386
|
* produce an md5 checksum for item
|
387
|
*/
|
388
|
private String checksum(String item)
|
389
|
throws Exception
|
390
|
{
|
391
|
StringBufferInputStream fis = new StringBufferInputStream(item);
|
392
|
|
393
|
byte[] buffer = new byte[1024];
|
394
|
MessageDigest complete = MessageDigest.getInstance("MD5");
|
395
|
int numRead;
|
396
|
|
397
|
do
|
398
|
{
|
399
|
numRead = fis.read(buffer);
|
400
|
if (numRead > 0)
|
401
|
{
|
402
|
complete.update(buffer, 0, numRead);
|
403
|
}
|
404
|
} while (numRead != -1);
|
405
|
|
406
|
|
407
|
return getHex(complete.digest());
|
408
|
}
|
409
|
|
410
|
/**
|
411
|
* convert a byte array to a hex string
|
412
|
*/
|
413
|
private static String getHex( byte [] raw )
|
414
|
{
|
415
|
final String HEXES = "0123456789ABCDEF";
|
416
|
if ( raw == null ) {
|
417
|
return null;
|
418
|
}
|
419
|
final StringBuilder hex = new StringBuilder( 2 * raw.length );
|
420
|
for ( final byte b : raw ) {
|
421
|
hex.append(HEXES.charAt((b & 0xF0) >> 4))
|
422
|
.append(HEXES.charAt((b & 0x0F)));
|
423
|
}
|
424
|
return hex.toString();
|
425
|
}
|
426
|
}
|