Files
alfresco-community-repo/source/test-java/org/alfresco/repo/usage/RepoUsageComponentTest.java
Alan Davis be1414ecde Merged 5.0.N (5.0.3) to 5.1.N (5.1.1)
112930: Merged V4.2-BUG-FIX (4.2.6) to 5.0.N (5.0.3)
      112707 adavis: Merged V4.2.5 (4.2.5) to V4.2-BUG-FIX (4.2.6)
         112677 dvaserin: MNT-14780 (MNT-13871): Licence Error message when in overage
          - Place overage buffer information into License MBean and use it in admin license ftl.
         112634 adavis: MNT-13871: Merged DEV (V4.2.5-MNT-13871-LATEST) to V4.2.5 (4.2.5)
            111550: Merged DEV (4.1.11) to DEV (4.2.5)
               MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
                  - Merged all made changes in core alfresco projects as single commit to fresh dev branch forked from V4.1-BUG-FIX.
               MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
                  - added try/finally approach to reset show delete users flag to default value.,
            111574: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Added V4.2.5 specific changes.,
            111582: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Updated the jar to correspond with the latest changes in authorised-users/4.2,
            111751: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Removed unnecessary configuration.,
            111752: Merged DEV (V4.1.11) to DEV (V4.2.5)
               111632: MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
                  - Fixed unit tests for build plan: https://bamboo.alfresco.com/bamboo/browse/DEV-ENT414-1,
            111753: Merged DEV (4.1.11) to DEV (V4.2.5)
               111719: MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
                  - Updated authorization related icons with ones provided by Mark Howarth.
                  - Information message in Deauthorize Dialog was changed to the on provided by Mark Howarth.,
            111754: MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
               - Updated to the latest authorised-users lib.,
            111761: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Fix for the SchemaReferenceFileTest.,
            111800: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Modified the test to create a tenant in a retrying transaction to solve the DataIntegrityViolationException.,
            111801: Megred DEV (4.1.11) to DEV (4.2.5)
               111795: MNT-13871 : Count authorised users as those who've logged in, not total # of person objects
                  - Updated Deleted column to display text instead of tick icon.
                  - Added name for actions column.,
            111813: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
               - Updated to the latest authorisation jar to fix the Scema Reference test.,
            112020: MNT-14775: De-authorisation pop-up has similar title to first line of text
              - Remove the duplicated text from the pop-up body
              - Updated link to point the 4.2 documentation,
            112134: MNT-14763: [UserAuth] New entries are added in the authorization table for each different username case
             - Update authorization jar,
            112144: MNT-13871: Optimized strings in users.get.properties,
            112184: MNT-14762 : [UserAuth]Users from the ALFRESCO_ADMINISTRATORS LDAP group are not pre-authorized
               - Updated authorized-users jar with fix for ldap administrators pre-authorization.,
            112196: MNT-14729 : [UserAuth] The column header for the Deauthorize and Reauthorize button should have a name
               - Updated name for re/de-authorize actions column.,
            112202: MNT-14763: [UserAuth] New entries are added in the authorization table for each different username case
             - Add new authorization jar to fix null errors,
            112203: MNT-14779: System behaviour when system set read only due to licence exception
             - Force read-write transaction for LoginPost script.,
            112225: MNT-14763: [UserAuth] New entries are added in the authorization table for each different username case
             - Add new authorization jar after unit test fixing.,
            112226: MNT-14740 : [UserAuth] The message for the failed re authorization is not displayed onIE
               - Code for reloading page was wrapped with YAHOO.lang.later() function.,
            112247: MNT-14747 : [UserAuth]The Autorization State isn't change when the user logs via FTP.
               - Updated authorization jar with fix for kerberos unit test.,
            112259: MNT-14776: Heading banner text when using all allowed authorised users.
               - Changed the message.
               - Updated the authorised-users jar.,
            112269: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
             - Wrap methods that uses 'authenticationComponent.setCurrentUser' into RetryingTransaction for retrying on DataIntegrityViolationException.,
            112285: MNT-14747 : [UserAuth]The Autorization State isn't change when the user logs via FTP.
               - Updated version for authorized-users jar.,
            112388: MNT-14809: [UserAuth] On a cluster setup after running the hourly job only one node goes into R/O mode
               - Updated the authorised-users.jar,
            112439: MNT-14815 : Wrong documentation link used in the deauthorize message window
               - Corrected link to documentation.,
            112445: MNT-14818 : [UserAuth] Missing quotation mark in the user reauthorization window message
               - Corrected message for reauthorization dialog.,
            112450: MNT-14802: [UserAuth] License warning message displayed for all the users
               - Updated the authorised-users jar.,
            112454: MNT-14780: Licence Error message when in overage
             - Make license warn messages on admin console the same as banner warn messages.,
            112471: MNT-14773 : Sort state not persisted after authorisation action
               - Sorting/paging state is now persists in sessionStorage.,
            112478: MNT-14773 : Sort state not persisted after authorisation action
               - Corrected previous fix.,
            112483: MNT-14814: [UserAuth] System goes into R/O mode after running the hourly job
               - Updated the authorised-users jar to the latest version.,
            112512: MNT-14833: [UserAuth] User cannot login on both nodes (cluster env.)
             - Update authorization jar.,
            112556: MNT-14787: [UserAuth] Login via NFS works when the license limit exceed
               - Modified the exception handling of setCurrentUser method. Now all the exceptions are rethrown.,
            112570: MNT-14838: Correct the Max Users string in the Admin Console License Screen
               - Corrected the string, remove the incorrect translations from the bundle.,
            112572: REVERSE MERGED  <<code is not worth to implement>>
               112471 : MNT-14773 : Sort state not persisted after authorisation action
                  - Sorting/paging state is now persists in sessionStorage. 
               112478 : MNT-14773 : Sort state not persisted after authorisation action
                  - Corrected previous fix. ,
            112610: MNT-14837 : Report license overage to the log files on authorization/deauthorization of users
               - Updated users authorization jar file.,
            112627: MNT-14839: [UserAuth]Alfresco goes in read -only after reauthorize a user from LDAP ALFESCO_ADMINISTRATORS group
               - Updated the authorised-users jar to the latest version.
   Merged DEV (V5.0.2-MNT-13871) to 5.0.N (5.0.3)
      111954: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         - Removed context reference to authorization.,
      112003: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         -  Added copying of sources and configuration from authorised-users to the output directory during build.,
      112014: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         - Modified the build configuration to unpack the resources to the test folders.,
      112027: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         - Added unpacking of authorisation resources to repoitory-crypto project.,
      112067: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         - Removed unpacking of resources, changed the context imports to point to authorization.,
   112952: Merged DEV (V5.0.2-MNT-13871) to 5.0.N (5.0.3)
      111887: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
         - Corrected the authorised-users version to correspond to 5.0.,
   113060: Merged V4.2-BUG-FIX (4.2.6) to 5.0.N (5.0.3)
      113059 adavis: Merged V4.2.5 (4.2.5) to V4.2-BUG-FIX (4.2.6)
         113058 adavis: MNT-13871: Count authorised users as those who've logged in, not total # of person objects
            - Version dependency for alfresco-authorised-users moved to top level pom


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.1.N/root@113101 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2015-09-25 13:42:39 +00:00

283 lines
10 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.usage;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.alfresco.repo.lock.JobLockService;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.admin.RepoUsage;
import org.alfresco.service.cmr.admin.RepoUsage.LicenseMode;
import org.alfresco.service.cmr.admin.RepoUsage.UsageType;
import org.alfresco.service.cmr.admin.RepoUsageStatus;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.test_category.OwnJVMTestsCategory;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.FixMethodOrder;
import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters;
import org.springframework.context.ApplicationContext;
/**
* Tests {@link RepoUsageComponent}
*
* @author Derek Hulley
* @since 3.5
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Category(OwnJVMTestsCategory.class)
public class RepoUsageComponentTest extends TestCase
{
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private static final Log logger = LogFactory.getLog(RepoUsageComponentTest.class);
private TransactionService transactionService;
private RepoUsageComponent repoUsageComponent;
private JobLockService jobLockService;
private UserTransaction txn;
private RepoUsage restrictionsBefore;
@Override
protected void setUp() throws Exception
{
if (AlfrescoTransactionSupport.isActualTransactionActive())
{
fail("Test started with transaction in progress");
}
transactionService = (TransactionService) ctx.getBean("transactionComponent");
repoUsageComponent = (RepoUsageComponent) ctx.getBean("repoUsageComponent");
jobLockService = (JobLockService) ctx.getBean("jobLockService");
AuthenticationUtil.setRunAsUserSystem();
txn = transactionService.getUserTransaction();
txn.begin();
restrictionsBefore = repoUsageComponent.getRestrictions();
}
@Override
protected void tearDown() throws Exception
{
// Reset restrictions
try
{
repoUsageComponent.setRestrictions(restrictionsBefore);
}
catch (Throwable e)
{
e.printStackTrace();
}
AuthenticationUtil.clearCurrentSecurityContext();
if (txn != null)
{
try
{
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw new RuntimeException("Failed to commit test transaction", e);
}
}
}
public void test1Setup()
{
}
/**
* Helper to wrap in a txn
*/
private RepoUsage getUsage()
{
RetryingTransactionCallback<RepoUsage> getCallback = new RetryingTransactionCallback<RepoUsage>()
{
@Override
public RepoUsage execute() throws Throwable
{
return repoUsageComponent.getUsage();
}
};
return transactionService.getRetryingTransactionHelper().doInTransaction(getCallback, true);
}
/**
* Helper to wrap in a txn
*/
private boolean updateUsage(final UsageType usageType)
{
RetryingTransactionCallback<Boolean> getCallback = new RetryingTransactionCallback<Boolean>()
{
@Override
public Boolean execute() throws Throwable
{
return repoUsageComponent.updateUsage(usageType);
}
};
return transactionService.getRetryingTransactionHelper().doInTransaction(getCallback, false);
}
public void test2NoTxn() throws Throwable
{
txn.commit();
txn = null;
try
{
repoUsageComponent.getUsage();
fail("Txn required for calls to RepoAdminComponent.");
}
catch (RuntimeException e)
{
// Expected
}
}
public void test3GetUsage()
{
getUsage();
}
public void test4FullUse() throws Exception
{
// Update usage
updateUsage(UsageType.USAGE_ALL);
// Set the restrictions
RepoUsage restrictions = new RepoUsage(
System.currentTimeMillis(),
getUsage().getUsers(),
10000L,
LicenseMode.TEAM,
System.currentTimeMillis() + 24*3600000,
false);
repoUsageComponent.setRestrictions(restrictions);
// Get the restrictions (should not need a txn for this)
RepoUsage restrictionsCheck = repoUsageComponent.getRestrictions();
assertEquals("Restrictions should return without change.", restrictions, restrictionsCheck);
// Update use
updateUsage(UsageType.USAGE_ALL);
// Get the usage
RepoUsage usage = getUsage();
// Check
assertNotNull("Usage is null", usage);
assertNotNull("Invalid user count", usage.getUsers());
assertNotNull("Invalid document count", usage.getDocuments());
assertEquals("License mode not set", restrictions.getLicenseMode(), usage.getLicenseMode());
assertEquals("License expiry not set", restrictions.getLicenseExpiryDate(), usage.getLicenseExpiryDate());
assertEquals("Read-only state not set", restrictions.isReadOnly(), usage.isReadOnly());
RepoUsageStatus status = repoUsageComponent.getUsageStatus();
logger.debug(status);
}
/**
* Tests license code interaction. This interaction would be done using runAs 'System'.
*/
public void test5LicenseUse() throws Exception
{
Long licenseUserLimit = 5L;
Long licenseDocumentLimit = 100000L;
LicenseMode licenseMode = LicenseMode.TEAM;
Long licenseExpiry = System.currentTimeMillis() + 24*3600000;
// Get actual license details (incl. generating trial license)
// Push license restrictions
RepoUsage restrictions = new RepoUsage(
System.currentTimeMillis(),
licenseUserLimit, // From license
licenseDocumentLimit, // From license
licenseMode, // From license
licenseExpiry, // From license
transactionService.getAllowWrite() == false);// After license validity has been verified
repoUsageComponent.setRestrictions(restrictions);
// Trigger a usage update
updateUsage(UsageType.USAGE_ALL);
// Get the usage
@SuppressWarnings("unused")
RepoUsage usage = getUsage();
}
/**
* Check that concurrent updates are prevented
*/
public void test6ConcurrentUpdates() throws Exception
{
// Firstly check that we can get an update
assertTrue("Failed to update all usages", updateUsage(UsageType.USAGE_ALL));
assertTrue("Failed to update user count", updateUsage(UsageType.USAGE_USERS));
assertTrue("Failed to update document count", updateUsage(UsageType.USAGE_DOCUMENTS));
// Now take a lock of it all and see that they fail
String lockToken = jobLockService.getLock(RepoUsageComponent.LOCK_USAGE, RepoUsageComponent.LOCK_TTL);
try
{
// Check
assertFalse("Expected usage updates to be kicked out", updateUsage(UsageType.USAGE_ALL));
assertFalse("Expected usage updates to be kicked out", updateUsage(UsageType.USAGE_USERS));
assertFalse("Expected usage updates to be kicked out", updateUsage(UsageType.USAGE_DOCUMENTS));
}
finally
{
jobLockService.releaseLock(lockToken, RepoUsageComponent.LOCK_USAGE);
}
// Lock documents updates only
lockToken = jobLockService.getLock(RepoUsageComponent.LOCK_USAGE_DOCUMENTS, RepoUsageComponent.LOCK_TTL);
try
{
// Check
assertFalse("Expected usage updates to be kicked out", updateUsage(UsageType.USAGE_ALL));
assertTrue("Failed to update user count", updateUsage(UsageType.USAGE_USERS));
assertFalse("Expected document usage updates to be kicked out", updateUsage(UsageType.USAGE_DOCUMENTS));
}
finally
{
jobLockService.releaseLock(lockToken, RepoUsageComponent.LOCK_USAGE_DOCUMENTS);
}
// Lock user updates only
lockToken = jobLockService.getLock(RepoUsageComponent.LOCK_USAGE_USERS, RepoUsageComponent.LOCK_TTL);
try
{
// Check
assertFalse("Expected usage updates to be kicked out", updateUsage(UsageType.USAGE_ALL));
assertFalse("Expected user usage updates to be kicked out", updateUsage(UsageType.USAGE_USERS));
assertTrue("Failed to update document count", updateUsage(UsageType.USAGE_DOCUMENTS));
}
finally
{
jobLockService.releaseLock(lockToken, RepoUsageComponent.LOCK_USAGE_USERS);
}
}
}