Project

General

Profile

« Previous | Next » 

Revision 5114

Added by daigle about 15 years ago

Beef up comments

View differences:

test/edu/ucsb/nceas/metacattest/AccessAPITest.java
52 52
import org.xml.sax.XMLReader;
53 53
import org.xml.sax.helpers.XMLReaderFactory;
54 54

  
55

  
56 55
/**
57 56
 * A JUnit test for testing Access Control API in Metacat.  Note that this is different
58 57
 * than the Access Control test.  That test is more concerned with making sure different
......
109 108
	}
110 109

  
111 110
	/**
112
	 * Test adding a single access record.
111
	 * Test adding access records for single principals.
113 112
	 */
114 113
	public void testSetSingleAccess() {
115 114
		String principal = null;
......
147 146
			accessDAO.setPermOrder(AccessControlInterface.ALLOWFIRST);
148 147
			accessDAOList.add(accessDAO);
149 148
			
149
			// get the access control for this doc id from metacat and make sure this
150
			// access record is in that list.
150 151
			accessXML = m.getAccessControl(docId);
151 152
			realAccessDAOList = getAccessDAOList(docId, accessXML);						
152 153
			compareAccessDAOs(realAccessDAOList, accessDAOList, false);	
......
178 179
	}
179 180
	
180 181
	/**
181
	 * Test setting a block of access for a document
182
	 * Test setting a block of access for a document.  Each call should replace the access for that
183
	 * document.
182 184
	 */
183 185
	public void testSetBlockAccess() {
184 186
		String accessBlockXML = null;
......
189 191
		try {
190 192
			debug("AccessAPITest.testSetBlockAccess - Running");
191 193
					
194
			// log in
192 195
			m.login(username, password);
193 196

  
197
			// insert a document for us to test with
194 198
			String docId = generateDocumentId() + ".1";
195 199
			String testDoc = getTestDocFromFile(testdatadir + "accessBlockTestFile1.xml");
196 200
			// debug("test doc: " + testDoc);
197 201
			insertDocumentId(docId, testDoc, SUCCESS, false);
198 202
			
203
			// replace access for doc using the access section in accessBlock1.xml, 
204
			// then test that the access was updated and that is the only access
205
			// for the doc
199 206
			debug("AccessAPITest.testSetBlockAccess - Test 1 using accessBlock1.xml");			
200 207
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock1.xml");
201 208
			setBlockAccess(docId, accessBlockXML, true);
......
205 212
			debugAccessDAOList(accessBlockDAOList);
206 213
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
207 214

  
215
			// replace access for doc using the access section in accessBlock2.xml, 
216
			// then test that the access was updated and that is the only access
217
			// for the doc
208 218
			debug("AccessAPITest.testSetBlockAccess - Test 2 using accessBlock2.xml");			
209 219
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock2.xml");
210 220
			setBlockAccess(docId, accessBlockXML, true);
......
214 224
			debugAccessDAOList(accessBlockDAOList);
215 225
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
216 226

  
227
			// replace access for doc using the access section in accessBlock3.xml, 
228
			// then test that the access was updated and that is the only access
229
			// for the doc
217 230
			debug("AccessAPITest.testSetBlockAccess - Test 3 using accessBlock3.xml");			
218 231
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock3.xml");
219 232
			setBlockAccess(docId, accessBlockXML, true);
......
223 236
			debugAccessDAOList(accessBlockDAOList);
224 237
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
225 238
			
239
			// replace access for doc using the access section in accessBlock4.xml, 
240
			// then test that the access was updated and that is the only access
241
			// for the doc
226 242
			debug("AccessAPITest.testSetBlockAccess - Test 4 using accessBlock4.xml");			
227 243
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock4.xml");
228 244
			setBlockAccess(docId, accessBlockXML, true);
......
232 248
			debugAccessDAOList(accessBlockDAOList);
233 249
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
234 250
			
251
			// replace access for doc using the access section in accessBlock5.xml, 
252
			// then test that the access was updated and that is the only access
253
			// for the doc
235 254
			debug("AccessAPITest.testSetBlockAccess - Test 5 using accessBlock5.xml");			
236 255
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock5.xml");
237 256
			setBlockAccess(docId, accessBlockXML, true);
......
241 260
			debugAccessDAOList(accessBlockDAOList);
242 261
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
243 262
			
263
			// replace access for doc using the access section in accessBlock6.xml, 
264
			// then test that the access was updated and that is the only access
265
			// for the doc
244 266
			debug("AccessAPITest.testSetBlockAccess - Test 6 using accessBlock6.xml");			
245 267
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock6.xml");
246 268
			setBlockAccess(docId, accessBlockXML, true);
......
250 272
			debugAccessDAOList(accessBlockDAOList);
251 273
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
252 274
			
275
			// replace access for doc using the access section in accessBlock7.xml, 
276
			// then test that the access was updated and that is the only access
277
			// for the doc
253 278
			debug("AccessAPITest.testSetBlockAccess - Test 7 using accessBlock7.xml");			
254 279
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock7.xml");
255 280
			setBlockAccess(docId, accessBlockXML, true);
......
259 284
			debugAccessDAOList(accessBlockDAOList);
260 285
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
261 286
			
287
			// replace access for doc using the access section in accessBlock8.xml, 
288
			// then test that the access was updated and that is the only access
289
			// for the doc
262 290
			debug("AccessAPITest.testSetBlockAccess - Test 8 using accessBlock8.xml");			
263 291
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock8.xml");
264 292
			setBlockAccess(docId, accessBlockXML, true);
......
268 296
			debugAccessDAOList(accessBlockDAOList);
269 297
			compareAccessDAOs(realAccessDAOList, accessBlockDAOList, true);
270 298
			
299
			// replace access for doc using the access section in accessBlock9.xml, 
300
			// then test that the access was updated and that is the only access
301
			// for the doc
271 302
			debug("AccessAPITest.testSetBlockAccess - Test 9 using accessBlock9.xml");			
272 303
			accessBlockXML = getTestDocFromFile(testdatadir + "accessBlock9.xml");
273 304
			setBlockAccess(docId, accessBlockXML, true);
......
291 322
			fail("AccessAPITest.testSetBlockAccess - Metacat Inaccessible Error: " + mie.getMessage());
292 323
		}
293 324
	}
294
	
325

  
295 326
	/**
296
	 * Update a document in metacat. The expected result is passed as result
327
	 * Insert a single row of access for a document.
328
	 * 
329
	 * @param docId
330
	 *            the id of the document to update
331
	 * @param principal
332
	 *            the principal credentails
333
	 * @param permission
334
	 *            the permission
335
	 * @param permType
336
	 *            the permission type
337
	 * @param permOrder
338
	 *            the permission order
339
	 * @param successExpected
340
	 *            if true, we expect the insert to succeed, otherwise, we expect
341
	 *            it to fail
342
	 * @return the metacat response xml
297 343
	 */
298 344
	private String setSingleAccess(String docId, String principal, String permission, 
299 345
			String permType, String permOrder, boolean successExpected) {
......
302 348
				", permOrder: " + permOrder + ", errorExpected: " + successExpected);
303 349
		String response = null;
304 350
		try {
351
			// set the access using metacat client
305 352
			response = m.setAccess(docId, principal, permission, permType, permOrder); 
306 353

  
307 354
			debug("AccessAPITest.setSingleAccess - response: " + response);
......
329 376
	}
330 377
	
331 378
	/**
332
	 * Update a document in metacat. The expected result is passed as result
379
	 * Replace a block of access
380
	 * 
381
	 * @param docId
382
	 *            the id of the doc we want to update
383
	 * @param accessBlockXML
384
	 *            the access xml section
385
	 * @param successExpected
386
	 *            if true, we expect the insert to succeed, otherwise, we expect
387
	 *            it to fail
388
	 * @return the metacat response xml
333 389
	 */
334 390
	private String setBlockAccess(String docId, String accessBlockXML, boolean successExpected) {
335 391
		debug("AccessAPITest.setBlockAccess - docid: " + docId + ", accessBlockXML: " + 
336 392
				accessBlockXML + ", errorExpected: " + successExpected);
337 393
		String response = null;
338 394
		try {
395
			// set the access using metacat client
339 396
			response = m.setAccess(docId, accessBlockXML); 
340 397

  
341 398
			debug("AccessAPITest.setBlockAccess - response: " + response);
......
362 419
		return response;
363 420
	}
364 421
	
422
	/**
423
	 * Compare two lists of XML Access DAO objects. The first list is the master
424
	 * list and the second is the sub list. All elements of the sub list must be
425
	 * in the master list or a failed assertion is generated. If the exactMatch
426
	 * parameter is true, the master and sub lists must be the same, otherwise,
427
	 * the sub list can be a subset of the master list.
428
	 * 
429
	 * @param mainAccessDAOList
430
	 *            The main list of DAOs
431
	 * @param subAccessDAOList
432
	 *            The sub list of DAOs
433
	 * @param exactMatch
434
	 *            if true, the master and sub lists must be exactly the same,
435
	 *            otherwise, the sublist must only be a subset of the master
436
	 */
365 437
	private void compareAccessDAOs(Vector<XMLAccessDAO> mainAccessDAOList, 
366 438
			Vector<XMLAccessDAO> subAccessDAOList, boolean exactMatch) {
367 439
		
......
371 443
		//debug("\naccess DAOs from database: ");
372 444
		//debugAccessDAOList(mainAccessDAOList);
373 445
		
446
		// if exactMatch is true, both lists must be the same size.  The next section will
447
		// make sure elements match.
374 448
		if (exactMatch) {
375 449
			if (mainAccessDAOList.size() != subAccessDAOList.size()) {
376 450
				fail("AccessAPITest.compareAccessDAOs - access DAO list sizes do not match.  " + 
......
379 453
			}
380 454
		}
381 455
		
456
		// iterate through the sub list and make sure all its elements are in the master list
382 457
		for (XMLAccessDAO subAccessDAO : subAccessDAOList) {
383 458
			boolean matchFound = false;
384 459
			for (XMLAccessDAO mainAccessDAO : mainAccessDAOList) {				
......
399 474
		}
400 475
	}
401 476
	
477
	/**
478
	 * Get a vector of access DAO objects from an EML 2.1.0-like access section
479
	 * of xml
480
	 * 
481
	 * @param docId
482
	 *            the doc id that we are getting access for. this makes it
483
	 *            easier to create a DocInfoHandler object.
484
	 * @param accessXML
485
	 *            the access xml we want to parse.
486
	 * @return a list of XML access DAO objects.
487
	 */
402 488
	private Vector<XMLAccessDAO> getAccessDAOList(String docId, String accessXML) 
403 489
			throws AccessControlException {
404 490
		
......
408 494
		ContentHandler chandler = docInfoHandler;
409 495

  
410 496
		try {
411
			// Get an instance of the parser
497
			// Get an instance of the parser.  the DocInfoHandler will handle 
498
			// the extraction of access info.
412 499
			String parserName = PropertyService.getProperty("xml.saxparser");
413 500
			parser = XMLReaderFactory.createXMLReader(parserName);
414 501
	
......
434 521
        return accessControlList;
435 522
	}
436 523
	
524
	/**
525
	 * Print out the contents of an access DAO list
526
	 * @param accessDAOList
527
	 */
437 528
	private void debugAccessDAOList(Vector<XMLAccessDAO> accessDAOList) {
438 529
		for (XMLAccessDAO xmlAccessDAO : accessDAOList) {
439 530
			debug("Doc ID:      " + xmlAccessDAO.getDocId());

Also available in: Unified diff