@Override public SecurityFunctionKeys getSecurityFunctions() { SecurityFunctionKeys securityFunctionKeys = new SecurityFunctionKeys(); securityFunctionKeys.getSecurityFunctionKeys() .addAll(securityFunctionDao.getSecurityFunctions().stream().map(SecurityFunctionKey::new).collect(Collectors.toList())); return securityFunctionKeys; }
@Override public SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request) { // Perform the validation. validateSecurityFunctionCreateRequest(request); // Ensure a security function with the specified security function name doesn't already exist. SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(request.getSecurityFunctionName()); if (securityFunctionEntity != null) { throw new AlreadyExistsException( String.format("Unable to create security function \"%s\" because it already exists.", request.getSecurityFunctionName())); } // Create a security function entity from the request information. securityFunctionEntity = createSecurityFunctionEntity(request); // Persist the new entity. securityFunctionEntity = securityFunctionDao.saveAndRefresh(securityFunctionEntity); // Create and return the security function object from the persisted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
for (String function : securityFunctionDao.getSecurityFunctions()) for (String function : securityFunctionDao.getSecurityFunctionsForRole(role))
/** * Gets a security function entity and ensure it exists. * * @param securityFunctionName the security function name (case insensitive) * * @return the security function entity */ public SecurityFunctionEntity getSecurityFunctionEntity(String securityFunctionName) { SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(securityFunctionName); if (securityFunctionEntity == null) { throw new ObjectNotFoundException(String.format("Security function with name \"%s\" doesn't exist.", securityFunctionName)); } return securityFunctionEntity; } }
/** * Creates and persists a security function entity. * * @param code the name of the security function name * * @return the security function entity */ public SecurityFunctionEntity createSecurityFunctionEntity(String code) { SecurityFunctionEntity securityFunctionEntity = new SecurityFunctionEntity(); securityFunctionEntity.setCode(code); return securityFunctionDao.saveAndRefresh(securityFunctionEntity); } }
@Override public SecurityFunction deleteSecurityFunction(SecurityFunctionKey securityFunctionKey) { // Perform validation and trim. validateAndTrimSecurityFunctionKey(securityFunctionKey); // Retrieve and ensure that a security function already exists with the specified name. SecurityFunctionEntity securityFunctionEntity = securityFunctionDaoHelper.getSecurityFunctionEntity(securityFunctionKey.getSecurityFunctionName()); // Delete the security function. securityFunctionDao.delete(securityFunctionEntity); // Create and return the security function object from the deleted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
/** * Retrieves functional points that have no roles mapped to them. * * @return set of {@link GrantedAuthority} representing functional points */ public Set<GrantedAuthority> getUnrestrictedFunctions() { // TODO Getting HerdDao from applicationContext statically because if we try to wire HerdDao here it does not get constructed with proxy class that is // needed for @Cacheable methods to work. SecurityFunctionDao securityFunctionDao = ApplicationContextHolder.getApplicationContext().getBean(SecurityFunctionDao.class); Set<GrantedAuthority> authorities = new HashSet<>(); // Add all unrestricted functional points. for (String function : securityFunctionDao.getUnrestrictedSecurityFunctions()) { authorities.add(new SimpleGrantedAuthority(function)); } return authorities; }
@Test public void testGetSecurityFunctionsByRole() throws Exception { // Create role and function. SecurityRoleEntity securityRoleEntity = securityRoleDaoTestHelper.createSecurityRoleEntity(SECURITY_ROLE); SecurityFunctionEntity securityFunctionEntity = securityFunctionDaoTestHelper.createSecurityFunctionEntity(SECURITY_FUNCTION); // Validate that no security functions are returned for the role. assertTrue(securityFunctionDao.getSecurityFunctionsForRole(SECURITY_ROLE).isEmpty()); // Add new role to functions mapping. SecurityRoleFunctionEntity securityRoleFunctionEntity = new SecurityRoleFunctionEntity(); securityRoleFunctionEntity.setSecurityRole(securityRoleEntity); securityRoleFunctionEntity.setSecurityFunction(securityFunctionEntity); herdDao.saveAndRefresh(securityRoleFunctionEntity); // Since the functions method is cached, the test function still will not be retrieved. assertTrue(securityFunctionDao.getSecurityFunctionsForRole(SECURITY_ROLE).isEmpty()); // Clear the cache and retrieve the functions again. cacheManager.getCache(DaoSpringModuleConfig.HERD_CACHE_NAME).clear(); // Validate that test security function mapped to the role is now retrieved. assertEquals(Arrays.asList(SECURITY_FUNCTION), securityFunctionDao.getSecurityFunctionsForRole(SECURITY_ROLE)); }
/** * Gets a security function entity and ensure it exists. * * @param securityFunctionName the security function name (case insensitive) * * @return the security function entity */ public SecurityFunctionEntity getSecurityFunctionEntity(String securityFunctionName) { SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(securityFunctionName); if (securityFunctionEntity == null) { throw new ObjectNotFoundException(String.format("Security function with name \"%s\" doesn't exist.", securityFunctionName)); } return securityFunctionEntity; } }
@Override public SecurityFunction deleteSecurityFunction(SecurityFunctionKey securityFunctionKey) { // Perform validation and trim. validateAndTrimSecurityFunctionKey(securityFunctionKey); // Retrieve and ensure that a security function already exists with the specified name. SecurityFunctionEntity securityFunctionEntity = securityFunctionDaoHelper.getSecurityFunctionEntity(securityFunctionKey.getSecurityFunctionName()); // Delete the security function. securityFunctionDao.delete(securityFunctionEntity); // Create and return the security function object from the deleted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
List<String> resultSecurityFunctions = securityFunctionDao.getUnrestrictedSecurityFunctions(); resultSecurityFunctions = securityFunctionDao.getUnrestrictedSecurityFunctions(); resultSecurityFunctions = securityFunctionDao.getUnrestrictedSecurityFunctions();
@Override public SecurityFunction createSecurityFunction(SecurityFunctionCreateRequest request) { // Perform the validation. validateSecurityFunctionCreateRequest(request); // Ensure a security function with the specified security function name doesn't already exist. SecurityFunctionEntity securityFunctionEntity = securityFunctionDao.getSecurityFunctionByName(request.getSecurityFunctionName()); if (securityFunctionEntity != null) { throw new AlreadyExistsException( String.format("Unable to create security function \"%s\" because it already exists.", request.getSecurityFunctionName())); } // Create a security function entity from the request information. securityFunctionEntity = createSecurityFunctionEntity(request); // Persist the new entity. securityFunctionEntity = securityFunctionDao.saveAndRefresh(securityFunctionEntity); // Create and return the security function object from the persisted entity. return createSecurityFunctionFromEntity(securityFunctionEntity); }
@Override public SecurityFunctionKeys getSecurityFunctions() { SecurityFunctionKeys securityFunctionKeys = new SecurityFunctionKeys(); securityFunctionKeys.getSecurityFunctionKeys() .addAll(securityFunctionDao.getSecurityFunctions().stream().map(SecurityFunctionKey::new).collect(Collectors.toList())); return securityFunctionKeys; }
@Test public void testCreateSecurityFunctionAlreadyExists() { expectedException.expect(AlreadyExistsException.class); expectedException.expectMessage(String.format("Unable to create security function \"%s\" because it already exists.", SECURITY_FUNCTION)); when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(SECURITY_FUNCTION_ENTITY); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(SECURITY_FUNCTION); securityFunctionService.createSecurityFunction(SECURITY_FUNCTION_CREATE_REQUEST); }
@Test public void testDeleteSecurityFunction() { when(securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION)).thenReturn(SECURITY_FUNCTION_ENTITY); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(SECURITY_FUNCTION); SecurityFunction securityFunction = securityFunctionService.deleteSecurityFunction(SECURITY_FUNCTION_KEY); assertEquals(SECURITY_FUNCTION, securityFunction.getSecurityFunctionName()); verify(alternateKeyHelper).validateStringParameter("security function name", SECURITY_FUNCTION); verify(securityFunctionDaoHelper).getSecurityFunctionEntity(SECURITY_FUNCTION); verify(securityFunctionDao).delete(SECURITY_FUNCTION_ENTITY); verifyNoMoreInteractionsHelper(); }
@Test public void testCreateSecurityFunction() { when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(null); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(SECURITY_FUNCTION); when(securityFunctionDao.saveAndRefresh(any(SecurityFunctionEntity.class))).thenReturn(SECURITY_FUNCTION_ENTITY); SecurityFunction securityFunction = securityFunctionService.createSecurityFunction(SECURITY_FUNCTION_CREATE_REQUEST); assertEquals(SECURITY_FUNCTION, securityFunction.getSecurityFunctionName()); verify(alternateKeyHelper).validateStringParameter("security function name", SECURITY_FUNCTION); verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verify(securityFunctionDao).saveAndRefresh(any(SecurityFunctionEntity.class)); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSecurityFunctionsEmptyList() { when(securityFunctionDao.getSecurityFunctions()).thenReturn(Collections.emptyList()); SecurityFunctionKeys securityFunctionKeys = securityFunctionService.getSecurityFunctions(); assertNotNull(securityFunctionKeys); assertEquals(0, securityFunctionKeys.getSecurityFunctionKeys().size()); verify(securityFunctionDao).getSecurityFunctions(); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSecurityFunctionEntitySecurityFunctionNoExists() { // Mock the external calls. when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(null); // Specify the expected exception. expectedException.expect(ObjectNotFoundException.class); expectedException.expectMessage(String.format("Security function with name \"%s\" doesn't exist.", SECURITY_FUNCTION)); // Call the method under test. securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION); // Verify the external calls. verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verifyNoMoreInteractionsHelper(); }
@Test public void testGetSecurityFunctions() throws Exception { List<String> functions = securityFunctionDao.getSecurityFunctions(); // Add a function in functions. securityFunctionDaoTestHelper.createSecurityFunctionEntity(SECURITY_FUNCTION); List<String> functions2 = securityFunctionDao.getSecurityFunctions(); // Since the functions method is cached, the test function will not be retrieved. assertEquals(functions, functions2); // Clear the cache and retrieve the functions again. cacheManager.getCache(DaoSpringModuleConfig.HERD_CACHE_NAME).clear(); functions2 = securityFunctionDao.getSecurityFunctions(); assertNotEquals(functions, functions2); }
@Test public void testGetSecurityFunctionEntity() { // Create a security function entity. SecurityFunctionEntity securityFunctionEntity = new SecurityFunctionEntity(); // Mock the external calls. when(securityFunctionDao.getSecurityFunctionByName(SECURITY_FUNCTION)).thenReturn(securityFunctionEntity); // Call the method under test. SecurityFunctionEntity result = securityFunctionDaoHelper.getSecurityFunctionEntity(SECURITY_FUNCTION); // Validate the results. assertEquals(securityFunctionEntity, result); // Verify the external calls. verify(securityFunctionDao).getSecurityFunctionByName(SECURITY_FUNCTION); verifyNoMoreInteractionsHelper(); }