Project

General

Profile

« Previous | Next » 

Revision 4512

Added by daigle about 16 years ago

Pulled common eml creation and metacat client methods into the MCTestCase base class. Eventually, all test cases should use these common methods.

View differences:

test/edu/ucsb/nceas/MCTestCase.java
27 27

  
28 28
import junit.framework.TestCase;
29 29

  
30
import java.io.File;
30 31
import java.io.IOException;
32
import java.io.Reader;
33
import java.io.StringReader;
31 34
import java.sql.PreparedStatement;
32 35
import java.sql.ResultSet;
33 36
import java.sql.ResultSetMetaData;
34 37
import java.sql.SQLException;
35 38
import java.sql.Statement;
39
import java.util.Calendar;
40
import java.util.Date;
41
import java.util.GregorianCalendar;
36 42
import java.util.HashMap;
37 43
import java.util.Hashtable;
44
import java.util.SimpleTimeZone;
45
import java.util.TimeZone;
38 46
import java.util.Vector;
39 47

  
40 48
import org.apache.commons.httpclient.HttpClient;
41 49

  
42 50
import edu.ucsb.nceas.metacat.DBConnection;
43 51
import edu.ucsb.nceas.metacat.DBConnectionPool;
52
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;
44 57
import edu.ucsb.nceas.metacat.service.PropertyService;
45 58
import edu.ucsb.nceas.metacat.service.ServiceException;
46 59
import edu.ucsb.nceas.metacat.util.RequestUtil;
60
import edu.ucsb.nceas.utilities.IOUtil;
47 61
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48 62
import edu.ucsb.nceas.utilities.SortedProperties;
49 63

  
......
62 76
	protected boolean SUCCESS = true;
63 77
	protected boolean FAILURE = false;
64 78
	
79
	protected static final String ALLOWFIRST = "allowFirst";
80
	protected static final String DENYFIRST = "denyFirst";
81
	
82
	protected String prefix = "test";
83
	protected String testdocument = "";
84
	
65 85
	protected static HttpClient httpClient = null;
86
	
87
	protected static boolean metacatConnectionNeeded = false;
88
	protected Metacat m;
66 89

  
90
	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
	
67 96
	static {
68 97
		try {
69 98
			SortedProperties testProperties = 
......
73 102
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
74 103
		    String printDebugString = PropertyService.getProperty("test.printdebug");
75 104
		    printDebug = Boolean.parseBoolean(printDebugString);
105

  
106
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
107
			username = PropertyService.getProperty("test.mcUser");
108
			password = PropertyService.getProperty("test.mcPassword");
109
			anotheruser = PropertyService.getProperty("test.mcAnotherUser");
110
			anotherpassword = PropertyService.getProperty("test.mcAnotherPassword");
76 111
		} catch (IOException ioe) {
77 112
			System.err.println("Could not read property file in static block: " 
78 113
					+ ioe.getMessage());
......
84 119
					+ se.getMessage());
85 120
		}
86 121
	}
122
	
123
	// header blocks
124
	protected String testEml_200_Header = "<?xml version=\"1.0\"?><eml:eml"
125
		+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\""
126
		+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
127
		+ " packageId=\"eml.1.1\" system=\"knb\""
128
		+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\""
129
		+ " scope=\"system\">";
130
	
131
	protected String testEml_201_Header = "<?xml version=\"1.0\"?><eml:eml"
132
		+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\""
133
		+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
134
		+ " packageId=\"eml.1.1\" system=\"knb\""
135
		+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\""
136
		+ " scope=\"system\">";
137
	
138
	protected String testEml_210_Header = "<?xml version=\"1.0\"?><eml:eml"
139
			+ " xmlns:eml=\"eml://ecoinformatics.org/eml-2.1.0\""
140
			+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
141
			+ " packageId=\"eml.1.1\" system=\"knb\""
142
			+ " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.1.0 eml.xsd\""
143
			+ " scope=\"system\">";
87 144

  
145
	protected String testEmlCreatorBlock = "<creator scope=\"document\">                                       "
146
			+ " <individualName>                                                  "
147
			+ "    <surName>Smith</surName>                                       "
148
			+ " </individualName>                                                 "
149
			+ "</creator>                                                         ";
150

  
151
	protected String testEmlContactBlock = "<contact scope=\"document\">                                       "
152
			+ " <individualName>                                                  "
153
			+ "    <surName>Jackson</surName>                                     "
154
			+ " </individualName>                                                 "
155
			+ "</contact>                                                         ";
156

  
157
	protected String testEmlInlineBlock1 = "<inline>                                                           "
158
			+ "  <admin>                                                          "
159
			+ "    <contact>                                                      "
160
			+ "      <name>Operator</name>                                        "
161
			+ "      <institution>PSI</institution>                               "
162
			+ "    </contact>                                                     "
163
			+ "  </admin>                                                         "
164
			+ "</inline>                                                          ";
165

  
166
	protected String testEmlInlineBlock2 = "<inline>                                                           "
167
			+ "  <instrument>                                                     "
168
			+ "    <instName>LCQ</instName>                                       "
169
			+ "    <source type=\"ESI\"></source>                                 "
170
			+ "    <detector type=\"EM\"></detector>                              "
171
			+ "  </instrument>                                                    "
172
			+ "</inline>                                                          ";
173

  
174
	/*
175
	 * Retrus an access block base on params passed and the defaul perm order -
176
	 * allow first
177
	 */
178
	protected String getAccessBlock(String principal, boolean grantAccess, boolean read,
179
			boolean write, boolean changePermission, boolean all) {
180
		return getAccessBlock(principal, grantAccess, read, write, changePermission, all,
181
				ALLOWFIRST);
182
	}
183

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

  
193
		accessBlock += generateOneAccessRule(principal, grantAccess, read, write,
194
				changePermission, all);
195
		accessBlock += "</access>";
196

  
197
		return accessBlock;
198

  
199
	}
200

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

  
214
			}
215
		}
216
		accessBlock += "</access>";
217
		return accessBlock;
218
	}
219

  
220
	/*
221
	 * Generates a access rule for given parameter. Note this xml portion
222
	 * doesn't include <access></access>
223
	 */
224
	protected String generateOneAccessRule(String principal, boolean grantAccess,
225
			boolean read, boolean write, boolean changePermission, boolean all) {
226
		String accessBlock = "";
227

  
228
		if (grantAccess) {
229
			accessBlock = "<allow>";
230
		} else {
231
			accessBlock = "<deny>";
232
		}
233

  
234
		accessBlock = accessBlock + "<principal>" + principal + "</principal>";
235

  
236
		if (all) {
237
			accessBlock += "<permission>all</permission>";
238
		} else {
239
			if (read) {
240
				accessBlock += "<permission>read</permission>";
241
			}
242
			if (write) {
243
				accessBlock += "<permission>write</permission>";
244
			}
245
			if (changePermission) {
246
				accessBlock += "<permission>changePermission</permission>";
247
			}
248
		}
249

  
250
		if (grantAccess) {
251
			accessBlock += "</allow>";
252
		} else {
253
			accessBlock += "</deny>";
254
		}
255
		return accessBlock;
256

  
257
	}
258

  
259
	/**
260
	 * This function returns a valid eml document with no access rules 
261
	 */
262
	protected String getTestEmlDoc(String title, String emlVersion, String inlineData1,
263
			String inlineData2, String onlineUrl1, String onlineUrl2,
264
			String docAccessBlock, String inlineAccessBlock1, String inlineAccessBlock2,
265
			String onlineAccessBlock1, String onlineAccessBlock2) {
266

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

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

  
317
		// if this is a 2.0.X document, the document level access block sits
318
		// inside the dataset element.
319
		if (docAccessBlock != null && 
320
				(emlVersion.equals(EML2_0_0) || emlVersion.equals(EML2_0_1))) {
321
			testDocument += docAccessBlock;
322
		}
323

  
324
		testDocument += "</dataset>";
325

  
326
		if (inlineAccessBlock1 != null) {
327
			testDocument += "<additionalMetadata>";
328
			testDocument += "<describes>inlineEntity1</describes>";
329
			testDocument += inlineAccessBlock1;
330
			testDocument += "</additionalMetadata>";
331
		}
332

  
333
		if (inlineAccessBlock2 != null) {
334
			testDocument += "<additionalMetadata>";
335
			testDocument += "<describes>inlineEntity2</describes>";
336
			testDocument += inlineAccessBlock2;
337
			testDocument += "</additionalMetadata>";
338
		}
339

  
340
		if (onlineAccessBlock1 != null) {
341
			testDocument += "<additionalMetadata>";
342
			testDocument += "<describes>onlineEntity1</describes>";
343
			testDocument += onlineAccessBlock1;
344
			testDocument += "</additionalMetadata>";
345
		}
346

  
347
		if (onlineAccessBlock2 != null) {
348
			testDocument += "<additionalMetadata>";
349
			testDocument += "<describes>onlineEntity2</describes>";
350
			testDocument += onlineAccessBlock2;
351
			testDocument += "</additionalMetadata>";
352
		}
353

  
354
		testDocument += "</eml:eml>";
355

  
356
		// System.out.println("Returning following document" + testDocument);
357
		return testDocument;
358
	}
359

  
360
	
88 361
    /**
89 362
     * Constructor to build the test
90 363
     */
......
101 374
        super(name);
102 375
    }
103 376
    
377
	/**
378
	 * Establish a testing framework by initializing appropriate objects
379
	 */
380
	protected void setUp() throws Exception {
381
		try {
382
			if (metacatConnectionNeeded)
383
			debug("Test Metacat: " + metacatUrl);
384
			m = MetacatFactory.createMetacatConnection(metacatUrl);
385
		} catch (MetacatInaccessibleException mie) {
386
			System.err.println("Metacat is: " + metacatUrl);
387
			fail("Metacat connection failed." + mie.getMessage());
388
		}
389
	}
390
    
104 391
    protected static void debug(String debugMessage) {
105 392
    	if (printDebug) {
106 393
    		System.err.println(debugMessage);
......
192 479
	protected static void resetHttpClient() {
193 480
		httpClient = null;
194 481
	}
482
	
483
	/**
484
	 * Create a unique docid for testing insert and update. Does not
485
	 * include the 'revision' part of the id.
486
	 * 
487
	 * @return a String docid based on the current date and time
488
	 */
489
	protected String generateDocumentId() {
490
		try {
491
			Thread.sleep(1010);
492
		} catch (InterruptedException ie) {
493
			debug("Could not sleep: " + ie.getMessage());
494
		}
495
		
496
		StringBuffer docid = new StringBuffer(prefix);
497
		docid.append(".");
498

  
499
		// Create a calendar to get the date formatted properly
500
		String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
501
		SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
502
		pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
503
		pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
504
		Calendar calendar = new GregorianCalendar(pdt);
505
		Date trialTime = new Date();
506
		calendar.setTime(trialTime);
507
		docid.append(calendar.get(Calendar.YEAR));
508
		docid.append(calendar.get(Calendar.DAY_OF_YEAR));
509
		docid.append(calendar.get(Calendar.HOUR_OF_DAY));
510
		docid.append(calendar.get(Calendar.MINUTE));
511
		docid.append(calendar.get(Calendar.SECOND));
512

  
513
		return docid.toString();
514
	}
515
	
516
	/**
517
	 * Insert a document into metacat. The expected result is passed as result
518
	 */
519

  
520
	protected String insertDocumentId(String docid, String docText, boolean result,
521
			boolean expectKarmaException) {
522
		debug("insertDocid(): docid=" + docid + " expectedResult=" + result
523
				+ " expectKarmaException=" + expectKarmaException);
524
		String response = null;
525
		try {
526
			response = m.insert(docid, new StringReader(testdocument), null);
527
			if (result) {
528
				assertTrue(response, (response.indexOf("<success>") != -1));
529
				assertTrue(response, response.indexOf(docid) != -1);
530
			} else {
531
				assertTrue(response, (response.indexOf("<success>") == -1));
532
			}
533
			debug("insertDocid():  response=" + response);
534
		} catch (MetacatInaccessibleException mie) {
535
			fail("Metacat Inaccessible:\n" + mie.getMessage());
536
		} catch (InsufficientKarmaException ike) {
537
			if (!expectKarmaException) {
538
				fail("Insufficient karma:\n" + ike.getMessage());
539
			}
540
		} catch (MetacatException me) {
541
			fail("Metacat Error:\n" + me.getMessage());
542
		} catch (Exception e) {
543
			fail("General exception:\n" + e.getMessage());
544
		}
545
		return response;
546
	}
547

  
548
	/**
549
	 * Insert a document into metacat. The expected result is passed as result
550
	 */
551

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

  
585
	/**
586
	 * Update a document in metacat. The expected result is passed as result
587
	 */
588
	protected String updateDocumentId(String docid, String docText, boolean result,
589
			boolean expectedKarmaFailure) {
590
		debug("updateDocid(): docid=" + docid + " expectedResult=" + result
591
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
592
		String response = null;
593
		try {
594
			response = m.update(docid, new StringReader(testdocument), null);
595

  
596
			if (result) {
597
				assertTrue(response, (response.indexOf("<success>") != -1));
598
				assertTrue(response, response.indexOf(docid) != -1);
599
			} else {
600
				assertTrue(response, (response.indexOf("<success>") == -1));
601
			}
602
			debug("updateDocid(): response=" + response);
603
		} catch (MetacatInaccessibleException mie) {
604
			fail("Metacat Inaccessible:\n" + mie.getMessage());
605
		} catch (InsufficientKarmaException ike) {
606
			if (!expectedKarmaFailure) {
607
				fail("Insufficient karma:\n" + ike.getMessage());
608
			}
609
		} catch (MetacatException me) {
610
			if (result) {
611
				fail("Metacat Error:\n" + me.getMessage());
612
			} else {
613
				debug("Metacat Error:\n" + me.getMessage());
614
			}
615
		} catch (Exception e) {
616
			fail("General exception:\n" + e.getMessage());
617
		}
618

  
619
		return response;
620
	}
621

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

  
667
				if (!testDoc.equals(doc)) {
668
					System.out.println("doc ***********************");
669
					System.out.println(doc);
670
					System.out.println("end doc ***********************");
671
					System.out.println("testDoc ***********************");
672
					System.out.println(testDoc);
673
					System.out.println("end testDoc ***********************");
674
				}
675

  
676
				assertTrue(testDoc.equals(doc));
677
			} else {
678
				assertTrue(doc.indexOf("<error>") != -1);
679
			}
680
			debug("readDocidWhichEqualsDoc():  doc=" + doc);
681
		} catch (MetacatInaccessibleException mie) {
682
			fail("Metacat Inaccessible:\n" + mie.getMessage());
683
		} catch (InsufficientKarmaException ike) {
684
			if (!expectedKarmaFailure) {
685
				fail("Insufficient karma:\n" + ike.getMessage());
686
			}
687
		} catch (MetacatException me) {
688
			fail("Metacat Error:\n" + me.getMessage());
689
		} catch (Exception e) {
690
			fail("General exception:\n" + e.getMessage());
691
		}
692

  
693
	}
694

  
195 695
}

Also available in: Unified diff