<commit_before>package test.gov.nih.nci.security;



import gov.nih.nci.security.SecurityServiceProvider;
import gov.nih.nci.security.UserProvisioningManager;
import gov.nih.nci.security.authorization.domainobjects.Application;
import gov.nih.nci.security.authorization.domainobjects.ApplicationContext;
import gov.nih.nci.security.authorization.domainobjects.Group;
import gov.nih.nci.security.authorization.domainobjects.Privilege;
import gov.nih.nci.security.authorization.domainobjects.ProtectionElement;
import gov.nih.nci.security.authorization.domainobjects.ProtectionGroup;
import gov.nih.nci.security.authorization.domainobjects.Role;
import gov.nih.nci.security.authorization.domainobjects.User;
import gov.nih.nci.security.authorization.jaas.AccessPermission;
import gov.nih.nci.security.dao.GroupSearchCriteria;
import gov.nih.nci.security.dao.SearchCriteria;
import gov.nih.nci.security.exceptions.CSException;
import gov.nih.nci.security.exceptions.CSObjectNotFoundException;
import gov.nih.nci.security.exceptions.CSTransactionException;

import java.security.Principal;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;


import junit.framework.TestCase;

public class UserProvisioningManagerTest extends TestCase {

	private UserProvisioningManager userProvisioningManager; 
	
	//Test Set-up variables
	private int NumberOfApplicationsToTest			= 5;
	private int NumberOfUsersToTest 				= 10;
	private int NumberOfGroupsToTest 				= 10;
	private int NumberOfPrivilegesToTest 			= 5;
	private int NumberOfProtectionElementsToTest 	= 50;  //Must be larger than NumberOfProtectionGroupsToTest?
	private int NumberOfProtectionGroupsToTest 		= 10;
	private int NumberOfRolesToTest 				= 5;
	private String	   StrangeCharacters			= new String("");  //-\\\\=[]\\;//,./{}:\"<>?-+/*&&||==.");
	//TODO: Get single quote and other characters above figured out, so they work
	
	//Test text comparison arrays - initialized in Setup() and used to confirm every text related field in each object (user, PE, etc.)
	//TODO: change all variable names to be more meaningful and easier to read (NumberOf = nbr, UserStringArray = UserTextFields
	private String[][] UserStringArray 				= new String[NumberOfUsersToTest]				[9];
	private String[][] RoleStringArray 				= new String[NumberOfRolesToTest]				[2];
	private String[][] GroupStringArray 			= new String[NumberOfGroupsToTest]				[2];
	private String[][] ApplicationStringArray 		= new String[NumberOfApplicationsToTest+3]		[2];
	private String[][] PrivilegeStringArray 		= new String[NumberOfPrivilegesToTest]			[2];
	private String[][] ProtectionElementStringArray = new String[NumberOfProtectionElementsToTest+3]	[4];
	private String[][] ProtectionGroupStringArray 	= new String[NumberOfProtectionGroupsToTest]	[4];
	private String[][] PG_PERelationship			= new String[NumberOfProtectionGroupsToTest]	[NumberOfProtectionElementsToTest];
	private String[][] Group_UserRelationship		= new String[NumberOfGroupsToTest]				[NumberOfUsersToTest];
	//private String[][] Privilege_RoleRelationship	= new String[NumberOfPrivilegesToTest+7]		[NumberOfRolesToTest];
	
	protected void setUp() throws Exception {
		super.setUp();
		System.setProperty("gov.nih.nci.security.configFile", "C:/securityConfig/ApplicationSecurityConfig.xml");
		userProvisioningManager = SecurityServiceProvider.getUserProvisioningManager("csmupt");

		//Initialize the userList - used to check the "get" functions
		InitializeUserStringArray();
		InitializeRoleStringArray();
		InitializeGroupStringArray();
		InitializeApplicationStringArray();
		InitializePrivilegeStringArray();
		InitializeProtectionElementStringArray();
		InitializeProtectionGroupStringArray();

	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testRun() throws CSException {
		
		//Order of Execution
		
		//Create Objects
		this.testCreateApplication();
		this.testCreateUser();
		this.testCreatePrivilege();
		this.testCreateProtectionElement();
		this.testCreateRole();
		this.testCreateGroup();
		this.testCreateProtectionGroup();

		
		//Confirm Created (only tests basic data - strings, IDs, and flags)
		this.testGetApplicationById();
		this.testGetUser();
		this.testGetUserById();
		this.testGetPrivilegeById();
		this.testGetProtectionElementById();
		this.testGetProtectionElementString();
		this.testGetProtectionElementStringString();
		this.testGetRoleById();
		this.testGetGroupById();
		this.testGetProtectionGroupById();
		this.testGetProtectionGroups();
		this.testGetProtectionElementString();

		this.testGetObjects();
		//this.testGetApplicationContext(); TODO			
		//Assigns each user 1 group
		this.testAssignUserToGroup();
		this.testGetGroups();
		
		//Remove the associations just made, then remake associations where first user
		// 		gets assigned to all the groups, second to all groups - 1, etc.
		this.testRemoveUserFromGroup(); 	// Removes only groups added by "testAssignUserToGroup()"
		this.testAssignGroupsToUser();  	// Assigns multiple groups per most users
		this.testGetGroupsMulti();			// Checks all user to group associations are correct
		this.testGetPrincipals();			//NOT tested by UPT  

		
		// PE to PG Associations
		this.testAssignProtectionElementStringStringString();	//This works, but is not dynamic, only assigns 1st PE to 1st PG
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testAssignToProtectionGroups();						//NOT tested by UPT  //Assigns all Protection Groups to all Protection Elements
		//this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testDeAssignProtectionElements();  				//BUGGED - posted in GForge
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testAssignProtectionElementStringString();  		//Needs above line before can execute //NOT Tested by UPT
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();

		
		//Assigns PE starting with all PE to first PG then decrementing the number assigned by one for each new PG, 
		//			Also initializes PG_PERelationships array, used in testGetProtectionGroupsString()
		this.testAssignProtectionElements();					//After this function executes is where the occational freeze occurs!!!!!!!!!
		this.testGetProtectionGroupsString();					//Must be paired with testAssignProtectionElements()
		
		
		
		
		//TODO: Enhance this method to ensure the CORRECT PEs are returned
		this.testGetProtectionElements();						//This method only checks to make sure a valid set of PEs was returned
		
		//MODIFY
		this.testModifyProtectionGroup();  				
		this.testModifyProtectionElement();  				
		this.testModifyRole();
		this.testModifyApplication();
		this.testModifyGroup();
		this.testModifyPrivilege();
		this.testModifyUser();
		
		//Associate Privileges to Role
		this.testAssignPrivilegesToRole();
		this.testGetPrivileges();  			
		
		this.testAssignUserRoleToProtectionGroup();					//NOT tested by UPT
//Method works. Uncomment to test.		this.testRemoveUserRoleFromProtectionGroup();				//NOT tested by UPT
//Method works. Uncomment to test.		this.testAssignParentProtectionGroup();						//NOT tested by UPT
		
		//this.testSetOwnerForProtectionElementStringStringArray();	//NOT tested by UPT  //Needs work
		this.testAssignOwners();									//TODO: make this more dynamic, only assigns one PE to each user
		this.testSetOwnerForProtectionElementStringStringString();	//NOT tested by UPT  //Assigns users 2 to PE 2 //TODO: make more diverse
		
			  	
		
//Method works. Uncomment to test.		this.testCheckOwnership();									//NOT tested by UPT
//Method works. Uncomment to test.		this.testGetOwners();										//Suggested by Kunal
 
		
//Method works. Uncomment to test.		this.testGetPrivilegeMap();	//NOT tested by UPT  
		
//Method works. Uncomment to test.		this.testRemoveProtectionElementsFromProtectionGroup(); 
		this.testAssignGroupRoleToProtectionGroup();
//Method works. Uncomment to test.		this.testGetProtectionElementPrivilegeContextForUser();
//Method works. Uncomment to test.		this.testGetProtectionElementPrivilegeContextForGroup();  // Requires this.testAssignGroupRoleToProtectionGroup()

//Method works. Uncomment to test.		this.testGetProtectionGroupRoleContextForGroup();
//Method works. Uncomment to test.		this.testGetProtectionGroupRoleContextForUser();
//Method works. Uncomment to test.		this.testRemoveGroupFromProtectionGroup();
//Method works. Uncomment to test.		this.testRemoveUserFromProtectionGroup();

//Method works. Uncomment to test.		this.testRemoveGroupRoleFromProtectionGroup(); Works. Uncomment!!
    	
		//DELETE EVERYTHING
//Method works. Uncomment to test.		this.testRemoveGroup();
//Method works. Uncomment to test.		this.testRemovePrivilege();
//Method works. Uncomment to test.		this.testRemoveRole();
//Method works. Uncomment to test.		this.testRemoveProtectionElement();
//Method works. Uncomment to test.		this.testRemoveProtectionGroup();
//Method works. Uncomment to test.		this.testRemoveUser();
//Method works. Uncomment to test.    	this.testRemoveApplication();

		
		//---------------------------------------------------------------------------------------------------
		//Still to be done
		//---------------------------------------------------------------------------------------------------

//		this.testSecureUpdate();									//NOT tested by UPT
		/* 	Parameters for Secure Update()
		 * 	userName - The user name of the User which is trying to update the object
		 	originalObject - The original data object as it was read from the data base
		 	mutatedObject - The data object which contains the changes which the user has made */
//		this.testSecureCollection();								//NOT tested by UPT
//		this.testSecureObject();									//NOT tested by UPT
		//this.testCheckPermissionAccessPermissionSubject();					<commit_msg>*** empty log message ***<commit_after>package test.gov.nih.nci.security;



import gov.nih.nci.security.SecurityServiceProvider;
import gov.nih.nci.security.UserProvisioningManager;
import gov.nih.nci.security.authorization.domainobjects.Application;
import gov.nih.nci.security.authorization.domainobjects.ApplicationContext;
import gov.nih.nci.security.authorization.domainobjects.Group;
import gov.nih.nci.security.authorization.domainobjects.Privilege;
import gov.nih.nci.security.authorization.domainobjects.ProtectionElement;
import gov.nih.nci.security.authorization.domainobjects.ProtectionGroup;
import gov.nih.nci.security.authorization.domainobjects.Role;
import gov.nih.nci.security.authorization.domainobjects.User;
import gov.nih.nci.security.authorization.jaas.AccessPermission;
import gov.nih.nci.security.dao.GroupSearchCriteria;
import gov.nih.nci.security.dao.SearchCriteria;
import gov.nih.nci.security.exceptions.CSException;
import gov.nih.nci.security.exceptions.CSObjectNotFoundException;
import gov.nih.nci.security.exceptions.CSTransactionException;

import java.security.Principal;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;


import junit.framework.TestCase;

public class UserProvisioningManagerTest extends TestCase {

	private UserProvisioningManager userProvisioningManager; 
	
	//Test Set-up variables
	private int NumberOfApplicationsToTest			= 5;
	private int NumberOfUsersToTest 				= 10;
	private int NumberOfGroupsToTest 				= 10;
	private int NumberOfPrivilegesToTest 			= 5;
	private int NumberOfProtectionElementsToTest 	= 50;  //Must be larger than NumberOfProtectionGroupsToTest?
	private int NumberOfProtectionGroupsToTest 		= 10;
	private int NumberOfRolesToTest 				= 5;
	private String	   StrangeCharacters			= new String("");  //-\\\\=[]\\;//,./{}:\"<>?-+/*&&||==.");
	//TODO: Get single quote and other characters above figured out, so they work
	
	//Test text comparison arrays - initialized in Setup() and used to confirm every text related field in each object (user, PE, etc.)
	//TODO: change all variable names to be more meaningful and easier to read (NumberOf = nbr, UserStringArray = UserTextFields
	private String[][] UserStringArray 				= new String[NumberOfUsersToTest]				[9];
	private String[][] RoleStringArray 				= new String[NumberOfRolesToTest]				[2];
	private String[][] GroupStringArray 			= new String[NumberOfGroupsToTest]				[2];
	private String[][] ApplicationStringArray 		= new String[NumberOfApplicationsToTest+3]		[2];
	private String[][] PrivilegeStringArray 		= new String[NumberOfPrivilegesToTest]			[2];
	private String[][] ProtectionElementStringArray = new String[NumberOfProtectionElementsToTest+3]	[4];
	private String[][] ProtectionGroupStringArray 	= new String[NumberOfProtectionGroupsToTest]	[4];
	private String[][] PG_PERelationship			= new String[NumberOfProtectionGroupsToTest]	[NumberOfProtectionElementsToTest];
	private String[][] Group_UserRelationship		= new String[NumberOfGroupsToTest]				[NumberOfUsersToTest];
	//private String[][] Privilege_RoleRelationship	= new String[NumberOfPrivilegesToTest+7]		[NumberOfRolesToTest];
	
	protected void setUp() throws Exception {
		super.setUp();
		System.setProperty("gov.nih.nci.security.configFile", "C:/securityConfig/ApplicationSecurityConfig.xml");
		userProvisioningManager = SecurityServiceProvider.getUserProvisioningManager("csmupt");

		//Initialize the userList - used to check the "get" functions
		InitializeUserStringArray();
		InitializeRoleStringArray();
		InitializeGroupStringArray();
		InitializeApplicationStringArray();
		InitializePrivilegeStringArray();
		InitializeProtectionElementStringArray();
		InitializeProtectionGroupStringArray();

	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testRun() throws CSException {
		
		//Order of Execution
		
		//Create Objects
		this.testCreateApplication();
		this.testCreateUser();
		this.testCreatePrivilege();
		this.testCreateProtectionElement();
		this.testCreateRole();
		this.testCreateGroup();
		this.testCreateProtectionGroup();

		
		//Confirm Created (only tests basic data - strings, IDs, and flags)
		this.testGetApplicationById();
		this.testGetUser();
		this.testGetUserById();
		this.testGetPrivilegeById();
		this.testGetProtectionElementById();
		this.testGetProtectionElementString();
		this.testGetProtectionElementStringString();
		this.testGetRoleById();
		this.testGetGroupById();
		this.testGetProtectionGroupById();
		this.testGetProtectionGroups();
		this.testGetProtectionElementString();

		this.testGetObjects();
		//this.testGetApplicationContext(); TODO			
		//Assigns each user 1 group
		this.testAssignUserToGroup();
		this.testGetGroups();
		
		//Remove the associations just made, then remake associations where first user
		// 		gets assigned to all the groups, second to all groups - 1, etc.
		this.testRemoveUserFromGroup(); 	// Removes only groups added by "testAssignUserToGroup()"
		this.testAssignGroupsToUser();  	// Assigns multiple groups per most users
		this.testGetGroupsMulti();			// Checks all user to group associations are correct
		this.testGetPrincipals();			//NOT tested by UPT  

		
		// PE to PG Associations
		this.testAssignProtectionElementStringStringString();	//This works, but is not dynamic, only assigns 1st PE to 1st PG
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testAssignToProtectionGroups();						//NOT tested by UPT  //Assigns all Protection Groups to all Protection Elements
		//this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testDeAssignProtectionElements();  				//BUGGED - posted in GForge
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();
		this.testAssignProtectionElementStringString();  		//Needs above line before can execute //NOT Tested by UPT
		this.testRemoveAllAssignedProtectionElementsFromAllProtectionGroups();

		
		//Assigns PE starting with all PE to first PG then decrementing the number assigned by one for each new PG, 
		//			Also initializes PG_PERelationships array, used in testGetProtectionGroupsString()
		this.testAssignProtectionElements();					//After this function executes is where the occational freeze occurs!!!!!!!!!
		this.testGetProtectionGroupsString();					//Must be paired with testAssignProtectionElements()
		
		
		
		
		//TODO: Enhance this method to ensure the CORRECT PEs are returned
		this.testGetProtectionElements();						//This method only checks to make sure a valid set of PEs was returned
		
		//MODIFY
		this.testModifyProtectionGroup();  				
		this.testModifyProtectionElement();  				
		this.testModifyRole();
		this.testModifyApplication();
		this.testModifyGroup();
		this.testModifyPrivilege();
		this.testModifyUser();
		
		//Associate Privileges to Role
		this.testAssignPrivilegesToRole();
		this.testGetPrivileges();  			
		
		this.testAssignUserRoleToProtectionGroup();					//NOT tested by UPT
		this.testRemoveUserRoleFromProtectionGroup();				//NOT tested by UPT
		this.testAssignParentProtectionGroup();						//NOT tested by UPT
		
		//this.testSetOwnerForProtectionElementStringStringArray();	//NOT tested by UPT  //Needs work
		this.testAssignOwners();									//TODO: make this more dynamic, only assigns one PE to each user
		this.testSetOwnerForProtectionElementStringStringString();	//NOT tested by UPT  //Assigns users 2 to PE 2 //TODO: make more diverse
		
			  	
		
		this.testCheckOwnership();									//NOT tested by UPT
		this.testGetOwners();										//Suggested by Kunal
 
		
		this.testGetPrivilegeMap();	//NOT tested by UPT  
		
		this.testRemoveProtectionElementsFromProtectionGroup(); 
		this.testAssignGroupRoleToProtectionGroup();
		this.testGetProtectionElementPrivilegeContextForUser();
		this.testGetProtectionElementPrivilegeContextForGroup();  // Requires this.testAssignGroupRoleToProtectionGroup()

		this.testGetProtectionGroupRoleContextForGroup();
		this.testGetProtectionGroupRoleContextForUser();
		this.testRemoveGroupFromProtectionGroup();
		this.testRemoveUserFromProtectionGroup();

		this.testRemoveGroupRoleFromProtectionGroup(); 
    	
		//DELETE EVERYTHING
		this.testRemoveGroup();
		this.testRemovePrivilege();
		this.testRemoveRole();
		this.testRemoveProtectionElement();
		this.testRemoveProtectionGroup();
		this.testRemoveUser();
    	this.testRemoveApplication();

		
		//---------------------------------------------------------------------------------------------------
		//Still to be done
		//---------------------------------------------------------------------------------------------------

//		this.testSecureUpdate();									//NOT tested by UPT
		/* 	Parameters for Secure Update()
		 * 	userName - The user name of the User which is trying to update the object
		 	originalObject - The original data object as it was read from the data base
		 	mutatedObject - The data object which contains the changes which the user has made */
//		this.testSecureCollection();								//NOT tested by UPT
//		this.testSecureObject();									//NOT tested by UPT
		//this.testCheckPermissionAccessPermissionSubject();					
{'fraction_non_alphanumeric': 0.11430809399477806, 'fraction_numerical': 0.0028198433420365534, 'mean_word_length': 4.202662320021733, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6346059', 'n_tokens_mistral': 6025, 'n_tokens_neox': 5402, 'n_words': 1352}