This is an automated email from the ASF dual-hosted git repository.

enorman pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-jcr-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 7929b38  SLING-12168 Implement MockUserManager#createSystemUser (#29)
7929b38 is described below

commit 7929b388537494b0d77fd6a12ef4352e30a75784
Author: Eric Norman <enor...@apache.org>
AuthorDate: Thu Dec 7 15:12:37 2023 -0800

    SLING-12168 Implement MockUserManager#createSystemUser (#29)
---
 .../apache/sling/testing/mock/jcr/MockUser.java    |  3 +-
 .../sling/testing/mock/jcr/MockUserManager.java    | 47 ++++++++++++++++--
 .../sling/testing/mock/jcr/package-info.java       |  2 +-
 .../testing/mock/jcr/MockUserManagerTest.java      | 57 +++++++++++++++++++++-
 4 files changed, 100 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockUser.java 
b/src/main/java/org/apache/sling/testing/mock/jcr/MockUser.java
index faa1b83..ad0f2bb 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockUser.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockUser.java
@@ -26,6 +26,7 @@ import javax.jcr.SimpleCredentials;
 
 import org.apache.jackrabbit.api.security.user.Impersonation;
 import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.oak.spi.security.principal.SystemUserPrincipal;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
@@ -62,7 +63,7 @@ class MockUser extends MockAuthorizable implements User {
 
     @Override
     public boolean isSystemUser() {
-        return false;
+        return principal instanceof SystemUserPrincipal;
     }
 
     @Override
diff --git 
a/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java 
b/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
index 4b0d14c..f8bd17a 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockUserManager.java
@@ -39,6 +39,7 @@ import org.apache.jackrabbit.api.security.user.Query;
 import org.apache.jackrabbit.api.security.user.User;
 import org.apache.jackrabbit.api.security.user.UserManager;
 import org.apache.jackrabbit.commons.visitor.FilteringItemVisitor;
+import org.apache.jackrabbit.oak.spi.security.principal.SystemUserPrincipal;
 import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -82,6 +83,10 @@ public class MockUserManager implements UserManager {
                         if (node.isNodeType(UserConstants.NT_REP_USER)) {
                             String userID = 
node.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString();
                             authorizables.computeIfAbsent(userID, id -> new 
MockUser(id, null, node, MockUserManager.this));
+                        } else if 
(node.isNodeType(UserConstants.NT_REP_SYSTEM_USER)) {
+                            String userID = 
node.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString();
+                            SystemUserPrincipal p = () -> userID;
+                            authorizables.computeIfAbsent(userID, id -> new 
MockUser(id, p, node, MockUserManager.this));
                         } else if 
(node.isNodeType(UserConstants.NT_REP_GROUP)) {
                             String userID = 
node.getProperty(UserConstants.REP_AUTHORIZABLE_ID).getString();
                             authorizables.computeIfAbsent(userID, id -> new 
MockGroup(id, null, node, MockUserManager.this));
@@ -187,7 +192,11 @@ public class MockUserManager implements UserManager {
             throw new AuthorizableExistsException("Group already exists");
         }
         String principalName = toPrincipalName(groupID, principal);
-        Node node = ensureAuthorizablePathExists(intermediatePath, 
principalName, true);
+        if (intermediatePath == null) {
+            intermediatePath = "/home/groups"; // NOSONAR
+        }
+
+        Node node = ensureAuthorizablePathExists(intermediatePath, 
principalName, UserConstants.NT_REP_GROUP);
         return (Group)authorizables.computeIfAbsent(groupID, id -> new 
MockGroup(id, principal, node, this));
     }
 
@@ -215,8 +224,10 @@ public class MockUserManager implements UserManager {
      * @param isGroup Is group
      * @return Existing or created node
      * @throws RepositoryException Repository exception
+     * @deprecated use {@link #ensureAuthorizablePathExists(String, String, 
String)} instead
      */
-    protected Node ensureAuthorizablePathExists(String intermediatePath, 
String principalName, boolean isGroup) throws RepositoryException {
+    @Deprecated(forRemoval = true, since = "1.4.0")
+    protected Node ensureAuthorizablePathExists(@Nullable String 
intermediatePath, @NotNull String principalName, boolean isGroup) throws 
RepositoryException {
         if (intermediatePath == null) {
             if (isGroup) {
                 intermediatePath = "/home/groups"; // NOSONAR
@@ -224,6 +235,20 @@ public class MockUserManager implements UserManager {
                 intermediatePath = "/home/users"; // NOSONAR
             }
         }
+        String authorizableType = isGroup ? UserConstants.NT_REP_GROUP : 
UserConstants.NT_REP_USER;
+        return ensureAuthorizablePathExists(intermediatePath, principalName, 
authorizableType);
+    }
+
+    /**
+     * Creates the user/group home folder if they don't exist yet
+     *
+     * @param intermediatePath the parent path
+     * @param principalName Principal name
+     * @param authorizableNodeType the node type for the user or group node
+     * @return Existing or created node
+     * @throws RepositoryException Repository exception
+     */
+    protected Node ensureAuthorizablePathExists(@NotNull String 
intermediatePath, @NotNull String principalName, @NotNull String 
authorizableNodeType) throws RepositoryException {
         // ensure the resource at the path exists
         String[] segments = intermediatePath.split("/");
         Node node = session.getRootNode();
@@ -236,9 +261,11 @@ public class MockUserManager implements UserManager {
             }
         }
         if (!node.hasNode(principalName)) {
-            node = node.addNode(principalName, isGroup ? 
UserConstants.NT_REP_GROUP : UserConstants.NT_REP_USER);
+            node = node.addNode(principalName, authorizableNodeType);
             node.setProperty(UserConstants.REP_PRINCIPAL_NAME, principalName);
             node.setProperty(UserConstants.REP_AUTHORIZABLE_ID, principalName);
+        } else {
+            node = node.getNode(principalName);
         }
 
         return node;
@@ -247,7 +274,8 @@ public class MockUserManager implements UserManager {
     @Override
     public @NotNull User createSystemUser(@NotNull String userID, @Nullable 
String intermediatePath)
             throws RepositoryException {
-        throw new UnsupportedOperationException();
+        SystemUserPrincipal p = () -> userID;
+        return maybeCreateUser(userID, null, p, intermediatePath);
     }
 
     @Override
@@ -268,7 +296,16 @@ public class MockUserManager implements UserManager {
             throw new AuthorizableExistsException("User already exists");
         }
         String principalName = toPrincipalName(userID, principal);
-        Node node = ensureAuthorizablePathExists(intermediatePath, 
principalName, false);
+        boolean isSystemUser = principal instanceof SystemUserPrincipal;
+        String authorizableNodeType = isSystemUser ? 
UserConstants.NT_REP_SYSTEM_USER : UserConstants.NT_REP_USER;
+        if (intermediatePath == null) {
+            if (isSystemUser) {
+                intermediatePath = "/home/users/system"; // NOSONAR
+            } else {
+                intermediatePath = "/home/users"; // NOSONAR
+            }
+        }
+        Node node = ensureAuthorizablePathExists(intermediatePath, 
principalName, authorizableNodeType);
         return (User)authorizables.computeIfAbsent(userID, id -> new 
MockUser(id, principal, node, this));
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/package-info.java 
b/src/main/java/org/apache/sling/testing/mock/jcr/package-info.java
index 78c894c..fd4f1e5 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected JCR APIs.
  */
-@org.osgi.annotation.versioning.Version("1.3.0")
+@org.osgi.annotation.versioning.Version("1.4.0")
 package org.apache.sling.testing.mock.jcr;
diff --git 
a/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java 
b/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
index acab813..bc03f2a 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockUserManagerTest.java
@@ -95,6 +95,10 @@ public class MockUserManagerTest {
         testuser1.setProperty(UserConstants.REP_AUTHORIZABLE_ID, "testuser1");
         testuser1.setProperty(UserConstants.REP_PRINCIPAL_NAME, "testuser1");
 
+        Node testsystemuser1 = usersNode.addNode("testsystemuser1", 
UserConstants.NT_REP_SYSTEM_USER);
+        testsystemuser1.setProperty(UserConstants.REP_AUTHORIZABLE_ID, 
"testsystemuser1");
+        testsystemuser1.setProperty(UserConstants.REP_PRINCIPAL_NAME, 
"testsystemuser1");
+
         Node groupsNode = homeNode.addNode("groups", 
UserConstants.NT_REP_AUTHORIZABLE_FOLDER);
         Node testgroup1 = groupsNode.addNode("testgroup1", 
UserConstants.NT_REP_GROUP);
         testgroup1.setProperty(UserConstants.REP_AUTHORIZABLE_ID, 
"testgroup1");
@@ -103,6 +107,7 @@ public class MockUserManagerTest {
         userManager.loadAlreadyExistingAuthorizables();
 
         assertNotNull(userManager.getAuthorizable("testuser1"));
+        assertNotNull(userManager.getAuthorizable("testsystemuser1"));
         assertNotNull(userManager.getAuthorizable("testgroup1"));
     }
 
@@ -191,8 +196,16 @@ public class MockUserManagerTest {
      * Test method for {@link 
org.apache.sling.testing.mock.jcr.MockUserManager#createSystemUser(java.lang.String,
 java.lang.String)}.
      */
     @Test
-    public void testCreateSystemUser() {
-        assertThrows(UnsupportedOperationException.class, () -> 
userManager.createSystemUser("systemuser1", "/home/users/system"));
+    public void testCreateSystemUser() throws RepositoryException {
+        @NotNull User user = userManager.createSystemUser("systemuser1", 
"/home/users/system/test");
+        assertTrue(user.isSystemUser());
+        assertEquals("/home/users/system/test", 
ResourceUtil.getParent(user.getPath()));
+    }
+    @Test
+    public void testCreateSystemUserWithNullIntermediatePath() throws 
RepositoryException {
+        @NotNull User user = userManager.createSystemUser("systemuser1", null);
+        assertTrue(user.isSystemUser());
+        assertEquals("/home/users/system", 
ResourceUtil.getParent(user.getPath()));
     }
 
     /**
@@ -215,6 +228,46 @@ public class MockUserManagerTest {
         assertNotNull(user1);
         assertEquals("/home/users/path1/user1", user1.getPath());
     }
+    @Test
+    public void 
testCreateUserStringStringPrincipalStringWithNullIntermediatePath() throws 
AuthorizableExistsException, RepositoryException {
+        @NotNull User user1 = userManager.createUser("user1", "pwd", () -> 
"user1", null);
+        assertNotNull(user1);
+        assertEquals("/home/users", ResourceUtil.getParent(user1.getPath()));
+    }
+
+    /**
+     * Test method for {@link 
org.apache.sling.testing.mock.jcr.MockUserManager#ensureAuthorizablePathExists(java.lang.String,
 java.lang.String, boolean)}.
+     */
+    @Deprecated
+    @Test
+    public void testEnsureAuthorizablePathExistsStringStringBoolean() throws 
RepositoryException {
+        // intermedate path supplied
+        Node user1node = 
userManager.ensureAuthorizablePathExists("/home/users/path1", "user1", false);
+        assertNotNull(user1node);
+        // no intermediate path for the user
+        Node user2node = userManager.ensureAuthorizablePathExists(null, 
"user2", false);
+        assertNotNull(user2node);
+        // no intermediate path for the group
+        Node group1node = userManager.ensureAuthorizablePathExists(null, 
"group1", true);
+        assertNotNull(group1node);
+
+        // one more time to ensure it works if the path already exists
+        user1node = 
userManager.ensureAuthorizablePathExists("/home/users/path1", "user1", false);
+        assertNotNull(user1node);
+    }
+
+    /**
+     * Test method for {@link 
org.apache.sling.testing.mock.jcr.MockUserManager#ensureAuthorizablePathExists(java.lang.String,
 java.lang.String, java.lang.String)}.
+     */
+    @Test
+    public void testEnsureAuthorizablePathExistsStringStringString() throws 
RepositoryException {
+        Node user1node = 
userManager.ensureAuthorizablePathExists("/home/users/path1", "user1", 
UserConstants.NT_REP_USER);
+        assertNotNull(user1node);
+
+        // one more time to ensure it works if the path already exists
+        user1node = 
userManager.ensureAuthorizablePathExists("/home/users/path1", "user1", 
UserConstants.NT_REP_USER);
+        assertNotNull(user1node);
+    }
 
     /**
      * Test method for {@link 
org.apache.sling.testing.mock.jcr.MockUserManager#findAuthorizables(org.apache.jackrabbit.api.security.user.Query)}.

Reply via email to