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: 2010-12-08 16:59:35 -0800 (Wed, 08 Dec 2010) $'
9
 * '$Revision: 5709 $'
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
	protected static String EML2_1_1 = "EML2_1_1";
80

    
81
	
82
	protected boolean SUCCESS = true;
83
	protected boolean FAILURE = false;
84
	
85
	protected static final String ALLOWFIRST = "allowFirst";
86
	protected static final String DENYFIRST = "denyFirst";
87
	
88
	protected String testdatadir = "test/clienttestfiles/";
89
	protected String prefix = "test";
90
	protected String testdocument = "";
91
	
92
	protected static HttpClient httpClient = null;
93
	
94
	protected static boolean metacatConnectionNeeded = false;
95
	protected Metacat m;
96

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

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

    
160
	protected String testEmlCreatorBlock = "<creator scope=\"document\">                                       "
161
			+ " <individualName>                                                  "
162
			+ "    <surName>Smith</surName>                                       "
163
			+ " </individualName>                                                 "
164
			+ "</creator>                                                         ";
165

    
166
	protected String testEmlContactBlock = "<contact scope=\"document\">                                       "
167
			+ " <individualName>                                                  "
168
			+ "    <surName>Jackson</surName>                                     "
169
			+ " </individualName>                                                 "
170
			+ "</contact>                                                         ";
171

    
172
	protected String testEmlInlineBlock1 = "<inline>                                                           "
173
			+ "  <admin>                                                          "
174
			+ "    <contact>                                                      "
175
			+ "      <name>Operator</name>                                        "
176
			+ "      <institution>PSI</institution>                               "
177
			+ "    </contact>                                                     "
178
			+ "  </admin>                                                         "
179
			+ "</inline>                                                          ";
180

    
181
	protected String testEmlInlineBlock2 = "<inline>                                                           "
182
			+ "  <instrument>                                                     "
183
			+ "    <instName>LCQ</instName>                                       "
184
			+ "    <source type=\"ESI\"></source>                                 "
185
			+ "    <detector type=\"EM\"></detector>                              "
186
			+ "  </instrument>                                                    "
187
			+ "</inline>                                                          ";
188

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

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

    
208
		accessBlock += generateOneAccessRule(principal, grantAccess, read, write,
209
				changePermission, all);
210
		accessBlock += "</access>";
211

    
212
		return accessBlock;
213

    
214
	}
215

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

    
229
			}
230
		}
231
		accessBlock += "</access>";
232
		return accessBlock;
233
	}
234

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

    
243
		if (grantAccess) {
244
			accessBlock = "<allow>";
245
		} else {
246
			accessBlock = "<deny>";
247
		}
248

    
249
		accessBlock = accessBlock + "<principal>" + principal + "</principal>";
250

    
251
		if (all) {
252
			accessBlock += "<permission>all</permission>";
253
		} else {
254
			if (read) {
255
				accessBlock += "<permission>read</permission>";
256
			}
257
			if (write) {
258
				accessBlock += "<permission>write</permission>";
259
			}
260
			if (changePermission) {
261
				accessBlock += "<permission>changePermission</permission>";
262
			}
263
		}
264

    
265
		if (grantAccess) {
266
			accessBlock += "</allow>";
267
		} else {
268
			accessBlock += "</deny>";
269
		}
270
		return accessBlock;
271

    
272
	}
273

    
274
	/**
275
	 * This function returns a valid eml document with no access rules 
276
	 */
277
	protected String getTestEmlDoc(String title, String emlVersion, String inlineData1,
278
			String inlineData2, String onlineUrl1, String onlineUrl2,
279
			String docAccessBlock, String inlineAccessBlock1, String inlineAccessBlock2,
280
			String onlineAccessBlock1, String onlineAccessBlock2) {
281

    
282
		debug("getTestEmlDoc(): title=" + title + " inlineData1=" + inlineData1
283
				+ " inlineData2=" + inlineData2 + " onlineUrl1=" + onlineUrl1
284
				+ " onlineUrl2=" + onlineUrl2 + " docAccessBlock=" + docAccessBlock
285
				+ " inlineAccessBlock1=" + inlineAccessBlock1 + " inlineAccessBlock2="
286
				+ inlineAccessBlock2 + " onlineAccessBlock1=" + onlineAccessBlock1
287
				+ " onlineAccessBlock2=" + onlineAccessBlock2);
288
		String testDocument = "";
289
		String header;
290
		if (emlVersion == EML2_0_0) {
291
			header = testEml_200_Header;
292
		} else if (emlVersion == EML2_0_1) {
293
			header = testEml_201_Header;
294
		} else if (emlVersion == EML2_1_0) {
295
			header = testEml_210_Header;
296
		} else { // if (emlVersion == EML2_1_1) {
297
			header = testEml_211_Header;
298
		}
299
		testDocument += header;
300
		
301
		// if this is a 2.1.0+ document, the document level access block sits
302
		// at the same level and before the dataset element.
303
		if (docAccessBlock != null && (emlVersion.equals(EML2_1_0) || emlVersion.equals(EML2_1_1))) {
304
			testDocument += docAccessBlock;
305
		}
306
		
307
		testDocument += "<dataset scope=\"document\"><title>"
308
				+ title + "</title>" + testEmlCreatorBlock;
309

    
310
		if (inlineData1 != null) {
311
			testDocument = testDocument
312
					+ "<distribution scope=\"document\" id=\"inlineEntity1\">"
313
					+ inlineData1 + "</distribution>";
314
		}
315
		if (inlineData2 != null) {
316
			testDocument = testDocument
317
					+ "<distribution scope=\"document\" id=\"inlineEntity2\">"
318
					+ inlineData2 + "</distribution>";
319
		}
320
		if (onlineUrl1 != null) {
321
			testDocument = testDocument
322
					+ "<distribution scope=\"document\" id=\"onlineEntity1\">"
323
					+ "<online><url function=\"download\">" + onlineUrl1
324
					+ "</url></online></distribution>";
325
		}
326
		if (onlineUrl2 != null) {
327
			testDocument = testDocument
328
					+ "<distribution scope=\"document\" id=\"onlineEntity2\">"
329
					+ "<online><url function=\"download\">" + onlineUrl2
330
					+ "</url></online></distribution>";
331
		}
332
		testDocument += testEmlContactBlock;
333

    
334
		// if this is a 2.0.X document, the document level access block sits
335
		// inside the dataset element.
336
		if (docAccessBlock != null && 
337
				(emlVersion.equals(EML2_0_0) || emlVersion.equals(EML2_0_1))) {
338
			testDocument += docAccessBlock;
339
		}
340

    
341
		testDocument += "</dataset>";
342

    
343
		if (inlineAccessBlock1 != null) {
344
			testDocument += "<additionalMetadata>";
345
			testDocument += "<describes>inlineEntity1</describes>";
346
			testDocument += inlineAccessBlock1;
347
			testDocument += "</additionalMetadata>";
348
		}
349

    
350
		if (inlineAccessBlock2 != null) {
351
			testDocument += "<additionalMetadata>";
352
			testDocument += "<describes>inlineEntity2</describes>";
353
			testDocument += inlineAccessBlock2;
354
			testDocument += "</additionalMetadata>";
355
		}
356

    
357
		if (onlineAccessBlock1 != null) {
358
			testDocument += "<additionalMetadata>";
359
			testDocument += "<describes>onlineEntity1</describes>";
360
			testDocument += onlineAccessBlock1;
361
			testDocument += "</additionalMetadata>";
362
		}
363

    
364
		if (onlineAccessBlock2 != null) {
365
			testDocument += "<additionalMetadata>";
366
			testDocument += "<describes>onlineEntity2</describes>";
367
			testDocument += onlineAccessBlock2;
368
			testDocument += "</additionalMetadata>";
369
		}
370

    
371
		testDocument += "</eml:eml>";
372

    
373
		// System.out.println("Returning following document" + testDocument);
374
		return testDocument;
375
	}
376
	
377
	/**
378
	 * 
379
	 */
380
	protected String getTestDocFromFile(String filePath) throws IOException{
381
		StringBuffer output = new StringBuffer();
382
		
383
		FileReader fileReader = new FileReader(new File(filePath));
384
		BufferedReader bufferedReader = new BufferedReader(fileReader);
385
		String readLine = null;
386
		
387
		while ((readLine = bufferedReader.readLine()) != null) {
388
			output.append(readLine);
389
		}
390
		
391
		return output.toString();
392
	}
393

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

    
436
		DBConnectionPool connPool = DBConnectionPool.getInstance();
437
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
438
		int serialNumber = dbconn.getCheckOutSerialNumber();
439

    
440
		PreparedStatement pstmt = null;
441

    
442
		debug("Selecting from db: " + sqlStatement);
443
		pstmt = dbconn.prepareStatement(sqlStatement);
444
		pstmt.execute();
445

    
446
		ResultSet resultSet = pstmt.getResultSet();
447
		ResultSetMetaData rsMetaData = resultSet.getMetaData();
448
		int numColumns = rsMetaData.getColumnCount();
449
		debug("Number of data columns: " + numColumns);
450
		while (resultSet.next()) {
451
			Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
452
			for (int i = 1; i <= numColumns; i++) {
453
				if (resultSet.getObject(i) != null) {
454
					hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
455
				}
456
			}
457
			debug("adding data row to result vector");
458
			resultVector.add(hashTable);
459
		}
460

    
461
		resultSet.close();
462
		pstmt.close();
463
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
464

    
465
		return resultVector;
466
	}
467
	
468
	protected static void dbQuery(String sqlStatement, String methodName)
469
			throws SQLException {
470

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

    
475
		Statement statement = dbconn.createStatement();
476

    
477
		debug("Executing against db: " + sqlStatement);
478
		statement.executeQuery(sqlStatement);
479

    
480
		statement.close();
481
		
482
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
483
	}
484
	
485
	protected static void dbUpdate(String sqlStatement, String methodName)
486
			throws SQLException {
487

    
488
		DBConnectionPool connPool = DBConnectionPool.getInstance();
489
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
490
		int serialNumber = dbconn.getCheckOutSerialNumber();
491

    
492
		Statement statement = dbconn.createStatement();
493

    
494
		debug("Executing against db: " + sqlStatement);
495
		statement.executeUpdate(sqlStatement);
496

    
497
		statement.close();
498

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

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

    
565
	/**
566
	 * Insert a document into metacat. The expected result is passed as result
567
	 */
568

    
569
	protected String uploadDocumentId(String docid, String filePath, boolean result,
570
			boolean expectedKarmaException) {
571
		debug("uploadDocumentId() - docid=" + docid + " filePath=" + filePath
572
				+ " expectedResult=" + result + " expectedKarmaException="
573
				+ expectedKarmaException);
574
		String response = null;
575
		try {
576
			response = m.upload(docid, new File(filePath));
577
			if (result) {
578
				assertTrue(response, (response.indexOf("<success>") != -1));
579
				assertTrue(response, response.indexOf(docid) != -1);
580
			} else {
581
				assertTrue(response, (response.indexOf("<success>") == -1));
582
			}
583
			debug("uploadDocid():  response=" + response);
584
		} catch (MetacatInaccessibleException mie) {
585
			fail("Metacat Inaccessible:\n" + mie.getMessage());
586
		} catch (InsufficientKarmaException ike) {
587
			if (!expectedKarmaException) {
588
				fail("Insufficient karma:\n" + ike.getMessage());
589
			}
590
		} catch (MetacatException me) {
591
			if (result) {
592
				fail("Metacat Error:\n" + me.getMessage());
593
			} else {
594
				debug("Metacat Error:\n" + me.getMessage());
595
			}
596
		} catch (Exception e) {
597
			fail("General exception:\n" + e.getMessage());
598
		}
599
		return response;
600
	}
601

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

    
636
		return response;
637
	}
638

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

    
687
				if (!testDoc.equals(doc)) {
688
					System.out.println("doc ***********************");
689
					System.out.println(doc);
690
					System.out.println("end doc ***********************");
691
					System.out.println("testDoc ***********************");
692
					System.out.println(testDoc);
693
					System.out.println("end testDoc ***********************");
694
				}
695

    
696
				assertTrue(testDoc.equals(doc));
697
			} else {
698
				assertTrue(doc.indexOf("<error>") != -1);
699
			}
700
		} catch (MetacatInaccessibleException mie) {
701
			fail("Metacat Inaccessible:\n" + mie.getMessage());
702
		} catch (InsufficientKarmaException ike) {
703
			if (!expectedKarmaFailure) {
704
				fail("Insufficient karma:\n" + ike.getMessage());
705
			}
706
		} catch (MetacatException me) {
707
			fail("Metacat Error:\n" + me.getMessage());
708
		} catch (Exception e) {
709
			fail("General exception:\n" + e.getMessage());
710
		}
711

    
712
	}
713

    
714
}
    (1-1/1)