[DSC-93] Added tests for ProcessCleaner

This commit is contained in:
Luca Giamminonni
2021-10-05 10:54:52 +02:00
parent 28bb6f93e0
commit e96107a6b9
5 changed files with 401 additions and 4 deletions

View File

@@ -62,7 +62,7 @@ public class ProcessCleaner extends DSpaceRunnable<ProcessCleanerConfiguration<P
this.days = configurationService.getIntProperty("process-cleaner.days", 14); this.days = configurationService.getIntProperty("process-cleaner.days", 14);
if (this.days <= 0) { if (this.days <= 0) {
throw new IllegalArgumentException("The number of days must be a positive integer."); throw new IllegalStateException("The number of days must be a positive integer.");
} }
} }
@@ -82,12 +82,16 @@ public class ProcessCleaner extends DSpaceRunnable<ProcessCleanerConfiguration<P
} }
/**
* Delete the processes based on the specified statuses and the configured days
* from their creation.
*/
private void performDeletion(Context context) throws SQLException, IOException, AuthorizeException { private void performDeletion(Context context) throws SQLException, IOException, AuthorizeException {
List<ProcessStatus> statuses = getProcessToDeleteStatuses(); List<ProcessStatus> statuses = getProcessToDeleteStatuses();
Date creationDate = calculateCreationDate(); Date creationDate = calculateCreationDate();
handler.logInfo("Searching for processes with one of the following status: " + statuses); handler.logInfo("Searching for processes with status: " + statuses);
List<Process> processes = processService.findByStatusAndCreationTimeOlderThan(context, statuses, creationDate); List<Process> processes = processService.findByStatusAndCreationTimeOlderThan(context, statuses, creationDate);
handler.logInfo("Found " + processes.size() + " processes to be deleted"); handler.logInfo("Found " + processes.size() + " processes to be deleted");
for (Process process : processes) { for (Process process : processes) {
@@ -98,6 +102,9 @@ public class ProcessCleaner extends DSpaceRunnable<ProcessCleanerConfiguration<P
} }
/**
* Returns the list of Process statuses do be deleted.
*/
private List<ProcessStatus> getProcessToDeleteStatuses() { private List<ProcessStatus> getProcessToDeleteStatuses() {
List<ProcessStatus> statuses = new ArrayList<ProcessStatus>(); List<ProcessStatus> statuses = new ArrayList<ProcessStatus>();
if (cleanCompleted) { if (cleanCompleted) {

View File

@@ -14,7 +14,6 @@ import java.util.List;
import org.dspace.content.ProcessStatus; import org.dspace.content.ProcessStatus;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.core.GenericDAO; import org.dspace.core.GenericDAO;
import org.dspace.eperson.EPerson;
import org.dspace.scripts.Process; import org.dspace.scripts.Process;
import org.dspace.scripts.ProcessQueryParameterContainer; import org.dspace.scripts.ProcessQueryParameterContainer;

View File

@@ -64,7 +64,12 @@
<property name="description" value="Perform the bulk synchronization of all the BATCH configured ORCID entities placed in the ORCID queue"/> <property name="description" value="Perform the bulk synchronization of all the BATCH configured ORCID entities placed in the ORCID queue"/>
<property name="dspaceRunnableClass" value="org.dspace.orcid.script.OrcidBulkPush"/> <property name="dspaceRunnableClass" value="org.dspace.orcid.script.OrcidBulkPush"/>
</bean> </bean>
<bean id="process-cleaner" class="org.dspace.administer.ProcessCleanerCliConfiguration">
<property name="description" value="Cleanup all the old processes in the specified state"/>
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli"/>
</bean>
<!-- Keep as last script; for test ScriptRestRepository#findOneScriptByNameTest --> <!-- Keep as last script; for test ScriptRestRepository#findOneScriptByNameTest -->
<bean id="mock-script" class="org.dspace.scripts.MockDSpaceRunnableScriptConfiguration" scope="prototype"> <bean id="mock-script" class="org.dspace.scripts.MockDSpaceRunnableScriptConfiguration" scope="prototype">
<property name="description" value="Mocking a script for testing purposes" /> <property name="description" value="Mocking a script for testing purposes" />

View File

@@ -0,0 +1,380 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.administer;
import static org.apache.commons.lang.time.DateUtils.addDays;
import static org.dspace.content.ProcessStatus.COMPLETED;
import static org.dspace.content.ProcessStatus.FAILED;
import static org.dspace.content.ProcessStatus.RUNNING;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.app.launcher.ScriptLauncher;
import org.dspace.app.scripts.handler.impl.TestDSpaceRunnableHandler;
import org.dspace.builder.ProcessBuilder;
import org.dspace.content.ProcessStatus;
import org.dspace.scripts.Process;
import org.dspace.scripts.factory.ScriptServiceFactory;
import org.dspace.scripts.service.ProcessService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Test;
/**
* Integration tests for {@link ProcessCleaner}.
*
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
*
*/
public class ProcessCleanerIT extends AbstractIntegrationTestWithDatabase {
private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
private ProcessService processService = ScriptServiceFactory.getInstance().getProcessService();
@Test
public void testWithoutProcessToDelete() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED]"));
assertThat(messages, hasItem("Found 0 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
}
@Test
public void testWithoutSpecifiedStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED]"));
assertThat(messages, hasItem("Found 2 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), nullValue());
assertThat(processService.find(context, process_5.getID()), nullValue());
assertThat(processService.find(context, process_6.getID()), notNullValue());
assertThat(processService.find(context, process_7.getID()), notNullValue());
}
@Test
public void testWithCompletedStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-c" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED]"));
assertThat(messages, hasItem("Found 2 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), nullValue());
assertThat(processService.find(context, process_5.getID()), nullValue());
assertThat(processService.find(context, process_6.getID()), notNullValue());
assertThat(processService.find(context, process_7.getID()), notNullValue());
}
@Test
public void testWithRunningStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(RUNNING, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-r" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [RUNNING]"));
assertThat(messages, hasItem("Found 2 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), notNullValue());
assertThat(processService.find(context, process_5.getID()), notNullValue());
assertThat(processService.find(context, process_6.getID()), nullValue());
assertThat(processService.find(context, process_7.getID()), notNullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
@Test
public void testWithFailedStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(FAILED, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-f" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [FAILED]"));
assertThat(messages, hasItem("Found 2 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), notNullValue());
assertThat(processService.find(context, process_5.getID()), notNullValue());
assertThat(processService.find(context, process_6.getID()), notNullValue());
assertThat(processService.find(context, process_7.getID()), nullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
@Test
public void testWithCompletedAndFailedStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(FAILED, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-c", "-f" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED, FAILED]"));
assertThat(messages, hasItem("Found 4 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), nullValue());
assertThat(processService.find(context, process_5.getID()), nullValue());
assertThat(processService.find(context, process_6.getID()), notNullValue());
assertThat(processService.find(context, process_7.getID()), nullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
@Test
public void testWithCompletedAndRunningStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(RUNNING, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-c", "-r" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED, RUNNING]"));
assertThat(messages, hasItem("Found 4 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), nullValue());
assertThat(processService.find(context, process_5.getID()), nullValue());
assertThat(processService.find(context, process_6.getID()), nullValue());
assertThat(processService.find(context, process_7.getID()), notNullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
@Test
public void testWithFailedAndRunningStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(RUNNING, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-f", "-r" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [FAILED, RUNNING]"));
assertThat(messages, hasItem("Found 3 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), notNullValue());
assertThat(processService.find(context, process_5.getID()), notNullValue());
assertThat(processService.find(context, process_6.getID()), nullValue());
assertThat(processService.find(context, process_7.getID()), nullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
@Test
public void testWithCompletedFailedAndRunningStatus() throws Exception {
Process process_1 = buildProcess(COMPLETED, addDays(new Date(), -2));
Process process_2 = buildProcess(RUNNING, addDays(new Date(), -1));
Process process_3 = buildProcess(FAILED, addDays(new Date(), -3));
Process process_4 = buildProcess(COMPLETED, addDays(new Date(), -6));
Process process_5 = buildProcess(COMPLETED, addDays(new Date(), -8));
Process process_6 = buildProcess(RUNNING, addDays(new Date(), -7));
Process process_7 = buildProcess(FAILED, addDays(new Date(), -8));
Process process_8 = buildProcess(RUNNING, addDays(new Date(), -9));
configurationService.setProperty("process-cleaner.days", 5);
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
String[] args = new String[] { "process-cleaner", "-f", "-r", "-c" };
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
assertThat(messages, hasSize(3));
assertThat(messages, hasItem("Searching for processes with status: [COMPLETED, FAILED, RUNNING]"));
assertThat(messages, hasItem("Found 5 processes to be deleted"));
assertThat(messages, hasItem("Process cleanup completed"));
assertThat(processService.find(context, process_1.getID()), notNullValue());
assertThat(processService.find(context, process_2.getID()), notNullValue());
assertThat(processService.find(context, process_3.getID()), notNullValue());
assertThat(processService.find(context, process_4.getID()), nullValue());
assertThat(processService.find(context, process_5.getID()), nullValue());
assertThat(processService.find(context, process_6.getID()), nullValue());
assertThat(processService.find(context, process_7.getID()), nullValue());
assertThat(processService.find(context, process_8.getID()), nullValue());
}
private Process buildProcess(ProcessStatus processStatus, Date creationTime) throws SQLException {
return ProcessBuilder.createProcess(context, admin, "test", List.of())
.withProcessStatus(processStatus)
.withCreationTime(creationTime)
.build();
}
}

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -60,6 +61,11 @@ public class ProcessBuilder extends AbstractBuilder<Process, ProcessService> {
return this; return this;
} }
public ProcessBuilder withCreationTime(Date creationTime) {
process.setCreationTime(creationTime);
return this;
}
public ProcessBuilder withStartAndEndTime(String startTime, String endTime) throws ParseException { public ProcessBuilder withStartAndEndTime(String startTime, String endTime) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
process.setStartTime(simpleDateFormat.parse(startTime)); process.setStartTime(simpleDateFormat.parse(startTime));