Author: [email protected]
Date: Fri Apr  6 16:35:36 2012
New Revision: 2195

Log:
[AMDATUAUTH-140] Added assertion tools, separated httptools into httptools and 
useradmintools

Added:
   
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/AssertTools.java
   
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/UserAdminTools.java
Modified:
   
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
   
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthServiceConsumerRegistryTest.java
   
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthThreeLeggedTest.java
   
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthTwoLeggedTest.java
   
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
   
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/HttpTools.java
   
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/ServiceTools.java

Modified: 
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
==============================================================================
--- 
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
        (original)
+++ 
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
        Fri Apr  6 16:35:36 2012
@@ -39,6 +39,7 @@
             
wrappedBundle(mavenBundle().groupId("commons-logging").artifactId("commons-logging").versionAsInProject()),
             
wrappedBundle(mavenBundle().groupId("org.json").artifactId("json").versionAsInProject()),
             
+            // Tools needed by this test
             
wrappedBundle(mavenBundle().groupId("org.amdatu.auth").artifactId("org.amdatu.auth.tools.itest").versionAsInProject()),
 
             // The following 2 artifacts are necessary for default tenant 
resolving

Modified: 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthServiceConsumerRegistryTest.java
==============================================================================
--- 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthServiceConsumerRegistryTest.java
 (original)
+++ 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthServiceConsumerRegistryTest.java
 Fri Apr  6 16:35:36 2012
@@ -15,7 +15,8 @@
  */
 package org.amdatu.auth.test.integration.tests;
 
-import static org.amdatu.auth.tools.itest.HttpTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.addAdminUser;
 
 import org.amdatu.auth.test.integration.tests.util.OAuthTestConsumer;
 import org.amdatu.auth.tools.itest.ServiceTools;
@@ -60,7 +61,7 @@
         // Log in using an Amdatu account, just after creating it
         m_logService.log(LogService.LOG_DEBUG, "*** Step 4: Log in with Amdatu 
account '" + TEST_USERNAME
             + "' ***");
-        m_httpUtil.addAdminUser(m_userAdmin);
+        addAdminUser(m_userAdmin);
         m_httpUtil.loginAsAdministrator();
 
         // And try again, now we should be authorized
@@ -109,7 +110,7 @@
         GetMethod getMethod = new GetMethod(url + "/" + consumerKey);
         try {
             // We want JSON!
-            m_httpUtil.copyCookieHeader(getMethod);
+            m_httpUtil.addCookieHeader(getMethod);
             
             getMethod.setRequestHeader("Accept", mimeType);
             int statusCode = httpClient.executeMethod(getMethod);
@@ -150,7 +151,7 @@
         RequestEntity requestEntity = new StringRequestEntity(body, mimeType, 
"UTF-8");
         method.setRequestEntity(requestEntity);
 
-        m_httpUtil.copyCookieHeader(method);
+        m_httpUtil.addCookieHeader(method);
 
         // Execute the method, this should return a 200
         try {
@@ -170,7 +171,7 @@
 
         // Execute the method, this should return a 200
         try {
-            m_httpUtil.copyCookieHeader(method);
+            m_httpUtil.addCookieHeader(method);
             return httpClient.executeMethod(method);
         }
         finally {

Modified: 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthThreeLeggedTest.java
==============================================================================
--- 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthThreeLeggedTest.java
     (original)
+++ 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthThreeLeggedTest.java
     Fri Apr  6 16:35:36 2012
@@ -17,7 +17,9 @@
 
 import static 
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_PARAM;
 import static 
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_THREE_LEGGED;
-import static org.amdatu.auth.tools.itest.HttpTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.addTestUser;
+import static org.amdatu.auth.tools.itest.UserAdminTools.removeTestUser;
 import net.oauth.OAuth;
 import net.oauth.OAuthMessage;
 
@@ -68,7 +70,7 @@
         // Step 4: Log in using an Amdatu account, just after creating it
         m_logService.log(LogService.LOG_DEBUG, "*** Step 4: Log in with Amdatu 
account '" + TEST_USERNAME
             + "' ***");
-        m_httpUtil.addTestUser(m_userAdmin);
+        addTestUser(m_userAdmin);
         m_httpUtil.loginAsTestUser();
 
         // Step 5: Authorize the request token for a user we define
@@ -104,7 +106,7 @@
 
         // Step 8: Cleanup; remove the consumer and our test user
         m_consumerRegistry.removeConsumer(consumer);
-        m_httpUtil.removeTestUser(m_userAdmin);
+        removeTestUser(m_userAdmin);
     }
 
     // Step 3: Generate a request token for our service consumer

Modified: 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthTwoLeggedTest.java
==============================================================================
--- 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthTwoLeggedTest.java
       (original)
+++ 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthTwoLeggedTest.java
       Fri Apr  6 16:35:36 2012
@@ -18,7 +18,9 @@
 import static 
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_PARAM;
 import static 
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_TWO_LEGGED;
 import static 
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.SERVLET_ALIAS;
-import static org.amdatu.auth.tools.itest.HttpTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.addTestUser;
+import static org.amdatu.auth.tools.itest.UserAdminTools.removeTestUser;
 import net.oauth.OAuth;
 import net.oauth.OAuthMessage;
 
@@ -64,7 +66,7 @@
 
         // Step 3c: Log in using an Amdatu account, but create that account 
first
         m_logService.log(LogService.LOG_DEBUG, "*** Step 3c: Log in with 
Amdatu account '" + TEST_USERNAME + "' ***");
-        m_httpUtil.addTestUser(m_userAdmin);
+        addTestUser(m_userAdmin);
         m_httpUtil.loginAsTestUser();
 
         // Step 3d: Authorize the request token for a user we define
@@ -103,7 +105,7 @@
 
         // Step 6: Cleanup; remove the consumer and our test user
         m_consumerRegistry.removeConsumer(consumer);
-        m_httpUtil.removeTestUser(m_userAdmin);
+        removeTestUser(m_userAdmin);
     }
 
     // Step 1c: Generate a request token for our service consumer

Modified: 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
==============================================================================
--- 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
        (original)
+++ 
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
        Fri Apr  6 16:35:36 2012
@@ -15,7 +15,7 @@
  */
 package org.amdatu.auth.test.integration.tests;
 
-import static org.amdatu.auth.tools.itest.HttpTools.TEST_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_USERNAME;
 
 import org.amdatu.auth.tools.itest.ServiceTools;
 import org.amdatu.core.tenant.TenantEntity;

Added: 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/AssertTools.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/AssertTools.java
    Fri Apr  6 16:35:36 2012
@@ -0,0 +1,100 @@
+package org.amdatu.auth.tools.itest;
+
+import org.hamcrest.Matcher;
+
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+public class AssertTools {
+    // End of line
+    private final static String EOL = System.getProperty("line.separator");
+
+    // Amount of root cause trace lines to skip and print
+    private final static int ROOT_CAUSE_SKIP_LINES = 2; // Skip first 3
+    private final static int ROOT_CAUSE_LINES = 4; // Print 3
+
+    public static void fail(String msg) {
+        try {
+            Assert.fail(msg);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static void assertEquals(Object a, Object b) {
+        try {
+            Assert.assertEquals(a, b);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static void assertTrue(String msg, boolean value) {
+        try {
+            Assert.assertTrue(msg, value);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static void assertTrue(boolean value) {
+        try {
+            Assert.assertTrue(value);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static void assertFalse(String msg, boolean value) {
+        try {
+            Assert.assertFalse(msg, value);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static void assertFalse(boolean value) {
+        try {
+            Assert.assertFalse(value);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+    
+    public static <T> void assertThat(String reason, T actual,
+        Matcher<T> matcher) {
+        try {
+            org.junit.Assert.assertThat(reason, actual, matcher);
+        }
+        catch (AssertionFailedError e) {
+            String errorMsg = e.getMessage() + EOL + getRootCause();
+            Assert.fail(errorMsg);
+        }
+    }
+
+    private static String getRootCause() {
+        String rootCause = "Root cause: ";
+        StackTraceElement[] st = Thread.currentThread().getStackTrace();
+        
+        // Omit first two lines, which are not of any interest
+        int i=ROOT_CAUSE_SKIP_LINES;
+        while (i<ROOT_CAUSE_SKIP_LINES + ROOT_CAUSE_LINES && i<st.length) {
+            rootCause += EOL + "  at " + st[i];
+            i++;
+        }
+        rootCause += EOL + "  ..." + EOL;
+        return rootCause;
+    }
+}

Modified: 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/HttpTools.java
==============================================================================
--- 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/HttpTools.java
      (original)
+++ 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/HttpTools.java
      Fri Apr  6 16:35:36 2012
@@ -17,6 +17,10 @@
 
 import static org.amdatu.auth.tools.itest.IntegrationTestBase.HOSTNAME;
 import static org.amdatu.auth.tools.itest.IntegrationTestBase.PORTNR;
+import static org.amdatu.auth.tools.itest.UserAdminTools.ADMIN_PASSWORD;
+import static org.amdatu.auth.tools.itest.UserAdminTools.ADMIN_USERNAME;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_PASSWORD;
+import static org.amdatu.auth.tools.itest.UserAdminTools.TEST_USERNAME;
 
 import java.io.IOException;
 import java.util.HashMap;
@@ -34,12 +38,9 @@
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
-import org.osgi.service.useradmin.Role;
-import org.osgi.service.useradmin.User;
-import org.osgi.service.useradmin.UserAdmin;
 
 /**
- * This class provides utility methods for user authentication, using OSGi 
UserAdmin and invoking REST services.
+ * This class provides utility methods for invoking (authentorized) REST 
services.
  * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  * 
@@ -47,42 +48,13 @@
 public class HttpTools {
     // Credential constants
     public final static String TOKEN_COOKIE_NAME = "amdatu_token";
-    public final static String PASSWORD_CREDENTIAL_KEY = "password";
-
-    // Test user
-    public final static String TEST_USERNAME = "georged";
-    public final static String TEST_PASSWORD = "georged";
-
-    // Account of the administrator (you must be Administrator to access some 
APIs)
-    public final static String ADMIN_USERNAME = "Administrator";
-    public final static String ADMIN_PASSWORD = "Administrator";
 
     // Private members
     private String m_host = null;
     private Map<String, HeaderElement> m_cookieHeaderElement = new 
HashMap<String, HeaderElement>();
     private String m_restBaseUrl = null;
 
-    public void copyCookieHeader(HttpMethod method) {
-        if (getCookieHeader() != null) {
-            for (String key : getCookieHeader().keySet()) {
-                method.addRequestHeader(key, getCookieHeader().get(key));
-            }
-        }
-    }
-
-    public Map<String, String> getCookieHeader() {
-        Map<String, String> requestHeaders = new HashMap<String, String>();
-        if (m_cookieHeaderElement != null) {
-            for (String key : m_cookieHeaderElement.keySet()) {
-                String header =
-                    m_cookieHeaderElement.get(key).getName() + "=" + 
m_cookieHeaderElement.get(key).getValue();
-                requestHeaders.put("Cookie", header);
-            }
-
-        }
-        return requestHeaders;
-    }
-
+    // Logs in as test user and remembers the amdatu_token returned in the 
cookie
     public void loginAsTestUser() throws HttpException, IOException {
         if (m_host == null) {
             m_host = HOSTNAME;
@@ -90,6 +62,7 @@
         m_cookieHeaderElement.put(m_host, login(HOSTNAME, TEST_USERNAME, 
TEST_PASSWORD));
     }
 
+    // Logs in as test user and remembers the amdatu_token returned in the 
cookie
     public void loginAsAdministrator() throws HttpException, IOException {
         if (m_host == null) {
             m_host = HOSTNAME;
@@ -97,38 +70,8 @@
         m_cookieHeaderElement.put(m_host, login(m_host, ADMIN_USERNAME, 
ADMIN_PASSWORD));
     }
 
-    public void addAdminUser(UserAdmin userAdmin) {
-        addUser(userAdmin, ADMIN_USERNAME, ADMIN_PASSWORD);
-    }
-
-    public void addTestUser(UserAdmin userAdmin) {
-        addUser(userAdmin, TEST_USERNAME, TEST_PASSWORD);
-    }
-
-    @SuppressWarnings("unchecked")
-    private void addUser(UserAdmin userAdmin, String name, String password) {
-        User user = (User) userAdmin.createRole(name, Role.USER);
-
-        // Verify if the user is not null, if it is null it did already exist. 
This is the case for
-        // example when the integration test is executed twice without 
removing the work directory n between.
-        if (user != null) {
-            user.getCredentials().put(PASSWORD_CREDENTIAL_KEY, password);
-            Assert.assertTrue("Test user '" + name + "' could not be created", 
userAdmin.getRole(name) != null);
-        }
-        else {
-            // Else, update the password.
-            user = (User) userAdmin.getRole(name);
-            user.getCredentials().put(PASSWORD_CREDENTIAL_KEY, password);
-        }
-    }
-
-    public void removeTestUser(UserAdmin userAdmin) {
-        userAdmin.removeRole(TEST_USERNAME);
-        Assert.assertFalse("Test user '" + TEST_USERNAME + "' could not be 
removed",
-            userAdmin.getRole(TEST_USERNAME) != null);
-    }
-
-    public HeaderElement login(String host, String username, String password) 
throws HttpException, IOException {
+    // Log in as the specified user and remembers the amdatu_token returned in 
the cookie
+    private HeaderElement login(String host, String username, String password) 
throws HttpException, IOException {
         String loginUrl = "http://"; + host + ":" + IntegrationTestBase.PORTNR 
+ "/rest/authorization/login";
         HttpClient httpClient = new HttpClient();
         PostMethod postMethod = null;
@@ -156,6 +99,26 @@
         return null;
     }
 
+    // Returns a request header map containing the amdatu_token Cookie for the 
current host
+    public Map<String, String> getCookieHeader() {
+        Map<String, String> requestHeaders = new HashMap<String, String>();
+        if (m_cookieHeaderElement.get(m_host) != null) {
+            String header =
+                m_cookieHeaderElement.get(m_host).getName() + "=" + 
m_cookieHeaderElement.get(m_host).getValue();
+            requestHeaders.put("Cookie", header);
+        }
+        return requestHeaders;
+    }
+    
+    // Append a request header to the method containing the amdatu_token 
Cookie for the current host
+    public void addCookieHeader(HttpMethod method) {
+        if (m_cookieHeaderElement.get(m_host) != null) {
+            String header =
+                m_cookieHeaderElement.get(m_host).getName() + "=" + 
m_cookieHeaderElement.get(m_host).getValue();
+            method.addRequestHeader("Cookie", header);
+        }
+    }
+    
     public String getRESTBaseUrl() throws Exception {
         if (m_restBaseUrl == null) {
             m_restBaseUrl = "http://"; + HOSTNAME + ":" + PORTNR + "/rest";
@@ -163,11 +126,13 @@
         return m_restBaseUrl;
     }
 
+    // Switch to a new host for REST invocations
     public void switchHost(String newHostName) {
         m_host = newHostName;
         m_restBaseUrl = "http://"; + newHostName + ":" + PORTNR + "/rest";
     }
 
+    // Invoke a REST API
     public String invokeRestApi(String urlPostfix, String httpMethod, int 
expectedStatus) throws Exception {
         String url = getRESTBaseUrl() + urlPostfix;
         HttpClient httpClient = new HttpClient();
@@ -200,12 +165,4 @@
             method.releaseConnection();
         }
     }
-
-    protected void addCookieHeader(HttpMethod method) {
-        if (m_cookieHeaderElement.get(m_host) != null) {
-            String header =
-                m_cookieHeaderElement.get(m_host).getName() + "=" + 
m_cookieHeaderElement.get(m_host).getValue();
-            method.addRequestHeader("Cookie", header);
-        }
-    }
 }

Modified: 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/ServiceTools.java
==============================================================================
--- 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/ServiceTools.java
   (original)
+++ 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/ServiceTools.java
   Fri Apr  6 16:35:36 2012
@@ -17,7 +17,6 @@
 
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.notNullValue;
-import static org.junit.Assert.assertThat;
 
 import org.amdatu.core.itest.base.TestContext;
 
@@ -29,6 +28,7 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
+import static org.amdatu.auth.tools.itest.AssertTools.*;
 
 /**
  * This class provides utility methods to manage services and service 
dependencies in
@@ -40,6 +40,9 @@
     // Time out for the http service to come available
     private final static int HTTP_TIMEOUT = 10000;
     
+    // Time out for the service to come available (10 seconds)
+    private static final long SERVICE_TIMEOUT = 10000;
+    
     /**
      * Verifies the availability of a particular service and returns it. If 
the service
      * is not available, assertion fails.
@@ -51,10 +54,19 @@
      */
     public static <T> T assertAvailable(TestContext testContext, Class<T> 
serviceClass) throws Exception {
         T service = getService(testContext, serviceClass);
+        long timeout = System.currentTimeMillis() + SERVICE_TIMEOUT;
+        while (service == null && System.currentTimeMillis() < timeout) {
+            // Wait a second
+            Thread.sleep(1000);
+            
+            // ... and try again
+            service = getService(testContext, serviceClass);
+        }
         assertThat("Expected a " + serviceClass.getName(), service, 
is(notNullValue()));
         return service;
     }
 
+    
     /**
      * FIXME: [AMDATU-379] This method is here since TestContext.getService 
doesn't (always) work.
      * For optimal performance we do this in 3 steps:

Added: 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/UserAdminTools.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/itest/src/main/java/org/amdatu/auth/tools/itest/UserAdminTools.java
 Fri Apr  6 16:35:36 2012
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed 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.amdatu.auth.tools.itest;
+
+import junit.framework.Assert;
+
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdmin;
+
+/**
+ * This class provides some utility methods to manage users in UserAdmin.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class UserAdminTools {
+    // Password credential key
+    public final static String PASSWORD_CREDENTIAL_KEY = "password";
+    
+    // Test user
+    public final static String TEST_USERNAME = "georged";
+    public final static String TEST_PASSWORD = "georged";
+
+    // Account of the administrator (you must be Administrator to access some 
APIs)
+    public final static String ADMIN_USERNAME = "Administrator";
+    public final static String ADMIN_PASSWORD = "Administrator";
+    
+    public static void addAdminUser(UserAdmin userAdmin) {
+        addUser(userAdmin, ADMIN_USERNAME, ADMIN_PASSWORD);
+    }
+
+    public static void addTestUser(UserAdmin userAdmin) {
+        addUser(userAdmin, TEST_USERNAME, TEST_PASSWORD);
+    }
+
+    public static void removeTestUser(UserAdmin userAdmin) {
+        userAdmin.removeRole(TEST_USERNAME);
+        Assert.assertFalse("Test user '" + TEST_USERNAME + "' could not be 
removed",
+            userAdmin.getRole(TEST_USERNAME) != null);
+    }
+    
+    @SuppressWarnings("unchecked")
+    public static void addUser(UserAdmin userAdmin, String name, String 
password) {
+        User user = (User) userAdmin.createRole(name, Role.USER);
+
+        // Verify if the user is not null, if it is null it did already exist. 
This is the case for
+        // example when the integration test is executed twice without 
removing the work directory n between.
+        if (user != null) {
+            user.getCredentials().put(PASSWORD_CREDENTIAL_KEY, password);
+            Assert.assertTrue("Test user '" + name + "' could not be created", 
userAdmin.getRole(name) != null);
+        }
+        else {
+            // Else, update the password.
+            user = (User) userAdmin.getRole(name);
+            user.getCredentials().put(PASSWORD_CREDENTIAL_KEY, password);
+        }
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to