MNT-21837 License expires 23h earlier (#541) (#543)

* MNT-21837 - License expires 23h earlier
* Original problem is that the remaining time to license expires is calculated in days (long, not double) and anything less then 24h computes to 0 days. The validation for license being valid is that the remaining days is more than zero.
* Added method calculateMs in DateUtil class to return the interval in ms considering 2 given dates and respective timezones
* Changed the validation to use the remaining milliseconds value instead of remaining days
* Added unit test where license expires 6 hours from now - repo is not supposed to enter read-only mode
* Added unit test where license expired 1 minute ago - repo is supposed to enter read-only mode

* Removed unused import

(cherry picked from commit 630cd99067)
This commit is contained in:
evasques
2021-06-18 15:34:35 +01:00
committed by GitHub
parent c4217b32fb
commit e523245a10
3 changed files with 100 additions and 5 deletions

View File

@@ -456,12 +456,16 @@ public class RepoUsageComponentImpl implements RepoUsageComponent
} }
} }
// Check the license expiry // Check the license expiration
Long licenseExpiryDate = restrictions.getLicenseExpiryDate(); Long licenseExpiryDate = restrictions.getLicenseExpiryDate();
if (licenseExpiryDate != null) if (licenseExpiryDate != null)
{ {
int remainingDays = DateUtil.calculateDays(System.currentTimeMillis(), licenseExpiryDate); //For informational purposes, get the remaining number of days, counting from the beginning of the day of each date (now and expiration date)
if (remainingDays <= 0) long remainingDays = DateUtil.calculateDays(System.currentTimeMillis(), licenseExpiryDate);
//Get exact number of milliseconds between license expiration time and now to see if is expired
long remainingMills = DateUtil.calculateMs(System.currentTimeMillis(), licenseExpiryDate);
if (remainingMills <= 0)
{ {
errors.add(I18NUtil.getMessage("system.usage.err.limit_license_expired")); errors.add(I18NUtil.getMessage("system.usage.err.limit_license_expired"));
level = RepoUsageLevel.LOCKED_DOWN; level = RepoUsageLevel.LOCKED_DOWN;

View File

@@ -71,4 +71,34 @@ public class DateUtil
} }
return days; return days;
} }
/**
* Calculate the number of milliseconds between start and end dates based on the <b>default</b> timezone.
* If the end date is before the start date, the returned value is negative.
*
* @param startMs start date in milliseconds
* @param endMs end date in milliseconds
* @return number milliseconds between
*/
public static long calculateMs(long startMs, long endMs)
{
DateTime startDateTime = new DateTime(startMs);
DateTime endDateTime = new DateTime(endMs);
long milliseconds;
if (endDateTime.isBefore(startDateTime))
{
Interval interval = new Interval(endDateTime, startDateTime);
Period period = interval.toPeriod(PeriodType.millis());
milliseconds = 0 - period.getMillis();
}
else
{
Interval interval = new Interval(startDateTime, endDateTime);
Period period = interval.toPeriod(PeriodType.millis());
milliseconds = period.getMillis();
}
return milliseconds;
}
} }

View File

@@ -25,9 +25,9 @@
*/ */
package org.alfresco.repo.usage; package org.alfresco.repo.usage;
import javax.transaction.UserTransaction; import java.util.concurrent.TimeUnit;
import junit.framework.TestCase; import javax.transaction.UserTransaction;
import org.alfresco.repo.lock.JobLockService; import org.alfresco.repo.lock.JobLockService;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
@@ -37,6 +37,7 @@ import org.alfresco.service.cmr.admin.RepoUsage;
import org.alfresco.service.cmr.admin.RepoUsage.LicenseMode; import org.alfresco.service.cmr.admin.RepoUsage.LicenseMode;
import org.alfresco.service.cmr.admin.RepoUsage.UsageType; import org.alfresco.service.cmr.admin.RepoUsage.UsageType;
import org.alfresco.service.cmr.admin.RepoUsageStatus; import org.alfresco.service.cmr.admin.RepoUsageStatus;
import org.alfresco.service.cmr.admin.RepoUsageStatus.RepoUsageLevel;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.transaction.TransactionService;
import org.alfresco.test_category.OwnJVMTestsCategory; import org.alfresco.test_category.OwnJVMTestsCategory;
import org.alfresco.util.ApplicationContextHelper; import org.alfresco.util.ApplicationContextHelper;
@@ -48,6 +49,8 @@ import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import junit.framework.TestCase;
/** /**
* Tests {@link RepoUsageComponent} * Tests {@link RepoUsageComponent}
* *
@@ -236,6 +239,64 @@ public class RepoUsageComponentTest extends TestCase
RepoUsage usage = getUsage(); RepoUsage usage = getUsage();
} }
public void testLicenceHoursBeforeExpiration() throws Exception
{
// Update usage
updateUsage(UsageType.USAGE_ALL);
// Set the restrictions for license to expire in 6 hours
RepoUsage restrictions = new RepoUsage(
System.currentTimeMillis(),
5000L,
100000L,
LicenseMode.TEAM,
System.currentTimeMillis() + TimeUnit.HOURS.toMillis(6),
false);
repoUsageComponent.setRestrictions(restrictions);
// Update use
updateUsage(UsageType.USAGE_ALL);
// Get the usage
RepoUsage usage = getUsage();
// Check
assertFalse("Usage is in read-only mode",usage.isReadOnly());
assertTrue("System is in read-only mode",transactionService.getAllowWrite());
RepoUsageStatus status = repoUsageComponent.getUsageStatus();
assertEquals("System is not at Warning All Level",status.getLevel(),RepoUsageLevel.WARN_ALL);
}
public void testLicenceMinutesAfterExpiration() throws Exception
{
// Update usage
updateUsage(UsageType.USAGE_ALL);
// Set the restrictions for license to expire in 6 hours
RepoUsage restrictions = new RepoUsage(
System.currentTimeMillis(),
5000L,
100000L,
LicenseMode.TEAM,
System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1),
false);
repoUsageComponent.setRestrictions(restrictions);
// Update use
updateUsage(UsageType.USAGE_ALL);
// Get the usage
RepoUsage usage = getUsage();
// Check we are in read-only mode
assertTrue("Usage is not in read-only mode",usage.isReadOnly());
assertFalse("System is not in read-only mode",transactionService.getAllowWrite());
RepoUsageStatus status = repoUsageComponent.getUsageStatus();
assertEquals("System is not at Locked Level",status.getLevel(),RepoUsageLevel.LOCKED_DOWN);
}
/** /**
* Check that concurrent updates are prevented * Check that concurrent updates are prevented
* *