Project

General

Profile

« Previous | Next » 

Revision 8614

include a few tests for isEqual method. https://projects.ecoinformatics.org/ecoinfo/issues/6407

View differences:

test/edu/ucsb/nceas/metacat/admin/upgrade/dataone/SyncAccessPolicyTest.java
26 26
package edu.ucsb.nceas.metacat.admin.upgrade.dataone;
27 27

  
28 28
import java.io.ByteArrayInputStream;
29
import java.io.File;
30 29
import java.io.InputStream;
31
import java.io.StringReader;
32
import java.io.UnsupportedEncodingException;
33 30
import java.util.Hashtable;
34 31

  
35
import org.apache.http.client.HttpClient;
36
import org.apache.http.impl.client.DefaultHttpClient;
32
import junit.framework.Test;
33
import junit.framework.TestSuite;
34

  
37 35
import org.dataone.client.CNode;
38 36
import org.dataone.client.D1Client;
39
import org.dataone.service.exceptions.IdentifierNotUnique;
40
import org.dataone.service.exceptions.InsufficientResources;
41
import org.dataone.service.exceptions.InvalidRequest;
42
import org.dataone.service.exceptions.InvalidSystemMetadata;
43
import org.dataone.service.exceptions.InvalidToken;
44
import org.dataone.service.exceptions.NotAuthorized;
45
import org.dataone.service.exceptions.NotImplemented;
46 37
import org.dataone.service.exceptions.ServiceFailure;
47
import org.dataone.service.exceptions.UnsupportedType;
48 38
import org.dataone.service.types.v1.AccessPolicy;
49 39
import org.dataone.service.types.v1.AccessRule;
50 40
import org.dataone.service.types.v1.Identifier;
......
52 42
import org.dataone.service.types.v1.Session;
53 43
import org.dataone.service.types.v1.Subject;
54 44
import org.dataone.service.types.v1.SystemMetadata;
55
import org.dataone.service.util.Constants;
56 45
import org.junit.Before;
57 46

  
58
import edu.ucsb.nceas.MCTestCase;
59 47
import edu.ucsb.nceas.metacat.IdentifierManager;
60
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
61
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
62 48
import edu.ucsb.nceas.metacat.client.Metacat;
63 49
import edu.ucsb.nceas.metacat.client.MetacatFactory;
64 50
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
65
import edu.ucsb.nceas.metacat.dataone.CNodeServiceTest;
51
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
66 52
import edu.ucsb.nceas.metacat.dataone.MNodeService;
67
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
68 53
import edu.ucsb.nceas.metacat.dataone.SyncAccessPolicy;
69
import edu.ucsb.nceas.metacat.properties.PropertyService;
70
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
71
import edu.ucsb.nceas.metacat.util.RequestUtil;
72
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
73 54
import edu.ucsb.nceas.utilities.access.AccessControlInterface;
74
import junit.framework.Test;
75
import junit.framework.TestSuite;
76 55

  
77 56
/**
78 57
 * A JUnit test for testing syncing access policies between MN -> CN after local
......
120 99
	public static Test suite() {
121 100
		TestSuite suite = new TestSuite();
122 101
		suite.addTest(new SyncAccessPolicyTest("initialize"));
102
		suite.addTest(new SyncAccessPolicyTest("testIsEqual"));
123 103
		suite.addTest(new SyncAccessPolicyTest("testSyncAccessPolicy"));
124 104

  
125 105
		return suite;
......
315 295

  
316 296
		debug("Done running testSyncAccessPolicy");
317 297
	}
298
	
299
	public void testIsEqual() {
300
		AccessPolicy ap1 = new AccessPolicy();
301
		AccessRule ar1 = new AccessRule();
302
		ar1.addPermission(Permission.READ);
303
		Subject subject1 = new Subject();
304
		subject1.setValue(username);
305
		ar1.addSubject(subject1);
306
		ap1.addAllow(ar1);
307
		
308
		AccessPolicy ap2 = new AccessPolicy();
309
		AccessRule ar2 = new AccessRule();
310
		ar2.addPermission(Permission.READ);
311
		Subject subject2 = new Subject();
312
		subject2.setValue(username);
313
		ar2.addSubject(subject2);
314
		ap2.addAllow(ar2);
315
		
316
		boolean isEqual = false;
317
		SyncAccessPolicy syncAP = new SyncAccessPolicy();
318
		
319
		// try something that should be true
320
		isEqual = syncAP.isEqual(ap1, ap2);
321
		assertTrue(isEqual);
322
		
323
		// try something that makes them not equal
324
		Subject subject3 = new Subject();
325
		subject3.setValue(anotheruser);
326
		ar2.addSubject(subject3);
327
		
328
		isEqual = syncAP.isEqual(ap1, ap2);
329
		assertFalse(isEqual);
330
		
331
		isEqual = syncAP.isEqual(ap1, null);
332
		assertFalse(isEqual);
333
	}
318 334
}
src/edu/ucsb/nceas/metacat/dataone/SyncAccessPolicy.java
50 50
import org.dataone.service.exceptions.ServiceFailure;
51 51
import org.dataone.service.exceptions.VersionMismatch;
52 52
import org.dataone.service.types.v1.AccessPolicy;
53
import org.dataone.service.types.v1.AccessRule;
53 54
import org.dataone.service.types.v1.Identifier;
54 55
import org.dataone.service.types.v1.ObjectFormatIdentifier;
55 56
import org.dataone.service.types.v1.ObjectInfo;
......
63 64
import edu.ucsb.nceas.metacat.IdentifierManager;
64 65
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
65 66
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
66
import edu.ucsb.nceas.metacat.admin.AdminException;
67 67
import edu.ucsb.nceas.metacat.properties.PropertyService;
68 68
import edu.ucsb.nceas.metacat.shared.ServiceException;
69 69
import edu.ucsb.nceas.utilities.GeneralPropertyException;
......
414 414
	 */
415 415
	public boolean isEqual(AccessPolicy ap1, AccessPolicy ap2) {
416 416

  
417
		// can't check when either is null
418
		if (ap1 == null || ap2 == null) {
419
			return false;
420
		}
421
		
417 422
		// Access Policy -> Access Rule -> (Subject, Permission)
418 423
		// i.e. Subject="slaughter", Permission="read,write,changePermission"
419 424
		// Get the list of access rules for each access policy
420
		List<org.dataone.service.types.v1.AccessRule> allowList1 = ap1
425
		List<AccessRule> allowList1 = ap1
421 426
				.getAllowList();
422
		List<org.dataone.service.types.v1.AccessRule> allowList2 = ap2
427
		List<AccessRule> allowList2 = ap2
423 428
				.getAllowList();
424 429

  
425 430
		HashMap<Subject, Set<Permission>> userPerms1 = new HashMap<Subject, Set<Permission>>();
......
436 441
		Set<Permission> perms = null;
437 442
		// Process first access policy
438 443
		// Loop through access rules of this allowList
439
		for (org.dataone.service.types.v1.AccessRule accessRule : allowList1) {
444
		for (AccessRule accessRule : allowList1) {
440 445
			for (Subject s : accessRule.getSubjectList()) {
441 446
				if (userPerms1.containsKey(s)) {
442 447
					perms = userPerms1.get(s);
......
451 456
		}
452 457

  
453 458
		// Process second access policy
454
		for (org.dataone.service.types.v1.AccessRule accessRule : allowList2) {
459
		for (AccessRule accessRule : allowList2) {
455 460
			for (Subject s : accessRule.getSubjectList()) {
456 461
				if (userPerms2.containsKey(s)) {
457 462
					perms = userPerms2.get(s);

Also available in: Unified diff