PrivilegeBitsProvider implements PrivilegeConstants { @NotNull public Iterable<String> getAggregatedPrivilegeNames(@NotNull String... privilegeNames) { if (privilegeNames.length == 0) { return Collections.emptySet(); } else if (privilegeNames.length == 1) { String privName = privilegeNames[0]; if (NON_AGGREGATE_PRIVILEGES.contains(privName)) { return ImmutableSet.of(privName); } else if (aggregation.containsKey(privName)) { return aggregation.get(privName); } else if (AGGREGATE_PRIVILEGES.keySet().contains(privName)) { Set<String> aggregates = resolveBuiltInAggregation(privName); aggregation.put(privName, aggregates); return aggregates; } else { return extractAggregatedPrivileges(Collections.singleton(privName)); } } else { Set<String> pNames = ImmutableSet.copyOf(privilegeNames); if (NON_AGGREGATE_PRIVILEGES.containsAll(pNames)) { return pNames; } else { return extractAggregatedPrivileges(pNames); } } } 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 testGetAggregatedPrivilegeNamesEmpty() { assertTrue(Iterables.isEmpty(bitsProvider.getAggregatedPrivilegeNames())); }
@Test public void testGetAggregatedPrivilegeNamesEmptyArray() { assertTrue(Iterables.isEmpty(bitsProvider.getAggregatedPrivilegeNames())); }
@Test public void testGetAggregatedPrivilegeNamesSingleNonAggregates() { for (String name : NON_AGGREGATE_PRIVILEGES) { assertEquals(ImmutableSet.of(name), bitsProvider.getAggregatedPrivilegeNames(name)); } }
@Test public void testGetAggregatedPrivilegeNamesNonAggregates() { assertEquals( ImmutableSet.of(REP_READ_NODES, JCR_LIFECYCLE_MANAGEMENT, JCR_READ_ACCESS_CONTROL), bitsProvider.getAggregatedPrivilegeNames(REP_READ_NODES, JCR_LIFECYCLE_MANAGEMENT, JCR_READ_ACCESS_CONTROL)); }
@Test public void testGetAggregatedPrivilegeNamesJcrRead() { assertEquals(ImmutableSet.copyOf(AGGREGATE_PRIVILEGES.get(JCR_READ)), ImmutableSet.copyOf(bitsProvider.getAggregatedPrivilegeNames(JCR_READ))); }
@Test public void testGetAggregatedPrivilegeNamesJcrWrite() { Set<String> result = ImmutableSet.copyOf(bitsProvider.getAggregatedPrivilegeNames(JCR_WRITE)); assertNotEquals(ImmutableSet.copyOf(AGGREGATE_PRIVILEGES.get(JCR_WRITE)), result); String[] expected = new String[] { JCR_ADD_CHILD_NODES, JCR_REMOVE_CHILD_NODES, JCR_REMOVE_NODE, REP_ADD_PROPERTIES, REP_ALTER_PROPERTIES, REP_REMOVE_PROPERTIES }; assertEquals(ImmutableSet.copyOf(expected), result); }
@Test public void testGetAggregatedPrivilegeNamesBuiltInTwice() { Iterable<String> agg = bitsProvider.getAggregatedPrivilegeNames(JCR_READ); assertSame(agg, bitsProvider.getAggregatedPrivilegeNames(JCR_READ)); }
@Test public void testGetAggregatedPrivilegeNamesMultipleBuiltIn() { Iterable<String> expected = ImmutableSet.copyOf(Iterables.concat( bitsProvider.getAggregatedPrivilegeNames(JCR_READ), bitsProvider.getAggregatedPrivilegeNames(JCR_WRITE))); PrivilegeBitsProvider bp = new PrivilegeBitsProvider(root); Iterable<String> result = bp.getAggregatedPrivilegeNames(JCR_READ, JCR_WRITE); assertEquals(expected, ImmutableSet.copyOf(result)); }
@Test public void testGetAggregatedPrivilegeNamesMultipleBuiltIn2() { Iterable<String> expected = ImmutableSet.copyOf(Iterables.concat( bitsProvider.getAggregatedPrivilegeNames(JCR_READ), bitsProvider.getAggregatedPrivilegeNames(JCR_WRITE))); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(JCR_READ, JCR_WRITE); assertEquals(expected, ImmutableSet.copyOf(result)); }
@Test public void testGetAggregatedPrivilegeNamesMixedBuiltIn() { Iterable<String> expected = ImmutableSet.copyOf(Iterables.concat( ImmutableSet.of(JCR_LOCK_MANAGEMENT), bitsProvider.getAggregatedPrivilegeNames(JCR_WRITE))); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(JCR_LOCK_MANAGEMENT, JCR_WRITE); assertEquals(expected, ImmutableSet.copyOf(result)); }
@Test public void testGetAggregatedPrivilegeNamesNonExistingTree() { ImmutableSet<String> names = ImmutableSet.of(JCR_LOCK_MANAGEMENT, JCR_READ_ACCESS_CONTROL); when(pTree.getProperty(REP_AGGREGATES)).thenReturn(PropertyStates.createProperty(REP_AGGREGATES, names, Type.NAMES)); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(KNOWN_PRIV_NAME); assertTrue(Iterables.isEmpty(result)); }
@Test public void testGetAggregatedPrivilegeNamesMissingAggProperty() { when(pTree.exists()).thenReturn(true); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(KNOWN_PRIV_NAME); assertTrue(Iterables.elementsEqual(ImmutableList.of(KNOWN_PRIV_NAME), result)); }
@Test public void testGetAggregatedPrivilegeNames() { ImmutableSet<String> expected = ImmutableSet.of(JCR_LOCK_MANAGEMENT, JCR_READ_ACCESS_CONTROL); when(pTree.getProperty(REP_AGGREGATES)).thenReturn(PropertyStates.createProperty(REP_AGGREGATES, expected, Type.NAMES)); when(pTree.exists()).thenReturn(true); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(KNOWN_PRIV_NAME); assertEquals(expected, ImmutableSet.copyOf(result)); }
@Test public void testGetAggregatedPrivilegeNamesNested() { ImmutableSet<String> values = ImmutableSet.of(JCR_READ, JCR_ADD_CHILD_NODES); when(pTree.getProperty(REP_AGGREGATES)).thenReturn(PropertyStates.createProperty(REP_AGGREGATES, values, Type.NAMES)); when(pTree.exists()).thenReturn(true); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(KNOWN_PRIV_NAME); ImmutableSet<String> expected = ImmutableSet.of(REP_READ_NODES, REP_READ_PROPERTIES, JCR_ADD_CHILD_NODES); assertEquals(expected, ImmutableSet.copyOf(result)); }
@Test public void testGetAggregatedPrivilegeNamesNestedWithCache() { ImmutableSet<String> values = ImmutableSet.of(JCR_READ, JCR_ADD_CHILD_NODES); when(pTree.getProperty(REP_AGGREGATES)).thenReturn(PropertyStates.createProperty(REP_AGGREGATES, values, Type.NAMES)); when(pTree.exists()).thenReturn(true); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); Iterable<String> result = bitsProvider.getAggregatedPrivilegeNames(KNOWN_PRIV_NAME); Set<String> expected = ImmutableSet.copyOf(Iterables.concat( ImmutableSet.of(JCR_ADD_CHILD_NODES), bitsProvider.getAggregatedPrivilegeNames(JCR_READ))); assertEquals(expected, ImmutableSet.copyOf(result)); }