PrivilegeBitsProvider implements PrivilegeConstants { @NotNull public Set<String> getPrivilegeNames(PrivilegeBits privilegeBits) { if (privilegeBits == null || privilegeBits.isEmpty()) { return Collections.emptySet(); } PrivilegeBits pb = privilegeBits.unmodifiable(); if (bitsToNames.containsKey(pb)) { return bitsToNames.get(pb); } else { Tree privilegesTree = getPrivilegesTree(); if (!privilegesTree.exists()) { return Collections.emptySet(); } if (bitsToNames.isEmpty()) { for (Tree child : privilegesTree.getChildren()) { bitsToNames.put(PrivilegeBits.getInstance(child), Collections.singleton(child.getName())); } } Set<String> privilegeNames; if (bitsToNames.containsKey(pb)) { privilegeNames = bitsToNames.get(pb); } else { privilegeNames = new HashSet<>(); Set<String> aggregates = new HashSet<>(); for (Tree child : privilegesTree.getChildren()) { PrivilegeBits bits = PrivilegeBits.getInstance(child); if (pb.includes(bits)) { privilegeNames.add(child.getName()); if (child.hasProperty(REP_AGGREGATES)) { aggregates.addAll(PrivilegeUtil.readDefinition(child).getDeclaredAggregateNames()); } } } privilegeNames.removeAll(aggregates); bitsToNames.put(pb, ImmutableSet.copyOf(privilegeNames)); } return privilegeNames; } } PrivilegeBitsProvider(Root root); @NotNull Tree getPrivilegesTree(); @NotNull PrivilegeBits getBits(@NotNull String... privilegeNames); @NotNull PrivilegeBits getBits(@NotNull Iterable<String> privilegeNames); @NotNull PrivilegeBits getBits(@NotNull Privilege[] privileges, @NotNull final NameMapper nameMapper); @NotNull Set<String> getPrivilegeNames(PrivilegeBits privilegeBits); @NotNull Iterable<String> getAggregatedPrivilegeNames(@NotNull String... privilegeNames);  }
@Test public void testGetPrivilegeNamesFromEmpty() { Set<String> names = bitsProvider.getPrivilegeNames(PrivilegeBits.EMPTY); assertTrue(names.isEmpty()); }
@Test public void testGetPrivilegeNamesFromNull() { Set<String> names = bitsProvider.getPrivilegeNames(null); assertTrue(names.isEmpty()); }
@Test public void testGetPrivilegeNamesNonExistingPrivilegesTree() { when(privTree.exists()).thenReturn(false); Set<String> names = bitsProvider.getPrivilegeNames(bits); assertTrue(names.isEmpty()); }
@Test public void testGetPrivilegeNames() { Set<String> names = bitsProvider.getPrivilegeNames(bits); assertFalse(names.isEmpty()); assertEquals(ImmutableSet.of(KNOWN_PRIV_NAME), names); }
@Test public void testGetPrivilegeNamesFromCache() { Set<String> names = bitsProvider.getPrivilegeNames(bits); assertSame(names, bitsProvider.getPrivilegeNames(bits)); }
@Test public void testGetPrivilegeNamesWithAggregation() { Tree anotherPriv = Mockito.mock(Tree.class); when(anotherPriv.exists()).thenReturn(true); when(anotherPriv.getName()).thenReturn("name2"); when(anotherPriv.hasProperty(REP_AGGREGATES)).thenReturn(true); when(anotherPriv.getProperty(REP_AGGREGATES)).thenReturn(PropertyStates.createProperty(REP_AGGREGATES, ImmutableList.of(KNOWN_PRIV_NAME), Type.NAMES)); PropertyState bits2 = PropertyStates.createProperty(REP_BITS, 7500L); when(anotherPriv.getProperty(REP_BITS)).thenReturn(bits2); when(privTree.getChildren()).thenReturn(ImmutableSet.of(pTree, anotherPriv)); Set<String> expected = ImmutableSet.of("name2"); Set<String> result = bitsProvider.getPrivilegeNames(PrivilegeBits.getInstance(PrivilegeBits.getInstance(bits), PrivilegeBits.getInstance(bits2))); assertEquals(expected, result); }