Skip to content

Commit

Permalink
feature(#1023): use digiwf-email in email-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoesle committed Nov 27, 2023
1 parent 209c7fb commit 99d919c
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-s3-integration-client-starter</artifactId>
</dependency>
<dependency>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-email-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.muenchen.oss.digiwf.email.integration.adapter.out;

import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi;
import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class MailAdapter implements MailPort {

private final DigiwfEmailApi digiwfEmailApi;

@Override
public void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List<FileAttachment> attachments) throws MessagingException {
this.digiwfEmailApi.sendMailWithAttachments(receivers, subject, body, replyTo, receiversCc, receiversBcc, attachments);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package de.muenchen.oss.digiwf.email.integration.adapter.out;

import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort;
import de.muenchen.oss.digiwf.email.integration.model.FileAttachment;
import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageClientErrorException;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageException;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageServerErrorException;
import de.muenchen.oss.digiwf.s3.integration.client.repository.transfer.S3FileTransferRepository;
import jakarta.mail.util.ByteArrayDataSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;

import jakarta.mail.util.ByteArrayDataSource;

@Slf4j
@RequiredArgsConstructor
public class S3Adapter implements LoadMailAttachmentPort {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.muenchen.oss.digiwf.email.integration.application.port.out;

import de.muenchen.oss.digiwf.email.integration.model.FileAttachment;
import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl;
import de.muenchen.oss.digiwf.email.model.FileAttachment;

public interface LoadMailAttachmentPort {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.muenchen.oss.digiwf.email.integration.application.port.out;

import de.muenchen.oss.digiwf.email.model.FileAttachment;
import jakarta.mail.MessagingException;

import java.util.List;

public interface MailPort {

void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List<FileAttachment> attachments) throws MessagingException;

}
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package de.muenchen.oss.digiwf.email.integration.application.usecase;

import de.muenchen.oss.digiwf.email.integration.model.FileAttachment;
import de.muenchen.oss.digiwf.email.integration.application.port.in.SendMail;
import de.muenchen.oss.digiwf.email.integration.application.port.out.CorrelateMessagePort;
import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort;
import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort;
import de.muenchen.oss.digiwf.email.integration.model.Mail;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import jakarta.mail.MessagingException;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.validation.annotation.Validated;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Validated
public class SendMailUseCase implements SendMail {

private final JavaMailSender mailSender;
private final LoadMailAttachmentPort loadAttachmentPort;
private final CorrelateMessagePort correlateMessagePort;
private final String fromAddress;
private final MailPort mailPort;

/**
* Send a mail.
Expand All @@ -41,50 +36,23 @@ public class SendMailUseCase implements SendMail {
*/
@Override
public void sendMail(final String processInstanceIde, final String messageName, @Valid final Mail mail) throws BpmnError {
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();

try {
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getReceivers()));

if (StringUtils.isNotEmpty(mail.getReceiversCc())) {
mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mail.getReceiversCc()));
}
if (StringUtils.isNotEmpty(mail.getReceiversBcc())) {
mimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mail.getReceiversBcc()));
}

final var helper = new MimeMessageHelper(mimeMessage, true);

helper.setSubject(mail.getSubject());
helper.setText(mail.getBody());
helper.setFrom(this.fromAddress);

if (StringUtils.isNotBlank(mail.getReplyTo())) {
helper.setReplyTo(mail.getReplyTo());
}

// mail attachments
// load Attachments
final List<FileAttachment> attachments = new ArrayList<>();
if (CollectionUtils.isNotEmpty(mail.getAttachments())) {
for (val attachment : mail.getAttachments()) {
final FileAttachment mailAttachment = this.loadAttachmentPort.loadAttachment(attachment);
helper.addAttachment(mailAttachment.getFileName(), mailAttachment.getFile());
attachments.add(this.loadAttachmentPort.loadAttachment(attachment));
}
}
// send mail
this.mailPort.sendMail(mail.getReceivers(), mail.getSubject(), mail.getBody(), mail.getReplyTo(), mail.getReceiversCc(), mail.getReceiversBcc(), attachments);
// correlate message
final Map<String, Object> correlatePayload = new HashMap<>();
correlatePayload.put("mailSentStatus", true);
this.correlateMessagePort.correlateMessage(processInstanceIde, messageName, correlatePayload);
} catch (final MessagingException ex) {
log.error(ex.getMessage());
throw new BpmnError("MESSAGING_EXCEPTION", ex.getMessage());
}

try {
this.mailSender.send(mimeMessage);
log.info("Mail sent to {}.", mail.getReceivers());
} catch (final MailException ex) {
log.error("Sending mail failed with exception: {}", ex.getMessage());
throw new BpmnError("MAIL_SENDING_FAILED", ex.getMessage());
}

final Map<String, Object> correlatePayload = new HashMap<>();
correlatePayload.put("mailSentStatus", true);
this.correlateMessagePort.correlateMessage(processInstanceIde, messageName, correlatePayload);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.muenchen.oss.digiwf.email.integration.adapter.out;

import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi;
import jakarta.mail.MessagingException;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

class MailAdapterTest {

private final DigiwfEmailApi digiwfEmailApi = mock(DigiwfEmailApi.class);

@Test
void sendMail() throws MessagingException {
final MailAdapter mailAdapter = new MailAdapter(digiwfEmailApi);
mailAdapter.sendMail("receivers", "subject", "body", "replyTo", "receiversCc", "receiversBcc", null);
verify(digiwfEmailApi).sendMailWithAttachments("receivers", "subject", "body", "replyTo", "receiversCc", "receiversBcc", null);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.muenchen.oss.digiwf.email.integration.adapter.out;

import de.muenchen.oss.digiwf.email.integration.model.FileAttachment;
import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageClientErrorException;
import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
package de.muenchen.oss.digiwf.email.integration.application.usecase;

import de.muenchen.oss.digiwf.email.integration.model.FileAttachment;
import de.muenchen.oss.digiwf.email.integration.adapter.out.ProcessAdapter;
import de.muenchen.oss.digiwf.email.integration.application.port.in.SendMail;
import de.muenchen.oss.digiwf.email.integration.application.port.out.CorrelateMessagePort;
import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort;
import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort;
import de.muenchen.oss.digiwf.email.integration.model.Mail;
import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl;
import de.muenchen.oss.digiwf.message.core.api.MessageApi;
import de.muenchen.oss.digiwf.message.process.api.ProcessApi;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import de.muenchen.oss.digiwf.message.process.api.error.BpmnError;
import de.muenchen.oss.digiwf.message.process.impl.ProcessApiImpl;
import org.junit.jupiter.api.Assertions;
import jakarta.mail.MessagingException;
import jakarta.mail.util.ByteArrayDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;

import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.util.ByteArrayDataSource;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

class SendMailUseCaseTest {

private final MessageApi messageApi = Mockito.spy(Mockito.mock(MessageApi.class));

private final JavaMailSender javaMailSender = mock(JavaMailSender.class);
private final LoadMailAttachmentPort loadMailAttachmentPort = mock(LoadMailAttachmentPort.class);
private final CorrelateMessagePort correlateMessagePort = mock(CorrelateMessagePort.class);
private final MailPort mailPort = mock(MailPort.class);

private final ProcessApi processApi = new ProcessApiImpl(
this.messageApi,
"correlateMessageDestination",
"startProcessDestination"
);

private final CorrelateMessagePort correlateMessagePort = new ProcessAdapter(processApi);
private final String fromAddress = "[email protected]";
private SendMail sendMail;

private final Mail mail = new Mail(
"[email protected],[email protected]",
Expand All @@ -52,57 +36,51 @@ class SendMailUseCaseTest {
"[email protected]",
null
);
private final String processInstanceId = "processInstanceId";
private final String messageName = "messageName";

@BeforeEach
void setUp() {
String anhangInhalt = "Anhang Inhalt";
byte[] anhangByt = anhangInhalt.getBytes();
when(this.javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null));
when(this.loadMailAttachmentPort.loadAttachment(any())).thenReturn(new FileAttachment("Testanhang", new ByteArrayDataSource(anhangByt, "AttName")));
this.sendMail = new SendMailUseCase(loadMailAttachmentPort, correlateMessagePort, mailPort);
}

@Test
void sendMail() throws MessagingException {

final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, fromAddress);

sendMailUseCase.sendMail("processInstanceIde", "messageName", this.mail);

final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class);

verify(this.javaMailSender).send(messageArgumentCaptor.capture());
Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length);
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length);
Assertions.assertEquals("Test Mail", messageArgumentCaptor.getValue().getSubject());
sendMail.sendMail(processInstanceId, messageName, mail);
verify(mailPort).sendMail(
mail.getReceivers(),
mail.getSubject(),
mail.getBody(),
mail.getReplyTo(),
mail.getReceiversCc(),
mail.getReceiversBcc(),
List.of());
verify(correlateMessagePort).correlateMessage(processInstanceId, messageName, Map.of("mailSentStatus", true));
}

@Test
void sendMailWithAttachments() throws MessagingException {
final Mail mailWithAttachments = this.mail;
mailWithAttachments.setAttachments(List.of(new PresignedUrl("http://localhost:9000/some-url", "test.txt", "GET")));
final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, this.fromAddress);
sendMailUseCase.sendMail("processInstanceIde", "messageName", mailWithAttachments);

final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class);
verify(this.javaMailSender).send(messageArgumentCaptor.capture());
Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length);
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length);
Assertions.assertEquals("Test Mail", messageArgumentCaptor.getValue().getSubject());
// attachment
verify(this.loadMailAttachmentPort, times(1)).loadAttachment(any());
final FileAttachment result = this.loadMailAttachmentPort.loadAttachment(any());
Assertions.assertEquals("Testanhang", result.getFileName());
final PresignedUrl presignedUrl = new PresignedUrl("http://localhost:9000/some-url", "test.txt", "GET");
mail.setAttachments(List.of(presignedUrl));

final FileAttachment fileAttachment = new FileAttachment("test.txt", new ByteArrayDataSource("Anhang Inhalt".getBytes(), "text/plain"));
when(loadMailAttachmentPort.loadAttachment(presignedUrl)).thenReturn(fileAttachment);

sendMail.sendMail(processInstanceId, messageName, mail);
verify(mailPort).sendMail(
mail.getReceivers(),
mail.getSubject(),
mail.getBody(),
mail.getReplyTo(),
mail.getReceiversCc(),
mail.getReceiversBcc(),
List.of(fileAttachment));
verify(correlateMessagePort).correlateMessage(processInstanceId, messageName, Map.of("mailSentStatus", true));
}

@Test
void testThatABpmnErrorIsThrowIfSendMailFailsWithAMailException() {
doThrow(mock(MailException.class)).when(this.javaMailSender).send(any(MimeMessage.class));
final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, this.fromAddress);

Assertions.assertThrows(BpmnError.class, () -> {
sendMailUseCase.sendMail("processInstanceIde", "messageName", this.mail);
});
void sendMailThrowsBpmnError() throws MessagingException {
doThrow(new MessagingException("Test Exception")).when(mailPort).sendMail(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any());
assertThrows(BpmnError.class, () -> sendMail.sendMail(processInstanceId, messageName, mail));
}


}

This file was deleted.

Loading

0 comments on commit 99d919c

Please sign in to comment.