From b310f5cdc90a3821358a7f7f9eecf01674bf6ef5 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 7 Oct 2016 14:51:38 +0100 Subject: [PATCH] RM-3386 Integration test to check that updating the disposition period works. This test is the scenario given in the bug report. Create a record that has a next disposition step in a year. Update the step to be three years, and check that the record's 'disposition as of' date is updated accordingly. --- .../disposition/DispositionTestSuite.java | 3 +- .../UpdateDispositionScheduleTest.java | 164 ++++++++++++++++++ .../test/util/CommonRMTestUtils.java | 36 ++-- 3 files changed, 185 insertions(+), 18 deletions(-) create mode 100644 rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/UpdateDispositionScheduleTest.java diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/DispositionTestSuite.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/DispositionTestSuite.java index 71fe483156..e1938d1d28 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/DispositionTestSuite.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/DispositionTestSuite.java @@ -31,7 +31,8 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses( { - CutOffTest.class + CutOffTest.class, + UpdateDispositionScheduleTest.class }) public class DispositionTestSuite { diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/UpdateDispositionScheduleTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/UpdateDispositionScheduleTest.java new file mode 100644 index 0000000000..06ce6a5399 --- /dev/null +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/disposition/UpdateDispositionScheduleTest.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2005-2014 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 . + */ +package org.alfresco.module.org_alfresco_module_rm.test.integration.disposition; + +import static org.alfresco.module.org_alfresco_module_rm.test.util.bdt.BehaviourTest.test; + +import java.io.Serializable; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import com.google.common.collect.ImmutableMap; + +import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.action.impl.CutOffAction; +import org.alfresco.module.org_alfresco_module_rm.action.impl.DestroyAction; +import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionActionDefinition; +import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionSchedule; +import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService; +import org.alfresco.module.org_alfresco_module_rm.job.publish.DispositionActionDefinitionPublishExecutor; +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase; +import org.alfresco.module.org_alfresco_module_rm.test.util.CommonRMTestUtils; +import org.alfresco.module.org_alfresco_module_rm.test.util.bdt.BehaviourTest; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.namespace.QName; +import org.alfresco.util.ApplicationContextHelper; +import org.springframework.extensions.webscripts.GUID; + +/** + * Integration tests for updating the disposition schedule. + * + * @author Tom Page + * @since 2.3.1 + */ +public class UpdateDispositionScheduleTest extends BaseRMTestCase +{ + /** A unique prefix for the constants in this test. */ + protected static final String TEST_PREFIX = UpdateDispositionScheduleTest.class.getName() + GUID.generate() + "_"; + /** The name to use for the category. */ + protected static final String CATEGORY_NAME = TEST_PREFIX + "Category"; + /** The name to use for the folder. */ + protected static final String FOLDER_NAME = TEST_PREFIX + "Folder"; + /** The name to use for the record. */ + protected static final String RECORD_NAME = TEST_PREFIX + "Record"; + + /** The executor for the disposition update job. */ + private DispositionActionDefinitionPublishExecutor dispositionActionDefinitionPublishExecutor; + /** The internal disposition service is used to avoid permissions issues when updating the record. */ + private DispositionService internalDispositionService; + + /** The category node. */ + private NodeRef category; + /** The folder node. */ + private NodeRef folder; + /** The record node. */ + private NodeRef record; + /** The 'disposition as of' date from before the 'when' step. */ + private Date originalAsOfDate; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + + BehaviourTest.initBehaviourTests(retryingTransactionHelper); + + // Get the application context + applicationContext = ApplicationContextHelper.getApplicationContext(getConfigLocations()); + dispositionActionDefinitionPublishExecutor = applicationContext.getBean(DispositionActionDefinitionPublishExecutor.class); + internalDispositionService = (DispositionService) applicationContext.getBean("dispositionService"); + } + + /** + * RM-3386 + *

+     * Given a record subject to a disposition schedule
+     * And the next step is due to run at some period after the date the content was created
+     * When I update the period of the next step (and wait for this to be processed)
+     * Then the "as of" date is updated to be at the new period after the creation date.
+     * 
+ */ + public void testUpdatePeriod() + { + test() + .given(() -> { + // Create a category. + category = filePlanService.createRecordCategory(filePlan, CATEGORY_NAME); + // Create a disposition schedule for the category (Cut off immediately, then Destroy 1 year after the creation date). + DispositionSchedule dispSched = utils.createBasicDispositionSchedule(category, "instructions", "authority", true, false); + Map cutOffParams = ImmutableMap.of(PROP_DISPOSITION_ACTION_NAME, CutOffAction.NAME, + PROP_DISPOSITION_DESCRIPTION, "description", + PROP_DISPOSITION_PERIOD, CommonRMTestUtils.PERIOD_IMMEDIATELY); + dispositionService.addDispositionActionDefinition(dispSched, cutOffParams); + Map destroyParams = ImmutableMap.of(PROP_DISPOSITION_ACTION_NAME, DestroyAction.NAME, + PROP_DISPOSITION_DESCRIPTION, "description", + PROP_DISPOSITION_PERIOD, CommonRMTestUtils.PERIOD_ONE_YEAR, + PROP_DISPOSITION_PERIOD_PROPERTY, ContentModel.PROP_CREATED); + dispositionService.addDispositionActionDefinition(dispSched, destroyParams); + // Create a folder containing a record within the category. + folder = recordFolderService.createRecordFolder(category, FOLDER_NAME); + record = fileFolderService.create(folder, RECORD_NAME, ContentModel.TYPE_CONTENT).getNodeRef(); + + dispositionService.cutoffDisposableItem(record); + // Ensure the update has been applied to the record. + internalDispositionService.updateNextDispositionAction(record); + + originalAsOfDate = dispositionService.getNextDispositionAction(record).getAsOfDate(); + }) + .when(() -> { + // Update the Destroy step to be 3 years after the creation date. + DispositionSchedule dispSched = dispositionService.getDispositionSchedule(category); + DispositionActionDefinition destroy = dispSched.getDispositionActionDefinitionByName(DestroyAction.NAME); + Map destroyParams = ImmutableMap.of(PROP_DISPOSITION_ACTION_NAME, DestroyAction.NAME, + PROP_DISPOSITION_DESCRIPTION, "description", + PROP_DISPOSITION_PERIOD, CommonRMTestUtils.PERIOD_THREE_YEARS, + PROP_DISPOSITION_PERIOD_PROPERTY, ContentModel.PROP_CREATED); + dispositionService.updateDispositionActionDefinition(destroy, destroyParams); + + // Make the disposition action definition update job run. + dispositionActionDefinitionPublishExecutor.publish(destroy.getNodeRef()); + }) + .then() + .expect(true) + .from(() -> aboutTwoYearsApart(originalAsOfDate, dispositionService.getNextDispositionAction(record).getAsOfDate())) + .because("Increasing the destroy period by two years should increase the 'as of' date by two years."); + } + + /** + * Check that the two given dates are approximately two years apart. + *

+ * This actually just checks that they're more than one and less than three years apart, because leap years make + * things hard to calculate. + * + * @return true if the two dates are about two years apart. + */ + private boolean aboutTwoYearsApart(Date start, Date end) + { + long days = daysBetween(start, end); + long yearInDays = 365; + return (yearInDays < days) && (days < 3 * yearInDays); + } + + /** Find the number of days between the two dates. */ + private long daysBetween(Date start, Date end) + { + return TimeUnit.MILLISECONDS.toDays(end.getTime() - start.getTime()); + } +} diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/CommonRMTestUtils.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/CommonRMTestUtils.java index 3d66cb81bb..275dc089da 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/CommonRMTestUtils.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/CommonRMTestUtils.java @@ -56,7 +56,7 @@ import org.springframework.context.ApplicationContext; /** * Common RM test utility methods. - * + * * @author Roy Wetherall */ public class CommonRMTestUtils implements RecordsManagementModel @@ -76,10 +76,12 @@ public class CommonRMTestUtils implements RecordsManagementModel public static final String DEFAULT_EVENT_NAME = "case_closed"; public static final String PERIOD_NONE = "none|0"; public static final String PERIOD_IMMEDIATELY = "immediately|0"; + public static final String PERIOD_ONE_YEAR = "year|1"; + public static final String PERIOD_THREE_YEARS = "year|3"; /** * Constructor - * + * * @param applicationContext application context */ public CommonRMTestUtils(ApplicationContext applicationContext) @@ -95,7 +97,7 @@ public class CommonRMTestUtils implements RecordsManagementModel /** * Create a disposition schedule - * + * * @param container record category * @return {@link DispositionSchedule} created disposition schedule node reference */ @@ -129,15 +131,15 @@ public class CommonRMTestUtils implements RecordsManagementModel boolean extendedDispositionSchedule) { return createDispositionSchedule( - container, - dispositionInstructions, - dispositionAuthority, - isRecordLevel, - defaultDispositionActions, - extendedDispositionSchedule, + container, + dispositionInstructions, + dispositionAuthority, + isRecordLevel, + defaultDispositionActions, + extendedDispositionSchedule, DEFAULT_EVENT_NAME); } - + /** * Create test disposition schedule */ @@ -241,8 +243,8 @@ public class CommonRMTestUtils implements RecordsManagementModel modelSecurityService.setEnabled(false); try { - nodeService.setProperty(record, RecordsManagementModel.PROP_DATE_FILED, new Date()); - nodeService.setProperty(record, ContentModel.PROP_TITLE, "titleValue"); + nodeService.setProperty(record, RecordsManagementModel.PROP_DATE_FILED, new Date()); + nodeService.setProperty(record, ContentModel.PROP_TITLE, "titleValue"); actionService.executeRecordsManagementAction(record, "declareRecord"); } finally @@ -255,7 +257,7 @@ public class CommonRMTestUtils implements RecordsManagementModel }, AuthenticationUtil.getAdminUserName()); - } + } public void closeFolder(final NodeRef recordFolder) { @@ -293,10 +295,10 @@ public class CommonRMTestUtils implements RecordsManagementModel return filePlanRoleService.createRole(filePlan, roleName, roleName, capabilities); } - + /** * Helper method to complete event on disposable item - * + * * @param disposableItem disposable item (record or record folder) * @param eventName event name */ @@ -305,8 +307,8 @@ public class CommonRMTestUtils implements RecordsManagementModel // build action properties Map params = new HashMap(1); params.put(CompleteEventAction.PARAM_EVENT_NAME, eventName); - + // complete event - actionService.executeRecordsManagementAction(disposableItem, CompleteEventAction.NAME, params); + actionService.executeRecordsManagementAction(disposableItem, CompleteEventAction.NAME, params); } }