PrivilegeManagerImpl implements PrivilegeManager { @NotNull @Override public Privilege registerPrivilege(@NotNull String privilegeName, boolean isAbstract, @Nullable String[] declaredAggregateNames) throws RepositoryException { if (root.hasPendingChanges()) { throw new InvalidItemStateException("Attempt to register a new privilege while there are pending changes."); } if (privilegeName == null || privilegeName.isEmpty()) { throw new RepositoryException("Invalid privilege name " + privilegeName); } PrivilegeDefinition definition = new ImmutablePrivilegeDefinition(getOakName(privilegeName), isAbstract, getOakNames(declaredAggregateNames)); PrivilegeDefinitionWriter writer = new PrivilegeDefinitionWriter(getWriteRoot()); writer.writeDefinition(definition); root.refresh(); return getPrivilege(definition); } PrivilegeManagerImpl(Root root, NamePathMapper namePathMapper); @NotNull @Override Privilege[] getRegisteredPrivileges(); @NotNull @Override Privilege getPrivilege(@NotNull String privilegeName); @NotNull @Override Privilege registerPrivilege(@NotNull String privilegeName, boolean isAbstract,
                                       @Nullable String[] declaredAggregateNames);  }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegePendingChanges() throws Exception { Root r = Mockito.mock(Root.class); when(r.hasPendingChanges()).thenReturn(true); create(r).registerPrivilege("privName", true, null); }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegeEmptyName() throws Exception { privilegeManager.registerPrivilege("", true, new String[]{"jcr:read", "jcr:write"}); }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegeNullName() throws Exception { privilegeManager.registerPrivilege(null, true, new String[]{"jcr:read", "jcr:write"}); }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegeUnknownAggreate() throws Exception { privilegeManager.registerPrivilege(null, true, new String[]{"unknown", "jcr:read"}); }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegeReservedNamespace() throws Exception { privilegeManager.registerPrivilege("jcr:customPrivilege", true, new String[]{"jcr:read", "jcr:write"}); }
@Test public void testRegisterAggregated() throws Exception { privilegeManager.registerPrivilege("test:customPrivilege", false, new String[] { "jcr:read", "jcr:write" }); }
@Test(expected = RepositoryException.class) public void testRegisterAggregatedNonExisting() throws Exception { privilegeManager.registerPrivilege("test:customPrivilege", false, new String[] { "test:nan" }); }
@Test(expected = RepositoryException.class) public void testRegisterPrivilegeReservedRemappedNamespace() throws Exception { NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(root, ImmutableMap.of("prefix", NamespaceRegistry.NAMESPACE_JCR))); PrivilegeManager pmgr = create(root, mapper); pmgr.registerPrivilege("prefix:customPrivilege", true, new String[] {"prefix:read", "prefix:write"}); }
@Test public void testRegisterPrivilegeRemappedNamespace() throws Exception { ReadWriteNamespaceRegistry nsRegistry = new ReadWriteNamespaceRegistry(root) { @Override protected Root getWriteRoot() { return root; } }; nsRegistry.registerNamespace("ns", "http: Map<String, String> localMapping = ImmutableMap.of( "prefix", NamespaceRegistry.NAMESPACE_JCR, "prefix2", "http: NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(root, localMapping)); PrivilegeManager pmgr = create(root, mapper); Privilege p = pmgr.registerPrivilege("prefix2:customPrivilege", true, new String[] {"prefix:read", "prefix:write"}); assertEquals("prefix2:customPrivilege", p.getName()); assertEquals(2, p.getDeclaredAggregatePrivileges().length); Tree privilegesTree = root.getTree(PrivilegeConstants.PRIVILEGES_PATH); assertFalse(privilegesTree.hasChild("prefix2:customPrivilege")); Tree privTree = privilegesTree.getChild("ns:customPrivilege"); assertTrue(privTree.exists()); assertTrue(TreeUtil.getBoolean(privTree, PrivilegeConstants.REP_IS_ABSTRACT)); Iterable<String> aggr = TreeUtil.getStrings(privTree, PrivilegeConstants.REP_AGGREGATES); assertNotNull(aggr); assertEquals(ImmutableSet.of("jcr:read", "jcr:write"), ImmutableSet.copyOf(aggr)); }