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