diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/FailedDepositRetry.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/FailedDepositRetry.java index 0a36a576..fafda31a 100644 --- a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/FailedDepositRetry.java +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/service/FailedDepositRetry.java @@ -22,6 +22,7 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.eclipse.pass.deposit.DepositServiceErrorHandler; import org.eclipse.pass.deposit.builder.DepositSubmissionModelBuilder; import org.eclipse.pass.deposit.model.DepositFile; import org.eclipse.pass.deposit.model.DepositSubmission; @@ -48,14 +49,17 @@ public class FailedDepositRetry { private final DepositSubmissionModelBuilder depositSubmissionModelBuilder; private final DepositTaskHelper depositTaskHelper; private final Registry packagerRegistry; + private final DepositServiceErrorHandler depositServiceErrorHandler; public FailedDepositRetry(PassClient passClient, DepositTaskHelper depositTaskHelper, Registry packagerRegistry, - DepositSubmissionModelBuilder depositSubmissionModelBuilder) { + DepositSubmissionModelBuilder depositSubmissionModelBuilder, + DepositServiceErrorHandler depositServiceErrorHandler) { this.passClient = passClient; this.depositTaskHelper = depositTaskHelper; this.packagerRegistry = packagerRegistry; this.depositSubmissionModelBuilder = depositSubmissionModelBuilder; + this.depositServiceErrorHandler = depositServiceErrorHandler; } public void retryFailedDeposit(Deposit failedDeposit) { @@ -94,6 +98,7 @@ public void retryFailedDeposit(Deposit failedDeposit) { } catch (Exception e) { LOG.warn(FAILED_TO_PROCESS, failedDeposit.getId(), e.getMessage(), e); + depositServiceErrorHandler.handleError(e); } } diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataService.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataService.java index b0cded5d..77152757 100644 --- a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataService.java +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataService.java @@ -48,6 +48,7 @@ public class DeploymentTestDataService { private static final Logger LOG = LoggerFactory.getLogger(DeploymentTestDataService.class); public static final String PASS_E2E_TEST_GRANT = "PASS_E2E_TEST_GRANT"; + public static final String SUBMISSION_ID = "submission.id"; private final PassClient passClient; private final DspaceDepositService dspaceDepositService; @@ -94,7 +95,7 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException { testSubmissions.forEach(testSubmission -> { try { PassClientSelector testDepositSelector = new PassClientSelector<>(Deposit.class); - testDepositSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + testDepositSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId())); testDepositSelector.setInclude("repositoryCopy"); List testDeposits = passClient.streamObjects(testDepositSelector).toList(); testDeposits.forEach(testDeposit -> { @@ -102,11 +103,11 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException { deleteObject(testDeposit.getRepositoryCopy()); }); PassClientSelector testFileSelector = new PassClientSelector<>(File.class); - testFileSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + testFileSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId())); List testFiles = passClient.streamObjects(testFileSelector).toList(); testFiles.forEach(this::deleteFile); PassClientSelector subEventSelector = new PassClientSelector<>(SubmissionEvent.class); - subEventSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + subEventSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId())); List testSubmissionEvents = passClient.streamObjects(subEventSelector).toList(); testSubmissionEvents.forEach(this::deleteObject); deleteObject(testSubmission); @@ -120,8 +121,8 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException { private void deleteDepositsInRepoIfNeeded(Grant testGrant) throws IOException { if (Boolean.FALSE.equals(skipDeploymentTestDeposits)) { - LOG.warn("Deleting Test Deposits In Repositories (skipDeploymentTestDeposits=" + - skipDeploymentTestDeposits + ")"); + LOG.warn("Deleting Test Deposits In Repositories (skipDeploymentTestDeposits={})", + skipDeploymentTestDeposits); ZonedDateTime submissionToDate = ZonedDateTime.now().minusHours(1); PassClientSelector testSubmissionSelector = new PassClientSelector<>(Submission.class); testSubmissionSelector.setFilter(RSQL.and( @@ -134,7 +135,7 @@ private void deleteDepositsInRepoIfNeeded(Grant testGrant) throws IOException { testSubmissions.forEach(testSubmission -> { try { PassClientSelector testDepositSelector = new PassClientSelector<>(Deposit.class); - testDepositSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId())); + testDepositSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId())); testDepositSelector.setInclude("submission", "repository", "repositoryCopy"); List testDeposits = passClient.streamObjects(testDepositSelector).toList(); testDeposits.forEach(testDeposit -> deleteDepositInRepoIfNeeded(testDeposit, authContext)); @@ -161,7 +162,9 @@ private boolean isDspaceDeposit(Deposit deposit) { private void deleteObject(PassEntity entity) { try { - passClient.deleteObject(entity); + if (Objects.nonNull(entity)) { + passClient.deleteObject(entity); + } } catch (IOException e) { throw new RuntimeException(e); } diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DspaceDepositService.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DspaceDepositService.java index 94477969..d8cd787d 100644 --- a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DspaceDepositService.java +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/support/deploymenttest/DspaceDepositService.java @@ -51,6 +51,11 @@ public class DspaceDepositService { private static final Logger LOG = LoggerFactory.getLogger(DspaceDepositService.class); + public static final String X_XSRF_TOKEN = "X-XSRF-TOKEN"; + public static final String COOKIE = "Cookie"; + public static final String DSPACE_XSRF_COOKIE = "DSPACE-XSRF-COOKIE="; + public static final String AUTHORIZATION = "Authorization"; + private final RestClient restClient; @Value("${dspace.user}") @@ -109,12 +114,12 @@ AuthContext authenticate() { ResponseEntity authResponse = restClient.post() .uri("/authn/login") .contentType(MediaType.APPLICATION_FORM_URLENCODED) - .header("X-XSRF-TOKEN", xsrfToken) - .header("Cookie", "DSPACE-XSRF-COOKIE=" + xsrfToken) + .header(X_XSRF_TOKEN, xsrfToken) + .header(COOKIE, DSPACE_XSRF_COOKIE + xsrfToken) .body(bodyPair) .retrieve() .toBodilessEntity(); - String authToken = getAuthHeaderValue(authResponse, "Authorization"); + String authToken = getAuthHeaderValue(authResponse, AUTHORIZATION); return new AuthContext(xsrfToken, authToken); } @@ -125,7 +130,9 @@ AuthContext authenticate() { */ void deleteDeposit(Deposit deposit, AuthContext authContext) { LOG.warn("Deleting Test Deposit In Dspace (PASS Deposit ID={})", deposit.getId()); - URI accessUrl = deposit.getRepositoryCopy().getAccessUrl(); + URI accessUrl = Objects.nonNull(deposit.getRepositoryCopy()) + ? deposit.getRepositoryCopy().getAccessUrl() + : null; LOG.warn("Deposit accessUrl={}", accessUrl); if (Objects.nonNull(accessUrl)) { String handleValue = parseHandleFilter(accessUrl); @@ -168,7 +175,7 @@ private String findItemUuid(String handleValue, AuthContext authContext, String String searchResponse = restClient.get() .uri("/discover/search/objects?query=handle:{handleValue}&dsoType=item", handleValue) .accept(MediaType.APPLICATION_JSON) - .header("Authorization", authContext.authToken()) + .header(AUTHORIZATION, authContext.authToken()) .retrieve() .body(String.class); List> searchArray = JsonPath.parse(searchResponse).read("$..indexableObject[?(@.handle)]"); @@ -195,7 +202,7 @@ private List findBundleUuids(String itemUuid, AuthContext authContext) String bundlesResponse = restClient.get() .uri("/core/items/{itemUuid}/bundles", itemUuid) .accept(MediaType.APPLICATION_JSON) - .header("Authorization", authContext.authToken()) + .header(AUTHORIZATION, authContext.authToken()) .retrieve() .body(String.class); return JsonPath.parse(bundlesResponse).read("$..bundles[*].uuid"); @@ -206,9 +213,9 @@ private void deleteBundle(String bundleUuid, AuthContext authContext) { restClient.delete() .uri("/core/bundles/{bundleUuid}", bundleUuid) .accept(MediaType.APPLICATION_JSON) - .header("Authorization", authContext.authToken()) - .header("X-XSRF-TOKEN", authContext.xsrfToken()) - .header("Cookie", "DSPACE-XSRF-COOKIE=" + authContext.xsrfToken()) + .header(AUTHORIZATION, authContext.authToken()) + .header(X_XSRF_TOKEN, authContext.xsrfToken()) + .header(COOKIE, DSPACE_XSRF_COOKIE + authContext.xsrfToken()) .retrieve() .toBodilessEntity(); LOG.warn("Deleted bundle UUID={}", bundleUuid); @@ -219,9 +226,9 @@ private void deleteItem(String itemUuid, AuthContext authContext) { restClient.delete() .uri("/core/items/{itemUuid}", itemUuid) .accept(MediaType.APPLICATION_JSON) - .header("Authorization", authContext.authToken()) - .header("X-XSRF-TOKEN", authContext.xsrfToken()) - .header("Cookie", "DSPACE-XSRF-COOKIE=" + authContext.xsrfToken()) + .header(AUTHORIZATION, authContext.authToken()) + .header(X_XSRF_TOKEN, authContext.xsrfToken()) + .header(COOKIE, DSPACE_XSRF_COOKIE + authContext.xsrfToken()) .retrieve() .toBodilessEntity(); LOG.warn("Deleted item UUID={}", itemUuid); diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryIT.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryIT.java index edf82dce..3bcbad5a 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryIT.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryIT.java @@ -16,9 +16,9 @@ package org.eclipse.pass.deposit.service; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; @@ -40,12 +40,12 @@ /** * @author Russ Poetker (rpoetke1@jh.edu) */ -public class FailedDepositRetryIT extends AbstractDepositIT { +class FailedDepositRetryIT extends AbstractDepositIT { @Autowired private DepositUpdater depositUpdater; @Test - public void testFailedDepositRetry() throws Exception { + void testFailedDepositRetry_SuccessOnRetry() throws Exception { // GIVEN Submission submission = initFailedSubmissionDeposit(); mockSword(); @@ -68,12 +68,36 @@ public void testFailedDepositRetry() throws Exception { RepositoryCopy popRepoCopy = passClient.getObject(updatedDeposit.getRepositoryCopy(), "repository", "publication"); updatedDeposit.setRepositoryCopy(popRepoCopy); - verify(passClient).updateObject(eq(updatedDeposit)); + verify(passClient).updateObject(updatedDeposit); assertEquals(submission.getPublication().getId(), popRepoCopy.getPublication().getId()); assertEquals(1, submission.getRepositories().size()); assertEquals(submission.getRepositories().get(0).getId(), popRepoCopy.getRepository().getId()); } + @Test + void testFailedDepositRetry_FailOnRetry() throws Exception { + // GIVEN + Submission submission = initFailedSubmissionDeposit(); + + // WHEN + try { + depositUpdater.doUpdate(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + // THEN + Condition> actualDeposits = depositsForSubmission(submission.getId(), 1, + (deposit, repo) -> true); + assertTrue(actualDeposits.awaitAndVerify(deposits -> deposits.size() == 1 && + DepositStatus.FAILED == deposits.iterator().next().getDepositStatus())); + + Deposit actualDeposit = actualDeposits.getResult().iterator().next(); + Deposit updatedDeposit = passClient.getObject(actualDeposit, "repositoryCopy"); + assertNull(updatedDeposit.getRepositoryCopy()); + verify(passClient).updateObject(updatedDeposit); + } + private Submission initFailedSubmissionDeposit() throws Exception { Submission submission = findSubmission(createSubmission( ResourceTestUtil.readSubmissionJson("sample2"))); diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryTest.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryTest.java index 3ce504c3..688db595 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryTest.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/service/FailedDepositRetryTest.java @@ -28,6 +28,8 @@ import java.util.Arrays; import java.util.List; +import org.eclipse.pass.deposit.DepositApp; +import org.eclipse.pass.deposit.DepositServiceErrorHandler; import org.eclipse.pass.deposit.builder.DepositSubmissionModelBuilder; import org.eclipse.pass.deposit.model.DepositFile; import org.eclipse.pass.deposit.model.DepositSubmission; @@ -40,11 +42,19 @@ import org.eclipse.pass.support.client.model.Submission; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; /** * @author Russ Poetker (rpoetke1@jh.edu) */ -public class FailedDepositRetryTest { +@SpringBootTest(classes = DepositApp.class) +@TestPropertySource("/test-application.properties") +class FailedDepositRetryTest { + + @Autowired + private DepositServiceErrorHandler depositServiceErrorHandler; @SuppressWarnings("unchecked") @Test @@ -56,18 +66,20 @@ void testRetryFailedDeposits_Success_WithMultipleFiles() throws IOException { final Packager packager = mock(Packager.class); final DepositSubmissionModelBuilder depositSubmissionModelBuilder = mock(DepositSubmissionModelBuilder.class); final FailedDepositRetry failedDepositRetry = new FailedDepositRetry(passClient, depositTaskHelper, - packagerRegistry, depositSubmissionModelBuilder); + packagerRegistry, depositSubmissionModelBuilder, depositServiceErrorHandler); Deposit deposit1 = new Deposit(); deposit1.setId("dp-1"); deposit1.setDepositStatus(DepositStatus.FAILED); Repository repository = new Repository(); deposit1.setRepository(repository); Submission submission = new Submission(); + submission.setId("sub-1"); deposit1.setSubmission(submission); when(passClient.getObject(same(deposit1), any(String[].class))).thenReturn(deposit1); when(passClient.getObject(same(submission), any(String[].class))).thenReturn(submission); when(packagerRegistry.get(any())).thenReturn(packager); DepositSubmission depositSubmission = new DepositSubmission(); + depositSubmission.setId(deposit1.getId()); DepositFile depositFile1 = new DepositFile(); depositFile1.setName("file1"); depositFile1.setLocation("filelocation1"); @@ -98,13 +110,16 @@ void testRetryFailedDeposits_Fail_NoPackagerFound() throws IOException { final Registry packagerRegistry = mock(Registry.class); final DepositSubmissionModelBuilder depositSubmissionModelBuilder = mock(DepositSubmissionModelBuilder.class); final FailedDepositRetry failedDepositRetry = new FailedDepositRetry(passClient, depositTaskHelper, - packagerRegistry, depositSubmissionModelBuilder); + packagerRegistry, depositSubmissionModelBuilder, depositServiceErrorHandler); Deposit deposit1 = new Deposit(); deposit1.setId("dp-1"); deposit1.setDepositStatus(DepositStatus.FAILED); deposit1.setRepository(new Repository()); - deposit1.setSubmission(new Submission()); + Submission submission = new Submission(); + submission.setId("sub-1"); + deposit1.setSubmission(submission); when(passClient.getObject(same(deposit1), any(String[].class))).thenReturn(deposit1); + when(passClient.getObject(same(submission), any(String[].class))).thenReturn(submission); when(packagerRegistry.get(any())).thenReturn(null); // WHEN @@ -128,13 +143,16 @@ void testRetryFailedDeposits_Fail_ErrorDepositSubmission() throws IOException { final Packager packager = mock(Packager.class); final DepositSubmissionModelBuilder depositSubmissionModelBuilder = mock(DepositSubmissionModelBuilder.class); final FailedDepositRetry failedDepositRetry = new FailedDepositRetry(passClient, depositTaskHelper, - packagerRegistry, depositSubmissionModelBuilder); + packagerRegistry, depositSubmissionModelBuilder, depositServiceErrorHandler); Deposit deposit1 = new Deposit(); deposit1.setId("dp-1"); deposit1.setDepositStatus(DepositStatus.FAILED); deposit1.setRepository(new Repository()); - deposit1.setSubmission(new Submission()); + Submission submission = new Submission(); + submission.setId("sub-1"); + deposit1.setSubmission(submission); when(passClient.getObject(same(deposit1), any(String[].class))).thenReturn(deposit1); + when(passClient.getObject(same(submission), any(String[].class))).thenReturn(submission); when(packagerRegistry.get(any())).thenReturn(packager); doThrow(new IOException("Testing depositsubmission error")) .when(depositSubmissionModelBuilder).build(any()); @@ -160,15 +178,20 @@ void testRetryFailedDeposits_Fail_InvalidFilesNoFiles() throws IOException { final Packager packager = mock(Packager.class); final DepositSubmissionModelBuilder depositSubmissionModelBuilder = mock(DepositSubmissionModelBuilder.class); final FailedDepositRetry failedDepositRetry = new FailedDepositRetry(passClient, depositTaskHelper, - packagerRegistry, depositSubmissionModelBuilder); + packagerRegistry, depositSubmissionModelBuilder, depositServiceErrorHandler); Deposit deposit1 = new Deposit(); deposit1.setId("dp-1"); deposit1.setDepositStatus(DepositStatus.FAILED); deposit1.setRepository(new Repository()); - deposit1.setSubmission(new Submission()); + Submission submission = new Submission(); + submission.setId("sub-1"); + deposit1.setSubmission(submission); when(passClient.getObject(same(deposit1), any(String[].class))).thenReturn(deposit1); + when(passClient.getObject(same(submission), any(String[].class))).thenReturn(submission); when(packagerRegistry.get(any())).thenReturn(packager); - when(depositSubmissionModelBuilder.build(any())).thenReturn(new DepositSubmission()); + DepositSubmission depositSubmission = new DepositSubmission(); + depositSubmission.setId(deposit1.getId()); + when(depositSubmissionModelBuilder.build(any())).thenReturn(depositSubmission); // WHEN failedDepositRetry.retryFailedDeposit(deposit1); @@ -191,15 +214,19 @@ void testRetryFailedDeposits_Fail_InvalidFilesFilesNoLocation() throws IOExcepti final Packager packager = mock(Packager.class); final DepositSubmissionModelBuilder depositSubmissionModelBuilder = mock(DepositSubmissionModelBuilder.class); final FailedDepositRetry failedDepositRetry = new FailedDepositRetry(passClient, depositTaskHelper, - packagerRegistry, depositSubmissionModelBuilder); + packagerRegistry, depositSubmissionModelBuilder, depositServiceErrorHandler); Deposit deposit1 = new Deposit(); deposit1.setId("dp-1"); deposit1.setDepositStatus(DepositStatus.FAILED); deposit1.setRepository(new Repository()); - deposit1.setSubmission(new Submission()); + Submission submission = new Submission(); + submission.setId("sub-1"); + deposit1.setSubmission(submission); when(passClient.getObject(same(deposit1), any(String[].class))).thenReturn(deposit1); + when(passClient.getObject(same(submission), any(String[].class))).thenReturn(submission); when(packagerRegistry.get(any())).thenReturn(packager); DepositSubmission depositSubmission = new DepositSubmission(); + depositSubmission.setId(deposit1.getId()); DepositFile depositFile1 = new DepositFile(); depositFile1.setName("file1"); DepositFile depositFile2 = new DepositFile(); diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataServiceIT.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataServiceIT.java index 306e5299..b300b210 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataServiceIT.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/support/deploymenttest/DeploymentTestDataServiceIT.java @@ -81,7 +81,7 @@ "pass.test.dspace.repo.key=TestDspace" }) @WireMockTest(httpPort = 9020) -public class DeploymentTestDataServiceIT extends AbstractDepositIT { +class DeploymentTestDataServiceIT extends AbstractDepositIT { @Autowired private DeploymentTestDataService deploymentTestDataService; @SpyBean private DspaceDepositService dspaceDepositService; @@ -147,7 +147,7 @@ private void deleteFile(File file) { } @Test - public void testProcessTestData_TestGrantDataCreatedIfNotExist() throws Exception { + void testProcessTestData_TestGrantDataCreatedIfNotExist() throws Exception { // WHEN deploymentTestDataService.processTestData(); @@ -166,7 +166,7 @@ public void testProcessTestData_TestGrantDataCreatedIfNotExist() throws Exceptio } @Test - public void testProcessTestData_DoesNotTestGrantDataCreatedIfExist() throws Exception { + void testProcessTestData_DoesNotTestGrantDataCreatedIfExist() throws Exception { // GIVEN Grant testGrant = new Grant(); testGrant.setProjectName(PASS_E2E_TEST_GRANT); @@ -190,13 +190,13 @@ public void testProcessTestData_DoesNotTestGrantDataCreatedIfExist() throws Exce } @Test - public void testProcessTestData_DeleteTestSubmissionsForTestGrant() throws Exception { + void testProcessTestData_DeleteTestSubmissionsForTestGrant() throws Exception { // GIVEN Grant testGrant = new Grant(); testGrant.setProjectName(PASS_E2E_TEST_GRANT); testGrant.setAwardStatus(AwardStatus.ACTIVE); passClient.createObject(testGrant); - initSubmissionAndDeposits(testGrant); + initSubmissionAndDeposits(testGrant, false); initDspaceApiStubs("Test-Title"); ReflectionTestUtils.setField(deploymentTestDataService, "skipDeploymentTestDeposits", Boolean.FALSE); @@ -211,23 +211,48 @@ public void testProcessTestData_DeleteTestSubmissionsForTestGrant() throws Excep verify(dspaceDepositService, times(0)).deleteDeposit( argThat(deposit -> deposit.getRepository().getRepositoryKey().equals("TestNihms")), any(DspaceDepositService.AuthContext.class)); - verifyDspaceApiStubs(1); + verifyDspaceApiStubs(1, 1); } @Test - public void testProcessTestData_DoesNotDeleteTestSubmissionsForOtherGrant() throws Exception { + void testProcessTestData_DeleteTestSubmissionsWithNullRepoCopy() throws Exception { + // GIVEN + Grant testGrant = new Grant(); + testGrant.setProjectName(PASS_E2E_TEST_GRANT); + testGrant.setAwardStatus(AwardStatus.ACTIVE); + passClient.createObject(testGrant); + initSubmissionAndDeposits(testGrant, true); + initDspaceApiStubs("Test-Title"); + ReflectionTestUtils.setField(deploymentTestDataService, "skipDeploymentTestDeposits", Boolean.FALSE); + + // WHEN + deploymentTestDataService.processTestData(); + + // THEN + verifyTestGrantDeleted(); + verify(dspaceDepositService, times(1)).deleteDeposit( + argThat(deposit -> deposit.getRepository().getRepositoryKey().equals("TestDspace")), + any(DspaceDepositService.AuthContext.class)); + verify(dspaceDepositService, times(0)).deleteDeposit( + argThat(deposit -> deposit.getRepository().getRepositoryKey().equals("TestNihms")), + any(DspaceDepositService.AuthContext.class)); + verifyDspaceApiStubs(0, 1); + } + + @Test + void testProcessTestData_DoesNotDeleteTestSubmissionsForOtherGrant() throws Exception { // GIVEN Grant deploymentGrant = new Grant(); deploymentGrant.setProjectName(PASS_E2E_TEST_GRANT); deploymentGrant.setAwardStatus(AwardStatus.ACTIVE); passClient.createObject(deploymentGrant); - initSubmissionAndDeposits(deploymentGrant); + initSubmissionAndDeposits(deploymentGrant, false); Grant testOtherGrant = new Grant(); testOtherGrant.setProjectName("Some Other Grant"); testOtherGrant.setAwardStatus(AwardStatus.ACTIVE); passClient.createObject(testOtherGrant); - Submission testOtherSubmission = initSubmissionAndDeposits(testOtherGrant); + Submission testOtherSubmission = initSubmissionAndDeposits(testOtherGrant, false); initDspaceApiStubs("Test-Title"); ReflectionTestUtils.setField(deploymentTestDataService, "skipDeploymentTestDeposits", Boolean.FALSE); @@ -287,17 +312,17 @@ public void testProcessTestData_DoesNotDeleteTestSubmissionsForOtherGrant() thro verify(dspaceDepositService, times(0)).deleteDeposit( argThat(deposit -> deposit.getRepository().getRepositoryKey().equals("TestNihms")), any(DspaceDepositService.AuthContext.class)); - verifyDspaceApiStubs(1); + verifyDspaceApiStubs(1, 1); } @Test - public void testProcessTestData_DoesNotDeleteDspaceDepositIfSkip() throws Exception { + void testProcessTestData_DoesNotDeleteDspaceDepositIfSkip() throws Exception { // GIVEN Grant testGrant = new Grant(); testGrant.setProjectName(PASS_E2E_TEST_GRANT); testGrant.setAwardStatus(AwardStatus.ACTIVE); passClient.createObject(testGrant); - initSubmissionAndDeposits(testGrant); + initSubmissionAndDeposits(testGrant, false); initDspaceApiStubs("Test-Title"); // WHEN @@ -311,17 +336,17 @@ public void testProcessTestData_DoesNotDeleteDspaceDepositIfSkip() throws Except verify(dspaceDepositService, times(0)).deleteDeposit( argThat(deposit -> deposit.getRepository().getRepositoryKey().equals("TestNihms")), any(DspaceDepositService.AuthContext.class)); - verifyDspaceApiStubs(0); + verifyDspaceApiStubs(0, 0); } @Test - public void testProcessTestData_DoesNotDeleteDspaceDepositIfNameMismatch() throws Exception { + void testProcessTestData_DoesNotDeleteDspaceDepositIfNameMismatch() throws Exception { // GIVEN Grant testGrant = new Grant(); testGrant.setProjectName(PASS_E2E_TEST_GRANT); testGrant.setAwardStatus(AwardStatus.ACTIVE); passClient.createObject(testGrant); - initSubmissionAndDeposits(testGrant); + initSubmissionAndDeposits(testGrant, false); initDspaceApiStubs("Test-Title-Mismatch"); ReflectionTestUtils.setField(deploymentTestDataService, "skipDeploymentTestDeposits", Boolean.FALSE); @@ -347,7 +372,7 @@ public void testProcessTestData_DoesNotDeleteDspaceDepositIfNameMismatch() throw WireMock.verify(0, deleteRequestedFor(urlEqualTo("/server/api/core/items/12345-aabb"))); } - private Submission initSubmissionAndDeposits(Grant testGrant) throws Exception { + private Submission initSubmissionAndDeposits(Grant testGrant, boolean skipRepoCopy) throws Exception { Submission submission = new Submission(); submission.setMetadata( "{\"issns\":[{\"issn\":\"1234\",\"pubType\":\"Print\"}],\"journal-title\":\"Test-Journal\"," + @@ -372,41 +397,45 @@ private Submission initSubmissionAndDeposits(Grant testGrant) throws Exception { publication.setTitle("test-publication"); passClient.createObject(publication); - RepositoryCopy repositoryCopyDspace = new RepositoryCopy(); - repositoryCopyDspace.setRepository(repositoryDspace); - repositoryCopyDspace.setPublication(publication); - repositoryCopyDspace.setAccessUrl(URI.create("http://localhost:9020/handle/a.1234/abcd")); - passClient.createObject(repositoryCopyDspace); - - RepositoryCopy repositoryCopyNihms = new RepositoryCopy(); - repositoryCopyNihms.setRepository(repositoryNihms); - repositoryCopyNihms.setPublication(publication); - repositoryCopyDspace.setAccessUrl(URI.create("http://foobar/nihms-fake")); - passClient.createObject(repositoryCopyNihms); - Deposit dspaceDeposit = new Deposit(); dspaceDeposit.setSubmission(submission); dspaceDeposit.setDepositStatus(DepositStatus.SUBMITTED); - dspaceDeposit.setRepositoryCopy(repositoryCopyDspace); dspaceDeposit.setRepository(repositoryDspace); passClient.createObject(dspaceDeposit); Deposit nihmsDeposit = new Deposit(); nihmsDeposit.setSubmission(submission); nihmsDeposit.setDepositStatus(DepositStatus.SUBMITTED); - nihmsDeposit.setRepositoryCopy(repositoryCopyNihms); nihmsDeposit.setRepository(repositoryNihms); passClient.createObject(nihmsDeposit); + if (!skipRepoCopy) { + RepositoryCopy repositoryCopyDspace = new RepositoryCopy(); + repositoryCopyDspace.setRepository(repositoryDspace); + repositoryCopyDspace.setPublication(publication); + repositoryCopyDspace.setAccessUrl(URI.create("http://localhost:9020/handle/a.1234/abcd")); + passClient.createObject(repositoryCopyDspace); + dspaceDeposit.setRepositoryCopy(repositoryCopyDspace); + passClient.updateObject(dspaceDeposit); + + RepositoryCopy repositoryCopyNihms = new RepositoryCopy(); + repositoryCopyNihms.setRepository(repositoryNihms); + repositoryCopyNihms.setPublication(publication); + repositoryCopyDspace.setAccessUrl(URI.create("http://foobar/nihms-fake")); + passClient.createObject(repositoryCopyNihms); + nihmsDeposit.setRepositoryCopy(repositoryCopyNihms); + passClient.updateObject(nihmsDeposit); + } + 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); + URI dataUri = passClient.uploadBinary(file.getName(), data.getBytes(StandardCharsets.UTF_8)); + assertNotNull(dataUri); + file.setUri(dataUri); file.setSubmission(submission); passClient.createObject(file); @@ -479,9 +508,9 @@ private void initDspaceApiStubs(String expectedName) throws Exception { .willReturn(ok())); } - private void verifyDspaceApiStubs(int expectedCount) { - WireMock.verify(expectedCount, getRequestedFor(urlEqualTo("/server/api/security/csrf"))); - WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/server/api/authn/login"))); + private void verifyDspaceApiStubs(int expectedCount, int expectedAuthCount) { + WireMock.verify(expectedAuthCount, getRequestedFor(urlEqualTo("/server/api/security/csrf"))); + WireMock.verify(expectedAuthCount, postRequestedFor(urlEqualTo("/server/api/authn/login"))); WireMock.verify(expectedCount, getRequestedFor( urlEqualTo("/server/api/discover/search/objects?query=handle:a.1234%2Fabcd&dsoType=item"))); WireMock.verify(expectedCount, getRequestedFor(urlEqualTo("/server/api/core/items/12345-aabb/bundles"))); diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/util/SubmissionTestUtil.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/util/SubmissionTestUtil.java index 2d2a2a71..2c2fae50 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/util/SubmissionTestUtil.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/util/SubmissionTestUtil.java @@ -66,7 +66,9 @@ public void deleteDepositsInPass() throws IOException { resultDeposits.getObjects().forEach(deposit -> { try { passClient.deleteObject(deposit); - passClient.deleteObject(deposit.getRepositoryCopy()); + if (Objects.nonNull(deposit.getRepositoryCopy())) { + passClient.deleteObject(deposit.getRepositoryCopy()); + } } catch (Exception ex) { throw new RuntimeException(ex); } @@ -114,13 +116,8 @@ public Submission createSubmissionFromJson(InputStream is, List enti return submission; } catch (Exception e) { - // TODO re-throw? - System.out.println("Error building Submission from stream."); - e.printStackTrace(); + throw new RuntimeException(e); } - - entities.clear(); - return null; } private Submission createEntitiesInPass(List entities) { @@ -132,8 +129,8 @@ private Submission createEntitiesInPass(List entities) { } }); - return entities.stream().filter(entity -> entity instanceof Submission) - .map(passEntity -> (Submission) passEntity) + return entities.stream().filter(Submission.class::isInstance) + .map(Submission.class::cast) .findFirst() .orElseThrow(() -> new RuntimeException("Submission not found")); }