1 |
5335
|
berkley
|
/**
|
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 |
5337
|
berkley
|
import java.security.MessageDigest;
|
32 |
|
|
|
33 |
5335
|
berkley
|
import edu.ucsb.nceas.MCTestCase;
|
34 |
5362
|
berkley
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
35 |
5335
|
berkley
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
36 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
37 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
38 |
|
|
import edu.ucsb.nceas.metacat.dataone.CrudService;
|
39 |
|
|
import junit.framework.Test;
|
40 |
|
|
import junit.framework.TestSuite;
|
41 |
|
|
|
42 |
5389
|
berkley
|
import org.apache.log.LogEvent;
|
43 |
5359
|
berkley
|
import org.dataone.service.exceptions.InvalidRequest;
|
44 |
|
|
import org.dataone.service.exceptions.InvalidToken;
|
45 |
|
|
import org.dataone.service.exceptions.NotAuthorized;
|
46 |
|
|
import org.dataone.service.exceptions.NotImplemented;
|
47 |
|
|
import org.dataone.service.exceptions.ServiceFailure;
|
48 |
5337
|
berkley
|
import org.dataone.service.types.*;
|
49 |
|
|
|
50 |
|
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
51 |
|
|
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
|
52 |
|
|
|
53 |
|
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
54 |
|
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
55 |
|
|
|
56 |
5335
|
berkley
|
/**
|
57 |
|
|
* A JUnit test for testing the dataone CrudService class
|
58 |
|
|
*/
|
59 |
|
|
public class CrudServiceTest extends MCTestCase
|
60 |
5340
|
berkley
|
{
|
61 |
|
|
/**
|
62 |
|
|
* consstructor for the test
|
63 |
|
|
*/
|
64 |
|
|
public CrudServiceTest(String name)
|
65 |
|
|
{
|
66 |
|
|
super(name);
|
67 |
|
|
}
|
68 |
5335
|
berkley
|
|
69 |
5340
|
berkley
|
/**
|
70 |
5335
|
berkley
|
* Establish a testing framework by initializing appropriate objects
|
71 |
|
|
*/
|
72 |
|
|
public void setUp() throws Exception
|
73 |
|
|
{
|
74 |
|
|
super.setUp();
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Release any objects after tests are complete
|
79 |
|
|
*/
|
80 |
|
|
public void tearDown()
|
81 |
|
|
{
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
* Create a suite of tests to be run together
|
86 |
|
|
*/
|
87 |
|
|
public static Test suite()
|
88 |
|
|
{
|
89 |
|
|
TestSuite suite = new TestSuite();
|
90 |
5384
|
berkley
|
suite.addTest(new CrudServiceTest("initialize"));
|
91 |
5337
|
berkley
|
suite.addTest(new CrudServiceTest("testSingletonAccessor"));
|
92 |
5346
|
berkley
|
suite.addTest(new CrudServiceTest("testCreateAndGet"));
|
93 |
5352
|
berkley
|
suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
|
94 |
5350
|
berkley
|
suite.addTest(new CrudServiceTest("testUpdate"));
|
95 |
5359
|
berkley
|
suite.addTest(new CrudServiceTest("testListObjects"));
|
96 |
5384
|
berkley
|
suite.addTest(new CrudServiceTest("testAccessControl"));
|
97 |
5377
|
berkley
|
suite.addTest(new CrudServiceTest("testGenerateMissingSystemMetadata"));
|
98 |
5384
|
berkley
|
suite.addTest(new CrudServiceTest("testGetLogRecords"));
|
99 |
5387
|
berkley
|
suite.addTest(new CrudServiceTest("testChecksumError"));
|
100 |
5335
|
berkley
|
return suite;
|
101 |
|
|
}
|
102 |
5337
|
berkley
|
|
103 |
|
|
/**
|
104 |
5387
|
berkley
|
* test for an error where the checksum algorithm gets appended onto the checksum.
|
105 |
|
|
*/
|
106 |
|
|
public void testChecksumError()
|
107 |
|
|
{
|
108 |
|
|
printTestHeader("testChecksumError");
|
109 |
|
|
try
|
110 |
|
|
{
|
111 |
|
|
Date d1 = new Date();
|
112 |
|
|
CrudService cs = CrudService.getInstance();
|
113 |
|
|
AuthToken token = getToken();
|
114 |
|
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
115 |
|
|
//create docs at different times
|
116 |
|
|
|
117 |
|
|
Identifier id = new Identifier();
|
118 |
|
|
String docid = generateDocumentId();
|
119 |
|
|
id.setValue(docid);
|
120 |
|
|
|
121 |
|
|
//create the system metadata then run the create method
|
122 |
|
|
String testDoc = getTestDoc();
|
123 |
|
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
124 |
|
|
SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
|
125 |
|
|
assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
|
126 |
|
|
//create the doc
|
127 |
|
|
Identifier idC = cs.create(token, id, sbis, sm);
|
128 |
|
|
assertTrue(idC.getValue().equals(id.getValue()));
|
129 |
|
|
SystemMetadata smC = getSystemMetadata(token, idC);
|
130 |
|
|
assertFalse(smC.getChecksum().getValue().startsWith("MD5"));
|
131 |
|
|
|
132 |
|
|
Date d2 = new Date();
|
133 |
|
|
ObjectList ol = cs.listObjects(token, d1, d2, of1);
|
134 |
|
|
assertTrue(ol.sizeObjectInfoList() == 1);
|
135 |
|
|
ObjectInfo oi = ol.getObjectInfo(0);
|
136 |
|
|
//this will fail if the error state exists.
|
137 |
|
|
assertFalse(oi.getChecksum().getValue().startsWith("MD5"));
|
138 |
|
|
}
|
139 |
|
|
catch(Exception e)
|
140 |
|
|
{
|
141 |
|
|
fail("Unexpected exception: " + e.getMessage());
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
5384
|
berkley
|
* test CrudService.getLogRecords
|
148 |
|
|
*/
|
149 |
|
|
public void testGetLogRecords()
|
150 |
|
|
{
|
151 |
|
|
printTestHeader("testGetLogRecords");
|
152 |
|
|
try
|
153 |
|
|
{
|
154 |
|
|
CrudService cs = CrudService.getInstance();
|
155 |
|
|
AuthToken token = getToken();
|
156 |
|
|
Date fromDate = new Date();
|
157 |
|
|
Identifier id = createDoc(token, getTestDoc());
|
158 |
|
|
Date toDate = new Date();
|
159 |
5389
|
berkley
|
Log lrs = cs.getLogRecords(token, fromDate, toDate);
|
160 |
|
|
assertNotNull(lrs);
|
161 |
|
|
assertTrue(lrs.sizeLogEntryList() == 1);
|
162 |
|
|
LogEntry lrLogEvent = lrs.getLogEntry(0);
|
163 |
|
|
assertTrue(lrLogEvent.getEvent().name().equals("CREATE"));
|
164 |
5384
|
berkley
|
}
|
165 |
|
|
catch(Exception e)
|
166 |
|
|
{
|
167 |
|
|
e.printStackTrace();
|
168 |
|
|
fail("testGetLogRecords threw an unexpected exception: " + e.getMessage());
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
5377
|
berkley
|
* test the generation of system metadata for docs that don't already
|
174 |
|
|
* have it. This will be used for migration of existing object stores
|
175 |
|
|
* to dataone.
|
176 |
|
|
*/
|
177 |
|
|
public void testGenerateMissingSystemMetadata()
|
178 |
|
|
{
|
179 |
|
|
printTestHeader("testGenerateMissingSystemMetadata");
|
180 |
|
|
try
|
181 |
|
|
{
|
182 |
|
|
CrudService cs = CrudService.getInstance();
|
183 |
5378
|
berkley
|
AuthToken token = getToken();
|
184 |
5380
|
berkley
|
//create a document with no system metadata
|
185 |
|
|
String testDoc = getTestDoc();
|
186 |
|
|
Identifier id = new Identifier();
|
187 |
|
|
String docid = generateDocumentId();
|
188 |
|
|
id.setValue(docid);
|
189 |
|
|
|
190 |
|
|
cs.insertOrUpdateDocument(testDoc, id, cs.getSessionData(token), "insert");
|
191 |
|
|
//try to get its system metadata, should fail
|
192 |
|
|
try
|
193 |
|
|
{
|
194 |
|
|
getSystemMetadata(token, id);
|
195 |
|
|
fail("call to getSystemMetadata should have failed.");
|
196 |
|
|
}
|
197 |
|
|
catch(Exception e)
|
198 |
|
|
{}
|
199 |
|
|
|
200 |
|
|
//generate missing system metadata
|
201 |
5378
|
berkley
|
cs.generateMissingSystemMetadata(token);
|
202 |
5380
|
berkley
|
//try to get system metadata again, should succeed
|
203 |
|
|
getSystemMetadata(token, id);
|
204 |
5377
|
berkley
|
}
|
205 |
|
|
catch(Exception e)
|
206 |
|
|
{
|
207 |
|
|
fail("Unexpected error generating missing system metadata: " + e.getMessage());
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
/**
|
212 |
5375
|
berkley
|
* make sure that only valid sessions can update/delete
|
213 |
|
|
*/
|
214 |
|
|
public void testAccessControl()
|
215 |
|
|
{
|
216 |
5377
|
berkley
|
printTestHeader("testAccessControl");
|
217 |
5375
|
berkley
|
try
|
218 |
|
|
{
|
219 |
|
|
CrudService cs = CrudService.getInstance();
|
220 |
|
|
AuthToken token = getToken();
|
221 |
|
|
//create a doc
|
222 |
|
|
Identifier id = createDoc(token, getTestDoc());
|
223 |
|
|
|
224 |
|
|
//get the doc and sysmetadata
|
225 |
|
|
String gotDoc = getDoc(token, id);
|
226 |
|
|
SystemMetadata sm = getSystemMetadata(token, id);
|
227 |
|
|
|
228 |
|
|
//break the session id
|
229 |
|
|
String sessionid = "somefakesessionid";
|
230 |
|
|
token = new AuthToken(sessionid);
|
231 |
|
|
|
232 |
|
|
//update the doc
|
233 |
|
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
234 |
|
|
Identifier newid = new Identifier();
|
235 |
|
|
newid.setValue(generateDocumentId());
|
236 |
|
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
237 |
|
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
238 |
|
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
239 |
|
|
fail("exception should have been thrown.");
|
240 |
|
|
}
|
241 |
|
|
catch(Exception e)
|
242 |
|
|
{
|
243 |
|
|
}
|
244 |
5376
|
berkley
|
|
245 |
|
|
try
|
246 |
|
|
{
|
247 |
|
|
CrudService cs = CrudService.getInstance();
|
248 |
|
|
AuthToken token = new AuthToken("somefakesessionid");
|
249 |
|
|
//create a doc
|
250 |
|
|
Identifier id = createDoc(token, getTestDoc());
|
251 |
|
|
fail("exception should have been thrown.");
|
252 |
|
|
}
|
253 |
|
|
catch(Exception e)
|
254 |
|
|
{
|
255 |
|
|
}
|
256 |
5375
|
berkley
|
}
|
257 |
|
|
|
258 |
|
|
/**
|
259 |
5359
|
berkley
|
* public ObjectList listObjects(AuthToken token, Date startTime, Date endTime,
|
260 |
|
|
* ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
|
261 |
|
|
* throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
|
262 |
|
|
*/
|
263 |
|
|
public void testListObjects()
|
264 |
|
|
{
|
265 |
|
|
printTestHeader("testListObjects");
|
266 |
|
|
try
|
267 |
|
|
{
|
268 |
|
|
CrudService cs = CrudService.getInstance();
|
269 |
|
|
AuthToken token = getToken();
|
270 |
|
|
ObjectFormat of1 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0");
|
271 |
5362
|
berkley
|
ObjectFormat of2 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.0.0");
|
272 |
5359
|
berkley
|
//create docs at different times
|
273 |
|
|
Date d1 = new Date();
|
274 |
|
|
Identifier id1 = createDoc(token, getTestDoc(), of1);
|
275 |
|
|
SystemMetadata sm1 = getSystemMetadata(token, id1);
|
276 |
|
|
|
277 |
|
|
Date d2 = new Date();
|
278 |
|
|
Identifier id2 = createDoc(token, getTestDoc(), of2);
|
279 |
|
|
SystemMetadata sm2 = getSystemMetadata(token, id2);
|
280 |
|
|
|
281 |
|
|
Date d3 = new Date();
|
282 |
|
|
Identifier id3 = createDoc(token, getTestDoc(), of1);
|
283 |
|
|
SystemMetadata sm3 = getSystemMetadata(token, id3);
|
284 |
|
|
|
285 |
|
|
Date d4 = new Date();
|
286 |
|
|
Identifier id4 = createDoc(token, getTestDoc(), of2);
|
287 |
|
|
SystemMetadata sm4 = getSystemMetadata(token, id4);
|
288 |
|
|
|
289 |
|
|
Date d5 = new Date();
|
290 |
|
|
Identifier id5 = createDoc(token, getTestDoc(), of1);
|
291 |
|
|
SystemMetadata sm5 = getSystemMetadata(token, id5);
|
292 |
5366
|
berkley
|
|
293 |
|
|
Date d6 = new Date();
|
294 |
|
|
|
295 |
5359
|
berkley
|
//now get the objects for specific time ranges and test that it returns
|
296 |
|
|
//the correct objects
|
297 |
5370
|
berkley
|
|
298 |
|
|
ObjectList list = cs.listObjects(token, null, null, null);
|
299 |
5374
|
berkley
|
//System.out.println("list size: " + list.sizeObjectInfoList());
|
300 |
5387
|
berkley
|
|
301 |
5359
|
berkley
|
//should return sm1 and sm2
|
302 |
5366
|
berkley
|
ObjectList list1 = cs.listObjects(token, d1, d3, null);
|
303 |
5359
|
berkley
|
assertTrue(list1.sizeObjectInfoList() == 2);
|
304 |
5366
|
berkley
|
assertTrue(idInObjectList(id1, list1));
|
305 |
|
|
assertTrue(idInObjectList(id2, list1));
|
306 |
5359
|
berkley
|
|
307 |
5387
|
berkley
|
ObjectInfo info = list1.getObjectInfo(0);
|
308 |
|
|
|
309 |
|
|
|
310 |
5359
|
berkley
|
//should only return sm1
|
311 |
5366
|
berkley
|
ObjectList list2 = cs.listObjects(token, d1, d3, of1);
|
312 |
5359
|
berkley
|
assertTrue(list2.sizeObjectInfoList() == 1);
|
313 |
5366
|
berkley
|
assertTrue(idInObjectList(id1, list2));
|
314 |
5359
|
berkley
|
|
315 |
|
|
//should return sm1-sm4
|
316 |
5366
|
berkley
|
ObjectList list3 = cs.listObjects(token, d1, d5, null);
|
317 |
5359
|
berkley
|
assertTrue(list3.sizeObjectInfoList() == 4);
|
318 |
|
|
ObjectInfo oi4 = list3.getObjectInfo(0);
|
319 |
5366
|
berkley
|
assertTrue(idInObjectList(id1, list3));
|
320 |
|
|
assertTrue(idInObjectList(id2, list3));
|
321 |
|
|
assertTrue(idInObjectList(id3, list3));
|
322 |
|
|
assertTrue(idInObjectList(id4, list3));
|
323 |
5359
|
berkley
|
|
324 |
|
|
//should only return sm2 and sm4
|
325 |
5366
|
berkley
|
ObjectList list4 = cs.listObjects(token, d1, d5, of2);
|
326 |
5359
|
berkley
|
assertTrue(list4.sizeObjectInfoList() == 2);
|
327 |
5366
|
berkley
|
assertTrue(idInObjectList(id2, list4));
|
328 |
|
|
assertTrue(idInObjectList(id4, list4));
|
329 |
5359
|
berkley
|
|
330 |
|
|
//should return all
|
331 |
5366
|
berkley
|
ObjectList list5 = cs.listObjects(token, d1, d6, null);
|
332 |
5359
|
berkley
|
assertTrue(list5.sizeObjectInfoList() == 5);
|
333 |
5366
|
berkley
|
assertTrue(idInObjectList(id1, list5));
|
334 |
|
|
assertTrue(idInObjectList(id2, list5));
|
335 |
|
|
assertTrue(idInObjectList(id3, list5));
|
336 |
|
|
assertTrue(idInObjectList(id4, list5));
|
337 |
|
|
assertTrue(idInObjectList(id5, list5));
|
338 |
|
|
|
339 |
5359
|
berkley
|
//should return 1, 3, 5
|
340 |
5366
|
berkley
|
ObjectList list6 = cs.listObjects(token, d1, d6, of1);
|
341 |
|
|
assertTrue(list6.sizeObjectInfoList() == 3);
|
342 |
|
|
assertTrue(idInObjectList(id1, list6));
|
343 |
|
|
assertTrue(idInObjectList(id3, list6));
|
344 |
|
|
assertTrue(idInObjectList(id5, list6));
|
345 |
5359
|
berkley
|
|
346 |
|
|
}
|
347 |
|
|
catch(Exception e)
|
348 |
|
|
{
|
349 |
5362
|
berkley
|
//e.printStackTrace();
|
350 |
5359
|
berkley
|
fail("Error in listObjects: " + e.getMessage());
|
351 |
|
|
}
|
352 |
|
|
}
|
353 |
|
|
|
354 |
5366
|
berkley
|
private boolean idInObjectList(Identifier id, ObjectList list)
|
355 |
|
|
{
|
356 |
|
|
for(int i=0; i<list.sizeObjectInfoList(); i++)
|
357 |
|
|
{
|
358 |
|
|
ObjectInfo oi = list.getObjectInfo(i);
|
359 |
|
|
if(id.getValue().equals(oi.getIdentifier().getValue()))
|
360 |
|
|
return true;
|
361 |
|
|
}
|
362 |
|
|
return false;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
5359
|
berkley
|
/**
|
366 |
5350
|
berkley
|
* public Identifier update(AuthToken token, Identifier guid,
|
367 |
|
|
* InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta)
|
368 |
|
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
369 |
|
|
* UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata,
|
370 |
|
|
* NotImplemented
|
371 |
|
|
*/
|
372 |
|
|
public void testUpdate()
|
373 |
|
|
{
|
374 |
|
|
printTestHeader("testUpdate");
|
375 |
|
|
try
|
376 |
|
|
{
|
377 |
|
|
CrudService cs = CrudService.getInstance();
|
378 |
|
|
AuthToken token = getToken();
|
379 |
|
|
//create a doc
|
380 |
|
|
Identifier id = createDoc(token, getTestDoc());
|
381 |
5351
|
berkley
|
|
382 |
5350
|
berkley
|
//get the doc and sysmetadata
|
383 |
|
|
String gotDoc = getDoc(token, id);
|
384 |
|
|
SystemMetadata sm = getSystemMetadata(token, id);
|
385 |
5351
|
berkley
|
|
386 |
5350
|
berkley
|
//update the doc
|
387 |
|
|
gotDoc = gotDoc.replaceAll("XXX", "YYY");
|
388 |
|
|
Identifier newid = new Identifier();
|
389 |
|
|
newid.setValue(generateDocumentId());
|
390 |
|
|
StringBufferInputStream sbis = new StringBufferInputStream(gotDoc);
|
391 |
|
|
SystemMetadata newsm = createSystemMetadata(newid, gotDoc);
|
392 |
|
|
Identifier updatedid = cs.update(token, newid, sbis, id, newsm);
|
393 |
5351
|
berkley
|
|
394 |
5350
|
berkley
|
//get doc - check that it matches update
|
395 |
|
|
String newdoc = getDoc(token, newid);
|
396 |
|
|
assertTrue(gotDoc.equals(newdoc));
|
397 |
5351
|
berkley
|
|
398 |
5350
|
berkley
|
//get sysmeta - check that ids and other fields are updated
|
399 |
|
|
SystemMetadata newnewsm = getSystemMetadata(token, id);
|
400 |
5352
|
berkley
|
assertTrue(newnewsm.getObsoletedBy(0).getValue().equals(newid.getValue()));
|
401 |
|
|
|
402 |
|
|
//get the new sysmeta and make sure the obsoletes field is set
|
403 |
|
|
SystemMetadata newnewnewsm = getSystemMetadata(token, newid);
|
404 |
|
|
assertTrue(newnewnewsm.getObsolete(0).getValue().equals(id.getValue()));
|
405 |
5350
|
berkley
|
}
|
406 |
|
|
catch(Exception e)
|
407 |
|
|
{
|
408 |
|
|
e.printStackTrace();
|
409 |
|
|
fail("Error in testUpdate: " + e.getMessage());
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
/**
|
414 |
5341
|
berkley
|
* public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
415 |
|
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
416 |
|
|
* InvalidRequest, NotImplemented
|
417 |
|
|
*/
|
418 |
|
|
public void testGetSystemMetadata()
|
419 |
|
|
{
|
420 |
5344
|
berkley
|
printTestHeader("testGetSystemMetadata");
|
421 |
|
|
try
|
422 |
|
|
{
|
423 |
|
|
CrudService cs = CrudService.getInstance();
|
424 |
|
|
AuthToken token = getToken();
|
425 |
|
|
//run create
|
426 |
|
|
Identifier id = createDoc(token, getTestDoc());
|
427 |
|
|
//get the systemMetadata and make sure it is the correct object for this testDoc
|
428 |
|
|
SystemMetadata sm = getSystemMetadata(token, id);
|
429 |
|
|
assertTrue(sm.getIdentifier().getValue().equals(id.getValue()));
|
430 |
|
|
assertTrue(sm.getChecksum().getValue().equals(checksum(getTestDoc())));
|
431 |
5348
|
berkley
|
|
432 |
|
|
try
|
433 |
|
|
{
|
434 |
|
|
Identifier fakeid = new Identifier();
|
435 |
|
|
fakeid.setValue("somethingfake.234234");
|
436 |
|
|
getSystemMetadata(token, fakeid);
|
437 |
|
|
fail("getSystemMetadata should have thrown an exception.");
|
438 |
|
|
}
|
439 |
|
|
catch(Exception e)
|
440 |
|
|
{
|
441 |
|
|
assertTrue(true);
|
442 |
|
|
}
|
443 |
5344
|
berkley
|
}
|
444 |
|
|
catch(Exception e)
|
445 |
|
|
{
|
446 |
|
|
e.printStackTrace();
|
447 |
|
|
fail("Error testing system metadata: " + e.getMessage());
|
448 |
|
|
}
|
449 |
5341
|
berkley
|
}
|
450 |
|
|
|
451 |
|
|
/**
|
452 |
5337
|
berkley
|
* create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta)
|
453 |
|
|
* throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
454 |
|
|
* InsufficientResources, InvalidSystemMetadata, NotImplemented
|
455 |
5340
|
berkley
|
*
|
456 |
|
|
* public InputStream get(AuthToken token, Identifier guid)
|
457 |
|
|
* throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
458 |
|
|
* NotImplemented
|
459 |
5337
|
berkley
|
*/
|
460 |
5340
|
berkley
|
public void testCreateAndGet()
|
461 |
5337
|
berkley
|
{
|
462 |
5344
|
berkley
|
printTestHeader("testCreateAndGet");
|
463 |
5337
|
berkley
|
try
|
464 |
|
|
{
|
465 |
5341
|
berkley
|
CrudService cs = CrudService.getInstance();
|
466 |
|
|
AuthToken token = getToken();
|
467 |
|
|
//run create
|
468 |
5343
|
berkley
|
Identifier id = createDoc(token, getTestDoc());
|
469 |
5362
|
berkley
|
//make these docs public for debugging purposes.
|
470 |
|
|
makeDocPublic(id, true);
|
471 |
5352
|
berkley
|
//compare the docs
|
472 |
5342
|
berkley
|
String gotDoc = getDoc(token, id);
|
473 |
5343
|
berkley
|
assertTrue(gotDoc.trim().equals(getTestDoc().trim()));
|
474 |
5348
|
berkley
|
|
475 |
|
|
try
|
476 |
|
|
{
|
477 |
|
|
Identifier fakeid = new Identifier();
|
478 |
|
|
fakeid.setValue("somethingfake.234234");
|
479 |
|
|
getDoc(token, fakeid);
|
480 |
|
|
fail("testCreateAndGet should have thrown an exception.");
|
481 |
|
|
}
|
482 |
|
|
catch(Exception e)
|
483 |
|
|
{
|
484 |
|
|
assertTrue(true);
|
485 |
|
|
}
|
486 |
5337
|
berkley
|
}
|
487 |
|
|
catch(Exception e)
|
488 |
|
|
{
|
489 |
|
|
e.printStackTrace();
|
490 |
|
|
fail("Error in testCreate: " + e.getMessage());
|
491 |
|
|
}
|
492 |
|
|
}
|
493 |
5335
|
berkley
|
|
494 |
|
|
/**
|
495 |
5337
|
berkley
|
* getInstance()
|
496 |
|
|
*/
|
497 |
|
|
public void testSingletonAccessor()
|
498 |
|
|
{
|
499 |
5344
|
berkley
|
printTestHeader("testSingletonAccessor");
|
500 |
5337
|
berkley
|
CrudService cs = CrudService.getInstance();
|
501 |
|
|
assertNotNull(cs);
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
/**
|
505 |
5335
|
berkley
|
* Run an initial test that always passes to check that the test harness is
|
506 |
|
|
* working.
|
507 |
|
|
*/
|
508 |
|
|
public void initialize()
|
509 |
|
|
{
|
510 |
5344
|
berkley
|
printTestHeader("initialize");
|
511 |
5335
|
berkley
|
assertTrue(1 == 1);
|
512 |
|
|
}
|
513 |
5341
|
berkley
|
|
514 |
5342
|
berkley
|
/**
|
515 |
5344
|
berkley
|
* return the systemMetadata object for id
|
516 |
|
|
*/
|
517 |
|
|
private SystemMetadata getSystemMetadata(AuthToken token, Identifier id)
|
518 |
|
|
throws Exception
|
519 |
|
|
{
|
520 |
|
|
CrudService cs = CrudService.getInstance();
|
521 |
|
|
return cs.getSystemMetadata(token, id);
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
/**
|
525 |
5342
|
berkley
|
* get a doc from metacat using CrudService.get()
|
526 |
|
|
*/
|
527 |
|
|
private String getDoc(AuthToken token, Identifier id)
|
528 |
|
|
throws Exception
|
529 |
|
|
{
|
530 |
|
|
CrudService cs = CrudService.getInstance();
|
531 |
|
|
//try to get the same doc then compare them
|
532 |
|
|
InputStream gotDocStream = cs.get(token, id);
|
533 |
|
|
StringBuffer sb = new StringBuffer();
|
534 |
|
|
byte[] b = new byte[1024];
|
535 |
|
|
int numread = gotDocStream.read(b, 0, 1024);
|
536 |
|
|
while(numread != -1)
|
537 |
|
|
{
|
538 |
|
|
sb.append(new String(b, 0, numread));
|
539 |
|
|
numread = gotDocStream.read(b, 0, 1024);
|
540 |
|
|
}
|
541 |
|
|
return sb.toString();
|
542 |
|
|
}
|
543 |
|
|
|
544 |
|
|
/**
|
545 |
|
|
* return a test document
|
546 |
|
|
*/
|
547 |
5341
|
berkley
|
private String getTestDoc()
|
548 |
|
|
{
|
549 |
5350
|
berkley
|
return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
|
550 |
5341
|
berkley
|
}
|
551 |
|
|
|
552 |
|
|
/**
|
553 |
|
|
* authenticate and return a token
|
554 |
|
|
*/
|
555 |
|
|
private AuthToken getToken()
|
556 |
|
|
throws Exception
|
557 |
|
|
{
|
558 |
|
|
CrudService cs = CrudService.getInstance();
|
559 |
|
|
//login and get a sessionid
|
560 |
|
|
MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
|
561 |
|
|
String username = PropertyService.getProperty("test.mcUser");
|
562 |
|
|
String password = PropertyService.getProperty("test.mcPassword");
|
563 |
|
|
String response = restClient.login(username, password);
|
564 |
|
|
String sessionid = restClient.getSessionId();
|
565 |
|
|
SessionService sessionService = SessionService.getInstance();
|
566 |
|
|
sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
|
567 |
|
|
AuthToken token = new AuthToken(sessionid);
|
568 |
|
|
return token;
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
/**
|
572 |
5342
|
berkley
|
* create a doc using CrudService.create() and return its id
|
573 |
5341
|
berkley
|
*/
|
574 |
5343
|
berkley
|
private Identifier createDoc(AuthToken token, String testDoc) throws Exception
|
575 |
5341
|
berkley
|
{
|
576 |
5359
|
berkley
|
return createDoc(token, testDoc, ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
577 |
|
|
}
|
578 |
|
|
|
579 |
|
|
/**
|
580 |
|
|
* create a doc using CrudService.create() and return its id
|
581 |
|
|
*/
|
582 |
|
|
private Identifier createDoc(AuthToken token, String testDoc, ObjectFormat format) throws Exception
|
583 |
|
|
{
|
584 |
5341
|
berkley
|
Identifier id;
|
585 |
|
|
CrudService cs = CrudService.getInstance();
|
586 |
|
|
|
587 |
|
|
id = new Identifier();
|
588 |
|
|
String docid = generateDocumentId();
|
589 |
|
|
id.setValue(docid);
|
590 |
5344
|
berkley
|
|
591 |
5341
|
berkley
|
//create the system metadata then run the create method
|
592 |
|
|
StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
|
593 |
5359
|
berkley
|
SystemMetadata sm = createSystemMetadata(id, testDoc, format);
|
594 |
5350
|
berkley
|
//create the doc
|
595 |
|
|
cs.create(token, id, sbis, sm);
|
596 |
|
|
return id;
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
/**
|
600 |
|
|
* create a generic SystemMetadata object for testing
|
601 |
|
|
*/
|
602 |
|
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc)
|
603 |
|
|
throws Exception
|
604 |
|
|
{
|
605 |
5359
|
berkley
|
return createSystemMetadata(id, testDoc,
|
606 |
|
|
ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
|
607 |
|
|
}
|
608 |
|
|
|
609 |
|
|
/**
|
610 |
|
|
* create system metadata with a specified id, doc and format
|
611 |
|
|
*/
|
612 |
|
|
private SystemMetadata createSystemMetadata(Identifier id, String testDoc, ObjectFormat format)
|
613 |
|
|
throws Exception
|
614 |
|
|
{
|
615 |
5350
|
berkley
|
SystemMetadata sm = new SystemMetadata();
|
616 |
5341
|
berkley
|
//set the id
|
617 |
|
|
sm.setIdentifier(id);
|
618 |
5359
|
berkley
|
sm.setObjectFormat(format);
|
619 |
5341
|
berkley
|
//create the checksum
|
620 |
|
|
String checksumS = checksum(testDoc);
|
621 |
|
|
ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
|
622 |
|
|
Checksum checksum = new Checksum();
|
623 |
|
|
checksum.setValue(checksumS);
|
624 |
|
|
checksum.setAlgorithm(ca);
|
625 |
|
|
sm.setChecksum(checksum);
|
626 |
|
|
//set the size
|
627 |
|
|
sm.setSize(testDoc.getBytes().length);
|
628 |
|
|
//submitter
|
629 |
|
|
Principal p = new Principal();
|
630 |
|
|
p.setValue("joe");
|
631 |
|
|
sm.setSubmitter(p);
|
632 |
|
|
sm.setRightsHolder(p);
|
633 |
|
|
sm.setDateUploaded(new Date());
|
634 |
|
|
sm.setDateSysMetadataModified(new Date());
|
635 |
|
|
NodeReference nr = new NodeReference();
|
636 |
|
|
nr.setValue("metacat");
|
637 |
|
|
sm.setOriginMemberNode(nr);
|
638 |
|
|
sm.setAuthoritativeMemberNode(nr);
|
639 |
5350
|
berkley
|
return sm;
|
640 |
5341
|
berkley
|
}
|
641 |
5344
|
berkley
|
|
642 |
|
|
/**
|
643 |
5362
|
berkley
|
* make a document public in metacat by inserting an access document
|
644 |
|
|
* @param id
|
645 |
|
|
*/
|
646 |
|
|
private void makeDocPublic(Identifier id, boolean systemMetadataToo)
|
647 |
|
|
throws Exception
|
648 |
|
|
{
|
649 |
|
|
CrudService cs = CrudService.getInstance();
|
650 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
651 |
|
|
cs.setAccess(getToken(), id, "public", "read", "allow", "allowFirst");
|
652 |
|
|
if(systemMetadataToo)
|
653 |
|
|
{
|
654 |
|
|
String smidS = im.getSystemMetadataId(id.getValue());
|
655 |
|
|
Identifier smid = new Identifier();
|
656 |
|
|
smid.setValue(smidS);
|
657 |
|
|
cs.setAccess(getToken(), smid, "public", "read", "allow", "allowFirst");
|
658 |
|
|
}
|
659 |
|
|
}
|
660 |
|
|
|
661 |
|
|
/**
|
662 |
5344
|
berkley
|
* print a header to start each test
|
663 |
|
|
*/
|
664 |
|
|
private void printTestHeader(String testName)
|
665 |
|
|
{
|
666 |
|
|
System.out.println();
|
667 |
5389
|
berkley
|
System.out.println("*************** " + testName + " ***************");
|
668 |
5344
|
berkley
|
}
|
669 |
5335
|
berkley
|
|
670 |
5337
|
berkley
|
/**
|
671 |
|
|
* produce an md5 checksum for item
|
672 |
|
|
*/
|
673 |
|
|
private String checksum(String item)
|
674 |
|
|
throws Exception
|
675 |
|
|
{
|
676 |
|
|
StringBufferInputStream fis = new StringBufferInputStream(item);
|
677 |
|
|
|
678 |
|
|
byte[] buffer = new byte[1024];
|
679 |
|
|
MessageDigest complete = MessageDigest.getInstance("MD5");
|
680 |
|
|
int numRead;
|
681 |
|
|
|
682 |
|
|
do
|
683 |
|
|
{
|
684 |
|
|
numRead = fis.read(buffer);
|
685 |
|
|
if (numRead > 0)
|
686 |
|
|
{
|
687 |
|
|
complete.update(buffer, 0, numRead);
|
688 |
|
|
}
|
689 |
|
|
} while (numRead != -1);
|
690 |
|
|
|
691 |
|
|
|
692 |
|
|
return getHex(complete.digest());
|
693 |
|
|
}
|
694 |
|
|
|
695 |
|
|
/**
|
696 |
|
|
* convert a byte array to a hex string
|
697 |
|
|
*/
|
698 |
|
|
private static String getHex( byte [] raw )
|
699 |
|
|
{
|
700 |
|
|
final String HEXES = "0123456789ABCDEF";
|
701 |
|
|
if ( raw == null ) {
|
702 |
|
|
return null;
|
703 |
|
|
}
|
704 |
|
|
final StringBuilder hex = new StringBuilder( 2 * raw.length );
|
705 |
|
|
for ( final byte b : raw ) {
|
706 |
|
|
hex.append(HEXES.charAt((b & 0xF0) >> 4))
|
707 |
|
|
.append(HEXES.charAt((b & 0x0F)));
|
708 |
|
|
}
|
709 |
|
|
return hex.toString();
|
710 |
|
|
}
|
711 |
5335
|
berkley
|
}
|