1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2008 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: daigle $'
|
8
|
* '$Date: 2009-11-06 14:48:29 -0800 (Fri, 06 Nov 2009) $'
|
9
|
* '$Revision: 5113 $'
|
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;
|
27
|
|
28
|
import junit.framework.TestCase;
|
29
|
|
30
|
import java.io.BufferedReader;
|
31
|
import java.io.File;
|
32
|
import java.io.FileReader;
|
33
|
import java.io.IOException;
|
34
|
import java.io.InputStreamReader;
|
35
|
import java.io.Reader;
|
36
|
import java.io.StringReader;
|
37
|
import java.sql.PreparedStatement;
|
38
|
import java.sql.ResultSet;
|
39
|
import java.sql.ResultSetMetaData;
|
40
|
import java.sql.SQLException;
|
41
|
import java.sql.Statement;
|
42
|
import java.util.Calendar;
|
43
|
import java.util.Date;
|
44
|
import java.util.GregorianCalendar;
|
45
|
import java.util.HashMap;
|
46
|
import java.util.Hashtable;
|
47
|
import java.util.SimpleTimeZone;
|
48
|
import java.util.TimeZone;
|
49
|
import java.util.Vector;
|
50
|
|
51
|
import org.apache.commons.httpclient.HttpClient;
|
52
|
|
53
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
54
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
55
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
56
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
57
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
58
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
59
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
60
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
61
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
62
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
63
|
import edu.ucsb.nceas.utilities.IOUtil;
|
64
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
65
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
66
|
|
67
|
/**
|
68
|
* A base JUnit class for Metacat tests
|
69
|
*/
|
70
|
public class MCTestCase
|
71
|
extends TestCase {
|
72
|
|
73
|
private static boolean printDebug = false;
|
74
|
|
75
|
protected static String EML2_0_0 = "EML2_0_0";
|
76
|
protected static String EML2_0_1 = "EML2_0_1";
|
77
|
protected static String EML2_1_0 = "EML2_1_0";
|
78
|
|
79
|
protected boolean SUCCESS = true;
|
80
|
protected boolean FAILURE = false;
|
81
|
|
82
|
protected static final String ALLOWFIRST = "allowFirst";
|
83
|
protected static final String DENYFIRST = "denyFirst";
|
84
|
|
85
|
protected String prefix = "test";
|
86
|
protected String testdocument = "";
|
87
|
|
88
|
protected static HttpClient httpClient = null;
|
89
|
|
90
|
protected static boolean metacatConnectionNeeded = false;
|
91
|
protected Metacat m;
|
92
|
|
93
|
protected static String metacatUrl;
|
94
|
protected static String username;
|
95
|
protected static String password;
|
96
|
protected static String anotheruser;
|
97
|
protected static String anotherpassword;
|
98
|
|
99
|
static {
|
100
|
try {
|
101
|
SortedProperties testProperties =
|
102
|
new SortedProperties("build/tests/test.properties");
|
103
|
testProperties.load();
|
104
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
105
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
106
|
// PropertyService.getInstance();
|
107
|
String printDebugString = PropertyService.getProperty("test.printdebug");
|
108
|
printDebug = Boolean.parseBoolean(printDebugString);
|
109
|
|
110
|
metacatUrl = PropertyService.getProperty("test.metacatUrl");
|
111
|
username = PropertyService.getProperty("test.mcUser");
|
112
|
password = PropertyService.getProperty("test.mcPassword");
|
113
|
anotheruser = PropertyService.getProperty("test.mcAnotherUser");
|
114
|
anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
|
115
|
} catch (IOException ioe) {
|
116
|
System.err.println("Could not read property file in static block: "
|
117
|
+ ioe.getMessage());
|
118
|
} catch (PropertyNotFoundException pnfe) {
|
119
|
System.err.println("Could not get property in static block: "
|
120
|
+ pnfe.getMessage());
|
121
|
} catch (ServiceException se) {
|
122
|
System.err.println("Could not get PropertyService instance in static block: "
|
123
|
+ se.getMessage());
|
124
|
}
|
125
|
}
|
126
|
|
127
|
// header blocks
|
128
|
protected String testEml_200_Header = "<?xml version=\"1.0\"?><eml:eml"
|
129
|
+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\""
|
130
|
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
131
|
+ " packageId=\"eml.1.1\" system=\"knb\""
|
132
|
+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\""
|
133
|
+ " scope=\"system\">";
|
134
|
|
135
|
protected String testEml_201_Header = "<?xml version=\"1.0\"?><eml:eml"
|
136
|
+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\""
|
137
|
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
138
|
+ " packageId=\"eml.1.1\" system=\"knb\""
|
139
|
+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\""
|
140
|
+ " scope=\"system\">";
|
141
|
|
142
|
protected String testEml_210_Header = "<?xml version=\"1.0\"?><eml:eml"
|
143
|
+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\""
|
144
|
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
145
|
+ " packageId=\"eml.1.1\" system=\"knb\""
|
146
|
+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\""
|
147
|
+ " scope=\"system\">";
|
148
|
|
149
|
protected String testEmlCreatorBlock = "<creator scope=\"document\"> "
|
150
|
+ " <individualName> "
|
151
|
+ " <surName>Smith</surName> "
|
152
|
+ " </individualName> "
|
153
|
+ "</creator> ";
|
154
|
|
155
|
protected String testEmlContactBlock = "<contact scope=\"document\"> "
|
156
|
+ " <individualName> "
|
157
|
+ " <surName>Jackson</surName> "
|
158
|
+ " </individualName> "
|
159
|
+ "</contact> ";
|
160
|
|
161
|
protected String testEmlInlineBlock1 = "<inline> "
|
162
|
+ " <admin> "
|
163
|
+ " <contact> "
|
164
|
+ " <name>Operator</name> "
|
165
|
+ " <institution>PSI</institution> "
|
166
|
+ " </contact> "
|
167
|
+ " </admin> "
|
168
|
+ "</inline> ";
|
169
|
|
170
|
protected String testEmlInlineBlock2 = "<inline> "
|
171
|
+ " <instrument> "
|
172
|
+ " <instName>LCQ</instName> "
|
173
|
+ " <source type=\"ESI\"></source> "
|
174
|
+ " <detector type=\"EM\"></detector> "
|
175
|
+ " </instrument> "
|
176
|
+ "</inline> ";
|
177
|
|
178
|
/*
|
179
|
* Retrus an access block base on params passed and the defaul perm order -
|
180
|
* allow first
|
181
|
*/
|
182
|
protected String getAccessBlock(String principal, boolean grantAccess, boolean read,
|
183
|
boolean write, boolean changePermission, boolean all) {
|
184
|
return getAccessBlock(principal, grantAccess, read, write, changePermission, all,
|
185
|
ALLOWFIRST);
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* This function returns an access block based on the params passed
|
190
|
*/
|
191
|
protected String getAccessBlock(String principal, boolean grantAccess, boolean read,
|
192
|
boolean write, boolean changePermission, boolean all, String permOrder) {
|
193
|
String accessBlock = "<access "
|
194
|
+ "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\""
|
195
|
+ " order=\"" + permOrder + "\"" + " scope=\"document\"" + ">";
|
196
|
|
197
|
accessBlock += generateOneAccessRule(principal, grantAccess, read, write,
|
198
|
changePermission, all);
|
199
|
accessBlock += "</access>";
|
200
|
|
201
|
return accessBlock;
|
202
|
|
203
|
}
|
204
|
|
205
|
/*
|
206
|
* Gets eml access block base on given acccess rules and perm order
|
207
|
*/
|
208
|
protected String getAccessBlock(Vector<String> accessRules, String permOrder) {
|
209
|
String accessBlock = "<access "
|
210
|
+ "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\""
|
211
|
+ " order=\"" + permOrder + "\"" + " scope=\"document\"" + ">";
|
212
|
// adding rules
|
213
|
if (accessRules != null && !accessRules.isEmpty()) {
|
214
|
for (int i = 0; i < accessRules.size(); i++) {
|
215
|
String rule = (String) accessRules.elementAt(i);
|
216
|
accessBlock += rule;
|
217
|
|
218
|
}
|
219
|
}
|
220
|
accessBlock += "</access>";
|
221
|
return accessBlock;
|
222
|
}
|
223
|
|
224
|
/*
|
225
|
* Generates a access rule for given parameter. Note this xml portion
|
226
|
* doesn't include <access></access>
|
227
|
*/
|
228
|
protected String generateOneAccessRule(String principal, boolean grantAccess,
|
229
|
boolean read, boolean write, boolean changePermission, boolean all) {
|
230
|
String accessBlock = "";
|
231
|
|
232
|
if (grantAccess) {
|
233
|
accessBlock = "<allow>";
|
234
|
} else {
|
235
|
accessBlock = "<deny>";
|
236
|
}
|
237
|
|
238
|
accessBlock = accessBlock + "<principal>" + principal + "</principal>";
|
239
|
|
240
|
if (all) {
|
241
|
accessBlock += "<permission>all</permission>";
|
242
|
} else {
|
243
|
if (read) {
|
244
|
accessBlock += "<permission>read</permission>";
|
245
|
}
|
246
|
if (write) {
|
247
|
accessBlock += "<permission>write</permission>";
|
248
|
}
|
249
|
if (changePermission) {
|
250
|
accessBlock += "<permission>changePermission</permission>";
|
251
|
}
|
252
|
}
|
253
|
|
254
|
if (grantAccess) {
|
255
|
accessBlock += "</allow>";
|
256
|
} else {
|
257
|
accessBlock += "</deny>";
|
258
|
}
|
259
|
return accessBlock;
|
260
|
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* This function returns a valid eml document with no access rules
|
265
|
*/
|
266
|
protected String getTestEmlDoc(String title, String emlVersion, String inlineData1,
|
267
|
String inlineData2, String onlineUrl1, String onlineUrl2,
|
268
|
String docAccessBlock, String inlineAccessBlock1, String inlineAccessBlock2,
|
269
|
String onlineAccessBlock1, String onlineAccessBlock2) {
|
270
|
|
271
|
debug("getTestEmlDoc(): title=" + title + " inlineData1=" + inlineData1
|
272
|
+ " inlineData2=" + inlineData2 + " onlineUrl1=" + onlineUrl1
|
273
|
+ " onlineUrl2=" + onlineUrl2 + " docAccessBlock=" + docAccessBlock
|
274
|
+ " inlineAccessBlock1=" + inlineAccessBlock1 + " inlineAccessBlock2="
|
275
|
+ inlineAccessBlock2 + " onlineAccessBlock1=" + onlineAccessBlock1
|
276
|
+ " onlineAccessBlock2=" + onlineAccessBlock2);
|
277
|
String testDocument = "";
|
278
|
String header;
|
279
|
if (emlVersion == EML2_0_0) {
|
280
|
header = testEml_200_Header;
|
281
|
} else if (emlVersion == EML2_0_1) {
|
282
|
header = testEml_201_Header;
|
283
|
} else {
|
284
|
header = testEml_210_Header;
|
285
|
}
|
286
|
testDocument += header;
|
287
|
|
288
|
// if this is a 2.1.0+ document, the document level access block sits
|
289
|
// at the same level and before the dataset element.
|
290
|
if (docAccessBlock != null && emlVersion.equals(EML2_1_0)) {
|
291
|
testDocument += docAccessBlock;
|
292
|
}
|
293
|
|
294
|
testDocument += "<dataset scope=\"document\"><title>"
|
295
|
+ title + "</title>" + testEmlCreatorBlock;
|
296
|
|
297
|
if (inlineData1 != null) {
|
298
|
testDocument = testDocument
|
299
|
+ "<distribution scope=\"document\" id=\"inlineEntity1\">"
|
300
|
+ inlineData1 + "</distribution>";
|
301
|
}
|
302
|
if (inlineData2 != null) {
|
303
|
testDocument = testDocument
|
304
|
+ "<distribution scope=\"document\" id=\"inlineEntity2\">"
|
305
|
+ inlineData2 + "</distribution>";
|
306
|
}
|
307
|
if (onlineUrl1 != null) {
|
308
|
testDocument = testDocument
|
309
|
+ "<distribution scope=\"document\" id=\"onlineEntity1\">"
|
310
|
+ "<online><url function=\"download\">" + onlineUrl1
|
311
|
+ "</url></online></distribution>";
|
312
|
}
|
313
|
if (onlineUrl2 != null) {
|
314
|
testDocument = testDocument
|
315
|
+ "<distribution scope=\"document\" id=\"onlineEntity2\">"
|
316
|
+ "<online><url function=\"download\">" + onlineUrl2
|
317
|
+ "</url></online></distribution>";
|
318
|
}
|
319
|
testDocument += testEmlContactBlock;
|
320
|
|
321
|
// if this is a 2.0.X document, the document level access block sits
|
322
|
// inside the dataset element.
|
323
|
if (docAccessBlock != null &&
|
324
|
(emlVersion.equals(EML2_0_0) || emlVersion.equals(EML2_0_1))) {
|
325
|
testDocument += docAccessBlock;
|
326
|
}
|
327
|
|
328
|
testDocument += "</dataset>";
|
329
|
|
330
|
if (inlineAccessBlock1 != null) {
|
331
|
testDocument += "<additionalMetadata>";
|
332
|
testDocument += "<describes>inlineEntity1</describes>";
|
333
|
testDocument += inlineAccessBlock1;
|
334
|
testDocument += "</additionalMetadata>";
|
335
|
}
|
336
|
|
337
|
if (inlineAccessBlock2 != null) {
|
338
|
testDocument += "<additionalMetadata>";
|
339
|
testDocument += "<describes>inlineEntity2</describes>";
|
340
|
testDocument += inlineAccessBlock2;
|
341
|
testDocument += "</additionalMetadata>";
|
342
|
}
|
343
|
|
344
|
if (onlineAccessBlock1 != null) {
|
345
|
testDocument += "<additionalMetadata>";
|
346
|
testDocument += "<describes>onlineEntity1</describes>";
|
347
|
testDocument += onlineAccessBlock1;
|
348
|
testDocument += "</additionalMetadata>";
|
349
|
}
|
350
|
|
351
|
if (onlineAccessBlock2 != null) {
|
352
|
testDocument += "<additionalMetadata>";
|
353
|
testDocument += "<describes>onlineEntity2</describes>";
|
354
|
testDocument += onlineAccessBlock2;
|
355
|
testDocument += "</additionalMetadata>";
|
356
|
}
|
357
|
|
358
|
testDocument += "</eml:eml>";
|
359
|
|
360
|
// System.out.println("Returning following document" + testDocument);
|
361
|
return testDocument;
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
*
|
366
|
*/
|
367
|
protected String getTestDocFromFile(String filePath) throws IOException{
|
368
|
StringBuffer output = new StringBuffer();
|
369
|
|
370
|
FileReader fileReader = new FileReader(new File(filePath));
|
371
|
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
372
|
String readLine = null;
|
373
|
|
374
|
while ((readLine = bufferedReader.readLine()) != null) {
|
375
|
output.append(readLine);
|
376
|
}
|
377
|
|
378
|
return output.toString();
|
379
|
}
|
380
|
|
381
|
|
382
|
/**
|
383
|
* Constructor to build the test
|
384
|
*/
|
385
|
public MCTestCase() {
|
386
|
super();
|
387
|
}
|
388
|
|
389
|
/**
|
390
|
* Constructor to build the test
|
391
|
*
|
392
|
* @param name the name of the test method
|
393
|
*/
|
394
|
public MCTestCase(String name) {
|
395
|
super(name);
|
396
|
}
|
397
|
|
398
|
/**
|
399
|
* Establish a testing framework by initializing appropriate objects
|
400
|
*/
|
401
|
protected void setUp() throws Exception {
|
402
|
try {
|
403
|
if (metacatConnectionNeeded)
|
404
|
debug("setUp() - Testing Metacat Url: " + metacatUrl);
|
405
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
406
|
} catch (MetacatInaccessibleException mie) {
|
407
|
System.err.println("Metacat is: " + metacatUrl);
|
408
|
fail("Metacat connection failed." + mie.getMessage());
|
409
|
}
|
410
|
}
|
411
|
|
412
|
protected static void debug(String debugMessage) {
|
413
|
if (printDebug) {
|
414
|
System.err.println(debugMessage);
|
415
|
}
|
416
|
}
|
417
|
|
418
|
protected static Vector<Hashtable<String, Object>> dbSelect(String sqlStatement,
|
419
|
String methodName) throws SQLException {
|
420
|
Vector<Hashtable<String, Object>> resultVector = new Vector<Hashtable<String, Object>>();
|
421
|
|
422
|
DBConnectionPool connPool = DBConnectionPool.getInstance();
|
423
|
DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
|
424
|
int serialNumber = dbconn.getCheckOutSerialNumber();
|
425
|
|
426
|
PreparedStatement pstmt = null;
|
427
|
|
428
|
debug("Selecting from db: " + sqlStatement);
|
429
|
pstmt = dbconn.prepareStatement(sqlStatement);
|
430
|
pstmt.execute();
|
431
|
|
432
|
ResultSet resultSet = pstmt.getResultSet();
|
433
|
ResultSetMetaData rsMetaData = resultSet.getMetaData();
|
434
|
int numColumns = rsMetaData.getColumnCount();
|
435
|
debug("Number of data columns: " + numColumns);
|
436
|
while (resultSet.next()) {
|
437
|
Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
|
438
|
for (int i = 1; i <= numColumns; i++) {
|
439
|
if (resultSet.getObject(i) != null) {
|
440
|
hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
|
441
|
}
|
442
|
}
|
443
|
debug("adding data row to result vector");
|
444
|
resultVector.add(hashTable);
|
445
|
}
|
446
|
|
447
|
resultSet.close();
|
448
|
pstmt.close();
|
449
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
450
|
|
451
|
return resultVector;
|
452
|
}
|
453
|
|
454
|
protected static void dbQuery(String sqlStatement, String methodName)
|
455
|
throws SQLException {
|
456
|
|
457
|
DBConnectionPool connPool = DBConnectionPool.getInstance();
|
458
|
DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
|
459
|
int serialNumber = dbconn.getCheckOutSerialNumber();
|
460
|
|
461
|
Statement statement = dbconn.createStatement();
|
462
|
|
463
|
debug("Executing against db: " + sqlStatement);
|
464
|
statement.executeQuery(sqlStatement);
|
465
|
|
466
|
statement.close();
|
467
|
|
468
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
469
|
}
|
470
|
|
471
|
protected static void dbUpdate(String sqlStatement, String methodName)
|
472
|
throws SQLException {
|
473
|
|
474
|
DBConnectionPool connPool = DBConnectionPool.getInstance();
|
475
|
DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
|
476
|
int serialNumber = dbconn.getCheckOutSerialNumber();
|
477
|
|
478
|
Statement statement = dbconn.createStatement();
|
479
|
|
480
|
debug("Executing against db: " + sqlStatement);
|
481
|
statement.executeUpdate(sqlStatement);
|
482
|
|
483
|
statement.close();
|
484
|
|
485
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
486
|
}
|
487
|
|
488
|
protected static void httpPost(String url, HashMap<String,String> paramMap) throws IOException {
|
489
|
debug("Posting to: " + url);
|
490
|
if (httpClient == null) {
|
491
|
httpClient = new HttpClient();
|
492
|
}
|
493
|
String postResponse = RequestUtil.post(httpClient, url, paramMap);
|
494
|
debug("Post response: " + postResponse);
|
495
|
if (postResponse.contains("<error>")) {
|
496
|
fail("Error posting to metacat: " + postResponse);
|
497
|
}
|
498
|
}
|
499
|
|
500
|
protected static void resetHttpClient() {
|
501
|
httpClient = null;
|
502
|
}
|
503
|
|
504
|
/**
|
505
|
* Create a unique docid for testing insert and update. Does not
|
506
|
* include the 'revision' part of the id.
|
507
|
*
|
508
|
* @return a String docid based on the current date and time
|
509
|
*/
|
510
|
protected String generateDocumentId() {
|
511
|
try {
|
512
|
Thread.sleep(1010);
|
513
|
} catch (InterruptedException ie) {
|
514
|
debug("Could not sleep: " + ie.getMessage());
|
515
|
}
|
516
|
|
517
|
StringBuffer docid = new StringBuffer(prefix);
|
518
|
docid.append(".");
|
519
|
|
520
|
// Create a calendar to get the date formatted properly
|
521
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
522
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
523
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
|
524
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
|
525
|
Calendar calendar = new GregorianCalendar(pdt);
|
526
|
Date trialTime = new Date();
|
527
|
calendar.setTime(trialTime);
|
528
|
docid.append(calendar.get(Calendar.YEAR));
|
529
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
530
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
531
|
docid.append(calendar.get(Calendar.MINUTE));
|
532
|
docid.append(calendar.get(Calendar.SECOND));
|
533
|
|
534
|
return docid.toString();
|
535
|
}
|
536
|
|
537
|
/**
|
538
|
* Insert a document into metacat. The expected result is passed as result
|
539
|
*/
|
540
|
|
541
|
protected String insertDocumentId(String docid, String docText, boolean result,
|
542
|
boolean expectKarmaException) {
|
543
|
debug("insertDocumentId() - docid=" + docid + " expectedResult=" + result
|
544
|
+ " expectKarmaException=" + expectKarmaException);
|
545
|
String response = null;
|
546
|
try {
|
547
|
response = m.insert(docid, new StringReader(docText), null);
|
548
|
if (result) {
|
549
|
assertTrue(response, (response.indexOf("<success>") != -1));
|
550
|
assertTrue(response, response.indexOf(docid) != -1);
|
551
|
} else {
|
552
|
assertTrue(response, (response.indexOf("<success>") == -1));
|
553
|
}
|
554
|
debug("insertDocid(): response=" + response);
|
555
|
} catch (MetacatInaccessibleException mie) {
|
556
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
557
|
} catch (InsufficientKarmaException ike) {
|
558
|
if (!expectKarmaException) {
|
559
|
fail("Insufficient karma:\n" + ike.getMessage());
|
560
|
}
|
561
|
} catch (MetacatException me) {
|
562
|
fail("Metacat Error:\n" + me.getMessage());
|
563
|
} catch (Exception e) {
|
564
|
fail("General exception:\n" + e.getMessage());
|
565
|
}
|
566
|
return response;
|
567
|
}
|
568
|
|
569
|
/**
|
570
|
* Insert a document into metacat. The expected result is passed as result
|
571
|
*/
|
572
|
|
573
|
protected String uploadDocumentId(String docid, String filePath, boolean result,
|
574
|
boolean expectedKarmaException) {
|
575
|
debug("uploadDocumentId() - docid=" + docid + " filePath=" + filePath
|
576
|
+ " expectedResult=" + result + " expectedKarmaException="
|
577
|
+ expectedKarmaException);
|
578
|
String response = null;
|
579
|
try {
|
580
|
response = m.upload(docid, new File(filePath));
|
581
|
if (result) {
|
582
|
assertTrue(response, (response.indexOf("<success>") != -1));
|
583
|
assertTrue(response, response.indexOf(docid) != -1);
|
584
|
} else {
|
585
|
assertTrue(response, (response.indexOf("<success>") == -1));
|
586
|
}
|
587
|
debug("uploadDocid(): response=" + response);
|
588
|
} catch (MetacatInaccessibleException mie) {
|
589
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
590
|
} catch (InsufficientKarmaException ike) {
|
591
|
if (!expectedKarmaException) {
|
592
|
fail("Insufficient karma:\n" + ike.getMessage());
|
593
|
}
|
594
|
} catch (MetacatException me) {
|
595
|
if (result) {
|
596
|
fail("Metacat Error:\n" + me.getMessage());
|
597
|
} else {
|
598
|
debug("Metacat Error:\n" + me.getMessage());
|
599
|
}
|
600
|
} catch (Exception e) {
|
601
|
fail("General exception:\n" + e.getMessage());
|
602
|
}
|
603
|
return response;
|
604
|
}
|
605
|
|
606
|
/**
|
607
|
* Update a document in metacat. The expected result is passed as result
|
608
|
*/
|
609
|
protected String updateDocumentId(String docid, String docText, boolean result,
|
610
|
boolean expectedKarmaFailure) {
|
611
|
debug("updateDocumentId() - docid=" + docid + " expectedResult=" + result
|
612
|
+ " expectedKarmaFailure=" + expectedKarmaFailure);
|
613
|
String response = null;
|
614
|
try {
|
615
|
response = m.update(docid, new StringReader(testdocument), null);
|
616
|
debug("updateDocumentId() - response=" + response);
|
617
|
|
618
|
if (result) {
|
619
|
assertTrue(response, (response.indexOf("<success>") != -1));
|
620
|
assertTrue(response, response.indexOf(docid) != -1);
|
621
|
} else {
|
622
|
assertTrue(response, (response.indexOf("<success>") == -1));
|
623
|
}
|
624
|
} catch (MetacatInaccessibleException mie) {
|
625
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
626
|
} catch (InsufficientKarmaException ike) {
|
627
|
if (!expectedKarmaFailure) {
|
628
|
fail("Insufficient karma:\n" + ike.getMessage());
|
629
|
}
|
630
|
} catch (MetacatException me) {
|
631
|
if (result) {
|
632
|
fail("Metacat Error:\n" + me.getMessage());
|
633
|
} else {
|
634
|
debug("Metacat Error:\n" + me.getMessage());
|
635
|
}
|
636
|
} catch (Exception e) {
|
637
|
fail("General exception:\n" + e.getMessage());
|
638
|
}
|
639
|
|
640
|
return response;
|
641
|
}
|
642
|
|
643
|
/**
|
644
|
* Delete a document into metacat. The expected result is passed as result
|
645
|
*/
|
646
|
protected void deleteDocumentId(String docid, boolean result, boolean expectedKarmaFailure) {
|
647
|
debug("deleteDocumentId() - docid=" + docid + " expectedResult=" + result
|
648
|
+ " expectedKarmaFailure=" + expectedKarmaFailure);
|
649
|
try {
|
650
|
Thread.sleep(5000);
|
651
|
String response = m.delete(docid);
|
652
|
debug("deleteDocumentId() - response=" + response);
|
653
|
|
654
|
if (result) {
|
655
|
assertTrue(response, response.indexOf("<success>") != -1);
|
656
|
} else {
|
657
|
assertTrue(response, response.indexOf("<success>") == -1);
|
658
|
}
|
659
|
} catch (MetacatInaccessibleException mie) {
|
660
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
661
|
} catch (InsufficientKarmaException ike) {
|
662
|
if (!expectedKarmaFailure) {
|
663
|
fail("Insufficient karma:\n" + ike.getMessage());
|
664
|
}
|
665
|
} catch (MetacatException me) {
|
666
|
if (result) {
|
667
|
fail("Metacat Error:\n" + me.getMessage());
|
668
|
} else {
|
669
|
debug("Metacat Error:\n" + me.getMessage());
|
670
|
}
|
671
|
} catch (Exception e) {
|
672
|
fail("General exception:\n" + e.getMessage());
|
673
|
}
|
674
|
}
|
675
|
|
676
|
/**
|
677
|
* Read a document from metacat and check if it is equal to a given string.
|
678
|
* The expected result is passed as result
|
679
|
*/
|
680
|
protected void readDocumentIdWhichEqualsDoc(String docid, String testDoc, boolean result,
|
681
|
boolean expectedKarmaFailure) {
|
682
|
debug("readDocumentIdWhichEqualsDoc() - docid=" + docid + " expectedResult=" + result
|
683
|
+ " expectedKarmaFailure=" + expectedKarmaFailure);
|
684
|
try {
|
685
|
Reader r = new InputStreamReader(m.read(docid));
|
686
|
String doc = IOUtil.getAsString(r, true);
|
687
|
debug("readDocumentIdWhichEqualsDoc() - doc=" + doc);
|
688
|
|
689
|
if (result) {
|
690
|
|
691
|
if (!testDoc.equals(doc)) {
|
692
|
System.out.println("doc ***********************");
|
693
|
System.out.println(doc);
|
694
|
System.out.println("end doc ***********************");
|
695
|
System.out.println("testDoc ***********************");
|
696
|
System.out.println(testDoc);
|
697
|
System.out.println("end testDoc ***********************");
|
698
|
}
|
699
|
|
700
|
assertTrue(testDoc.equals(doc));
|
701
|
} else {
|
702
|
assertTrue(doc.indexOf("<error>") != -1);
|
703
|
}
|
704
|
} catch (MetacatInaccessibleException mie) {
|
705
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
706
|
} catch (InsufficientKarmaException ike) {
|
707
|
if (!expectedKarmaFailure) {
|
708
|
fail("Insufficient karma:\n" + ike.getMessage());
|
709
|
}
|
710
|
} catch (MetacatException me) {
|
711
|
fail("Metacat Error:\n" + me.getMessage());
|
712
|
} catch (Exception e) {
|
713
|
fail("General exception:\n" + e.getMessage());
|
714
|
}
|
715
|
|
716
|
}
|
717
|
|
718
|
}
|