generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(#1023): use digiwf-email in email-integration
- Loading branch information
Showing
14 changed files
with
169 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
...on-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3Adapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...de/muenchen/oss/digiwf/email/integration/application/port/out/LoadMailAttachmentPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/MailPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
...ion-core/src/main/java/de/muenchen/oss/digiwf/email/integration/model/FileAttachment.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
...e/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ore/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3AdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]", | ||
|
@@ -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)); | ||
} | ||
|
||
|
||
} |
18 changes: 0 additions & 18 deletions
18
...ain/java/de/muenchen/oss/digiwf/email/integration/configuration/CustomMailProperties.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.