Skip to content

Commit

Permalink
Populate submission as needed for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoet-jh committed Oct 16, 2023
1 parent 0b69fc3 commit 9b793d3
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.Objects;
import java.util.function.BiFunction;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -79,11 +78,7 @@ public class Composer implements BiFunction<SubmissionEvent, SubmissionEventMess
*/
@Override
public Notification apply(SubmissionEvent event, SubmissionEventMessage submissionEventMessage) {
Objects.requireNonNull(event, "Event must not be null.");

Submission submission = event.getSubmission();
Objects.requireNonNull(submission, "Submission must not be null.");

if (!event.getSubmission().getId().equals(submission.getId())) {
// todo: exception?
log.warn("Composing a Notification for tuple [{},{}] but {} references a different Submission: {}.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.eclipse.pass.notification.service;

import java.io.IOException;
import java.util.Objects;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.pass.notification.dispatch.DispatchService;
Expand Down Expand Up @@ -47,28 +50,33 @@ public class NotificationService {
*/
public void notify(SubmissionEventMessage submissionEventMessage) {

SubmissionEvent submissionEvent;
try {
submissionEvent = passClient.getObject(SubmissionEvent.class, submissionEventMessage.getSubmissionEventId(),
"submission", "performedBy");
} catch (Exception e) {
log.error("Unable to retrieve SubmissionEvent '{}'", submissionEventMessage.getSubmissionEventId(), e);
return;
}
SubmissionEvent submissionEvent = passClient.getObject(SubmissionEvent.class,
submissionEventMessage.getSubmissionEventId(), "submission", "performedBy");

Submission submission = submissionEvent.getSubmission();
if (isSelfSubmission(submission)) {
log.debug("Dropping self-submission SubmissionEvent (Event URI: {}, Resource URI: {})",
populateSubmission(submissionEvent);
if (isSelfSubmission(submissionEvent.getSubmission())) {
log.debug("Dropping self-submission SubmissionEvent (Event URI: {}, Resource URI: {})",
submissionEvent.getId(),
submission.getId());
return;
}
submissionEvent.getSubmission().getId());
return;
}

// Compose Notification
Notification notification = composer.apply(submissionEvent, submissionEventMessage);
// Compose Notification
Notification notification = composer.apply(submissionEvent, submissionEventMessage);

// Invoke Dispatch
dispatchService.dispatch(notification);
} catch (Exception e) {
log.error("Unable to process SubmissionEvent '{}'", submissionEventMessage.getSubmissionEventId(), e);
}
}

// Invoke Dispatch
dispatchService.dispatch(notification);
private void populateSubmission(SubmissionEvent submissionEvent) throws IOException {
Objects.requireNonNull(submissionEvent.getSubmission(), "Submission must not be null.");
Submission populatedSubmission = passClient.getObject(submissionEvent.getSubmission(),
"submitter", "preparers");
submissionEvent.setSubmission(populatedSubmission);
}

private boolean isSelfSubmission(Submission submission) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ void testNotify() throws Exception {
assertEquals(expectedBody, message.getContent().toString());
}

@Test
void testNotify_Submitter() throws Exception {
// GIVEN
final String expectedBody = "Dear [email protected]\r\n\r\nA submission titled \"Specific protein " +
"supplementation using soya, casein or whey differentially affects regional gut growth and luminal " +
"growth factor bioactivity in rats; implications for the treatment of gut injury and stimulating " +
"repair\" has been prepared on your behalf by [email protected] with comment \"How " +
"does this submission look?\"\r\n\r\n\r\nPlease review the submission at the following URL: " +
"http://example.org/user-token-test\r\n\r\nA test inline footer";

SubmissionEvent submissionEvent = stagePassDataSubmitter();

SubmissionEventMessage submissionEventMessage = new SubmissionEventMessage();
submissionEventMessage.setSubmissionEventId(submissionEvent.getId());
submissionEventMessage.setUserApprovalLink(URI.create("http://example.org/user-token-test"));

notificationService.notify(submissionEventMessage);

List<MimeMessage> receivedMessages = Arrays.asList(greenMail.getReceivedMessages());
// 3 = 1 To + 1 CC + 1 BCC
assertEquals(3, receivedMessages.size());

MimeMessage message = receivedMessages.get(0);
assertTrue(message.getSubject().contains("PASS Submission Approval: Specific protein"));
assertEquals(SENDER, message.getFrom()[0].toString());
assertEquals(CC, message.getRecipients(MimeMessage.RecipientType.CC)[0].toString());
assertEquals(RECIPIENT, message.getRecipients(MimeMessage.RecipientType.TO)[0].toString());
assertEquals(expectedBody, message.getContent().toString());
}

private SubmissionEvent stagePassData() throws IOException {
// This User prepares the submission on behalf of the Submission.submitter
// Confusingly, this User has the ability to submit to PASS. The authorization-related role of
Expand Down Expand Up @@ -185,4 +215,62 @@ private SubmissionEvent stagePassData() throws IOException {

return event;
}

private SubmissionEvent stagePassDataSubmitter() throws IOException {
// This User prepares the submission on behalf of the Submission.submitter
// Confusingly, this User has the ability to submit to PASS. The authorization-related role of
// User.Role.SUBMITTER should not be confused with the the logical role as a preparer of a submission.
User preparer = new User();
preparer.setEmail("[email protected]");
preparer.setDisplayName("Submission Preparer");
preparer.setFirstName("Pre");
preparer.setLastName("Parer");
preparer.setRoles(List.of(UserRole.SUBMITTER));

passClient.createObject(preparer);

User submitter = new User();
submitter.setEmail(RECIPIENT);
submitter.setDisplayName("Submission Submitter");
submitter.setFirstName("Sub");
submitter.setLastName("Mitter");
submitter.setRoles(List.of(UserRole.SUBMITTER));

passClient.createObject(submitter);

// The Submission as prepared by the preparer.
// The preparer did not find the authorized submitter in PASS, so they filled in the email address of the
// authorized submitter. Therefore, the Submission.submitter field will be null (because that *must* be a URI
// to a User resource, and the User does not exist). The Submission.submitterEmail will be set to the email
// address of the authorized submitter
Submission submission = new Submission();
submission.setMetadata(resourceToString("/" + PathUtil.packageAsPath(this.getClass()) +
"/submission-metadata.json", StandardCharsets.UTF_8));
submission.setPreparers(List.of(preparer));
submission.setSource(Source.PASS);
submission.setSubmitter(submitter);

passClient.createObject(submission);

// When this event is processed, the authorized submitter will recieve an email notification with a link that
// will invite them to use PASS, and link the Submission to their newly created User (created when they login
// to PASS for the first time)
SubmissionEvent event = new SubmissionEvent();
event.setSubmission(submission);
event.setPerformerRole(PerformerRole.PREPARER);
event.setPerformedBy(preparer);
String comment = "How does this submission look?";
event.setComment(comment);
event.setEventType(EventType.APPROVAL_REQUESTED_NEWUSER);
event.setPerformedDate(ZonedDateTime.now());

String submissionId = submission.getId();
Link link = new Link(URI.create(submissionId
.replace("http://localhost", "https://pass.local")), SUBMISSION_REVIEW_INVITE);
event.setLink(link.getHref());

passClient.createObject(event);

return event;
}
}

0 comments on commit 9b793d3

Please sign in to comment.