From 0c3ad3ee0e3583c12cf780eadbb479fd46630cbf Mon Sep 17 00:00:00 2001 From: Patrick Austin Date: Wed, 27 Nov 2024 14:02:05 +0000 Subject: [PATCH] Remove EntityManager from StatusCheckTest and re-enable #38 --- pom.xml | 6 - .../org/icatproject/topcat/StatusCheck.java | 9 +- .../icatproject/topcat/StatusCheckTest.java | 1418 +++++++++-------- 3 files changed, 726 insertions(+), 707 deletions(-) diff --git a/pom.xml b/pom.xml index f037f2c0..33980910 100644 --- a/pom.xml +++ b/pom.xml @@ -244,12 +244,6 @@ maven-surefire-plugin 2.19.1 - - - **/StatusCheckTest.java - - - -Xmx768m -XX:MaxMetaspaceSize=256m diff --git a/src/main/java/org/icatproject/topcat/StatusCheck.java b/src/main/java/org/icatproject/topcat/StatusCheck.java index feec6248..5a485d0f 100644 --- a/src/main/java/org/icatproject/topcat/StatusCheck.java +++ b/src/main/java/org/icatproject/topcat/StatusCheck.java @@ -278,8 +278,11 @@ private void prepareDownload(Download download, IdsClient injectedIdsClient) thr * @param maxActiveDownloads Limit on the number of concurrent jobs with RESTORING status * @throws Exception */ - private void startQueuedDownloads(int maxActiveDownloads) throws Exception { - if (maxActiveDownloads == 0) { return; } + public void startQueuedDownloads(int maxActiveDownloads) throws Exception { + if (maxActiveDownloads == 0) { + return; + } + String selectString = "select download from Download download where download.isDeleted != true"; String restoringCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.RESTORING"; String pausedCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.PAUSED"; @@ -288,7 +291,7 @@ private void startQueuedDownloads(int maxActiveDownloads) throws Exception { TypedQuery activeDownloadsQuery = em.createQuery(activeQueryString, Download.class); List activeDownloads = activeDownloadsQuery.getResultList(); - String queuedQueryString = selectString + " and " + pausedCondition + " and download.preparedId == null"; + String queuedQueryString = selectString + " and " + pausedCondition + " and download.preparedId = null"; if (maxActiveDownloads > 0) { int freeActiveDownloads = maxActiveDownloads - activeDownloads.size(); if (freeActiveDownloads <= 0) { diff --git a/src/test/java/org/icatproject/topcat/StatusCheckTest.java b/src/test/java/org/icatproject/topcat/StatusCheckTest.java index f96ddbae..d1753785 100644 --- a/src/test/java/org/icatproject/topcat/StatusCheckTest.java +++ b/src/test/java/org/icatproject/topcat/StatusCheckTest.java @@ -1,12 +1,8 @@ package org.icatproject.topcat; import java.util.*; -import java.util.Date; import java.util.concurrent.TimeUnit; -import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -20,53 +16,46 @@ import org.junit.*; import org.junit.runner.RunWith; import jakarta.inject.Inject; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import jakarta.persistence.TypedQuery; import jakarta.ejb.EJB; import org.icatproject.topcat.domain.*; -import org.icatproject.topcat.exceptions.NotFoundException; import org.icatproject.topcat.exceptions.TopcatException; import org.icatproject.topcat.repository.DownloadRepository; -import org.icatproject.topcat.StatusCheck; - -import java.sql.*; @RunWith(Arquillian.class) public class StatusCheckTest { - @PersistenceContext(unitName = "topcat") - EntityManager em; - @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) - .addClasses(StatusCheck.class, DownloadRepository.class, IdsClient.class) - .addPackages(true,"org.icatproject.topcat.domain","org.icatproject.topcat.exceptions") - .addAsResource("META-INF/persistence.xml") - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + .addClasses(StatusCheck.class, DownloadRepository.class, IdsClient.class) + .addPackages(true, "org.icatproject.topcat.domain", "org.icatproject.topcat.exceptions") + .addAsResource("META-INF/persistence.xml") + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } - + // StatusCheck treats TopcatExceptions differently to all other Exceptions, - // so need to test both cases. However, the only IdsClient method used by StatusCheck - // that can throw anything other than a TopcatException is isPrepared; - // so it will not make sense to use FailMode.EXCEPTION for prepareData or getSize. - - public enum FailMode { OK, EXCEPTION, TOPCAT_EXCEPTION }; - + // so need to test both cases. However, the only IdsClient method used by + // StatusCheck that can throw anything other than a TopcatException is + // isPrepared; so it will not make sense to use FailMode.EXCEPTION for + // prepareData or getSize. + + public enum FailMode { + OK, EXCEPTION, TOPCAT_EXCEPTION + }; + private class MockIdsClient extends IdsClient { - + // Make size and preparedId available for tests - + public Long size = 26L; public String preparedId = "DummyPreparedId"; - + private boolean isPreparedValue; private FailMode failMode; private boolean prepareDataCalledFlag; private boolean isPreparedCalledFlag; - + public MockIdsClient(String url) { // We are forced to do this as IdsClient has no no-args constructor; // This forces us to have the Properties defined, even though we won't use them. @@ -76,53 +65,56 @@ public MockIdsClient(String url) { prepareDataCalledFlag = false; isPreparedCalledFlag = false; } - + // Mock overrides - - public String prepareData(String sessionId, List investigationIds, List datasetIds, List datafileIds) throws TopcatException { + + public String prepareData(String sessionId, List investigationIds, List datasetIds, + List datafileIds) throws TopcatException { prepareDataCalledFlag = true; - if( failMode == FailMode.TOPCAT_EXCEPTION ) { - throw new TopcatException(500,"Deliberate TopcatException for testing"); + if (failMode == FailMode.TOPCAT_EXCEPTION) { + throw new TopcatException(500, "Deliberate TopcatException for testing"); } return preparedId; } - + public boolean isPrepared(String preparedId) throws TopcatException, IOException { - // This is the only IdsClient method used by StatusCheck that can throw anything other than a TopcatException + // This is the only IdsClient method used by StatusCheck that can throw anything + // other than a TopcatException isPreparedCalledFlag = true; - if( failMode == FailMode.TOPCAT_EXCEPTION ) { - throw new TopcatException(500,"Deliberate TopcatException for testing"); - } else if( failMode == FailMode.EXCEPTION ) { + if (failMode == FailMode.TOPCAT_EXCEPTION) { + throw new TopcatException(500, "Deliberate TopcatException for testing"); + } else if (failMode == FailMode.EXCEPTION) { throw new IOException("Deliberate exception for testing"); } return isPreparedValue; } - public Long getSize(String sessionId, List investigationIds, List datasetIds, List datafileIds) throws TopcatException { - if( failMode == FailMode.TOPCAT_EXCEPTION ) { - throw new TopcatException(500,"Deliberate TopcatException for testing"); + public Long getSize(String sessionId, List investigationIds, List datasetIds, + List datafileIds) throws TopcatException { + if (failMode == FailMode.TOPCAT_EXCEPTION) { + throw new TopcatException(500, "Deliberate TopcatException for testing"); } return size; } - + // Mock utility methods - + public void setIsPrepared(Boolean aBool) { isPreparedValue = aBool; } - + public void resetPrepareDataCalledFlag() { prepareDataCalledFlag = false; } - + public void resetIsPreparedCalledFlag() { isPreparedCalledFlag = false; } - + public void setFailMode(FailMode aFailMode) { failMode = aFailMode; } - + public boolean prepareDataWasCalled() { return prepareDataCalledFlag; } @@ -137,691 +129,732 @@ public boolean isPreparedWasCalled() { @Inject private StatusCheck statusCheck; - + @Test @Transactional public void testSimpleDownload() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId"; - String transport = "http"; - - // Create a single-tier download; initial status should be COMPLETE - Download dummyDownload = createDummyDownload(preparedId, transport, false); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.COMPLETE, dummyDownload.getStatus()); - - /* - * If (as I suspect) the scheduled poll() is running, it might add a lastCheck timestamp for our test download, - * which could prevent the test call below from doing any useful work. - * We are not (yet) testing the delay behaviour, so together these imply that we should set very short wait times. - * Of course, even 1 second is too long! - */ - - int pollDelay = 0; - int pollIntervalWait = 0; - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // This download should have been ignored - no status change, no email sent. - // REMEMBER: dummyDownload.email is null, so it should be excluded by the query in updateStatuses() - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + String preparedId = "InitialPreparedId"; + String transport = "http"; + + // Create a single-tier download; initial status should be COMPLETE + Download dummyDownload = createDummyDownload(preparedId, transport, false, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.COMPLETE, dummyDownload.getStatus()); + + /* + * If (as I suspect) the scheduled poll() is running, it might add a lastCheck + * timestamp for our test download, which could prevent the test call below from + * doing any useful work. We are not (yet) testing the delay behaviour, so + * together these imply that we should set very short wait times. + * Of course, even 1 second is too long! + */ + + int pollDelay = 0; + int pollIntervalWait = 0; + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // This download should have been ignored - no status change, no email sent. + // REMEMBER: dummyDownload.email is null, so it should be excluded by the query + // in updateStatuses() + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } - + @Test @Transactional public void testTwoTierDownload() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId2"; - String transport = "http"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * If (as I suspect) the scheduled poll() is running, it might add a lastCheck timestamp for our test download, - * which could prevent the test call below from doing any useful work. - * We are not (yet) testing the delay behaviour, so together these imply that we should set very short wait times. - * Of course, even 1 second is too long! - * TODO: consider adding sleeps to test more realistic behaviour. - */ - - int pollDelay = 0; - int pollIntervalWait = 0; - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be RESTORING, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now mock the IDS having prepared the data - - mockIdsClient.setIsPrepared(true); - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download should now be COMPLETE, and email flagged as sent (though it wasn't!) - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); - assertTrue(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId2"; + String transport = "http"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * If (as I suspect) the scheduled poll() is running, it might add a lastCheck + * timestamp for our test download, which could prevent the test call below from + * doing any useful work. We are not (yet) testing the delay behaviour, so + * together these imply that we should set very short wait times. + * Of course, even 1 second is too long! + * TODO: consider adding sleeps to test more realistic behaviour. + */ + + int pollDelay = 0; + int pollIntervalWait = 0; + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be RESTORING, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now mock the IDS having prepared the data + + mockIdsClient.setIsPrepared(true); + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download should now be COMPLETE, and email flagged as sent (though it + // wasn't!) + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); + assertTrue(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } - + @Test @Transactional public void testTwoTierNonHttpDownload() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId2"; - String transport = "globus"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * If (as I suspect) the scheduled poll() is running, it might add a lastCheck timestamp for our test download, - * which could prevent the test call below from doing any useful work. - * We are not (yet) testing the delay behaviour, so together these imply that we should set very short wait times. - * Of course, even 1 second is too long! - * TODO: consider adding sleeps to test more realistic behaviour. - */ - - int pollDelay = 0; - int pollIntervalWait = 0; - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be RESTORING, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now mock the IDS having prepared the data - - mockIdsClient.setIsPrepared(true); - - // But as it's not an http[s] download, updateStatuses won't test this - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download still be RESTORING, and email still not sent - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Mock pollcat setting the status to COMPLETE - // It does this using the PUT /admin/download/{id}/status API, - // which uses the DownloadRepository - - postDownload = downloadRepository.getDownload(downloadId); - postDownload.setStatus(DownloadStatus.COMPLETE); - postDownload.setCompletedAt(new Date()); - - downloadRepository.save(postDownload); - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download still be RESTORING, but download.email is null, so isEmailSent should still be false - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + Long completeDownloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId2"; + String transport = "globus"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * If (as I suspect) the scheduled poll() is running, it might add a lastCheck + * timestamp for our test download, which could prevent the test call below from + * doing any useful work. We are not (yet) testing the delay behaviour, so + * together these imply that we should set very short wait times. + * Of course, even 1 second is too long! + * TODO: consider adding sleeps to test more realistic behaviour. + */ + + int pollDelay = 0; + int pollIntervalWait = 0; + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be RESTORING, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now mock the IDS having prepared the data + + mockIdsClient.setIsPrepared(true); + + // But as it's not an http[s] download, updateStatuses won't test this + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download still be RESTORING, and email still not sent + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Mock pollcat setting the status to COMPLETE + // It does this using the PUT /admin/download/{id}/status API, + // which uses the DownloadRepository + + postDownload = downloadRepository.getDownload(downloadId); + postDownload.setStatus(DownloadStatus.COMPLETE); + postDownload.setCompletedAt(new Date()); + + // Note this creates a new Download with a different id + Download completeDownload = downloadRepository.save(postDownload); + completeDownloadId = completeDownload.getId(); + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + postDownload = getDummyDownload(completeDownloadId); + assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + deleteDummyDownload(completeDownloadId); + } } - + @Test @Transactional public void testPrepareDataFailure() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId3"; - String transport = "http"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * If (as I suspect) the scheduled poll() is running, it might add a lastCheck timestamp for our test download, - * which could prevent the test call below from doing any useful work. - * We are not (yet) testing the delay behaviour, so together these imply that we should set very short wait times. - * Of course, even 1 second is too long! - * TODO: consider adding sleeps to test more realistic behaviour. - */ - - int pollDelay = 0; - int pollIntervalWait = 0; - - // In this test, have the prepareData call fail. - // Note: IdsClient.prepareData() can only throw TopcatException; - // we cannot test handling of other exceptions using the mock. - - // A TopcatException - should expire the download - - mockIdsClient.setFailMode(FailMode.TOPCAT_EXCEPTION); - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be EXPIRED, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.EXPIRED, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId3"; + String transport = "http"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * If (as I suspect) the scheduled poll() is running, it might add a lastCheck + * timestamp for our test download, which could prevent the test call below from + * doing any useful work. We are not (yet) testing the delay behaviour, so + * together these imply that we should set very short wait times. + * Of course, even 1 second is too long! + * TODO: consider adding sleeps to test more realistic behaviour. + */ + + int pollDelay = 0; + int pollIntervalWait = 0; + + // In this test, have the prepareData call fail. + // Note: IdsClient.prepareData() can only throw TopcatException; + // we cannot test handling of other exceptions using the mock. + + // A TopcatException - should expire the download + + mockIdsClient.setFailMode(FailMode.TOPCAT_EXCEPTION); + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be EXPIRED, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.EXPIRED, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + } finally { + // clean up + deleteDummyDownload(downloadId); + } } @Test @Transactional public void testIsPreparedFailure() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId3"; - String transport = "http"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * If (as I suspect) the scheduled poll() is running, it might add a lastCheck timestamp for our test download, - * which could prevent the test call below from doing any useful work. - * We are not (yet) testing the delay behaviour, so together these imply that we should set very short wait times. - * Of course, even 1 second is too long! - * TODO: consider adding sleeps to test more realistic behaviour. - */ - - int pollDelay = 0; - int pollIntervalWait = 0; - - // In this test, have the prepareData call succeed - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be RESTORING, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now mock the IDS failing - // First, with an arbitrary exception - download status should not change - - mockIdsClient.setFailMode(FailMode.EXCEPTION); - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should not have changed - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now fail with a TopcatException - download should be Expired - - mockIdsClient.setFailMode(FailMode.TOPCAT_EXCEPTION); - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download should now be EXPIRED - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.EXPIRED, postDownload.getStatus()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId3"; + String transport = "http"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * If (as I suspect) the scheduled poll() is running, it might add a lastCheck + * timestamp for our test download, which could prevent the test call below from + * doing any useful work. We are not (yet) testing the delay behaviour, so + * together these imply that we should set very short wait times. + * Of course, even 1 second is too long! + * TODO: consider adding sleeps to test more realistic behaviour. + */ + + int pollDelay = 0; + int pollIntervalWait = 0; + + // In this test, have the prepareData call succeed + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be RESTORING, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now mock the IDS failing + // First, with an arbitrary exception - download status should not change + + mockIdsClient.setFailMode(FailMode.EXCEPTION); + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should not have changed + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now fail with a TopcatException - download should be Expired + + mockIdsClient.setFailMode(FailMode.TOPCAT_EXCEPTION); + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download should now be EXPIRED + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.EXPIRED, postDownload.getStatus()); + + } finally { + // clean up + deleteDummyDownload(downloadId); + } } @Test @Transactional public void testDelays() throws Exception { - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId4"; - String transport = "http"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * We assume that the scheduled poll() is not doing any work! - */ - - int pollDelay = 1; - int pollIntervalWait = 3; - - // FIRST mock-scheduled call - expect prepareData to be called, status set to RESTORING - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be RESTORING, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // SECOND mock-scheduled call - too early: expect isPrepared NOT to be called, and nothing changed - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertFalse(mockIdsClient.isPreparedWasCalled()); - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now sleep for at least pollDelay seconds, and try again - - TimeUnit.SECONDS.sleep(pollDelay+1); - - // THIRD mock-scheduled call, after pollDelay seconds - expect isPrepared called, but no changes - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertTrue(mockIdsClient.isPreparedWasCalled()); - mockIdsClient.resetIsPreparedCalledFlag(); - - // But the status should not have changed, as isPrepared will have returned false - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // FOURTH mock-scheduled call, before pollIntervalWait seconds have passed: isPrepared should NOT be called - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertFalse(mockIdsClient.isPreparedWasCalled()); - - // Now mock the IDS having prepared the data - - mockIdsClient.setIsPrepared(true); - - // Now wait for at least pollIntervalWaitSeconds, and try again - - TimeUnit.SECONDS.sleep(pollIntervalWait+1); - - // FIFTH mock-scheduled call - expect isPrepared called, status changed to COMPLETE etc. - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertTrue(mockIdsClient.isPreparedWasCalled()); - mockIdsClient.resetIsPreparedCalledFlag(); - - // Download should now be COMPLETE, and email flagged as sent (though it wasn't!) - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); - assertTrue(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId4"; + String transport = "http"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * We assume that the scheduled poll() is not doing any work! + */ + + int pollDelay = 1; + int pollIntervalWait = 3; + + // FIRST mock-scheduled call - expect prepareData to be called, status set to + // RESTORING + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be RESTORING, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // SECOND mock-scheduled call - too early: expect isPrepared NOT to be called, + // and nothing changed + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertFalse(mockIdsClient.isPreparedWasCalled()); + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now sleep for at least pollDelay seconds, and try again + + TimeUnit.SECONDS.sleep(pollDelay + 1); + + // THIRD mock-scheduled call, after pollDelay seconds - expect isPrepared + // called, but no changes + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertTrue(mockIdsClient.isPreparedWasCalled()); + mockIdsClient.resetIsPreparedCalledFlag(); + + // But the status should not have changed, as isPrepared will have returned + // false + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // FOURTH mock-scheduled call, before pollIntervalWait seconds have passed: + // isPrepared should NOT be called + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertFalse(mockIdsClient.isPreparedWasCalled()); + + // Now mock the IDS having prepared the data + + mockIdsClient.setIsPrepared(true); + + // Now wait for at least pollIntervalWaitSeconds, and try again + + TimeUnit.SECONDS.sleep(pollIntervalWait + 1); + + // FIFTH mock-scheduled call - expect isPrepared called, status changed to + // COMPLETE etc. + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertTrue(mockIdsClient.isPreparedWasCalled()); + mockIdsClient.resetIsPreparedCalledFlag(); + + // Download should now be COMPLETE, and email flagged as sent (though it + // wasn't!) + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); + assertTrue(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } - + @Test @Transactional public void testExpiredDownloadsIgnored() throws Exception { + Long downloadId = null; + try { + DownloadStatus status = DownloadStatus.EXPIRED; + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId"; + String transport = "http"; + + // Create a single-tier download; initial status should be COMPLETE + Download dummyDownload = createDummyDownload(preparedId, transport, false, status, false); + downloadId = dummyDownload.getId(); + + // Not testing delays, so set to zero - DownloadStatus status = DownloadStatus.EXPIRED; - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId"; - String transport = "http"; - - // Create a single-tier download; initial status should be COMPLETE - Download dummyDownload = createDummyDownload(preparedId, transport, false); - Long downloadId = dummyDownload.getId(); - - // Set the status and persist it - - dummyDownload.setStatus(status); - em.persist(dummyDownload); - em.flush(); - - // Not testing delays, so set to zero - - int pollDelay = 0; - int pollIntervalWait = 0; - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // This download should have been ignored - no status change, no email sent. - - assertFalse(mockIdsClient.prepareDataWasCalled()); - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(status, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + int pollDelay = 0; + int pollIntervalWait = 0; + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // This download should have been ignored - no status change, no email sent. + + assertFalse(mockIdsClient.prepareDataWasCalled()); + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(status, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } - + @Test @Transactional public void testDeletedDownloadsIgnored() throws Exception { + Long downloadId = null; + try { + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId"; + String transport = "http"; + + // Create a single-tier download; initial status should be COMPLETE + Download dummyDownload = createDummyDownload(preparedId, transport, false, true); + downloadId = dummyDownload.getId(); + + // Not testing delays, so set to zero + + int pollDelay = 0; + int pollIntervalWait = 0; + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // This download should have been ignored - still deleted, no email sent. + + assertFalse(mockIdsClient.prepareDataWasCalled()); - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId"; - String transport = "http"; - - // Create a single-tier download; initial status should be COMPLETE - Download dummyDownload = createDummyDownload(preparedId, transport, false); - Long downloadId = dummyDownload.getId(); - - // Set download deleted and persist it - - dummyDownload.setIsDeleted(true); - em.persist(dummyDownload); - em.flush(); - - // Not testing delays, so set to zero - - int pollDelay = 0; - int pollIntervalWait = 0; - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // This download should have been ignored - still deleted, no email sent. - - assertFalse(mockIdsClient.prepareDataWasCalled()); - - Download postDownload = getDummyDownload(downloadId); - - assertTrue(postDownload.getIsDeleted()); - assertFalse(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Download postDownload = getDummyDownload(downloadId); + + assertTrue(postDownload.getIsDeleted()); + assertFalse(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } - + @Test @Transactional public void testExceptionDelays() throws Exception { - - // Similar to testDelays, but set MockIdsClient to throw an (IO)Exception when used by performCheck - - String dummyUrl = "DummyUrl"; - MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); - - String preparedId = "InitialPreparedId4"; - String transport = "http"; - - // Create a two-tier download; initial status should be PREPARING - Download dummyDownload = createDummyDownload(preparedId, transport, true); - Long downloadId = dummyDownload.getId(); - - assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); - - /* - * We assume that the scheduled poll() is not doing any work! - */ - - int pollDelay = 1; - int pollIntervalWait = 3; - - // FIRST mock-scheduled call - expect prepareData to be called, status set to RESTORING - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - // Download status should now be RESTORING, no email sent. - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now set mockIdsClient to throw an (IO)Exception when isPrepared is called - - mockIdsClient.setFailMode(FailMode.EXCEPTION); - - // SECOND mock-scheduled call - too early: expect isPrepared NOT to be called, and nothing changed - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertFalse(mockIdsClient.isPreparedWasCalled()); - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now sleep for at least pollDelay seconds, and try again - - TimeUnit.SECONDS.sleep(pollDelay+1); - - // THIRD mock-scheduled call, after pollDelay seconds - expect isPrepared called, but no changes - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertTrue(mockIdsClient.isPreparedWasCalled()); - mockIdsClient.resetIsPreparedCalledFlag(); - - // But the status should not have changed, as isPrepared will have thrown an exception - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // FOURTH mock-scheduled call, before pollIntervalWait seconds have passed: isPrepared should NOT be called - // (the exception handling should have set the timestamp) - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertFalse(mockIdsClient.isPreparedWasCalled()); - - // Now mock the IDS having prepared the data - but will still throw an exception - - mockIdsClient.setIsPrepared(true); - - // Now wait for at least pollIntervalWaitSeconds, and try again - - TimeUnit.SECONDS.sleep(pollIntervalWait+1); - - // FIFTH mock-scheduled call. Nothing should have changed, because an IOException was thrown - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertTrue(mockIdsClient.isPreparedWasCalled()); - mockIdsClient.resetIsPreparedCalledFlag(); - - // But the status should not have changed, as isPrepared will have thrown an exception - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); - assertFalse(postDownload.getIsEmailSent()); - - // Now tell the client to stop throwing exceptions - - mockIdsClient.setFailMode(FailMode.OK); - - // Now wait for at least pollIntervalWaitSeconds, and try again - - TimeUnit.SECONDS.sleep(pollIntervalWait+1); - - // SIXTH mock-scheduled call - expect isPrepared called, status changed to COMPLETE etc. - - statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); - - assertTrue(mockIdsClient.isPreparedWasCalled()); - mockIdsClient.resetIsPreparedCalledFlag(); - - // Download should now be COMPLETE, and email flagged as sent (though it wasn't!) - - postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); - assertTrue(postDownload.getIsEmailSent()); - - // clean up - deleteDummyDownload(postDownload); + Long downloadId = null; + try { + // Similar to testDelays, but set MockIdsClient to throw an (IO)Exception when + // used by performCheck + + String dummyUrl = "DummyUrl"; + MockIdsClient mockIdsClient = new MockIdsClient(dummyUrl); + + String preparedId = "InitialPreparedId4"; + String transport = "http"; + + // Create a two-tier download; initial status should be PREPARING + Download dummyDownload = createDummyDownload(preparedId, transport, true, false); + downloadId = dummyDownload.getId(); + + assertEquals(DownloadStatus.PREPARING, dummyDownload.getStatus()); + + /* + * We assume that the scheduled poll() is not doing any work! + */ + + int pollDelay = 1; + int pollIntervalWait = 3; + + // FIRST mock-scheduled call - expect prepareData to be called, status set to + // RESTORING + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + // Download status should now be RESTORING, no email sent. + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now set mockIdsClient to throw an (IO)Exception when isPrepared is called + + mockIdsClient.setFailMode(FailMode.EXCEPTION); + + // SECOND mock-scheduled call - too early: expect isPrepared NOT to be called, + // and nothing changed + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertFalse(mockIdsClient.isPreparedWasCalled()); + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now sleep for at least pollDelay seconds, and try again + + TimeUnit.SECONDS.sleep(pollDelay + 1); + + // THIRD mock-scheduled call, after pollDelay seconds - expect isPrepared + // called, but no changes + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertTrue(mockIdsClient.isPreparedWasCalled()); + mockIdsClient.resetIsPreparedCalledFlag(); + + // But the status should not have changed, as isPrepared will have thrown an + // exception + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // FOURTH mock-scheduled call, before pollIntervalWait seconds have passed: + // isPrepared should NOT be called + // (the exception handling should have set the timestamp) + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertFalse(mockIdsClient.isPreparedWasCalled()); + + // Now mock the IDS having prepared the data - but will still throw an exception + + mockIdsClient.setIsPrepared(true); + + // Now wait for at least pollIntervalWaitSeconds, and try again + + TimeUnit.SECONDS.sleep(pollIntervalWait + 1); + + // FIFTH mock-scheduled call. Nothing should have changed, because an + // IOException was thrown + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertTrue(mockIdsClient.isPreparedWasCalled()); + mockIdsClient.resetIsPreparedCalledFlag(); + + // But the status should not have changed, as isPrepared will have thrown an + // exception + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.RESTORING, postDownload.getStatus()); + assertFalse(postDownload.getIsEmailSent()); + + // Now tell the client to stop throwing exceptions + + mockIdsClient.setFailMode(FailMode.OK); + + // Now wait for at least pollIntervalWaitSeconds, and try again + + TimeUnit.SECONDS.sleep(pollIntervalWait + 1); + + // SIXTH mock-scheduled call - expect isPrepared called, status changed to + // COMPLETE etc. + + statusCheck.updateStatuses(pollDelay, pollIntervalWait, mockIdsClient); + + assertTrue(mockIdsClient.isPreparedWasCalled()); + mockIdsClient.resetIsPreparedCalledFlag(); + + // Download should now be COMPLETE, and email flagged as sent (though it + // wasn't!) + + postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.COMPLETE, postDownload.getStatus()); + assertTrue(postDownload.getIsEmailSent()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } @Test @Transactional - public void testStartQueuedDownloadsNegative() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method method = StatusCheck.class.getDeclaredMethod("startQueuedDownloads", int.class); - method.setAccessible(true); - - String transport = "http"; - Download dummyDownload1 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED); - Download dummyDownload2 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED); - Long downloadId1 = dummyDownload1.getId(); - Long downloadId2 = dummyDownload2.getId(); - - method.invoke(statusCheck, -1); - - // All Downloads should have been prepared - - Download postDownload1 = getDummyDownload(downloadId1); - Download postDownload2 = getDummyDownload(downloadId2); - - assertEquals(DownloadStatus.RESTORING, postDownload1.getStatus()); - assertNotNull(postDownload1.getPreparedId()); - assertEquals(DownloadStatus.RESTORING, postDownload2.getStatus()); - assertNotNull(postDownload2.getPreparedId()); - - // clean up - deleteDummyDownload(postDownload1); - deleteDummyDownload(postDownload2); + public void testStartQueuedDownloadsNegative() throws Exception { + Long downloadId1 = null; + Long downloadId2 = null; + try { + String transport = "http"; + Download dummyDownload1 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED, false); + Download dummyDownload2 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED, false); + downloadId1 = dummyDownload1.getId(); + downloadId2 = dummyDownload2.getId(); + + statusCheck.startQueuedDownloads(-1); + + // All Downloads should have been prepared, but because they have dummy + // (non-UUID) sessionId when prepareData is called it will throw and then mark + // the Downloads as EXPIRED as part of the error handling. This is OK, as it + // still indicates startQueuedDownloads called prepareData + + Download postDownload1 = getDummyDownload(downloadId1); + Download postDownload2 = getDummyDownload(downloadId2); + + assertEquals(DownloadStatus.EXPIRED, postDownload1.getStatus()); + assertNull(postDownload1.getPreparedId()); + assertEquals(DownloadStatus.EXPIRED, postDownload2.getStatus()); + assertNull(postDownload2.getPreparedId()); + } finally { + // clean up + deleteDummyDownload(downloadId1); + deleteDummyDownload(downloadId2); + } } @Test @Transactional - public void testStartQueuedDownloadsZero() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method method = StatusCheck.class.getDeclaredMethod("startQueuedDownloads", int.class); - method.setAccessible(true); - - String transport = "http"; - Download dummyDownload = createDummyDownload(null, transport, true, DownloadStatus.PAUSED); - Long downloadId = dummyDownload.getId(); - - method.invoke(statusCheck, 0); - - // Download status should still be PAUSED, as we unqueued a max of 0 downloads - - Download postDownload = getDummyDownload(downloadId); - - assertEquals(DownloadStatus.PAUSED, postDownload.getStatus()); - assertNull(postDownload.getPreparedId()); - - // clean up - deleteDummyDownload(postDownload); + public void testStartQueuedDownloadsZero() throws Exception { + Long downloadId = null; + try { + String transport = "http"; + Download dummyDownload = createDummyDownload(null, transport, true, DownloadStatus.PAUSED, false); + downloadId = dummyDownload.getId(); + + statusCheck.startQueuedDownloads(0); + + // Download status should still be PAUSED, as we unqueued a max of 0 downloads + + Download postDownload = getDummyDownload(downloadId); + + assertEquals(DownloadStatus.PAUSED, postDownload.getStatus()); + assertNull(postDownload.getPreparedId()); + } finally { + // clean up + deleteDummyDownload(downloadId); + } } @Test @Transactional - public void testStartQueuedDownloadsNonZero() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method method = StatusCheck.class.getDeclaredMethod("startQueuedDownloads", int.class); - method.setAccessible(true); - - String transport = "http"; - Download dummyDownload1 = createDummyDownload("preparedId", transport, true, DownloadStatus.RESTORING); - Download dummyDownload2 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED); - Long downloadId1 = dummyDownload1.getId(); - Long downloadId2 = dummyDownload2.getId(); - - method.invoke(statusCheck, 1); - - // Should not schedule the second Download, as we already have 1 which is RESTORING - - Download postDownload1 = getDummyDownload(downloadId1); - Download postDownload2 = getDummyDownload(downloadId2); - - assertEquals(DownloadStatus.RESTORING, postDownload1.getStatus()); - assertNotNull(postDownload1.getPreparedId()); - assertEquals(DownloadStatus.PAUSED, postDownload2.getStatus()); - assertNull(postDownload2.getPreparedId()); - - // clean up - deleteDummyDownload(postDownload1); - deleteDummyDownload(postDownload2); + public void testStartQueuedDownloadsNonZero() throws Exception { + Long downloadId1 = null; + Long downloadId2 = null; + try { + String transport = "http"; + Download dummyDownload1 = createDummyDownload("preparedId", transport, true, DownloadStatus.RESTORING, + false); + Download dummyDownload2 = createDummyDownload(null, transport, true, DownloadStatus.PAUSED, false); + downloadId1 = dummyDownload1.getId(); + downloadId2 = dummyDownload2.getId(); + + statusCheck.startQueuedDownloads(1); + + // Should not schedule the second Download, as we already have 1 which is + // RESTORING + + Download postDownload1 = getDummyDownload(downloadId1); + Download postDownload2 = getDummyDownload(downloadId2); + + assertEquals(DownloadStatus.RESTORING, postDownload1.getStatus()); + assertNotNull("Expected RESTORING Download to still have preparedId set", postDownload1.getPreparedId()); + assertEquals(DownloadStatus.PAUSED, postDownload2.getStatus()); + assertNull("Expected PAUSED Download to now have preparedId set", postDownload2.getPreparedId()); + } finally { + // clean up + deleteDummyDownload(downloadId1); + deleteDummyDownload(downloadId2); + } } - - private Download createDummyDownload(String preparedId, String transport, Boolean isTwoLevel) { - if(isTwoLevel){ - return createDummyDownload(preparedId, transport, isTwoLevel, DownloadStatus.PREPARING); + + private Download createDummyDownload(String preparedId, String transport, Boolean isTwoLevel, Boolean isDeleted) { + if (isTwoLevel) { + return createDummyDownload(preparedId, transport, isTwoLevel, DownloadStatus.PREPARING, isDeleted); } else { - return createDummyDownload(preparedId, transport, isTwoLevel, DownloadStatus.COMPLETE); + return createDummyDownload(preparedId, transport, isTwoLevel, DownloadStatus.COMPLETE, isDeleted); } } - private Download createDummyDownload(String preparedId, String transport, Boolean isTwoLevel, DownloadStatus downloadStatus) { - + private Download createDummyDownload(String preparedId, String transport, Boolean isTwoLevel, + DownloadStatus downloadStatus, Boolean isDeleted) { + // This mocks what UserResource.submitCart() might do. - + String facilityName = "LILS"; String sessionId = "DummySessionId"; String fileName = "DummyFilename"; String userName = "DummyUsername"; String fullName = "Dummy Full Name"; - // Note: setting email to null means we won't exercise (or test!) the mail-sending code + // Note: setting email to null means we won't exercise (or test!) the + // mail-sending code String email = null; - + Download download = new Download(); download.setSessionId(sessionId); download.setFacilityName(facilityName); @@ -832,14 +865,14 @@ private Download createDummyDownload(String preparedId, String transport, Boolea download.setEmail(email); download.setIsEmailSent(false); download.setSize(0); - - // Create one or more dummy DownloadItems + download.setIsDeleted(isDeleted); + download.setPreparedId(preparedId); List downloadItems = new ArrayList(); - for (int i=0; i <= 2; i++) { + for (int i = 0; i <= 2; i++) { DownloadItem downloadItem = new DownloadItem(); - downloadItem.setEntityId( 10L + i ); + downloadItem.setEntityId(10L + i); downloadItem.setEntityType(EntityType.dataset); downloadItem.setDownload(download); downloadItems.add(downloadItem); @@ -849,37 +882,26 @@ private Download createDummyDownload(String preparedId, String transport, Boolea download.setIsTwoLevel(isTwoLevel); - if(isTwoLevel){ + if (isTwoLevel) { download.setStatus(downloadStatus); } else { - download.setPreparedId(preparedId); download.setStatus(downloadStatus); } - em.persist(download); - em.flush(); - em.refresh(download); - em.flush(); - - return download; + return downloadRepository.save(download); } - + private Download getDummyDownload(Long downloadId) { - - Download download; - - TypedQuery query = em.createQuery("select download from Download download where download.id = :id", Download.class); - query.setParameter("id",downloadId); - try { - download = query.getSingleResult(); - } catch (Exception e) { - download = null; - } - return download; + try { + return downloadRepository.getDownload(downloadId); + } catch (Exception e) { + return null; + } } - - private void deleteDummyDownload(Download download) { - em.remove(download);; - em.flush(); + + private void deleteDummyDownload(Long downloadId) { + if (downloadId != null) { + downloadRepository.removeDownload(downloadId); + } } }