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: leinfelder $'
8
 *     '$Date: 2011-11-04 14:45:59 -0700 (Fri, 04 Nov 2011) $'
9
 * '$Revision: 6606 $'
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 java.io.BufferedReader;
29
import java.io.File;
30
import java.io.FileReader;
31
import java.io.IOException;
32
import java.io.InputStreamReader;
33
import java.io.Reader;
34
import java.io.StringReader;
35
import java.sql.PreparedStatement;
36
import java.sql.ResultSet;
37
import java.sql.ResultSetMetaData;
38
import java.sql.SQLException;
39
import java.util.HashMap;
40
import java.util.Hashtable;
41
import java.util.Vector;
42

    
43
import junit.framework.TestCase;
44

    
45
import org.apache.http.client.HttpClient;
46
import org.apache.http.impl.client.DefaultHttpClient;
47

    
48
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
49
import edu.ucsb.nceas.metacat.client.Metacat;
50
import edu.ucsb.nceas.metacat.client.MetacatException;
51
import edu.ucsb.nceas.metacat.client.MetacatFactory;
52
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
53
import edu.ucsb.nceas.metacat.database.DBConnection;
54
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
55
import edu.ucsb.nceas.metacat.properties.PropertyService;
56
import edu.ucsb.nceas.metacat.shared.ServiceException;
57
import edu.ucsb.nceas.metacat.util.DocumentUtil;
58
import edu.ucsb.nceas.metacat.util.RequestUtil;
59
import edu.ucsb.nceas.utilities.IOUtil;
60
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
61
import edu.ucsb.nceas.utilities.SortedProperties;
62

    
63
/**
64
 * A base JUnit class for Metacat tests
65
 */
66
public class MCTestCase
67
    extends TestCase {
68
	
69
    private static boolean printDebug = false;
70
    
71
	protected static String EML2_0_0 = "EML2_0_0";
72
	protected static String EML2_0_1 = "EML2_0_1";
73
	protected static String EML2_1_0 = "EML2_1_0";
74
	protected static String EML2_1_1 = "EML2_1_1";
75

    
76
	
77
	protected boolean SUCCESS = true;
78
	protected boolean FAILURE = false;
79
	
80
	protected static final String ALLOWFIRST = "allowFirst";
81
	protected static final String DENYFIRST = "denyFirst";
82
	
83
	protected String testdatadir = "test/clienttestfiles/";
84
	protected String prefix = "test";
85
	protected String testdocument = "";
86
	
87
	protected static HttpClient httpClient = null;
88
	
89
	protected static boolean metacatConnectionNeeded = false;
90
	protected Metacat m;
91

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

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

    
155
	protected String testEmlCreatorBlock = "<creator scope=\"document\">                                       "
156
			+ " <individualName>                                                  "
157
			+ "    <surName>Smith</surName>                                       "
158
			+ " </individualName>                                                 "
159
			+ "</creator>                                                         ";
160

    
161
	protected String testEmlContactBlock = "<contact scope=\"document\">                                       "
162
			+ " <individualName>                                                  "
163
			+ "    <surName>Jackson</surName>                                     "
164
			+ " </individualName>                                                 "
165
			+ "</contact>                                                         ";
166

    
167
	protected String testEmlInlineBlock1 = "<inline>                                                           "
168
			+ "  <admin>                                                          "
169
			+ "    <contact>                                                      "
170
			+ "      <name>Operator</name>                                        "
171
			+ "      <institution>PSI</institution>                               "
172
			+ "    </contact>                                                     "
173
			+ "  </admin>                                                         "
174
			+ "</inline>                                                          ";
175

    
176
	protected String testEmlInlineBlock2 = "<inline>                                                           "
177
			+ "  <instrument>                                                     "
178
			+ "    <instName>LCQ</instName>                                       "
179
			+ "    <source type=\"ESI\"></source>                                 "
180
			+ "    <detector type=\"EM\"></detector>                              "
181
			+ "  </instrument>                                                    "
182
			+ "</inline>                                                          ";
183

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

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

    
203
		accessBlock += generateOneAccessRule(principal, grantAccess, read, write,
204
				changePermission, all);
205
		accessBlock += "</access>";
206

    
207
		return accessBlock;
208

    
209
	}
210

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

    
224
			}
225
		}
226
		accessBlock += "</access>";
227
		return accessBlock;
228
	}
229

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

    
238
		if (grantAccess) {
239
			accessBlock = "<allow>";
240
		} else {
241
			accessBlock = "<deny>";
242
		}
243

    
244
		accessBlock = accessBlock + "<principal>" + principal + "</principal>";
245

    
246
		if (all) {
247
			accessBlock += "<permission>all</permission>";
248
		} else {
249
			if (read) {
250
				accessBlock += "<permission>read</permission>";
251
			}
252
			if (write) {
253
				accessBlock += "<permission>write</permission>";
254
			}
255
			if (changePermission) {
256
				accessBlock += "<permission>changePermission</permission>";
257
			}
258
		}
259

    
260
		if (grantAccess) {
261
			accessBlock += "</allow>";
262
		} else {
263
			accessBlock += "</deny>";
264
		}
265
		return accessBlock;
266

    
267
	}
268
	
269
    /**
270
     * This function returns a valid eml document with public read access
271
     */
272
    protected String getTestEmlDoc(String title, String emlVersion) {
273

    
274
        String testDocument = "";
275

    
276
        String header;
277
		if (emlVersion == EML2_0_1) {
278
			header = testEml_201_Header;
279
		} else if (emlVersion == EML2_1_0) {
280
			header = testEml_210_Header;
281
		} else { // if (emlVersion == EML2_1_1) {
282
			header = testEml_211_Header;
283
		}
284
        
285
        testDocument += header;
286
        
287
        // if this is an EML 2.1.0 or later document, the document level access is
288
        // before the dataset element.
289
        if (emlVersion == EML2_1_0 || emlVersion == EML2_1_1) {
290
        	testDocument += getAccessBlock("public", true, true, false, false, false);
291
        }
292
        testDocument += "<dataset scope=\"document\"><title>" + title + "</title>"
293
				+ testEmlCreatorBlock;
294

    
295
        testDocument += testEmlContactBlock;
296
        
297
        // if this is an EML 2.0.1 or earlier document, the document level access is
298
        // inside the dataset element.
299
        if (emlVersion != EML2_1_0 && emlVersion != EML2_1_1) {
300
        	testDocument += getAccessBlock("public", true, true, false, false, false);
301
        }
302
        testDocument += "</dataset>";
303
        testDocument += "</eml:eml>";
304

    
305
        return testDocument;
306
    }
307

    
308
	/**
309
	 * This function returns a valid eml document with no access rules 
310
	 */
311
	protected String getTestEmlDoc(String title, String emlVersion, String inlineData1,
312
			String inlineData2, String onlineUrl1, String onlineUrl2,
313
			String docAccessBlock, String inlineAccessBlock1, String inlineAccessBlock2,
314
			String onlineAccessBlock1, String onlineAccessBlock2) {
315

    
316
		debug("getTestEmlDoc(): title=" + title + " inlineData1=" + inlineData1
317
				+ " inlineData2=" + inlineData2 + " onlineUrl1=" + onlineUrl1
318
				+ " onlineUrl2=" + onlineUrl2 + " docAccessBlock=" + docAccessBlock
319
				+ " inlineAccessBlock1=" + inlineAccessBlock1 + " inlineAccessBlock2="
320
				+ inlineAccessBlock2 + " onlineAccessBlock1=" + onlineAccessBlock1
321
				+ " onlineAccessBlock2=" + onlineAccessBlock2);
322
		String testDocument = "";
323
		String header;
324
		if (emlVersion == EML2_0_0) {
325
			header = testEml_200_Header;
326
		} else if (emlVersion == EML2_0_1) {
327
			header = testEml_201_Header;
328
		} else if (emlVersion == EML2_1_0) {
329
			header = testEml_210_Header;
330
		} else { // if (emlVersion == EML2_1_1) {
331
			header = testEml_211_Header;
332
		}
333
		testDocument += header;
334
		
335
		// if this is a 2.1.0+ document, the document level access block sits
336
		// at the same level and before the dataset element.
337
		if (docAccessBlock != null && (emlVersion.equals(EML2_1_0) || emlVersion.equals(EML2_1_1))) {
338
			testDocument += docAccessBlock;
339
		}
340
		
341
		testDocument += "<dataset scope=\"document\"><title>"
342
				+ title + "</title>" + testEmlCreatorBlock;
343

    
344
		if (inlineData1 != null) {
345
			testDocument = testDocument
346
					+ "<distribution scope=\"document\" id=\"inlineEntity1\">"
347
					+ inlineData1 + "</distribution>";
348
		}
349
		if (inlineData2 != null) {
350
			testDocument = testDocument
351
					+ "<distribution scope=\"document\" id=\"inlineEntity2\">"
352
					+ inlineData2 + "</distribution>";
353
		}
354
		if (onlineUrl1 != null) {
355
			testDocument = testDocument
356
					+ "<distribution scope=\"document\" id=\"onlineEntity1\">"
357
					+ "<online><url function=\"download\">" + onlineUrl1
358
					+ "</url></online></distribution>";
359
		}
360
		if (onlineUrl2 != null) {
361
			testDocument = testDocument
362
					+ "<distribution scope=\"document\" id=\"onlineEntity2\">"
363
					+ "<online><url function=\"download\">" + onlineUrl2
364
					+ "</url></online></distribution>";
365
		}
366
		testDocument += testEmlContactBlock;
367

    
368
		// if this is a 2.0.X document, the document level access block sits
369
		// inside the dataset element.
370
		if (docAccessBlock != null && 
371
				(emlVersion.equals(EML2_0_0) || emlVersion.equals(EML2_0_1))) {
372
			testDocument += docAccessBlock;
373
		}
374

    
375
		testDocument += "</dataset>";
376

    
377
		if (inlineAccessBlock1 != null) {
378
			testDocument += "<additionalMetadata>";
379
			testDocument += "<describes>inlineEntity1</describes>";
380
			testDocument += inlineAccessBlock1;
381
			testDocument += "</additionalMetadata>";
382
		}
383

    
384
		if (inlineAccessBlock2 != null) {
385
			testDocument += "<additionalMetadata>";
386
			testDocument += "<describes>inlineEntity2</describes>";
387
			testDocument += inlineAccessBlock2;
388
			testDocument += "</additionalMetadata>";
389
		}
390

    
391
		if (onlineAccessBlock1 != null) {
392
			testDocument += "<additionalMetadata>";
393
			testDocument += "<describes>onlineEntity1</describes>";
394
			testDocument += onlineAccessBlock1;
395
			testDocument += "</additionalMetadata>";
396
		}
397

    
398
		if (onlineAccessBlock2 != null) {
399
			testDocument += "<additionalMetadata>";
400
			testDocument += "<describes>onlineEntity2</describes>";
401
			testDocument += onlineAccessBlock2;
402
			testDocument += "</additionalMetadata>";
403
		}
404

    
405
		testDocument += "</eml:eml>";
406

    
407
		// System.out.println("Returning following document" + testDocument);
408
		return testDocument;
409
	}
410
	
411
	/**
412
	 * 
413
	 */
414
	protected String getTestDocFromFile(String filePath) throws IOException{
415
		StringBuffer output = new StringBuffer();
416
		
417
		FileReader fileReader = new FileReader(new File(filePath));
418
		BufferedReader bufferedReader = new BufferedReader(fileReader);
419
		String readLine = null;
420
		
421
		while ((readLine = bufferedReader.readLine()) != null) {
422
			output.append(readLine);
423
		}
424
		
425
		return output.toString();
426
	}
427

    
428
	
429
    /**
430
     * Constructor to build the test
431
     */
432
    public MCTestCase() {
433
        super();
434
    }
435
	
436
    /**
437
     * Constructor to build the test
438
     *
439
     * @param name the name of the test method
440
     */
441
    public MCTestCase(String name) {
442
        super(name);
443
    }
444
    
445
	/**
446
	 * Establish a testing framework by initializing appropriate objects
447
	 */
448
	protected void setUp() throws Exception {
449
		try {
450
			if (metacatConnectionNeeded) {
451
				debug("setUp() - Testing Metacat Url: " + metacatUrl);
452
				m = MetacatFactory.createMetacatConnection(metacatUrl);
453
			}
454
		} catch (MetacatInaccessibleException mie) {
455
			System.err.println("Metacat is: " + metacatUrl);
456
			fail("Metacat connection failed." + mie.getMessage());
457
		}
458
	}
459
    
460
    protected static void debug(String debugMessage) {
461
    	if (printDebug) {
462
    		System.err.println(debugMessage);
463
    	}
464
    }
465
    
466
	protected static Vector<Hashtable<String, Object>> dbSelect(String sqlStatement,
467
			String methodName) throws SQLException {
468
		Vector<Hashtable<String, Object>> resultVector = new Vector<Hashtable<String, Object>>();
469

    
470
		DBConnectionPool connPool = DBConnectionPool.getInstance();
471
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
472
		int serialNumber = dbconn.getCheckOutSerialNumber();
473

    
474
		PreparedStatement pstmt = null;
475

    
476
		debug("Selecting from db: " + sqlStatement);
477
		pstmt = dbconn.prepareStatement(sqlStatement);
478
		pstmt.execute();
479

    
480
		ResultSet resultSet = pstmt.getResultSet();
481
		ResultSetMetaData rsMetaData = resultSet.getMetaData();
482
		int numColumns = rsMetaData.getColumnCount();
483
		debug("Number of data columns: " + numColumns);
484
		while (resultSet.next()) {
485
			Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
486
			for (int i = 1; i <= numColumns; i++) {
487
				if (resultSet.getObject(i) != null) {
488
					hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
489
				}
490
			}
491
			debug("adding data row to result vector");
492
			resultVector.add(hashTable);
493
		}
494

    
495
		resultSet.close();
496
		pstmt.close();
497
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
498

    
499
		return resultVector;
500
	}
501
	
502
	protected static void dbQuery(String sqlStatement, String methodName)
503
			throws SQLException {
504

    
505
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
506
		int serialNumber = dbconn.getCheckOutSerialNumber();
507

    
508
		PreparedStatement statement = dbconn.prepareStatement(sqlStatement);
509
		debug("Executing against db: " + sqlStatement);
510
		statement.executeQuery();
511

    
512
		statement.close();
513
		
514
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
515
	}
516
	
517
	protected static void dbUpdate(String sqlStatement, String methodName)
518
			throws SQLException {
519

    
520
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
521
		int serialNumber = dbconn.getCheckOutSerialNumber();
522

    
523
		PreparedStatement statement = dbconn.prepareStatement(sqlStatement);
524
		debug("Executing against db: " + sqlStatement);
525
		statement.executeUpdate();
526
		statement.close();
527

    
528
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
529
	}
530
	
531
	protected static void httpPost(String url, HashMap<String,String> paramMap) throws Exception {
532
		debug("Posting to: " + url);
533
		if (httpClient == null) {
534
			httpClient = new DefaultHttpClient();
535
		}
536
		String postResponse = RequestUtil.post(httpClient, url, paramMap);
537
		debug("Post response: " + postResponse);
538
		if (postResponse.contains("<error>")) {
539
			fail("Error posting to metacat: " + postResponse);
540
		}
541
	}
542
	
543
	protected static void resetHttpClient() {
544
		httpClient = null;
545
	}
546
	
547
	/**
548
	 * Create a unique docid for testing insert and update. Does not
549
	 * include the 'revision' part of the id.
550
	 * 
551
	 * @return a String docid based on the current date and time
552
	 */
553
	protected String generateDocumentId() {
554
		try {
555
			Thread.sleep(1010);
556
		} catch (InterruptedException ie) {
557
			debug("Could not sleep: " + ie.getMessage());
558
		}
559

    
560
		return DocumentUtil.generateDocumentId(prefix, 0);
561
	}
562
	
563
	/**
564
	 * Insert a document into metacat. The expected result is passed as result
565
	 */
566
	protected String insertDocumentId(String docid, String docText, boolean result,
567
			boolean expectKarmaException) {
568
		debug("insertDocumentId() - docid=" + docid + " expectedResult=" + result
569
				+ " expectKarmaException=" + expectKarmaException);
570
		String response = null;
571
		try {
572
			response = m.insert(docid, new StringReader(docText), null);
573
			if (result) {
574
				assertTrue(response, (response.indexOf("<success>") != -1));
575
				assertTrue(response, response.indexOf(docid) != -1);
576
			} else {
577
				assertTrue(response, (response.indexOf("<success>") == -1));
578
			}
579
			debug("insertDocid():  response=" + response);
580
		} catch (MetacatInaccessibleException mie) {
581
			fail("Metacat Inaccessible:\n" + mie.getMessage());
582
		} catch (InsufficientKarmaException ike) {
583
			if (!expectKarmaException) {
584
				fail("Insufficient karma:\n" + ike.getMessage());
585
			}
586
		} catch (MetacatException me) {
587
			fail("Metacat Error:\n" + me.getMessage());
588
		} catch (Exception e) {
589
			fail("General exception:\n" + e.getMessage());
590
		}
591
		return response;
592
	}
593

    
594
	/**
595
	 * Insert a document into metacat. The expected result is passed as result
596
	 */
597

    
598
	protected String uploadDocumentId(String docid, String filePath, boolean result,
599
			boolean expectedKarmaException) {
600
		debug("uploadDocumentId() - docid=" + docid + " filePath=" + filePath
601
				+ " expectedResult=" + result + " expectedKarmaException="
602
				+ expectedKarmaException);
603
		String response = null;
604
		try {
605
			response = m.upload(docid, new File(filePath));
606
			if (result) {
607
				assertTrue(response, (response.indexOf("<success>") != -1));
608
				assertTrue(response, response.indexOf(docid) != -1);
609
			} else {
610
				assertTrue(response, (response.indexOf("<success>") == -1));
611
			}
612
			debug("uploadDocid():  response=" + response);
613
		} catch (MetacatInaccessibleException mie) {
614
			fail("Metacat Inaccessible:\n" + mie.getMessage());
615
		} catch (InsufficientKarmaException ike) {
616
			if (!expectedKarmaException) {
617
				fail("Insufficient karma:\n" + ike.getMessage());
618
			}
619
		} catch (MetacatException me) {
620
			if (result) {
621
				fail("Metacat Error:\n" + me.getMessage());
622
			} else {
623
				debug("Metacat Error:\n" + me.getMessage());
624
			}
625
		} catch (Exception e) {
626
			fail("General exception:\n" + e.getMessage());
627
		}
628
		return response;
629
	}
630

    
631
	/**
632
	 * Update a document in metacat. The expected result is passed as result
633
	 */
634
	protected String updateDocumentId(String docid, String docText, boolean result,
635
			boolean expectedKarmaFailure) {
636
		debug("updateDocumentId() - docid=" + docid + " expectedResult=" + result
637
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
638
		String response = null;
639
		try {
640
			response = m.update(docid, new StringReader(testdocument), null);
641
			debug("updateDocumentId() - response=" + response);
642
			
643
			if (result) {
644
				assertTrue(response, (response.indexOf("<success>") != -1));
645
				assertTrue(response, response.indexOf(docid) != -1);
646
			} else {
647
				assertTrue(response, (response.indexOf("<success>") == -1));
648
			}			
649
		} catch (MetacatInaccessibleException mie) {
650
			fail("Metacat Inaccessible:\n" + mie.getMessage());
651
		} catch (InsufficientKarmaException ike) {
652
			if (!expectedKarmaFailure) {
653
				fail("Insufficient karma:\n" + ike.getMessage());
654
			}
655
		} catch (MetacatException me) {
656
			if (result) {
657
				fail("Metacat Error:\n" + me.getMessage());
658
			} else {
659
				debug("Metacat Error:\n" + me.getMessage());
660
			}
661
		} catch (Exception e) {
662
			fail("General exception:\n" + e.getMessage());
663
		}
664

    
665
		return response;
666
	}
667

    
668
	/**
669
	 * Delete a document into metacat. The expected result is passed as result
670
	 */
671
	protected void deleteDocumentId(String docid, boolean result, boolean expectedKarmaFailure) {
672
		debug("deleteDocumentId() - docid=" + docid + " expectedResult=" + result
673
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
674
		try {
675
			Thread.sleep(5000);
676
			String response = m.delete(docid);
677
			debug("deleteDocumentId() -  response=" + response);
678
			
679
			if (result) {
680
				assertTrue(response, response.indexOf("<success>") != -1);
681
			} else {
682
				assertTrue(response, response.indexOf("<success>") == -1);
683
			}
684
		} catch (MetacatInaccessibleException mie) {
685
			fail("Metacat Inaccessible:\n" + mie.getMessage());
686
		} catch (InsufficientKarmaException ike) {
687
			if (!expectedKarmaFailure) {
688
				fail("Insufficient karma:\n" + ike.getMessage());
689
			}
690
		} catch (MetacatException me) {
691
			if (result) {
692
				fail("Metacat Error:\n" + me.getMessage());
693
			} else {
694
				debug("Metacat Error:\n" + me.getMessage());
695
			}
696
		} catch (Exception e) {
697
			fail("General exception:\n" + e.getMessage());
698
		}
699
	}
700
	
701
	/**
702
	 * Read a document from metacat and check if it is equal to a given string.
703
	 * The expected result is passed as result
704
	 */
705
	protected void readDocumentIdWhichEqualsDoc(String docid, String testDoc, boolean result,
706
			boolean expectedKarmaFailure) {
707
		debug("readDocumentIdWhichEqualsDoc() - docid=" + docid + " expectedResult=" + result
708
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
709
		try {
710
			Reader r = new InputStreamReader(m.read(docid));
711
			String doc = IOUtil.getAsString(r, true);
712
			debug("readDocumentIdWhichEqualsDoc() -  doc=" + doc);
713
			
714
			if (result) {
715

    
716
				if (!testDoc.equals(doc)) {
717
					System.out.println("doc ***********************");
718
					System.out.println(doc);
719
					System.out.println("end doc ***********************");
720
					System.out.println("testDoc ***********************");
721
					System.out.println(testDoc);
722
					System.out.println("end testDoc ***********************");
723
				}
724

    
725
				assertTrue(testDoc.equals(doc));
726
			} else {
727
				assertTrue(doc.indexOf("<error>") != -1);
728
			}
729
		} catch (MetacatInaccessibleException mie) {
730
			fail("Metacat Inaccessible:\n" + mie.getMessage());
731
		} catch (InsufficientKarmaException ike) {
732
			if (!expectedKarmaFailure) {
733
				fail("Insufficient karma:\n" + ike.getMessage());
734
			}
735
		} catch (MetacatException me) {
736
			fail("Metacat Error:\n" + me.getMessage());
737
		} catch (Exception e) {
738
			fail("General exception:\n" + e.getMessage());
739
		}
740

    
741
	}
742

    
743
}
    (1-1/1)