Skip to content

Commit

Permalink
Use deposit ref to lookup deposit
Browse files Browse the repository at this point in the history
And add IT
  • Loading branch information
rpoet-jh committed Jan 4, 2024
1 parent d778354 commit faae7f1
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public class NihmsAssembler extends AbstractAssembler {
* bulk publishing pdf.
*/
public static final String SPEC_NIHMS_NATIVE_2022_05 = "nihms-native-2022-05";

static final String NIHMS_PKG_DEP_REF_PREFIX = "nihms-package:";
public static final String NIHMS_PKG_DEP_REF_PREFIX = "nihms-package:";

/**
* Mime type of zip files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.eclipse.pass.deposit.provider.nihms.NihmsAssembler;
import org.eclipse.pass.support.client.PassClient;
import org.eclipse.pass.support.client.PassClientSelector;
import org.eclipse.pass.support.client.RSQL;
Expand All @@ -49,10 +50,6 @@ public class NihmsReceiveMailService {
static final String NIHMS_DEP_STATUS_REF_PREFIX = "nihms-id:";

private final PassClient passClient;

@Value("${pass.deposit.pmc.repo.key}")
private String nihmsRepositoryKey;

private final Address nihmsFromEmail;

public NihmsReceiveMailService(PassClient passClient,
Expand Down Expand Up @@ -93,7 +90,7 @@ public void handleReceivedMail(MimeMessage receivedMessage) {
String packageId = matchResult.group(1);
String submissionId = parseSubmissionId(packageId);
try {
updateDepositRejected(submissionId, message);
updateDepositRejected(submissionId, packageId, message);
} catch (Exception e) {
LOG.error("Error updating nihms deposit for submission ID " + submissionId, e);
}
Expand All @@ -105,7 +102,7 @@ public void handleReceivedMail(MimeMessage receivedMessage) {
String submissionId = parseSubmissionId(packageId);
String nihmsId = matchResult.group(2);
try {
updateDepositAccepted(submissionId, nihmsId);
updateDepositAccepted(submissionId, packageId, nihmsId);
} catch (Exception e) {
LOG.error("Error updating nihms deposit for submission ID " + submissionId, e);
}
Expand All @@ -122,27 +119,27 @@ private boolean isEmailNotNihms(MimeMessage mimeMessage) throws MessagingExcepti
return !fromNihms || !mimeMessage.getSubject().startsWith("Bulk submission");
}

private void updateDepositRejected(String submissionId, String message) throws IOException {
getDeposits(submissionId).forEach(deposit -> {
private void updateDepositRejected(String submissionId, String packageId, String message) throws IOException {
getDeposits(submissionId, packageId).forEach(deposit -> {
deposit.setDepositStatus(DepositStatus.REJECTED);
deposit.setStatusMessage(message);
updateDeposit(deposit);
});
}

private void updateDepositAccepted(String submissionId, String nihmsId) throws IOException {
getDeposits(submissionId).forEach(deposit -> {
private void updateDepositAccepted(String submissionId, String packageId, String nihmsId) throws IOException {
getDeposits(submissionId, packageId).forEach(deposit -> {
deposit.setDepositStatus(DepositStatus.ACCEPTED);
deposit.setDepositStatusRef(NIHMS_DEP_STATUS_REF_PREFIX + nihmsId);
updateDeposit(deposit);
});
}

private Stream<Deposit> getDeposits(String submissionId) throws IOException {
private Stream<Deposit> getDeposits(String submissionId, String packageKey) throws IOException {
PassClientSelector<Deposit> sel = new PassClientSelector<>(Deposit.class);
sel.setFilter(RSQL.and(
RSQL.equals("submission.id", submissionId),
RSQL.equals("repository.repositoryKey", nihmsRepositoryKey)
RSQL.equals("depositStatusRef", NihmsAssembler.NIHMS_PKG_DEP_REF_PREFIX + packageKey)
));
return passClient.streamObjects(sel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ jscholarship.hack.sword.statement.uri-replacement=https://jscholarship.library.j

pass.deposit.nihms.email.enabled=false
pass.deposit.nihms.email.delay=30000
pass.deposit.nihms.email.from=${PASS_DEPOSIT_NIHMS_EMAIL_FROM:[email protected]}
pass.deposit.pmc.repo.key=${PASS_DEPOSIT_PMC_REPO_KEY:pmc}
pass.deposit.nihms.email.from=${PASS_DEPOSIT_NIHMS_EMAIL_FROM:[email protected]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2023 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 java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.eclipse.pass.deposit.service.NihmsReceiveMailService.NIHMS_DEP_STATUS_REF_PREFIX;
import static org.eclipse.pass.deposit.util.ResourceTestUtil.findByNameAsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.time.ZonedDateTime;
import java.util.List;

import com.icegreen.greenmail.configuration.GreenMailConfiguration;
import com.icegreen.greenmail.junit5.GreenMailExtension;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest;
import jakarta.mail.internet.MimeMessage;
import org.eclipse.pass.deposit.AbstractDepositSubmissionIT;
import org.eclipse.pass.deposit.DepositApp;
import org.eclipse.pass.deposit.provider.nihms.NihmsAssembler;
import org.eclipse.pass.deposit.util.ResourceTestUtil;
import org.eclipse.pass.support.client.PassClientSelector;
import org.eclipse.pass.support.client.RSQL;
import org.eclipse.pass.support.client.model.Deposit;
import org.eclipse.pass.support.client.model.DepositStatus;
import org.eclipse.pass.support.client.model.Submission;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestPropertySource;

/**
* @author Russ Poetker ([email protected])
*/
@SpringBootTest(classes = DepositApp.class)
@TestPropertySource("classpath:test-application.properties")
@TestPropertySource(properties = {
"pass.deposit.nihms.email.enabled=true",
"pass.deposit.nihms.email.delay=2000",
"pass.deposit.nihms.email.from=test-from@localhost",
"nihms.mail.host=localhost",
"nihms.mail.port=3993",
"nihms.mail.username=testnihms@localhost",
"nihms.mail.password=testnihmspassword"

})
public class NihmsReceiveMailServiceIT extends AbstractDepositSubmissionIT {

@RegisterExtension
static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.IMAPS)
.withConfiguration(new GreenMailConfiguration().withUser("testnihms@localhost", "testnihmspassword"))
.withPerMethodLifecycle(false);

@SpyBean private NihmsReceiveMailService nihmsReceiveMailService;

@Test
void testHandleReceivedMail() throws Exception {
// GIVEN
Submission testSubmission = initSubmissionDeposit();
final String subject1 = "Bulk submission";
final String body1 = findByNameAsString("nihmsemail-success.html", this.getClass())
.replace("{test-submission-id}", testSubmission.getId());
MimeMessage message1 = GreenMailUtil.createTextEmail("testnihms@localhost", "test-from@localhost",
subject1, body1, greenMail.getImaps().getServerSetup());
GreenMailUser user = greenMail.setUser("testnihms@localhost", "testnihmspassword");

// WHEN
user.deliver(message1);

// THEN
// wait for email poller to run, every 2 seconds plus few seconds for processing
await().pollDelay(5, SECONDS).until(() -> true);
verify(nihmsReceiveMailService, times(1))
.handleReceivedMail(any());
PassClientSelector<Deposit> sel = new PassClientSelector<>(Deposit.class);
sel.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
List<Deposit> actualDeposits = passClient.selectObjects(sel).getObjects();
assertEquals(1, actualDeposits.size());
Deposit pmcDeposit = actualDeposits.get(0);
assertEquals(DepositStatus.ACCEPTED, pmcDeposit.getDepositStatus());
assertEquals(NIHMS_DEP_STATUS_REF_PREFIX + "test-nihms-id", pmcDeposit.getDepositStatusRef());
assertNull(pmcDeposit.getStatusMessage());
}

private Submission initSubmissionDeposit() throws Exception {
Submission submission = findSubmission(createSubmission(
ResourceTestUtil.readSubmissionJson("sample2")));
submission.setSubmittedDate(ZonedDateTime.now());
passClient.updateObject(submission);
triggerSubmission(submission);
final Submission actualSubmission = passClient.getObject(Submission.class, submission.getId());
Deposit pmcDeposit = new Deposit();
pmcDeposit.setSubmission(actualSubmission);
// There is only the pmc repo on this submission
pmcDeposit.setRepository(actualSubmission.getRepositories().get(0));
pmcDeposit.setDepositStatus(DepositStatus.SUBMITTED);
pmcDeposit.setDepositStatusRef(NihmsAssembler.NIHMS_PKG_DEP_REF_PREFIX +
"nihms-native-2017-07_2023-10-23_13-10-30_" + actualSubmission.getId());
passClient.createObject(pmcDeposit);
return actualSubmission;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"pass.deposit.nihms.email.enabled=true",
"pass.deposit.nihms.email.delay=2000",
"pass.deposit.nihms.email.from=test-from@localhost",
"pass.deposit.pmc.repo.key=pmc",
"nihms.mail.host=localhost",
"nihms.mail.port=3993",
"nihms.mail.username=testnihms@localhost",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<title>Bulk submission (errors encountered)</title>
</head>
<body style='font-family:"Helvetica Neue", Helvetica, Arial, sans-serif'>
<table class="table" style="background-color:transparent; margin-bottom:20px; max-width:100%; width:100%; border-collapse:collapse" width="100%">
<colgroup>
<col span="1" style="width:50px">
<col span="1">
</colgroup>
<tbody>
<tr style="page-break-inside:avoid">
<td class="label label-info" style="border:0 solid #000; border-radius:0.25em; color:#fff; font-size:75%; font-weight:bold; line-height:1.428571; padding:8px; text-align:center; vertical-align:top; white-space:nowrap; background-color:#5bc0de; border-top:1px solid #ddd" align="center" valign="top" bgcolor="#5bc0de">INFO</td>
<td class="message" style="border-top:1px solid #ddd; line-height:1.428571; padding:8px; vertical-align:top" valign="top">Package ID=nihms-native-2017-07_2023-10-23_13-10-30_{test-submission-id} for Manuscript ID test-nihms-id was submitted successfully.</td>
</tr>
</tbody>
</table>
</body>
</html>

0 comments on commit faae7f1

Please sign in to comment.