PrivilegeBitsProvider implements PrivilegeConstants { @NotNull public PrivilegeBits getBits(@NotNull String... privilegeNames) { if (privilegeNames.length == 0) { return PrivilegeBits.EMPTY; } else { return getBits(Arrays.asList(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 testGetBitsNonExistingPrivilegesTree() { when(privTree.exists()).thenReturn(false); assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits(KNOWN_PRIV_NAME)); }
@Test public void testGetBitsEmptyNames() { assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits()); }
@Test public void testGetBitsEmptyArray() { assertEquals(PrivilegeBits.EMPTY, bitsProvider.getBits(new String[0])); }
@Test public void testGetBitsEmptyString() { assertEquals(PrivilegeBits.EMPTY, bitsProvider.getBits("")); }
@Test public void testGetBitsEmptyIterable() { assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits(ImmutableList.of())); }
@Test public void testGetBitsBuiltInSingleName() { PrivilegeBits bits = bitsProvider.getBits(JCR_LOCK_MANAGEMENT); assertFalse(bits.isEmpty()); assertEquals(PrivilegeBits.BUILT_IN.get(JCR_LOCK_MANAGEMENT), bits); }
@Test public void testGetBitsBuiltInSingleton() { PrivilegeBits bits = bitsProvider.getBits(ImmutableList.of(JCR_LOCK_MANAGEMENT)); assertFalse(bits.isEmpty()); assertEquals(PrivilegeBits.BUILT_IN.get(JCR_LOCK_MANAGEMENT), bits); }
@Test public void testGetBitsBuiltInNames() { PrivilegeBits bits = bitsProvider.getBits(JCR_ADD_CHILD_NODES, JCR_REMOVE_CHILD_NODES); assertFalse(bits.isEmpty()); PrivilegeBits mod = PrivilegeBits.getInstance(bitsProvider.getBits(JCR_ADD_CHILD_NODES)).add(bitsProvider.getBits(JCR_REMOVE_CHILD_NODES)); assertEquals(bits, mod.unmodifiable()); }
@Test public void testGetBitsBuiltInIterable() { PrivilegeBits bits = bitsProvider.getBits(ImmutableList.of(JCR_ADD_CHILD_NODES, JCR_REMOVE_CHILD_NODES)); assertFalse(bits.isEmpty()); PrivilegeBits mod = PrivilegeBits.getInstance(bitsProvider.getBits(JCR_ADD_CHILD_NODES)).add(bitsProvider.getBits(JCR_REMOVE_CHILD_NODES)); assertEquals(bits, mod.unmodifiable()); }
@Test public void testGetBitsNonExistingTree() { when(privTree.exists()).thenReturn(true); when(privTree.hasChild(KNOWN_PRIV_NAME)).thenReturn(false); assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits(KNOWN_PRIV_NAME)); }
@Test public void testGetBitsKnownPrivName() { when(privTree.exists()).thenReturn(true); when(privTree.hasChild(KNOWN_PRIV_NAME)).thenReturn(true); when(privTree.getChild(KNOWN_PRIV_NAME)).thenReturn(pTree); assertEquals(bits.unmodifiable(), bitsProvider.getBits(KNOWN_PRIV_NAME)); }
@Test public void testGetBitsTwiceSingleBuiltIn() { Iterable<String> names = ImmutableList.of(JCR_ADD_CHILD_NODES); PrivilegeBits bits1 = bitsProvider.getBits(names); PrivilegeBits bits2 = bitsProvider.getBits(names); assertSame(bits1, bits2); assertEquals(bits1, bits2); verify(root, never()).getTree(PRIVILEGES_PATH); verify(privTree, never()).getChild(KNOWN_PRIV_NAME); }
@Test public void testGetBitsTwiceMultipleBuiltIn() { Iterable<String> names = ImmutableList.of(JCR_ADD_CHILD_NODES, JCR_REMOVE_CHILD_NODES); PrivilegeBits bits1 = bitsProvider.getBits(names); PrivilegeBits bits2 = bitsProvider.getBits(names); assertNotSame(bits1, bits2); assertEquals(bits1, bits2); verify(root, never()).getTree(PRIVILEGES_PATH); verify(privTree, never()).getChild(KNOWN_PRIV_NAME); }
@Test public void testGetBitsTwiceKnown() { Iterable<String> names = ImmutableList.of(KNOWN_PRIV_NAME); PrivilegeBits bits1 = bitsProvider.getBits(names); PrivilegeBits bits2 = bitsProvider.getBits(names); assertNotSame(bits1, bits2); assertEquals(bits1, bits2); verify(root, times(1)).getTree(PRIVILEGES_PATH); verify(privTree, times(1)).getChild(KNOWN_PRIV_NAME); }
@Test public void testGetBitsTwiceBuitInKnown() { Iterable<String> names = ImmutableList.of(KNOWN_PRIV_NAME, JCR_ADD_CHILD_NODES); PrivilegeBits bits1 = bitsProvider.getBits(names); PrivilegeBits bits2 = bitsProvider.getBits(names); assertNotSame(bits1, bits2); assertEquals(bits1, bits2); verify(root, times(1)).getTree(PRIVILEGES_PATH); verify(privTree, times(1)).getChild(KNOWN_PRIV_NAME); }
@Test public void testGetBitsTwiceKnownUnknown() { Iterable<String> names = ImmutableList.of(KNOWN_PRIV_NAME, "unknown"); PrivilegeBits bits1 = bitsProvider.getBits(names); PrivilegeBits bits2 = bitsProvider.getBits(names); assertNotSame(bits1, bits2); assertEquals(bits1, bits2); assertEquals(bitsProvider.getBits(KNOWN_PRIV_NAME), bits1); verify(root, times(2)).getTree(PRIVILEGES_PATH); verify(privTree, times(1)).getChild(KNOWN_PRIV_NAME); }
@Test public void testGetBitsFromEmptyPrivileges() { assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits(new Privilege[0], NamePathMapper.DEFAULT)); }
@Test public void testGetBitsFromPrivilegesInvalidMapping() { Privilege p = Mockito.mock(Privilege.class); when(p.getName()).thenReturn("name"); NamePathMapper mapper = new NamePathMapper.Default() { @Override public String getOakNameOrNull(@NotNull String jcrName) { return null; } }; assertSame(PrivilegeBits.EMPTY, bitsProvider.getBits(new Privilege[] {p}, mapper)); }