Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment test data job #105

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ public Spliterator<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Grant> grantSelector = new PassClientSelector<>(Grant.class);
grantSelector.setFilter(RSQL.equals("projectName", PASS_E2E_TEST_GRANT));
List<Grant> 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<Submission> 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<Submission> testSubmissions = passClient.streamObjects(testSubmissionSelector).toList();
testSubmissions.forEach(testSubmission -> {
try {
PassClientSelector<Deposit> testDepositSelector = new PassClientSelector<>(Deposit.class);
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()));
List<File> 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<Policy> policySelector = new PassClientSelector<>(Policy.class);
policySelector.setFilter(RSQL.equals("title", testPolicyTitle));
List<Policy> 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<User> userSelector = new PassClientSelector<>(User.class);
userSelector.setFilter(RSQL.equals("email", testUserEmail));
List<User> testUsers = passClient.streamObjects(userSelector).toList();
User testUser = testUsers.get(0);
testGrant.setPi(testUser);

passClient.createObject(testGrant);
return testGrant;
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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:[email protected]}

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:}
Loading
Loading