Project

General

Profile

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: jones $'
8
 *     '$Date: 2010-03-18 03:11:35 -0700 (Thu, 18 Mar 2010) $'
9
 * '$Revision: 5286 $'
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.DocumentUtil;
63
import edu.ucsb.nceas.metacat.util.RequestUtil;
64
import edu.ucsb.nceas.utilities.IOUtil;
65
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
66
import edu.ucsb.nceas.utilities.SortedProperties;
67

    
68
/**
69
 * A base JUnit class for Metacat tests
70
 */
71
public class MCTestCase
72
    extends TestCase {
73
	
74
    private static boolean printDebug = false;
75
    
76
	protected static String EML2_0_0 = "EML2_0_0";
77
	protected static String EML2_0_1 = "EML2_0_1";
78
	protected static String EML2_1_0 = "EML2_1_0";
79
	
80
	protected boolean SUCCESS = true;
81
	protected boolean FAILURE = false;
82
	
83
	protected static final String ALLOWFIRST = "allowFirst";
84
	protected static final String DENYFIRST = "denyFirst";
85
	
86
	protected String testdatadir = "test/clienttestfiles/";
87
	protected String prefix = "test";
88
	protected String testdocument = "";
89
	
90
	protected static HttpClient httpClient = null;
91
	
92
	protected static boolean metacatConnectionNeeded = false;
93
	protected Metacat m;
94

    
95
	protected static String metacatUrl;
96
	protected static String username;
97
	protected static String password;
98
	protected static String anotheruser;
99
	protected static String anotherpassword;
100
	
101
	static {
102
		try {
103
			SortedProperties testProperties = 
104
				new SortedProperties("build/tests/test.properties");
105
			testProperties.load();
106
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
107
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
108
//			PropertyService.getInstance();
109
		    String printDebugString = PropertyService.getProperty("test.printdebug");
110
		    printDebug = Boolean.parseBoolean(printDebugString);
111

    
112
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
113
			username = PropertyService.getProperty("test.mcUser");
114
			password = PropertyService.getProperty("test.mcPassword");
115
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
116
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
117
		} catch (IOException ioe) {
118
			System.err.println("Could not read property file in static block: " 
119
					+ ioe.getMessage());
120
		} catch (PropertyNotFoundException pnfe) {
121
			System.err.println("Could not get property in static block: " 
122
					+ pnfe.getMessage());
123
		} catch (ServiceException se) {
124
			System.err.println("Could not get PropertyService instance in static block: " 
125
					+ se.getMessage());
126
		}
127
	}
128
	
129
	// header blocks
130
	protected String testEml_200_Header = "<?xml version=\"1.0\"?><eml:eml"
131
		+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\""
132
		+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
133
		+ " packageId=\"eml.1.1\" system=\"knb\""
134
		+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\""
135
		+ " scope=\"system\">";
136
	
137
	protected String testEml_201_Header = "<?xml version=\"1.0\"?><eml:eml"
138
		+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\""
139
		+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
140
		+ " packageId=\"eml.1.1\" system=\"knb\""
141
		+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\""
142
		+ " scope=\"system\">";
143
	
144
	protected String testEml_210_Header = "<?xml version=\"1.0\"?><eml:eml"
145
			+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\""
146
			+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
147
			+ " packageId=\"eml.1.1\" system=\"knb\""
148
			+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\""
149
			+ " scope=\"system\">";
150

    
151
	protected String testEmlCreatorBlock = "<creator scope=\"document\">                                       "
152
			+ " <individualName>                                                  "
153
			+ "    <surName>Smith</surName>                                       "
154
			+ " </individualName>                                                 "
155
			+ "</creator>                                                         ";
156

    
157
	protected String testEmlContactBlock = "<contact scope=\"document\">                                       "
158
			+ " <individualName>                                                  "
159
			+ "    <surName>Jackson</surName>                                     "
160
			+ " </individualName>                                                 "
161
			+ "</contact>                                                         ";
162

    
163
	protected String testEmlInlineBlock1 = "<inline>                                                           "
164
			+ "  <admin>                                                          "
165
			+ "    <contact>                                                      "
166
			+ "      <name>Operator</name>                                        "
167
			+ "      <institution>PSI</institution>                               "
168
			+ "    </contact>                                                     "
169
			+ "  </admin>                                                         "
170
			+ "</inline>                                                          ";
171

    
172
	protected String testEmlInlineBlock2 = "<inline>                                                           "
173
			+ "  <instrument>                                                     "
174
			+ "    <instName>LCQ</instName>                                       "
175
			+ "    <source type=\"ESI\"></source>                                 "
176
			+ "    <detector type=\"EM\"></detector>                              "
177
			+ "  </instrument>                                                    "
178
			+ "</inline>                                                          ";
179

    
180
	/*
181
	 * Retrus an access block base on params passed and the defaul perm order -
182
	 * allow first
183
	 */
184
	protected String getAccessBlock(String principal, boolean grantAccess, boolean read,
185
			boolean write, boolean changePermission, boolean all) {
186
		return getAccessBlock(principal, grantAccess, read, write, changePermission, all,
187
				ALLOWFIRST);
188
	}
189

    
190
	/**
191
	 * This function returns an access block based on the params passed
192
	 */
193
	protected String getAccessBlock(String principal, boolean grantAccess, boolean read,
194
			boolean write, boolean changePermission, boolean all, String permOrder) {
195
		String accessBlock = "<access "
196
				+ "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\""
197
				+ " order=\"" + permOrder + "\"" + " scope=\"document\"" + ">";
198

    
199
		accessBlock += generateOneAccessRule(principal, grantAccess, read, write,
200
				changePermission, all);
201
		accessBlock += "</access>";
202

    
203
		return accessBlock;
204

    
205
	}
206

    
207
	/*
208
	 * Gets eml access block base on given acccess rules and perm order
209
	 */
210
	protected String getAccessBlock(Vector<String> accessRules, String permOrder) {
211
		String accessBlock = "<access "
212
				+ "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\""
213
				+ " order=\"" + permOrder + "\"" + " scope=\"document\"" + ">";
214
		// adding rules
215
		if (accessRules != null && !accessRules.isEmpty()) {
216
			for (int i = 0; i < accessRules.size(); i++) {
217
				String rule = (String) accessRules.elementAt(i);
218
				accessBlock += rule;
219

    
220
			}
221
		}
222
		accessBlock += "</access>";
223
		return accessBlock;
224
	}
225

    
226
	/*
227
	 * Generates a access rule for given parameter. Note this xml portion
228
	 * doesn't include <access></access>
229
	 */
230
	protected String generateOneAccessRule(String principal, boolean grantAccess,
231
			boolean read, boolean write, boolean changePermission, boolean all) {
232
		String accessBlock = "";
233

    
234
		if (grantAccess) {
235
			accessBlock = "<allow>";
236
		} else {
237
			accessBlock = "<deny>";
238
		}
239

    
240
		accessBlock = accessBlock + "<principal>" + principal + "</principal>";
241

    
242
		if (all) {
243
			accessBlock += "<permission>all</permission>";
244
		} else {
245
			if (read) {
246
				accessBlock += "<permission>read</permission>";
247
			}
248
			if (write) {
249
				accessBlock += "<permission>write</permission>";
250
			}
251
			if (changePermission) {
252
				accessBlock += "<permission>changePermission</permission>";
253
			}
254
		}
255

    
256
		if (grantAccess) {
257
			accessBlock += "</allow>";
258
		} else {
259
			accessBlock += "</deny>";
260
		}
261
		return accessBlock;
262

    
263
	}
264

    
265
	/**
266
	 * This function returns a valid eml document with no access rules 
267
	 */
268
	protected String getTestEmlDoc(String title, String emlVersion, String inlineData1,
269
			String inlineData2, String onlineUrl1, String onlineUrl2,
270
			String docAccessBlock, String inlineAccessBlock1, String inlineAccessBlock2,
271
			String onlineAccessBlock1, String onlineAccessBlock2) {
272

    
273
		debug("getTestEmlDoc(): title=" + title + " inlineData1=" + inlineData1
274
				+ " inlineData2=" + inlineData2 + " onlineUrl1=" + onlineUrl1
275
				+ " onlineUrl2=" + onlineUrl2 + " docAccessBlock=" + docAccessBlock
276
				+ " inlineAccessBlock1=" + inlineAccessBlock1 + " inlineAccessBlock2="
277
				+ inlineAccessBlock2 + " onlineAccessBlock1=" + onlineAccessBlock1
278
				+ " onlineAccessBlock2=" + onlineAccessBlock2);
279
		String testDocument = "";
280
		String header;
281
		if (emlVersion == EML2_0_0) {
282
			header = testEml_200_Header;
283
		} else if (emlVersion == EML2_0_1) {
284
			header = testEml_201_Header;
285
		} else {
286
			header = testEml_210_Header;
287
		}
288
		testDocument += header;
289
		
290
		// if this is a 2.1.0+ document, the document level access block sits
291
		// at the same level and before the dataset element.
292
		if (docAccessBlock != null && emlVersion.equals(EML2_1_0)) {
293
			testDocument += docAccessBlock;
294
		}
295
		
296
		testDocument += "<dataset scope=\"document\"><title>"
297
				+ title + "</title>" + testEmlCreatorBlock;
298

    
299
		if (inlineData1 != null) {
300
			testDocument = testDocument
301
					+ "<distribution scope=\"document\" id=\"inlineEntity1\">"
302
					+ inlineData1 + "</distribution>";
303
		}
304
		if (inlineData2 != null) {
305
			testDocument = testDocument
306
					+ "<distribution scope=\"document\" id=\"inlineEntity2\">"
307
					+ inlineData2 + "</distribution>";
308
		}
309
		if (onlineUrl1 != null) {
310
			testDocument = testDocument
311
					+ "<distribution scope=\"document\" id=\"onlineEntity1\">"
312
					+ "<online><url function=\"download\">" + onlineUrl1
313
					+ "</url></online></distribution>";
314
		}
315
		if (onlineUrl2 != null) {
316
			testDocument = testDocument
317
					+ "<distribution scope=\"document\" id=\"onlineEntity2\">"
318
					+ "<online><url function=\"download\">" + onlineUrl2
319
					+ "</url></online></distribution>";
320
		}
321
		testDocument += testEmlContactBlock;
322

    
323
		// if this is a 2.0.X document, the document level access block sits
324
		// inside the dataset element.
325
		if (docAccessBlock != null && 
326
				(emlVersion.equals(EML2_0_0) || emlVersion.equals(EML2_0_1))) {
327
			testDocument += docAccessBlock;
328
		}
329

    
330
		testDocument += "</dataset>";
331

    
332
		if (inlineAccessBlock1 != null) {
333
			testDocument += "<additionalMetadata>";
334
			testDocument += "<describes>inlineEntity1</describes>";
335
			testDocument += inlineAccessBlock1;
336
			testDocument += "</additionalMetadata>";
337
		}
338

    
339
		if (inlineAccessBlock2 != null) {
340
			testDocument += "<additionalMetadata>";
341
			testDocument += "<describes>inlineEntity2</describes>";
342
			testDocument += inlineAccessBlock2;
343
			testDocument += "</additionalMetadata>";
344
		}
345

    
346
		if (onlineAccessBlock1 != null) {
347
			testDocument += "<additionalMetadata>";
348
			testDocument += "<describes>onlineEntity1</describes>";
349
			testDocument += onlineAccessBlock1;
350
			testDocument += "</additionalMetadata>";
351
		}
352

    
353
		if (onlineAccessBlock2 != null) {
354
			testDocument += "<additionalMetadata>";
355
			testDocument += "<describes>onlineEntity2</describes>";
356
			testDocument += onlineAccessBlock2;
357
			testDocument += "</additionalMetadata>";
358
		}
359

    
360
		testDocument += "</eml:eml>";
361

    
362
		// System.out.println("Returning following document" + testDocument);
363
		return testDocument;
364
	}
365
	
366
	/**
367
	 * 
368
	 */
369
	protected String getTestDocFromFile(String filePath) throws IOException{
370
		StringBuffer output = new StringBuffer();
371
		
372
		FileReader fileReader = new FileReader(new File(filePath));
373
		BufferedReader bufferedReader = new BufferedReader(fileReader);
374
		String readLine = null;
375
		
376
		while ((readLine = bufferedReader.readLine()) != null) {
377
			output.append(readLine);
378
		}
379
		
380
		return output.toString();
381
	}
382

    
383
	
384
    /**
385
     * Constructor to build the test
386
     */
387
    public MCTestCase() {
388
        super();
389
    }
390
	
391
    /**
392
     * Constructor to build the test
393
     *
394
     * @param name the name of the test method
395
     */
396
    public MCTestCase(String name) {
397
        super(name);
398
    }
399
    
400
	/**
401
	 * Establish a testing framework by initializing appropriate objects
402
	 */
403
	protected void setUp() throws Exception {
404
		try {
405
			if (metacatConnectionNeeded) {
406
				debug("setUp() - Testing Metacat Url: " + metacatUrl);
407
				m = MetacatFactory.createMetacatConnection(metacatUrl);
408
			}
409
		} catch (MetacatInaccessibleException mie) {
410
			System.err.println("Metacat is: " + metacatUrl);
411
			fail("Metacat connection failed." + mie.getMessage());
412
		}
413
	}
414
    
415
    protected static void debug(String debugMessage) {
416
    	if (printDebug) {
417
    		System.err.println(debugMessage);
418
    	}
419
    }
420
    
421
	protected static Vector<Hashtable<String, Object>> dbSelect(String sqlStatement,
422
			String methodName) throws SQLException {
423
		Vector<Hashtable<String, Object>> resultVector = new Vector<Hashtable<String, Object>>();
424

    
425
		DBConnectionPool connPool = DBConnectionPool.getInstance();
426
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
427
		int serialNumber = dbconn.getCheckOutSerialNumber();
428

    
429
		PreparedStatement pstmt = null;
430

    
431
		debug("Selecting from db: " + sqlStatement);
432
		pstmt = dbconn.prepareStatement(sqlStatement);
433
		pstmt.execute();
434

    
435
		ResultSet resultSet = pstmt.getResultSet();
436
		ResultSetMetaData rsMetaData = resultSet.getMetaData();
437
		int numColumns = rsMetaData.getColumnCount();
438
		debug("Number of data columns: " + numColumns);
439
		while (resultSet.next()) {
440
			Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
441
			for (int i = 1; i <= numColumns; i++) {
442
				if (resultSet.getObject(i) != null) {
443
					hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
444
				}
445
			}
446
			debug("adding data row to result vector");
447
			resultVector.add(hashTable);
448
		}
449

    
450
		resultSet.close();
451
		pstmt.close();
452
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
453

    
454
		return resultVector;
455
	}
456
	
457
	protected static void dbQuery(String sqlStatement, String methodName)
458
			throws SQLException {
459

    
460
		DBConnectionPool connPool = DBConnectionPool.getInstance();
461
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
462
		int serialNumber = dbconn.getCheckOutSerialNumber();
463

    
464
		Statement statement = dbconn.createStatement();
465

    
466
		debug("Executing against db: " + sqlStatement);
467
		statement.executeQuery(sqlStatement);
468

    
469
		statement.close();
470
		
471
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
472
	}
473
	
474
	protected static void dbUpdate(String sqlStatement, String methodName)
475
			throws SQLException {
476

    
477
		DBConnectionPool connPool = DBConnectionPool.getInstance();
478
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
479
		int serialNumber = dbconn.getCheckOutSerialNumber();
480

    
481
		Statement statement = dbconn.createStatement();
482

    
483
		debug("Executing against db: " + sqlStatement);
484
		statement.executeUpdate(sqlStatement);
485

    
486
		statement.close();
487

    
488
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
489
	}
490
	
491
	protected static void httpPost(String url, HashMap<String,String> paramMap)  throws IOException {
492
		debug("Posting to: " + url);
493
		if (httpClient == null) {
494
			httpClient = new HttpClient();
495
		}
496
		String postResponse = RequestUtil.post(httpClient, url, paramMap);
497
		debug("Post response: " + postResponse);
498
		if (postResponse.contains("<error>")) {
499
			fail("Error posting to metacat: " + postResponse);
500
		}
501
	}
502
	
503
	protected static void resetHttpClient() {
504
		httpClient = null;
505
	}
506
	
507
	/**
508
	 * Create a unique docid for testing insert and update. Does not
509
	 * include the 'revision' part of the id.
510
	 * 
511
	 * @return a String docid based on the current date and time
512
	 */
513
	protected String generateDocumentId() {
514
		try {
515
			Thread.sleep(1010);
516
		} catch (InterruptedException ie) {
517
			debug("Could not sleep: " + ie.getMessage());
518
		}
519

    
520
		return DocumentUtil.generateDocumentId(prefix, 0);
521
	}
522
	
523
	/**
524
	 * Insert a document into metacat. The expected result is passed as result
525
	 */
526
	protected String insertDocumentId(String docid, String docText, boolean result,
527
			boolean expectKarmaException) {
528
		debug("insertDocumentId() - docid=" + docid + " expectedResult=" + result
529
				+ " expectKarmaException=" + expectKarmaException);
530
		String response = null;
531
		try {
532
			response = m.insert(docid, new StringReader(docText), null);
533
			if (result) {
534
				assertTrue(response, (response.indexOf("<success>") != -1));
535
				assertTrue(response, response.indexOf(docid) != -1);
536
			} else {
537
				assertTrue(response, (response.indexOf("<success>") == -1));
538
			}
539
			debug("insertDocid():  response=" + response);
540
		} catch (MetacatInaccessibleException mie) {
541
			fail("Metacat Inaccessible:\n" + mie.getMessage());
542
		} catch (InsufficientKarmaException ike) {
543
			if (!expectKarmaException) {
544
				fail("Insufficient karma:\n" + ike.getMessage());
545
			}
546
		} catch (MetacatException me) {
547
			fail("Metacat Error:\n" + me.getMessage());
548
		} catch (Exception e) {
549
			fail("General exception:\n" + e.getMessage());
550
		}
551
		return response;
552
	}
553

    
554
	/**
555
	 * Insert a document into metacat. The expected result is passed as result
556
	 */
557

    
558
	protected String uploadDocumentId(String docid, String filePath, boolean result,
559
			boolean expectedKarmaException) {
560
		debug("uploadDocumentId() - docid=" + docid + " filePath=" + filePath
561
				+ " expectedResult=" + result + " expectedKarmaException="
562
				+ expectedKarmaException);
563
		String response = null;
564
		try {
565
			response = m.upload(docid, new File(filePath));
566
			if (result) {
567
				assertTrue(response, (response.indexOf("<success>") != -1));
568
				assertTrue(response, response.indexOf(docid) != -1);
569
			} else {
570
				assertTrue(response, (response.indexOf("<success>") == -1));
571
			}
572
			debug("uploadDocid():  response=" + response);
573
		} catch (MetacatInaccessibleException mie) {
574
			fail("Metacat Inaccessible:\n" + mie.getMessage());
575
		} catch (InsufficientKarmaException ike) {
576
			if (!expectedKarmaException) {
577
				fail("Insufficient karma:\n" + ike.getMessage());
578
			}
579
		} catch (MetacatException me) {
580
			if (result) {
581
				fail("Metacat Error:\n" + me.getMessage());
582
			} else {
583
				debug("Metacat Error:\n" + me.getMessage());
584
			}
585
		} catch (Exception e) {
586
			fail("General exception:\n" + e.getMessage());
587
		}
588
		return response;
589
	}
590

    
591
	/**
592
	 * Update a document in metacat. The expected result is passed as result
593
	 */
594
	protected String updateDocumentId(String docid, String docText, boolean result,
595
			boolean expectedKarmaFailure) {
596
		debug("updateDocumentId() - docid=" + docid + " expectedResult=" + result
597
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
598
		String response = null;
599
		try {
600
			response = m.update(docid, new StringReader(testdocument), null);
601
			debug("updateDocumentId() - response=" + response);
602
			
603
			if (result) {
604
				assertTrue(response, (response.indexOf("<success>") != -1));
605
				assertTrue(response, response.indexOf(docid) != -1);
606
			} else {
607
				assertTrue(response, (response.indexOf("<success>") == -1));
608
			}			
609
		} catch (MetacatInaccessibleException mie) {
610
			fail("Metacat Inaccessible:\n" + mie.getMessage());
611
		} catch (InsufficientKarmaException ike) {
612
			if (!expectedKarmaFailure) {
613
				fail("Insufficient karma:\n" + ike.getMessage());
614
			}
615
		} catch (MetacatException me) {
616
			if (result) {
617
				fail("Metacat Error:\n" + me.getMessage());
618
			} else {
619
				debug("Metacat Error:\n" + me.getMessage());
620
			}
621
		} catch (Exception e) {
622
			fail("General exception:\n" + e.getMessage());
623
		}
624

    
625
		return response;
626
	}
627

    
628
	/**
629
	 * Delete a document into metacat. The expected result is passed as result
630
	 */
631
	protected void deleteDocumentId(String docid, boolean result, boolean expectedKarmaFailure) {
632
		debug("deleteDocumentId() - docid=" + docid + " expectedResult=" + result
633
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
634
		try {
635
			Thread.sleep(5000);
636
			String response = m.delete(docid);
637
			debug("deleteDocumentId() -  response=" + response);
638
			
639
			if (result) {
640
				assertTrue(response, response.indexOf("<success>") != -1);
641
			} else {
642
				assertTrue(response, response.indexOf("<success>") == -1);
643
			}
644
		} catch (MetacatInaccessibleException mie) {
645
			fail("Metacat Inaccessible:\n" + mie.getMessage());
646
		} catch (InsufficientKarmaException ike) {
647
			if (!expectedKarmaFailure) {
648
				fail("Insufficient karma:\n" + ike.getMessage());
649
			}
650
		} catch (MetacatException me) {
651
			if (result) {
652
				fail("Metacat Error:\n" + me.getMessage());
653
			} else {
654
				debug("Metacat Error:\n" + me.getMessage());
655
			}
656
		} catch (Exception e) {
657
			fail("General exception:\n" + e.getMessage());
658
		}
659
	}
660
	
661
	/**
662
	 * Read a document from metacat and check if it is equal to a given string.
663
	 * The expected result is passed as result
664
	 */
665
	protected void readDocumentIdWhichEqualsDoc(String docid, String testDoc, boolean result,
666
			boolean expectedKarmaFailure) {
667
		debug("readDocumentIdWhichEqualsDoc() - docid=" + docid + " expectedResult=" + result
668
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
669
		try {
670
			Reader r = new InputStreamReader(m.read(docid));
671
			String doc = IOUtil.getAsString(r, true);
672
			debug("readDocumentIdWhichEqualsDoc() -  doc=" + doc);
673
			
674
			if (result) {
675

    
676
				if (!testDoc.equals(doc)) {
677
					System.out.println("doc ***********************");
678
					System.out.println(doc);
679
					System.out.println("end doc ***********************");
680
					System.out.println("testDoc ***********************");
681
					System.out.println(testDoc);
682
					System.out.println("end testDoc ***********************");
683
				}
684

    
685
				assertTrue(testDoc.equals(doc));
686
			} else {
687
				assertTrue(doc.indexOf("<error>") != -1);
688
			}
689
		} catch (MetacatInaccessibleException mie) {
690
			fail("Metacat Inaccessible:\n" + mie.getMessage());
691
		} catch (InsufficientKarmaException ike) {
692
			if (!expectedKarmaFailure) {
693
				fail("Insufficient karma:\n" + ike.getMessage());
694
			}
695
		} catch (MetacatException me) {
696
			fail("Metacat Error:\n" + me.getMessage());
697
		} catch (Exception e) {
698
			fail("General exception:\n" + e.getMessage());
699
		}
700

    
701
	}
702

    
703
}
    (1-1/1)