directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smckin...@apache.org
Subject directory-fortress-core git commit: LoadTestUserSample for benchmarking
Date Sun, 25 Jun 2017 17:02:24 GMT
Repository: directory-fortress-core
Updated Branches:
  refs/heads/master 75ac18a43 -> dc8a7cac5


LoadTestUserSample for benchmarking


Project: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/commit/dc8a7cac
Tree: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/tree/dc8a7cac
Diff: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/diff/dc8a7cac

Branch: refs/heads/master
Commit: dc8a7cac5c360a56858b809215386ba82aaef670
Parents: 75ac18a
Author: Shawn McKinney <smckinney@apache.org>
Authored: Sun Jun 25 12:02:18 2017 -0500
Committer: Shawn McKinney <smckinney@apache.org>
Committed: Sun Jun 25 12:02:18 2017 -0500

----------------------------------------------------------------------
 .../core/samples/LoadTestUserSample.java        | 223 +++++++++++++++++++
 1 file changed, 223 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-fortress-core/blob/dc8a7cac/src/test/java/org/apache/directory/fortress/core/samples/LoadTestUserSample.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/directory/fortress/core/samples/LoadTestUserSample.java
b/src/test/java/org/apache/directory/fortress/core/samples/LoadTestUserSample.java
new file mode 100644
index 0000000..8066a7a
--- /dev/null
+++ b/src/test/java/org/apache/directory/fortress/core/samples/LoadTestUserSample.java
@@ -0,0 +1,223 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.fortress.core.samples;
+
+import org.apache.directory.fortress.core.GlobalErrIds;
+import org.apache.directory.fortress.core.SecurityException;
+import org.apache.directory.fortress.core.AdminMgr;
+import org.apache.directory.fortress.core.AdminMgrFactory;
+import org.apache.directory.fortress.core.ReviewMgr;
+import org.apache.directory.fortress.core.ReviewMgrFactory;
+import org.apache.directory.fortress.core.impl.TestUtils;
+import org.apache.directory.fortress.core.model.User;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.directory.fortress.core.model.UserRole;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * LoadTestUserSample JUnit Test.  These samples create many users and assign them to roles.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LoadTestUserSample extends TestCase
+{
+    private static final String CLS_NM = CreateUserSample.class.getName();
+    private static final Logger LOG = LoggerFactory.getLogger( CLS_NM );
+    public static int NUMBER_TEST_USERS = 1000;
+    public static int NUMBER_TEST_ROLES = 10;
+    public static final String TEST_ROLE = "loadtestrole";
+    public static final String TEST_USERID = "loadtestuser";
+    public static final String TEST_PASSWORD = "secret";
+
+
+    /**
+     * @param name
+     */
+    public LoadTestUserSample(String name)
+    {
+        super(name);
+    }
+
+    /**
+     * Run the Create User samples.
+     *
+     * @return Test
+     */
+    public static Test suite()
+    {
+        TestSuite suite = new TestSuite();
+
+        if(teardownRequired())
+        {
+            suite.addTest(new LoadTestUserSample("testDeleteUser"));
+        }
+        suite.addTest(new LoadTestUserSample("testCreateUser"));
+        suite.addTest(new LoadTestUserSample("testAssignUser"));
+        return suite;
+    }
+
+    /**
+     * Determines if teardown needs to occur on sample data.
+     *
+     * @return true if teardown is required
+     */
+    static boolean teardownRequired()
+    {
+        // The default for this check is 'true'
+        boolean tearDown = true;
+        String methodName = ".teardownRequired";
+        try
+        {
+            ReviewMgr reviewMgr = ReviewMgrFactory.createInstance(TestUtils.getContext());
+            User inUser = new User(TEST_USERID + 1, TEST_PASSWORD);
+            reviewMgr.readUser(inUser);
+            // If we get here, the sample data needs to be removed:
+        }
+        catch ( SecurityException ex)
+        {
+            if(ex.getErrorId() == GlobalErrIds.USER_NOT_FOUND)
+            {
+                // If we get here the sample data does not need to be removed:
+                tearDown = false;
+            }
+            else
+            {
+                String warning = methodName + " caught SecurityException=" + ex.getMessage();
+                LOG.warn(warning);
+            }
+        }
+        LOG.info(methodName + ":" + tearDown);
+        return tearDown;
+    }
+
+    /**
+     * The deleteUser will completely remove the User data from the LDAP directory.  There
is also a 'softDelete' that
+     * can be used to disable the User if hard delete is not the aim.
+     *
+     */
+    public static void testDeleteUser()
+    {
+        String szLocation = ".testDeleteUser";
+
+        try
+        {
+            // Instantiate the AdminMgr implementation which is used to provision RBAC policies.
+            AdminMgr adminMgr = AdminMgrFactory.createInstance(TestUtils.getContext());
+
+            for( int i = 1; i <= NUMBER_TEST_USERS; i++)
+            {
+                User inUser = new User( TEST_USERID + i );
+                adminMgr.deleteUser( inUser );
+            }
+            LOG.info(szLocation + " users delete success");
+        }
+        catch (SecurityException ex)
+        {
+            LOG.error(szLocation + " caught SecurityException rc=" + ex.getErrorId() + ",
msg=" + ex.getMessage(), ex);
+            fail(ex.getMessage());
+        }
+    }
+
+    /**
+     * Demonstrate how to create a simple user and assign to a single RBAC Role in one API
call.  The example will
+     * also read the User back from LDAP after creation but this is not required for real
world examples.
+     */
+    public static void testCreateUser()
+    {
+        String szLocation = ".testCreateUser";
+        try
+        {
+            // Instantiate the AdminMgr implementation.  All AdminMgr APIs can throw a SecurityException
in the event
+            // of rule violation or system error.
+            AdminMgr adminMgr = AdminMgrFactory.createInstance(TestUtils.getContext());
+            // You do not have to assign a Role to User when calling 'addUser'.  Role assignment
may be done using the 'assignUser' API.
+            /**
+             * Create new User entity:
+             *   {@link org.apache.directory.fortress.core.model.User#userId}="sampleUser1"
+             *   {@link User#password}="password1"
+             *   {@link User#setRole(String)}="sampleRole1"
+             *   {@link User#ou}="sampleUserOU1"
+             */
+            //User inUser = new User(TEST_USERID, TEST_PASSWORD, CreateRoleSample.TEST_SIMPLE_ROLE,
CreateUserOrgSample.TEST_USER_OU_NM);
+            //User inUser = new User(TEST_USERID, TEST_PASSWORD, CreateRoleSample.TEST_SIMPLE_ROLE,
CreateUserOrgSample.TEST_USER_OU_NM);
+
+            for( int i = 1; i <= NUMBER_TEST_USERS; i++)
+            {
+                User inUser = new User(TEST_USERID + i, TEST_PASSWORD);
+                inUser.setOu("DEV0");
+                // Now call the add API.  The API will return User entity with associated
LDAP dn if creation was successful.
+                User outUser = adminMgr.addUser(inUser);
+                assertNotNull(outUser);
+            }
+
+            LOG.info(szLocation + " users create success");
+        }
+        catch (SecurityException ex)
+        {
+            LOG.error(szLocation + " caught SecurityException rc=" + ex.getErrorId() + ",
msg=" + ex.getMessage(), ex);
+            fail(ex.getMessage());
+        }
+    }
+
+    /**
+     * Demonstrate how to create a simple user and assign to a single RBAC Role in one API
call.  The example will
+     * also read the User back from LDAP after creation but this is not required for real
world examples.
+     */
+    public static void testAssignUser()
+    {
+        String szLocation = ".testCreateUser";
+        try
+        {
+            // Instantiate the AdminMgr implementation.  All AdminMgr APIs can throw a SecurityException
in the event
+            // of rule violation or system error.
+            AdminMgr adminMgr = AdminMgrFactory.createInstance(TestUtils.getContext());
+            // You do not have to assign a Role to User when calling 'addUser'.  Role assignment
may be done using the 'assignUser' API.
+            /**
+             * Create new User entity:
+             *   {@link org.apache.directory.fortress.core.model.User#userId}="sampleUser1"
+             *   {@link User#password}="password1"
+             *   {@link User#setRole(String)}="sampleRole1"
+             *   {@link User#ou}="sampleUserOU1"
+             */
+            //User inUser = new User(TEST_USERID, TEST_PASSWORD, CreateRoleSample.TEST_SIMPLE_ROLE,
CreateUserOrgSample.TEST_USER_OU_NM);
+            //User inUser = new User(TEST_USERID, TEST_PASSWORD, CreateRoleSample.TEST_SIMPLE_ROLE,
CreateUserOrgSample.TEST_USER_OU_NM);
+
+            for( int i = 1; i <= NUMBER_TEST_USERS; i++)
+            {
+                for( int j = 1; j <= NUMBER_TEST_ROLES; j++ )
+                {
+                    UserRole inUserRole = new UserRole(TEST_USERID + i, TEST_ROLE + j);
+                    // Now call the assignUser API.  The API will assign user to specified
role.
+                    adminMgr.assignUser( inUserRole );
+                }
+            }
+
+            LOG.info(szLocation + " users assignment success");
+        }
+        catch (SecurityException ex)
+        {
+            LOG.error(szLocation + " caught SecurityException rc=" + ex.getErrorId() + ",
msg=" + ex.getMessage(), ex);
+            fail(ex.getMessage());
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message