diff --git a/pass-data-client/src/main/java/org/eclipse/pass/support/client/JsonApiPassClient.java b/pass-data-client/src/main/java/org/eclipse/pass/support/client/JsonApiPassClient.java index d3573576d..80b2911bd 100644 --- a/pass-data-client/src/main/java/org/eclipse/pass/support/client/JsonApiPassClient.java +++ b/pass-data-client/src/main/java/org/eclipse/pass/support/client/JsonApiPassClient.java @@ -674,6 +674,23 @@ public InputStream downloadFile(File file) throws IOException { return response.body().byteStream(); } + @Override + public void deleteFile(File file) throws IOException { + // Transform File URI to use baseUrl in order to avoid authentication issues + HttpUrl urlFileBin = HttpUrl.parse(baseUrl).newBuilder() + .addEncodedPathSegments(file.getUri().getRawPath().substring(1)).build(); + + Request requestFileBin = new Request.Builder().url(urlFileBin).delete().build(); + Response responseFileBin = client.newCall(requestFileBin).execute(); + + if (!responseFileBin.isSuccessful() && responseFileBin.code() != 404) { + throw new IOException(String.format("Failed to delete for File: %s, URL: %s, Status code: %d", + file.getId(), urlFileBin, responseFileBin.code())); + } + + deleteObject(file); + } + @Override public URI uploadBinary(String name, byte[] data) throws IOException { HttpUrl url = HttpUrl.parse(baseUrl).newBuilder() diff --git a/pass-data-client/src/main/java/org/eclipse/pass/support/client/PassClient.java b/pass-data-client/src/main/java/org/eclipse/pass/support/client/PassClient.java index 707cf8e46..c96ecdf0d 100644 --- a/pass-data-client/src/main/java/org/eclipse/pass/support/client/PassClient.java +++ b/pass-data-client/src/main/java/org/eclipse/pass/support/client/PassClient.java @@ -211,6 +211,13 @@ public Spliterator trySplit() { */ InputStream downloadFile(File file) throws IOException; + /** + * Deletes the binary data associated to the File parameter and the File entity. + * @param file the File to delete + * @throws IOException if operation fails + */ + void deleteFile(File file) throws IOException; + /** * @param id of File * @return InputStream of bytes diff --git a/pass-data-client/src/test/java/org/eclipse/pass/support/client/JsonApiPassClientIT.java b/pass-data-client/src/test/java/org/eclipse/pass/support/client/JsonApiPassClientIT.java index 2b1e812df..a2f144603 100644 --- a/pass-data-client/src/test/java/org/eclipse/pass/support/client/JsonApiPassClientIT.java +++ b/pass-data-client/src/test/java/org/eclipse/pass/support/client/JsonApiPassClientIT.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertIterableEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -737,4 +738,29 @@ public void testUploadDownloadFile() throws IOException { assertEquals(data, test_data); } + + @Test + public void testDeleteFile() throws IOException { + // GIVEN + File file = new File(); + String data = "What's in a name?"; + file.setName("rose.txt"); + URI data_uri = client.uploadBinary(file.getName(), data.getBytes(StandardCharsets.UTF_8)); + assertNotNull(data_uri); + file.setUri(data_uri); + client.createObject(file); + + // WHEN + client.deleteFile(file); + + // THEN + IOException ioExBin = assertThrows(IOException.class, () -> { + client.downloadFile(file); + }); + assertEquals("Failed to retrieve binary for File: " + file.getId() + ", URL: " + + file.getUri().toString() + ", Status code: 404", ioExBin.getMessage()); + + File actualFile = client.getObject(File.class, file.getId()); + assertNull(actualFile); + } } diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/DeploymentTestDataService.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/DeploymentTestDataService.java new file mode 100644 index 000000000..04c8166e6 --- /dev/null +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/DeploymentTestDataService.java @@ -0,0 +1,159 @@ +/* + * Copyright 2024 Johns Hopkins University + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.eclipse.pass.deposit.service; + +import java.io.IOException; +import java.time.ZonedDateTime; +import java.util.List; + +import org.eclipse.pass.support.client.ModelUtil; +import org.eclipse.pass.support.client.PassClient; +import org.eclipse.pass.support.client.PassClientSelector; +import org.eclipse.pass.support.client.RSQL; +import org.eclipse.pass.support.client.model.AwardStatus; +import org.eclipse.pass.support.client.model.Deposit; +import org.eclipse.pass.support.client.model.File; +import org.eclipse.pass.support.client.model.Funder; +import org.eclipse.pass.support.client.model.Grant; +import org.eclipse.pass.support.client.model.Journal; +import org.eclipse.pass.support.client.model.PassEntity; +import org.eclipse.pass.support.client.model.Policy; +import org.eclipse.pass.support.client.model.Submission; +import org.eclipse.pass.support.client.model.User; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class DeploymentTestDataService { + private static final Logger LOG = LoggerFactory.getLogger(DeploymentTestDataService.class); + + static final String PASS_E2E_TEST_GRANT = "PASS_E2E_TEST_GRANT"; + + private final PassClient passClient; + + @Value("${pass.test.data.policy.title}") + private String testPolicyTitle; + + @Value("${pass.test.data.user.email}") + private String testUserEmail; + + @Autowired + public DeploymentTestDataService(PassClient passClient) { + this.passClient = passClient; + } + + public void processTestData() throws IOException { + LOG.warn("Deployment Test Data Service running..."); + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + List testGrants = passClient.streamObjects(grantSelector).toList(); + Grant testGrant = testGrants.isEmpty() ? createTestGrantData() : testGrants.get(0); + deleteTestSubmissions(testGrant); + LOG.warn("Done running Deployment Test Data Service"); + } + + private void deleteTestSubmissions(Grant testGrant) throws IOException { + LOG.warn("Deleting Test Submissions"); + ZonedDateTime submissionToDate = ZonedDateTime.now().minusDays(1); + PassClientSelector testSubmissionSelector = new PassClientSelector<>(Submission.class); + testSubmissionSelector.setFilter(RSQL.and( + RSQL.equals("grants.id", testGrant.getId()), + RSQL.lte("submittedDate", ModelUtil.dateTimeFormatter().format(submissionToDate)) + )); + testSubmissionSelector.setInclude("publication"); + List testSubmissions = passClient.streamObjects(testSubmissionSelector).toList(); + testSubmissions.forEach(testSubmission -> { + try { + PassClientSelector testDepositSelector = new PassClientSelector<>(Deposit.class); + testDepositSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + testDepositSelector.setInclude("repositoryCopy"); + List testDeposits = passClient.streamObjects(testDepositSelector).toList(); + testDeposits.forEach(testDeposit -> { + deleteObject(testDeposit); + deleteObject(testDeposit.getRepositoryCopy()); + }); + PassClientSelector testFileSelector = new PassClientSelector<>(File.class); + testFileSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + List testFiles = passClient.streamObjects(testFileSelector).toList(); + testFiles.forEach(this::deleteFile); + deleteObject(testSubmission); + deleteObject(testSubmission.getPublication()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + LOG.warn("Deleted {} Test Submissions", testSubmissions.size()); + } + + private void deleteObject(PassEntity entity) { + try { + passClient.deleteObject(entity); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void deleteFile(File file) { + try { + passClient.deleteFile(file); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private Grant createTestGrantData() throws IOException { + LOG.warn("Creating Test Grant Data"); + Journal testJournal = new Journal(); + testJournal.setJournalName("PASS_E2E_TEST_JOURNAL"); + testJournal.setIssns(List.of("Print:test-fake")); + passClient.createObject(testJournal); + + PassClientSelector policySelector = new PassClientSelector<>(Policy.class); + policySelector.setFilter(RSQL.equals("title", testPolicyTitle)); + List testPolicies = passClient.streamObjects(policySelector).toList(); + Policy testPolicy = testPolicies.get(0); + + Funder testFunder = new Funder(); + testFunder.setLocalKey("E2E_TEST_FUNDER_LK"); + testFunder.setName("PASS_E2E_TEST_FUNDER"); + testFunder.setPolicy(testPolicy); + passClient.createObject(testFunder); + + Grant testGrant = new Grant(); + testGrant.setProjectName(PASS_E2E_TEST_GRANT); + testGrant.setAwardNumber("TEST_E2E_AWD_NUM"); + testGrant.setLocalKey("PASS_E2E_TEST_GRANT_LK"); + testGrant.setAwardDate(ZonedDateTime.parse("2020-02-01T00:00:00Z")); + testGrant.setStartDate(ZonedDateTime.parse("2020-01-01T00:00:00Z")); + testGrant.setEndDate(ZonedDateTime.parse("2088-01-01T00:00:00Z")); + testGrant.setAwardStatus(AwardStatus.ACTIVE); + testGrant.setDirectFunder(testFunder); + testGrant.setPrimaryFunder(testFunder); + + PassClientSelector userSelector = new PassClientSelector<>(User.class); + userSelector.setFilter(RSQL.equals("email", testUserEmail)); + List testUsers = passClient.streamObjects(userSelector).toList(); + User testUser = testUsers.get(0); + testGrant.setPi(testUser); + + passClient.createObject(testGrant); + return testGrant; + } + +} diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/jobs/DeploymentTestDataJob.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/jobs/DeploymentTestDataJob.java new file mode 100644 index 000000000..d5eeb0c09 --- /dev/null +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/jobs/DeploymentTestDataJob.java @@ -0,0 +1,53 @@ +/* + * Copyright 2024 Johns Hopkins University + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.eclipse.pass.deposit.support.jobs; + +import org.eclipse.pass.deposit.service.DeploymentTestDataService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@ConditionalOnExpression( + "'${pass.test.data.job.enabled}'=='true' and '${pass.deposit.jobs.disabled}'=='false'" +) +@Component +public class DeploymentTestDataJob { + + private static final Logger LOG = LoggerFactory.getLogger(DeploymentTestDataJob.class); + + private final DeploymentTestDataService deploymentTestDataService; + + public DeploymentTestDataJob(DeploymentTestDataService deploymentTestDataService) { + this.deploymentTestDataService = deploymentTestDataService; + } + + @Scheduled( + fixedDelayString = "${pass.test.data.job.interval-ms}", + initialDelayString = "${pass.deposit.jobs.3.init.delay}" + ) + public void processDeploymentTestData() { + LOG.warn("Starting {}", this.getClass().getSimpleName()); + try { + deploymentTestDataService.processTestData(); + } catch (Exception e) { + LOG.error("DeploymentTestDataJob execution failed: {}", e.getMessage(), e); + } + LOG.warn("Finished {}", this.getClass().getSimpleName()); + } + +} diff --git a/pass-deposit-services/deposit-core/src/main/resources/application.properties b/pass-deposit-services/deposit-core/src/main/resources/application.properties index 28651603e..b5c19de7d 100644 --- a/pass-deposit-services/deposit-core/src/main/resources/application.properties +++ b/pass-deposit-services/deposit-core/src/main/resources/application.properties @@ -48,6 +48,7 @@ pass.deposit.jobs.disabled=false pass.deposit.jobs.default-interval-ms=600000 pass.deposit.jobs.1.init.delay=5000 pass.deposit.jobs.2.init.delay=10000 +pass.deposit.jobs.3.init.delay=20000 jscholarship.hack.sword.statement.uri-prefix=http://dspace-prod.mse.jhu.edu:8080/swordv2/ jscholarship.hack.sword.statement.uri-replacement=https://jscholarship.library.jhu.edu/swordv2/ @@ -66,3 +67,9 @@ pass.deposit.nihms.email.enabled=false pass.deposit.nihms.email.delay=720000 pass.deposit.nihms.email.auth=${NIHMS_MAIL_AUTH:LOGIN} pass.deposit.nihms.email.from=${PASS_DEPOSIT_NIHMS_EMAIL_FROM:nihms-help@ncbi.nlm.nih.gov} + +pass.test.data.job.enabled=false +# Run every 12 hours +pass.test.data.job.interval-ms=43200000 +pass.test.data.policy.title=${TEST_DATA_POLICY_TITLE:} +pass.test.data.user.email=${TEST_DATA_USER_EMAIL:} diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/DeploymentTestDataServiceIT.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/DeploymentTestDataServiceIT.java new file mode 100644 index 000000000..103711944 --- /dev/null +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/DeploymentTestDataServiceIT.java @@ -0,0 +1,300 @@ +/* + * Copyright 2024 Johns Hopkins University + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.eclipse.pass.deposit.service; + +import static org.eclipse.pass.deposit.service.DeploymentTestDataService.PASS_E2E_TEST_GRANT; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.time.ZonedDateTime; +import java.util.List; + +import org.eclipse.pass.support.client.PassClientSelector; +import org.eclipse.pass.support.client.RSQL; +import org.eclipse.pass.support.client.model.AwardStatus; +import org.eclipse.pass.support.client.model.Deposit; +import org.eclipse.pass.support.client.model.DepositStatus; +import org.eclipse.pass.support.client.model.File; +import org.eclipse.pass.support.client.model.Grant; +import org.eclipse.pass.support.client.model.PassEntity; +import org.eclipse.pass.support.client.model.Policy; +import org.eclipse.pass.support.client.model.Publication; +import org.eclipse.pass.support.client.model.Repository; +import org.eclipse.pass.support.client.model.RepositoryCopy; +import org.eclipse.pass.support.client.model.Submission; +import org.eclipse.pass.support.client.model.User; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.TestPropertySource; + +/** + * @author Russ Poetker (rpoetke1@jh.edu) + */ +@TestPropertySource(properties = { + "pass.test.data.policy.title=test-policy-title", + "pass.test.data.user.email=test-user-email@foo" +}) +public class DeploymentTestDataServiceIT extends AbstractDepositIT { + + @Autowired private DeploymentTestDataService deploymentTestDataService; + + @BeforeEach + public void initPolicyAndUser() throws IOException { + PassClientSelector selector = new PassClientSelector<>(Policy.class); + selector.setFilter(RSQL.equals("title", "test-policy-title")); + List testPolicies = passClient.streamObjects(selector).toList(); + if (testPolicies.isEmpty()) { + Policy policy = new Policy(); + policy.setTitle("test-policy-title"); + passClient.createObject(policy); + User testUser = new User(); + testUser.setFirstName("test"); + testUser.setLastName("user"); + testUser.setEmail("test-user-email@foo"); + passClient.createObject(testUser); + } + } + + @AfterEach + public void cleanUp() throws IOException { + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + List testGrants = passClient.streamObjects(grantSelector).toList(); + testGrants.forEach(this::deleteObject); + PassClientSelector depositSelector = new PassClientSelector<>(Deposit.class); + List testDeposits = passClient.streamObjects(depositSelector).toList(); + testDeposits.forEach(this::deleteObject); + PassClientSelector repoCopySelector = new PassClientSelector<>(RepositoryCopy.class); + List testRepoCopies = passClient.streamObjects(repoCopySelector).toList(); + testRepoCopies.forEach(this::deleteObject); + PassClientSelector fileSelector = new PassClientSelector<>(File.class); + List testFiles = passClient.streamObjects(fileSelector).toList(); + testFiles.forEach(this::deleteFile); + PassClientSelector submissionSelector = new PassClientSelector<>(Submission.class); + List testSubmissions = passClient.streamObjects(submissionSelector).toList(); + testSubmissions.forEach(this::deleteObject); + PassClientSelector publicationSelector = new PassClientSelector<>(Publication.class); + List testPublications = passClient.streamObjects(publicationSelector).toList(); + testPublications.forEach(this::deleteObject); + } + + private void deleteObject(PassEntity entity) { + try { + passClient.deleteObject(entity); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void deleteFile(File file) { + try { + passClient.deleteFile(file); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + public void testProcessData_TestGrantDataCreatedIfNotExist() throws Exception { + // WHEN + deploymentTestDataService.processTestData(); + + // THEN + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + grantSelector.setInclude("pi", "directFunder", "primaryFunder"); + List testGrants = passClient.streamObjects(grantSelector).toList(); + assertEquals(1, testGrants.size()); + Grant actualTestGrant = testGrants.get(0); + assertEquals(PASS_E2E_TEST_GRANT, actualTestGrant.getProjectName()); + assertEquals(AwardStatus.ACTIVE, actualTestGrant.getAwardStatus()); + assertEquals("test-user-email@foo", actualTestGrant.getPi().getEmail()); + assertEquals("PASS_E2E_TEST_FUNDER", actualTestGrant.getDirectFunder().getName()); + assertEquals("PASS_E2E_TEST_FUNDER", actualTestGrant.getPrimaryFunder().getName()); + } + + @Test + public void testProcessData_DoesNotTestGrantDataCreatedIfExist() throws Exception { + // GIVEN + Grant testGrant = new Grant(); + testGrant.setProjectName(PASS_E2E_TEST_GRANT); + testGrant.setAwardStatus(AwardStatus.ACTIVE); + passClient.createObject(testGrant); + Mockito.reset(passClient); + + // WHEN + deploymentTestDataService.processTestData(); + + // THEN + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + List testGrants = passClient.streamObjects(grantSelector).toList(); + assertEquals(1, testGrants.size()); + Grant actualTestGrant = testGrants.get(0); + assertEquals(testGrant.getId(), actualTestGrant.getId()); + assertEquals(PASS_E2E_TEST_GRANT, actualTestGrant.getProjectName()); + assertEquals(AwardStatus.ACTIVE, actualTestGrant.getAwardStatus()); + verify(passClient, times(0)).createObject(any()); + } + + @Test + public void testProcessData_DeleteTestSubmissionsForTestGrant() throws Exception { + // GIVEN + Grant testGrant = new Grant(); + testGrant.setProjectName(PASS_E2E_TEST_GRANT); + testGrant.setAwardStatus(AwardStatus.ACTIVE); + passClient.createObject(testGrant); + initSubmissionAndDeposits(testGrant); + + // WHEN + deploymentTestDataService.processTestData(); + + // THEN + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + List testGrants = passClient.streamObjects(grantSelector).toList(); + assertEquals(1, testGrants.size()); + Grant actualTestGrant = testGrants.get(0); + assertEquals(PASS_E2E_TEST_GRANT, actualTestGrant.getProjectName()); + assertEquals(AwardStatus.ACTIVE, actualTestGrant.getAwardStatus()); + PassClientSelector depositSelector = new PassClientSelector<>(Deposit.class); + List testDeposits = passClient.streamObjects(depositSelector).toList(); + assertTrue(testDeposits.isEmpty()); + PassClientSelector repoCopySelector = new PassClientSelector<>(RepositoryCopy.class); + List testRepoCopies = passClient.streamObjects(repoCopySelector).toList(); + assertTrue(testRepoCopies.isEmpty()); + PassClientSelector submissionSelector = new PassClientSelector<>(Submission.class); + List testSubmissions = passClient.streamObjects(submissionSelector).toList(); + assertTrue(testSubmissions.isEmpty()); + PassClientSelector publicationSelector = new PassClientSelector<>(Publication.class); + List testPublications = passClient.streamObjects(publicationSelector).toList(); + assertTrue(testPublications.isEmpty()); + PassClientSelector fileSelector = new PassClientSelector<>(File.class); + List testFiles = passClient.streamObjects(fileSelector).toList(); + assertTrue(testFiles.isEmpty()); + } + + @Test + public void testProcessData_DoesNotDeleteTestSubmissionsForOtherGrant() throws Exception { + // GIVEN + Grant deploymentGrant = new Grant(); + deploymentGrant.setProjectName(PASS_E2E_TEST_GRANT); + deploymentGrant.setAwardStatus(AwardStatus.ACTIVE); + passClient.createObject(deploymentGrant); + initSubmissionAndDeposits(deploymentGrant); + + Grant testOtherGrant = new Grant(); + testOtherGrant.setProjectName("Some Other Grant"); + testOtherGrant.setAwardStatus(AwardStatus.ACTIVE); + passClient.createObject(testOtherGrant); + Submission testOtherSubmission = initSubmissionAndDeposits(testOtherGrant); + + // WHEN + deploymentTestDataService.processTestData(); + + // THEN + final PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT)); + List actualTestGrants = passClient.streamObjects(grantSelector).toList(); + assertEquals(1, actualTestGrants.size()); + Grant actualTestGrant = actualTestGrants.get(0); + assertEquals(PASS_E2E_TEST_GRANT, actualTestGrant.getProjectName()); + assertEquals(AwardStatus.ACTIVE, actualTestGrant.getAwardStatus()); + + PassClientSelector submissionSelector = new PassClientSelector<>(Submission.class); + List testSubmissions = passClient.streamObjects(submissionSelector).toList(); + assertEquals(1, testSubmissions.size()); + Submission actualSubmission = testSubmissions.get(0); + assertEquals(testOtherSubmission.getId(), actualSubmission.getId()); + assertEquals(testOtherGrant.getId(), actualSubmission.getGrants().get(0).getId()); + + PassClientSelector depositSelector = new PassClientSelector<>(Deposit.class); + List testDeposits = passClient.streamObjects(depositSelector).toList(); + assertEquals(1, testDeposits.size()); + Deposit actualDeposit = testDeposits.get(0); + assertEquals(testOtherSubmission.getId(), actualDeposit.getSubmission().getId()); + + PassClientSelector repoCopySelector = new PassClientSelector<>(RepositoryCopy.class); + List testRepoCopies = passClient.streamObjects(repoCopySelector).toList(); + assertEquals(1, testRepoCopies.size()); + RepositoryCopy actualRepoCopy = testRepoCopies.get(0); + assertEquals(actualDeposit.getRepositoryCopy().getId(), actualRepoCopy.getId()); + + PassClientSelector publicationSelector = new PassClientSelector<>(Publication.class); + List testPublications = passClient.streamObjects(publicationSelector).toList(); + assertEquals(1, testPublications.size()); + Publication actualPublication = testPublications.get(0); + assertEquals(actualRepoCopy.getPublication().getId(), actualPublication.getId()); + assertEquals(actualSubmission.getPublication().getId(), actualPublication.getId()); + + PassClientSelector fileSelector = new PassClientSelector<>(File.class); + List testFiles = passClient.streamObjects(fileSelector).toList(); + assertEquals(1, testFiles.size()); + File actualFile = testFiles.get(0); + assertEquals(actualSubmission.getId(), actualFile.getSubmission().getId()); + } + + private Submission initSubmissionAndDeposits(Grant testGrant) throws Exception { + Submission submission = new Submission(); + submission.setGrants(List.of(testGrant)); + submission.setSubmittedDate(ZonedDateTime.now().minusDays(2)); + passClient.createObject(submission); + + Repository repository = new Repository(); + repository.setName("test-repository"); + passClient.createObject(repository); + + Publication publication = new Publication(); + publication.setTitle("test-publication"); + passClient.createObject(publication); + + RepositoryCopy repositoryCopy = new RepositoryCopy(); + repositoryCopy.setRepository(repository); + repositoryCopy.setPublication(publication); + passClient.createObject(repositoryCopy); + + Deposit j10pDeposit = new Deposit(); + j10pDeposit.setSubmission(submission); + j10pDeposit.setDepositStatus(DepositStatus.SUBMITTED); + j10pDeposit.setRepositoryCopy(repositoryCopy); + passClient.createObject(j10pDeposit); + + submission.setPublication(publication); + passClient.updateObject(submission); + + File file = new File(); + String data = "Test data file"; + file.setName("test_data_file.txt"); + URI data_uri = passClient.uploadBinary(file.getName(), data.getBytes(StandardCharsets.UTF_8)); + assertNotNull(data_uri); + file.setUri(data_uri); + file.setSubmission(submission); + passClient.createObject(file); + + return submission; + } + +} diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/jobs/ScheduledJobsTest.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/jobs/ScheduledJobsTest.java index 3aef6df91..8e583a374 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/jobs/ScheduledJobsTest.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/jobs/ScheduledJobsTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.verify; import org.eclipse.pass.deposit.DepositApp; +import org.eclipse.pass.deposit.service.DeploymentTestDataService; import org.eclipse.pass.deposit.service.DepositUpdater; import org.eclipse.pass.deposit.service.SubmissionStatusUpdater; import org.junit.jupiter.api.Test; @@ -34,14 +35,18 @@ @TestPropertySource("classpath:test-application.properties") @TestPropertySource(properties = { "pass.deposit.jobs.disabled=false", + "pass.test.data.job.enabled=true", "pass.deposit.jobs.default-interval-ms=1500", + "pass.test.data.job.interval-ms=1500", "pass.deposit.jobs.1.init.delay=50", - "pass.deposit.jobs.2.init.delay=100" + "pass.deposit.jobs.2.init.delay=100", + "pass.deposit.jobs.3.init.delay=120" }) public class ScheduledJobsTest { @MockBean private SubmissionStatusUpdater submissionStatusUpdater; @MockBean private DepositUpdater depositUpdater; + @MockBean private DeploymentTestDataService deploymentTestDataService; @Test void testDepositUpdaterJob() { @@ -61,4 +66,13 @@ void testSubmissionStatusUpdaterJob() { }); } + @Test + void testDeploymentTestDataJob() { + // GIVEN/WHEN + // deploymentTestDataService.processTestData() will be called from Scheduled method in job + await().atMost(3, SECONDS).untilAsserted(() -> { + verify(deploymentTestDataService).processTestData(); + }); + } + }