/** * Filters a set of roles based on a list of role values specific for herd. * * @param roles A given set of roles * * @return Valid roles from the specified set of roles */ private Set<String> getValidSecurityRoles(final Set<String> roles) { // Copy the set of specified roles to another set Set<String> incomingRoles = new HashSet<>(roles); // Copy the roles to a set for easier computation Set<SecurityRoleEntity> securityRoleEntities = new HashSet<>(securityRoleDao.getAllSecurityRoles()); // Collect all security role codes from the entities Set<String> securityRoles = securityRoleEntities.stream().map(SecurityRoleEntity::getCode).collect(Collectors.toSet()); // The Set of valid roles is the intersection of the two collections incomingRoles.retainAll(securityRoles); // Return valid roles return incomingRoles; } }
/** * Filters a set of roles based on a list of role values specific for herd. * * @param roles A given set of roles * * @return Valid roles from the specified set of roles */ private Set<String> getValidSecurityRoles(final Set<String> roles) { // Copy the set of specified roles to another set Set<String> incomingRoles = new HashSet<>(roles); // Copy the roles to a set for easier computation Set<SecurityRoleEntity> securityRoleEntities = new HashSet<>(securityRoleDao.getAllSecurityRoles()); // Collect all security role codes from the entities Set<String> securityRoles = securityRoleEntities.stream().map(SecurityRoleEntity::getCode).collect(Collectors.toSet()); // The Set of valid roles is the intersection of the two collections incomingRoles.retainAll(securityRoles); // Return valid roles return incomingRoles; } }
@Test public void testGetAllSecurityRoles() { // Get all security roles List<SecurityRoleEntity> securityRoleEntities = securityRoleDao.getAllSecurityRoles(); // Add new security roles securityRoleDaoTestHelper.createTestSecurityRoles(); List<SecurityRoleEntity> securityRoleEntitiesNew = securityRoleDao.getAllSecurityRoles(); // Since the roles are cached, the test roles added will not be returned assertEquals(securityRoleEntities, securityRoleEntitiesNew); // Clear the cache and fetch the roles again cacheManager.getCache(DaoSpringModuleConfig.HERD_CACHE_NAME).clear(); securityRoleEntitiesNew = securityRoleDao.getAllSecurityRoles(); assertNotEquals(securityRoleEntities, securityRoleEntitiesNew); }
@Test public void testGetAllSecurityRolesValidateOrder() { // Add new security roles List<SecurityRoleEntity> testSecurityRoleEntities = securityRoleDaoTestHelper.createTestSecurityRoles(); // Clear the cache and fetch the roles cacheManager.getCache(DaoSpringModuleConfig.HERD_CACHE_NAME).clear(); // Get all security roles List<SecurityRoleEntity> securityRoleEntities = securityRoleDao.getAllSecurityRoles(); // validate that new roles have been added assertTrue(securityRoleEntities.containsAll(testSecurityRoleEntities)); // validate that security roles are returned in a sorted order assertTrue(securityRoleEntities.indexOf(testSecurityRoleEntities.get(1)) > securityRoleEntities.indexOf(testSecurityRoleEntities.get(0))); }