Project

General

Profile

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