Skip to content

Commit

Permalink
Handle delete test data with null repoCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoet-jh committed Jan 15, 2025
1 parent 9573adc commit adea58a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,19 +95,19 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException {
testSubmissions.forEach(testSubmission -> {
try {
PassClientSelector<Deposit> 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<Deposit> testDeposits = passClient.streamObjects(testDepositSelector).toList();
testDeposits.forEach(testDeposit -> {
deleteObject(testDeposit);
deleteObject(testDeposit.getRepositoryCopy());
});
PassClientSelector<File> testFileSelector = new PassClientSelector<>(File.class);
testFileSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
testFileSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
List<File> testFiles = passClient.streamObjects(testFileSelector).toList();
testFiles.forEach(this::deleteFile);
PassClientSelector<SubmissionEvent> subEventSelector = new PassClientSelector<>(SubmissionEvent.class);
subEventSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
subEventSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
List<SubmissionEvent> testSubmissionEvents = passClient.streamObjects(subEventSelector).toList();
testSubmissionEvents.forEach(this::deleteObject);
deleteObject(testSubmission);
Expand All @@ -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<Submission> testSubmissionSelector = new PassClientSelector<>(Submission.class);
testSubmissionSelector.setFilter(RSQL.and(
Expand All @@ -134,7 +135,7 @@ private void deleteDepositsInRepoIfNeeded(Grant testGrant) throws IOException {
testSubmissions.forEach(testSubmission -> {
try {
PassClientSelector<Deposit> 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<Deposit> testDeposits = passClient.streamObjects(testDepositSelector).toList();
testDeposits.forEach(testDeposit -> deleteDepositInRepoIfNeeded(testDeposit, authContext));
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -109,12 +114,12 @@ AuthContext authenticate() {
ResponseEntity<Void> 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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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<Map<String, ?>> searchArray = JsonPath.parse(searchResponse).read("$..indexableObject[?(@.handle)]");
Expand All @@ -195,7 +202,7 @@ private List<String> 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");
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,7 +147,7 @@ private void deleteFile(File file) {
}

@Test
public void testProcessTestData_TestGrantDataCreatedIfNotExist() throws Exception {
void testProcessTestData_TestGrantDataCreatedIfNotExist() throws Exception {
// WHEN
deploymentTestDataService.processTestData();

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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\"," +
Expand All @@ -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);

Expand Down Expand Up @@ -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")));
Expand Down

0 comments on commit adea58a

Please sign in to comment.