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