Skip to content

Commit

Permalink
feature(#1023): init digiwf-email lib with the functionality of the d…
Browse files Browse the repository at this point in the history
…igiwf-email-integration-core
  • Loading branch information
lmoesle authored and Lukas Mösle committed Nov 30, 2023
1 parent c55e31f commit fa31195
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 0 deletions.
5 changes: 5 additions & 0 deletions digiwf-coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@
<artifactId>digiwf-dms-integration-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-email-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- digiwf-libs -->
<dependency>
Expand Down
42 changes: 42 additions & 0 deletions digiwf-libs/digiwf-email/digiwf-email-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-email</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<artifactId>digiwf-email-api</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.muenchen.oss.digiwf.email.api;

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

import java.util.List;

public interface DigiwfEmailApi {


void sendMail(String receivers, String subject, String body, String replyTo) throws MessagingException;

void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc) throws MessagingException;

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

void sendMailWithAttachments(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
@@ -0,0 +1,74 @@
package de.muenchen.oss.digiwf.email.impl;

import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class DigiwfEmailApiImpl implements DigiwfEmailApi {

private final JavaMailSender mailSender;
private final String fromAddress;

@Override
public void sendMail(String receivers, String subject, String body, String replyTo) throws MessagingException {
this.sendMailWithAttachments(receivers, subject, body, replyTo, null, null, List.of());
}

@Override
public void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc) throws MessagingException {
this.sendMailWithAttachments(receivers, subject, body, replyTo, receiversCc, receiversBcc, List.of());
}

@Override
public void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, List<FileAttachment> attachments) throws MessagingException {
this.sendMailWithAttachments(receivers, subject, body, replyTo, null, null, attachments);
}

@Override
public void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List<FileAttachment> attachments) throws MessagingException {
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();

mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receivers));

if (StringUtils.isNotEmpty(receiversCc)) {
mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(receiversCc));
}
if (StringUtils.isNotEmpty(receiversCc)) {
mimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(receiversBcc));
}

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

helper.setSubject(subject);
helper.setText(subject);
helper.setFrom(this.fromAddress);

if (StringUtils.isNotBlank(replyTo)) {
helper.setReplyTo(replyTo);
}

// mail attachments
if (attachments != null) {
for (val attachment : attachments) {
helper.addAttachment(attachment.getFileName(), attachment.getFile());
}
}

this.mailSender.send(mimeMessage);
log.info("Mail {} sent to {}.", subject, receivers);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.muenchen.oss.digiwf.email.model;

import jakarta.mail.util.ByteArrayDataSource;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class FileAttachment {

private String fileName;

private ByteArrayDataSource file;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package de.muenchen.oss.digiwf.email;

import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi;
import de.muenchen.oss.digiwf.email.impl.DigiwfEmailApiImpl;
import de.muenchen.oss.digiwf.email.model.FileAttachment;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.util.ByteArrayDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.List;

import static org.mockito.Mockito.*;

class DigiwfEmailApiImplTest {


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

// test data
private final String receiver = "[email protected],[email protected]";
private final String receiverCC = "[email protected]";
private final String receiverBCC = "[email protected]";
private final String subject = "Test Mail";
private final String body = "This is a test mail";
private final String replyTo = "[email protected]";


@BeforeEach
void setUp() {
when(this.javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null));
this.digiwfEmailApi = new DigiwfEmailApiImpl(this.javaMailSender, "[email protected]");
}

@Test
void sendSimpleMail() throws MessagingException {
this.digiwfEmailApi.sendMail(this.receiver, this.subject, this.body, this.replyTo);

final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class);
verify(this.javaMailSender).send(messageArgumentCaptor.capture());

Assertions.assertEquals(2, messageArgumentCaptor.getValue().getAllRecipients().length);
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length);
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject());
}

@Test
void sendMailWithCCAndBCC() throws MessagingException {
this.digiwfEmailApi.sendMail(this.receiver, this.subject, this.body, this.replyTo, this.receiverCC, this.receiverBCC);

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(this.subject, messageArgumentCaptor.getValue().getSubject());
}

@Test
void sendMailWithAttachments() throws MessagingException {
final List<FileAttachment> fileAttachments = List.of(
new FileAttachment("Testanhang", new ByteArrayDataSource("FooBar".getBytes(), "text/plain"))
);
this.digiwfEmailApi.sendMailWithAttachments(this.receiver, this.subject, this.body, this.replyTo, fileAttachments);

final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class);
verify(this.javaMailSender).send(messageArgumentCaptor.capture());

Assertions.assertEquals(2, messageArgumentCaptor.getValue().getAllRecipients().length);
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length);
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject());
}

@Test
void sendMailWithReceiversCCAndBCCAndAttachments() throws MessagingException {
final List<FileAttachment> fileAttachments = List.of(
new FileAttachment("Testanhang", new ByteArrayDataSource("FooBar".getBytes(), "text/plain"))
);
this.digiwfEmailApi.sendMailWithAttachments(this.receiver, this.subject, this.body, this.replyTo, this.receiverCC, this.receiverBCC, fileAttachments);

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(this.subject, messageArgumentCaptor.getValue().getSubject());
}

}
31 changes: 31 additions & 0 deletions digiwf-libs/digiwf-email/digiwf-email-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-email</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<artifactId>digiwf-email-starter</artifactId>

<dependencies>
<dependency>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-email-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.muenchen.oss.digiwf.email.configuration;

import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi;
import de.muenchen.oss.digiwf.email.impl.DigiwfEmailApiImpl;
import de.muenchen.oss.digiwf.email.properties.CustomMailProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;

@RequiredArgsConstructor
@EnableConfigurationProperties(value = CustomMailProperties.class)
public class DigiwfEmailAutoConfiguration {

private final CustomMailProperties customMailProperties;

@ConditionalOnMissingBean
@Bean
public DigiwfEmailApi digiwfEmailApi(final JavaMailSender javaMailSender) {
return new DigiwfEmailApiImpl(javaMailSender, this.customMailProperties.getFromAddress());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.muenchen.oss.digiwf.email.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "io.muenchendigital.digiwf.mail")
public class CustomMailProperties {

/**
* Sender mail address.
*/
private String fromAddress;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de.muenchen.oss.digiwf.email.configuration.DigiwfEmailAutoConfiguration
20 changes: 20 additions & 0 deletions digiwf-libs/digiwf-email/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muenchen.oss.digiwf</groupId>
<artifactId>digiwf-libs</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<artifactId>digiwf-email</artifactId>
<packaging>pom</packaging>

<modules>
<module>digiwf-email-api</module>
<module>digiwf-email-starter</module>
</modules>

</project>
1 change: 1 addition & 0 deletions digiwf-libs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>digiwf-message</module>
<module>digiwf-spring-security</module>
<module>digiwf-spring-logging-and-tracing</module>
<module>digiwf-email</module>
</modules>

<properties>
Expand Down

0 comments on commit fa31195

Please sign in to comment.