Revision 5344
Added by berkley over 14 years ago
test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java | ||
---|---|---|
94 | 94 |
public static Test suite() |
95 | 95 |
{ |
96 | 96 |
TestSuite suite = new TestSuite(); |
97 |
suite.addTest(new CrudServiceTest("initialize")); |
|
97 |
/*suite.addTest(new CrudServiceTest("initialize"));
|
|
98 | 98 |
suite.addTest(new CrudServiceTest("testSingletonAccessor")); |
99 |
suite.addTest(new CrudServiceTest("testCreateAndGet")); |
|
99 |
suite.addTest(new CrudServiceTest("testCreateAndGet"));*/
|
|
100 | 100 |
suite.addTest(new CrudServiceTest("testGetSystemMetadata")); |
101 | 101 |
//suite.addTest(new CrudServiceTest("")); |
102 | 102 |
//suite.addTest(new CrudServiceTest("")); |
... | ... | |
111 | 111 |
*/ |
112 | 112 |
public void testGetSystemMetadata() |
113 | 113 |
{ |
114 |
|
|
114 |
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 |
} |
|
126 |
catch(Exception e) |
|
127 |
{ |
|
128 |
e.printStackTrace(); |
|
129 |
fail("Error testing system metadata: " + e.getMessage()); |
|
130 |
} |
|
115 | 131 |
} |
116 | 132 |
|
117 | 133 |
/** |
... | ... | |
125 | 141 |
*/ |
126 | 142 |
public void testCreateAndGet() |
127 | 143 |
{ |
144 |
printTestHeader("testCreateAndGet"); |
|
128 | 145 |
try |
129 | 146 |
{ |
130 | 147 |
CrudService cs = CrudService.getInstance(); |
... | ... | |
149 | 166 |
*/ |
150 | 167 |
public void testSingletonAccessor() |
151 | 168 |
{ |
169 |
printTestHeader("testSingletonAccessor"); |
|
152 | 170 |
CrudService cs = CrudService.getInstance(); |
153 | 171 |
assertNotNull(cs); |
154 | 172 |
} |
... | ... | |
159 | 177 |
*/ |
160 | 178 |
public void initialize() |
161 | 179 |
{ |
180 |
printTestHeader("initialize"); |
|
162 | 181 |
assertTrue(1 == 1); |
163 | 182 |
} |
164 | 183 |
|
165 | 184 |
/** |
185 |
* return the systemMetadata object for id |
|
186 |
*/ |
|
187 |
private SystemMetadata getSystemMetadata(AuthToken token, Identifier id) |
|
188 |
throws Exception |
|
189 |
{ |
|
190 |
System.out.println("getting system metadata for id: " + id.getValue()); |
|
191 |
CrudService cs = CrudService.getInstance(); |
|
192 |
return cs.getSystemMetadata(token, id); |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
166 | 196 |
* get a doc from metacat using CrudService.get() |
167 | 197 |
*/ |
168 | 198 |
private String getDoc(AuthToken token, Identifier id) |
169 | 199 |
throws Exception |
170 | 200 |
{ |
201 |
System.out.println("getting doc for id " + id.getValue()); |
|
171 | 202 |
CrudService cs = CrudService.getInstance(); |
172 | 203 |
//try to get the same doc then compare them |
173 | 204 |
InputStream gotDocStream = cs.get(token, id); |
... | ... | |
179 | 210 |
sb.append(new String(b, 0, numread)); |
180 | 211 |
numread = gotDocStream.read(b, 0, 1024); |
181 | 212 |
} |
182 |
|
|
213 |
System.out.println("returning doc for id " + id.getValue()); |
|
183 | 214 |
return sb.toString(); |
184 | 215 |
} |
185 | 216 |
|
... | ... | |
225 | 256 |
id = new Identifier(); |
226 | 257 |
String docid = generateDocumentId(); |
227 | 258 |
id.setValue(docid); |
228 |
|
|
259 |
System.out.println("creating doc with id " + id.getValue()); |
|
260 |
|
|
229 | 261 |
//create the system metadata then run the create method |
230 | 262 |
StringBufferInputStream sbis = new StringBufferInputStream(testDoc); |
231 | 263 |
SystemMetadata sm = new SystemMetadata(); |
232 | 264 |
//set the id |
265 |
System.out.println("creating system metadata object for document with id " + id.getValue()); |
|
233 | 266 |
sm.setIdentifier(id); |
234 | 267 |
System.out.println("sm id is " + id.getValue()); |
235 | 268 |
sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0")); |
... | ... | |
258 | 291 |
sm.setAuthoritativeMemberNode(nr); |
259 | 292 |
//create the doc |
260 | 293 |
cs.create(token, id, sbis, sm); |
294 |
System.out.println("document with id " + id.getValue() + " created."); |
|
261 | 295 |
return id; |
262 | 296 |
} |
297 |
|
|
298 |
/** |
|
299 |
* print a header to start each test |
|
300 |
*/ |
|
301 |
private void printTestHeader(String testName) |
|
302 |
{ |
|
303 |
System.out.println(); |
|
304 |
System.out.println("********************************** " + testName + " **********************************"); |
|
305 |
} |
|
263 | 306 |
|
264 | 307 |
/** |
265 | 308 |
* produce an md5 checksum for item |
src/edu/ucsb/nceas/metacat/dataone/CrudService.java | ||
---|---|---|
265 | 265 |
public String produce(final OutputStream dataSink) throws Exception { |
266 | 266 |
|
267 | 267 |
try { |
268 |
handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null,
|
|
268 |
handler.readFromMetacat(metacatUrl, null, |
|
269 | 269 |
dataSink, localId, "xml", |
270 | 270 |
sessionData.getUserName(), |
271 | 271 |
sessionData.getGroupNames(), true, params); |
... | ... | |
327 | 327 |
|
328 | 328 |
try { |
329 | 329 |
IdentifierManager im = IdentifierManager.getInstance(); |
330 |
// This is a test id of an existing sysmeta document -- for temporary testing |
|
331 |
String testGuid = "autogen.20101192441566.1"; |
|
332 |
// TODO: Look up ID of system metadata based on guid |
|
333 |
// TODO: Initially from document, later from entry in table? |
|
334 |
// String localId = im.getLocalId(guid.getValue()); |
|
335 |
final String localId = im.getLocalId(testGuid); |
|
330 |
System.out.println("looking up systemmetadata id for guid " + guid.getValue()); |
|
331 |
final String localId = im.getSystemMetadataId(guid.getValue()); |
|
332 |
System.out.println("localId: " + localId); |
|
336 | 333 |
|
337 | 334 |
// Read system metadata from metacat's db |
338 | 335 |
final InputStreamFromOutputStream<String> objectStream = |
... | ... | |
341 | 338 |
@Override |
342 | 339 |
public String produce(final OutputStream dataSink) throws Exception { |
343 | 340 |
try { |
344 |
handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null,
|
|
341 |
handler.readFromMetacat(metacatUrl, null, |
|
345 | 342 |
dataSink, localId, "xml", |
346 | 343 |
sessionData.getUserName(), |
347 | 344 |
sessionData.getGroupNames(), true, params); |
... | ... | |
370 | 367 |
return sysmeta; |
371 | 368 |
|
372 | 369 |
} catch (McdbDocNotFoundException e) { |
370 |
e.printStackTrace(); |
|
373 | 371 |
throw new NotFound(1000, e.getMessage()); |
374 | 372 |
} |
375 | 373 |
} |
... | ... | |
484 | 482 |
} |
485 | 483 |
|
486 | 484 |
logMetacat.debug("Logging the creation event."); |
487 |
EventLog.getInstance().log(/*request.getRemoteAddr()*/metacatUrl,
|
|
485 |
EventLog.getInstance().log(metacatUrl, |
|
488 | 486 |
username, localId, "create"); |
489 | 487 |
|
490 | 488 |
// Schedule replication for this data file |
... | ... | |
546 | 544 |
String localId = insertDocument(xml, sysMetaGuid, sessionData); |
547 | 545 |
//insert the system metadata doc id into the identifiers table to |
548 | 546 |
//link it to the data or metadata document |
549 |
IdentifierManager.getInstance().createSystemMetadataMapping(sysMetaGuid.getValue(), localId); |
|
547 |
System.out.println("mapping systemmetadata id to docid in im.insertSystemMetadata"); |
|
548 |
IdentifierManager.getInstance().createSystemMetadataMapping(sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue()); |
|
550 | 549 |
} |
551 | 550 |
|
552 | 551 |
/** |
... | ... | |
578 | 577 |
// generate the right exceptions |
579 | 578 |
//ByteArrayOutputStream output = new ByteArrayOutputStream(); |
580 | 579 |
//PrintWriter pw = new PrintWriter(output); |
581 |
String result = handler.handleInsertOrUpdateAction(/*request.getRemoteAddr()*/metacatUrl, null,
|
|
580 |
String result = handler.handleInsertOrUpdateAction(metacatUrl, null, |
|
582 | 581 |
null, params, sessionData.getUserName(), sessionData.getGroupNames()); |
583 | 582 |
//String outputS = new String(output.toByteArray()); |
584 | 583 |
logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result); |
src/edu/ucsb/nceas/metacat/IdentifierManager.java | ||
---|---|---|
254 | 254 |
int rev = 0; |
255 | 255 |
|
256 | 256 |
String query = "select guid, docid, rev from " + type + " where guid = ?"; |
257 |
System.out.println("query: " + query + " for params: (" + guid + ")"); |
|
257 | 258 |
|
258 | 259 |
DBConnection dbConn = null; |
259 | 260 |
int serialNumber = -1; |
... | ... | |
301 | 302 |
// Parse the localId into scope and rev parts |
302 | 303 |
AccessionNumber acc = new AccessionNumber(localId, "NOACTION"); |
303 | 304 |
String docid = acc.getDocid(); |
304 |
int rev = (new Integer(acc.getRev()).intValue()); |
|
305 |
int rev = 1; |
|
306 |
if(acc.getRev() != null) |
|
307 |
{ |
|
308 |
rev = (new Integer(acc.getRev()).intValue()); |
|
309 |
} |
|
305 | 310 |
|
306 | 311 |
// Get a database connection from the pool |
307 | 312 |
dbConn = |
... | ... | |
309 | 314 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
310 | 315 |
|
311 | 316 |
// Execute the insert statement |
312 |
String query = "insert into " + type + " (guid, docid, rev) values (?, ?, ?)"; |
|
317 |
String query = "insert into " + type + " (guid, docid, rev) values (?, ?, ?)"; |
|
318 |
System.out.println("query: " + query + " for params: (" + guid + ", " + docid + ", " + rev + ")"); |
|
313 | 319 |
PreparedStatement stmt = dbConn.prepareStatement(query); |
314 | 320 |
stmt.setString(1, guid); |
315 | 321 |
stmt.setString(2, docid); |
... | ... | |
318 | 324 |
|
319 | 325 |
stmt.close(); |
320 | 326 |
} catch (SQLException e) { |
327 |
e.printStackTrace(); |
|
321 | 328 |
logMetacat.error("SQL error while creating a mapping to the system metadata identifier: " |
322 | 329 |
+ e.getMessage()); |
323 | 330 |
} catch (NumberFormatException e) { |
331 |
e.printStackTrace(); |
|
324 | 332 |
logMetacat.error("NumberFormat error while creating a mapping to the system metadata identifier: " |
325 | 333 |
+ e.getMessage()); |
326 | 334 |
} catch (AccessionNumberException e) { |
335 |
e.printStackTrace(); |
|
327 | 336 |
logMetacat.error("AccessionNumber error while creating a mapping to the system metadata identifier: " |
328 | 337 |
+ e.getMessage()); |
329 | 338 |
} finally { |
Also available in: Unified diff
getting getSystemMetadata to work