2852 - Simplify java mail code (#2874)
* testing * [2852] adding SimpleJavaEmail * [2852] refactor email sender code * [2852] dependency cleanup * [2852] add missing dependency * [2852] add missing spring framework test dependency * [2852] add change log * [2852] add change log * Update hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_5_0/2852-simplify-java-mail-code.yaml Co-authored-by: Tadgh <tadgh@cs.toronto.edu> * [2852] minor test code cleanup * [2852] add more tests Co-authored-by: Tadgh <tadgh@cs.toronto.edu>
This commit is contained in:
parent
a7f0929405
commit
1e0f843d6c
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
type: add
|
||||
issue: 2852
|
||||
title: "Replace existing email implementation code with SimpleJavaMail library."
|
|
@ -410,22 +410,6 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>javax.mail-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
|
@ -533,25 +517,19 @@
|
|||
<artifactId>spring-boot-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail-spring</artifactId>
|
||||
<artifactId>greenmail-junit5</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package ca.uhn.fhir.jpa.config;
|
||||
|
||||
import ca.uhn.fhir.jpa.search.HapiLuceneAnalysisConfigurer;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailSenderImpl;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.IEmailSender;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.JavaMailEmailSender;
|
||||
import ca.uhn.fhir.jpa.util.CircularQueueCaptureQueriesListener;
|
||||
import ca.uhn.fhir.jpa.util.CurrentThreadCaptureQueriesListener;
|
||||
import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor;
|
||||
import ca.uhn.fhir.rest.server.mail.MailConfig;
|
||||
import ca.uhn.fhir.validation.ResultSeverityEnum;
|
||||
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
@ -132,10 +133,7 @@ public class TestDstu3Config extends BaseJavaConfigDstu3 {
|
|||
|
||||
@Bean
|
||||
public IEmailSender emailSender() {
|
||||
JavaMailEmailSender retVal = new JavaMailEmailSender();
|
||||
retVal.setSmtpServerHostname("localhost");
|
||||
retVal.setSmtpServerPort(3025);
|
||||
return retVal;
|
||||
return new EmailSenderImpl(new MailConfig().setSmtpHostname("localhost").setSmtpPort(3025));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -702,26 +702,6 @@ public abstract class BaseJpaTest extends BaseTest {
|
|||
|
||||
}
|
||||
|
||||
public static void waitForSize(int theTarget, Callable<Number> theCallable) throws Exception {
|
||||
waitForSize(theTarget, 10000, theCallable);
|
||||
}
|
||||
|
||||
@SuppressWarnings("BusyWait")
|
||||
public static void waitForSize(int theTarget, int theTimeout, Callable<Number> theCallable) throws Exception {
|
||||
StopWatch sw = new StopWatch();
|
||||
while (theCallable.call().intValue() != theTarget && sw.getMillis() < theTimeout) {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException theE) {
|
||||
throw new Error(theE);
|
||||
}
|
||||
}
|
||||
if (sw.getMillis() >= theTimeout) {
|
||||
fail("Size " + theCallable.call() + " is != target " + theTarget);
|
||||
}
|
||||
Thread.sleep(500);
|
||||
}
|
||||
|
||||
public static void waitForSize(int theTarget, Callable<Number> theCallable, Callable<String> theFailureMessage) throws Exception {
|
||||
waitForSize(theTarget, 10000, theCallable, theFailureMessage);
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@ package ca.uhn.fhir.jpa.subscription;
|
|||
import ca.uhn.fhir.jpa.api.config.DaoConfig;
|
||||
import ca.uhn.fhir.jpa.cache.IResourceChangeListenerCacheRefresher;
|
||||
import ca.uhn.fhir.jpa.subscription.channel.impl.LinkedBlockingChannel;
|
||||
import ca.uhn.fhir.jpa.subscription.submit.interceptor.SubscriptionSubmitInterceptorLoader;
|
||||
import ca.uhn.fhir.jpa.subscription.match.registry.ActiveSubscription;
|
||||
import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionRegistry;
|
||||
import ca.uhn.fhir.jpa.subscription.channel.subscription.SubscriptionChannelRegistry;
|
||||
import ca.uhn.fhir.jpa.subscription.channel.subscription.SubscriptionChannelWithHandlers;
|
||||
import ca.uhn.fhir.jpa.subscription.submit.interceptor.SubscriptionMatcherInterceptor;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.JavaMailEmailSender;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailSenderImpl;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.SubscriptionDeliveringEmailSubscriber;
|
||||
import ca.uhn.fhir.jpa.subscription.match.registry.ActiveSubscription;
|
||||
import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionRegistry;
|
||||
import ca.uhn.fhir.jpa.subscription.submit.interceptor.SubscriptionMatcherInterceptor;
|
||||
import ca.uhn.fhir.jpa.subscription.submit.interceptor.SubscriptionSubmitInterceptorLoader;
|
||||
import org.hl7.fhir.dstu2.model.Subscription;
|
||||
import org.hl7.fhir.instance.model.api.IIdType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -18,8 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
public class SubscriptionTestUtil {
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SubscriptionTestUtil.class);
|
||||
|
||||
private JavaMailEmailSender myEmailSender;
|
||||
|
||||
@Autowired
|
||||
private DaoConfig myDaoConfig;
|
||||
@Autowired
|
||||
|
@ -77,21 +75,15 @@ public class SubscriptionTestUtil {
|
|||
return getExecutorQueueSize();
|
||||
}
|
||||
|
||||
public void initEmailSender(int theListenerPort) {
|
||||
myEmailSender = new JavaMailEmailSender();
|
||||
myEmailSender.setSmtpServerHostname("localhost");
|
||||
myEmailSender.setSmtpServerPort(theListenerPort);
|
||||
myEmailSender.start();
|
||||
}
|
||||
|
||||
public void setEmailSender(IIdType theIdElement) {
|
||||
public void setEmailSender(IIdType theIdElement, EmailSenderImpl theEmailSender) {
|
||||
ActiveSubscription activeSubscription = mySubscriptionRegistry.get(theIdElement.getIdPart());
|
||||
SubscriptionChannelWithHandlers subscriptionChannelWithHandlers = mySubscriptionChannelRegistry.getDeliveryReceiverChannel(activeSubscription.getChannelName());
|
||||
SubscriptionDeliveringEmailSubscriber subscriber = (SubscriptionDeliveringEmailSubscriber) subscriptionChannelWithHandlers.getDeliveryHandlerForUnitTest();
|
||||
subscriber.setEmailSender(myEmailSender);
|
||||
subscriber.setEmailSender(theEmailSender);
|
||||
}
|
||||
|
||||
public int getActiveSubscriptionCount() {
|
||||
return mySubscriptionRegistry.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
package ca.uhn.fhir.jpa.subscription.email;
|
||||
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailDetails;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailSenderImpl;
|
||||
import ca.uhn.fhir.rest.server.mail.MailConfig;
|
||||
import com.icegreen.greenmail.junit5.GreenMailExtension;
|
||||
import com.icegreen.greenmail.util.GreenMailUtil;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
import org.hl7.fhir.dstu3.model.IdType;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EmailSenderImplTest {
|
||||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(EmailSenderImplTest.class);
|
||||
|
||||
@RegisterExtension
|
||||
static GreenMailExtension ourGreenMail = new GreenMailExtension(ServerSetupTest.SMTP);
|
||||
|
||||
private EmailSenderImpl fixture;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
fixture = new EmailSenderImpl(withMailConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSend() throws Exception {
|
||||
EmailDetails details = new EmailDetails();
|
||||
details.setSubscription(new IdType("Subscription/123"));
|
||||
details.setFrom("foo@example.com ");
|
||||
details.setTo(Arrays.asList(" to1@example.com", "to2@example.com "));
|
||||
details.setSubjectTemplate("test subject");
|
||||
details.setBodyTemplate("foo");
|
||||
fixture.send(details);
|
||||
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
|
||||
MimeMessage[] messages = ourGreenMail.getReceivedMessages();
|
||||
assertEquals(2, messages.length);
|
||||
final MimeMessage message = messages[0];
|
||||
ourLog.info("Received: " + GreenMailUtil.getWholeMessage(message));
|
||||
assertEquals("test subject", message.getSubject());
|
||||
assertEquals(1, message.getFrom().length);
|
||||
assertEquals("foo@example.com", ((InternetAddress) message.getFrom()[0]).getAddress());
|
||||
assertEquals(2, message.getAllRecipients().length);
|
||||
assertEquals("to1@example.com", ((InternetAddress) message.getAllRecipients()[0]).getAddress());
|
||||
assertEquals("to2@example.com", ((InternetAddress) message.getAllRecipients()[1]).getAddress());
|
||||
assertEquals(1, message.getHeader("Content-Type").length);
|
||||
assertEquals("text/plain; charset=UTF-8", message.getHeader("Content-Type")[0]);
|
||||
String foundBody = GreenMailUtil.getBody(message);
|
||||
assertEquals("foo", foundBody);
|
||||
}
|
||||
|
||||
private MailConfig withMailConfig() {
|
||||
return new MailConfig()
|
||||
.setSmtpHostname(ServerSetupTest.SMTP.getBindAddress())
|
||||
.setSmtpPort(ServerSetupTest.SMTP.getPort());
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@ package ca.uhn.fhir.jpa.subscription.email;
|
|||
|
||||
import ca.uhn.fhir.jpa.provider.BaseResourceProviderDstu2Test;
|
||||
import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil;
|
||||
import ca.uhn.fhir.jpa.subscription.match.registry.ActiveSubscription;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailSenderImpl;
|
||||
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
|
||||
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Observation;
|
||||
|
@ -11,15 +11,16 @@ import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum;
|
|||
import ca.uhn.fhir.model.dstu2.valueset.SubscriptionChannelTypeEnum;
|
||||
import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
|
||||
import ca.uhn.fhir.rest.api.MethodOutcome;
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
import ca.uhn.fhir.rest.server.mail.MailConfig;
|
||||
import com.icegreen.greenmail.junit5.GreenMailExtension;
|
||||
import com.icegreen.greenmail.util.GreenMailUtil;
|
||||
import com.icegreen.greenmail.util.ServerSetup;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.hl7.fhir.instance.model.api.IIdType;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -29,16 +30,19 @@ import javax.mail.internet.MimeMessage;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static ca.uhn.fhir.jpa.subscription.resthook.RestHookTestDstu3Test.logAllInterceptors;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EmailSubscriptionDstu2Test extends BaseResourceProviderDstu2Test {
|
||||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(EmailSubscriptionDstu2Test.class);
|
||||
private static GreenMail ourTestSmtp;
|
||||
private static int ourListenerPort;
|
||||
|
||||
@RegisterExtension
|
||||
static GreenMailExtension ourGreenMail = new GreenMailExtension(ServerSetupTest.SMTP);
|
||||
|
||||
private List<IIdType> mySubscriptionIds = new ArrayList<>();
|
||||
|
||||
@Autowired
|
||||
|
@ -65,8 +69,6 @@ public class EmailSubscriptionDstu2Test extends BaseResourceProviderDstu2Test {
|
|||
ourLog.info("Before re-registering interceptors");
|
||||
logAllInterceptors(myInterceptorRegistry);
|
||||
|
||||
mySubscriptionTestUtil.initEmailSender(ourListenerPort);
|
||||
|
||||
mySubscriptionTestUtil.registerEmailInterceptor();
|
||||
|
||||
ourLog.info("After re-registering interceptors");
|
||||
|
@ -121,51 +123,32 @@ public class EmailSubscriptionDstu2Test extends BaseResourceProviderDstu2Test {
|
|||
Subscription subscription1 = createSubscription(criteria1, payload, "to1@example.com,to2@example.com");
|
||||
mySubscriptionTestUtil.waitForQueueToDrain();
|
||||
await().until(()->mySubscriptionRegistry.get(subscription1.getIdElement().getIdPart()), Matchers.not(Matchers.nullValue()));
|
||||
mySubscriptionTestUtil.setEmailSender(subscription1.getIdElement());
|
||||
assertEquals(0, Arrays.asList(ourTestSmtp.getReceivedMessages()).size());
|
||||
mySubscriptionTestUtil.setEmailSender(subscription1.getIdElement(), new EmailSenderImpl(withMailConfig()));
|
||||
assertEquals(0, Arrays.asList(ourGreenMail.getReceivedMessages()).size());
|
||||
|
||||
Observation observation1 = sendObservation(code, "SNOMED-CT");
|
||||
|
||||
waitForSize(2, 60000, new Callable<Number>() {
|
||||
@Override
|
||||
public Number call() {
|
||||
int length = ourTestSmtp.getReceivedMessages().length;
|
||||
ourLog.trace("Have received {}", length);
|
||||
return length;
|
||||
}
|
||||
});
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
|
||||
MimeMessage[] messages = ourTestSmtp.getReceivedMessages();
|
||||
MimeMessage[] messages = ourGreenMail.getReceivedMessages();
|
||||
assertEquals(2, messages.length);
|
||||
int msgIdx = 0;
|
||||
ourLog.info("Received: " + GreenMailUtil.getWholeMessage(messages[msgIdx]));
|
||||
assertEquals("HAPI FHIR Subscriptions", messages[msgIdx].getSubject());
|
||||
assertEquals(1, messages[msgIdx].getFrom().length);
|
||||
assertEquals("noreply@unknown.com", ((InternetAddress) messages[msgIdx].getFrom()[0]).getAddress());
|
||||
assertEquals(2, messages[msgIdx].getAllRecipients().length);
|
||||
assertEquals("to1@example.com", ((InternetAddress) messages[msgIdx].getAllRecipients()[0]).getAddress());
|
||||
assertEquals("to2@example.com", ((InternetAddress) messages[msgIdx].getAllRecipients()[1]).getAddress());
|
||||
assertEquals(1, messages[msgIdx].getHeader("Content-Type").length);
|
||||
assertEquals("text/plain; charset=us-ascii", messages[msgIdx].getHeader("Content-Type")[0]);
|
||||
String foundBody = GreenMailUtil.getBody(messages[msgIdx]);
|
||||
ourLog.info("Received: " + GreenMailUtil.getWholeMessage(messages[0]));
|
||||
assertEquals("HAPI FHIR Subscriptions", messages[0].getSubject());
|
||||
assertEquals(1, messages[0].getFrom().length);
|
||||
assertEquals("noreply@unknown.com", ((InternetAddress) messages[0].getFrom()[0]).getAddress());
|
||||
assertEquals(2, messages[0].getAllRecipients().length);
|
||||
assertEquals("to1@example.com", ((InternetAddress) messages[0].getAllRecipients()[0]).getAddress());
|
||||
assertEquals("to2@example.com", ((InternetAddress) messages[0].getAllRecipients()[1]).getAddress());
|
||||
assertEquals(1, messages[0].getHeader("Content-Type").length);
|
||||
assertEquals("text/plain; charset=UTF-8", messages[0].getHeader("Content-Type")[0]);
|
||||
String foundBody = GreenMailUtil.getBody(messages[0]);
|
||||
assertEquals("", foundBody);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
ourTestSmtp.stop();
|
||||
private MailConfig withMailConfig() {
|
||||
return new MailConfig()
|
||||
.setSmtpHostname(ServerSetupTest.SMTP.getBindAddress())
|
||||
.setSmtpPort(ServerSetupTest.SMTP.getPort());
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP);
|
||||
smtp.setServerStartupTimeout(2000);
|
||||
ourTestSmtp = new GreenMail(smtp);
|
||||
ourTestSmtp.start();
|
||||
ourListenerPort = ourTestSmtp.getSmtp().getPort();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,13 @@ package ca.uhn.fhir.jpa.subscription.email;
|
|||
import ca.uhn.fhir.jpa.api.config.DaoConfig;
|
||||
import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test;
|
||||
import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailSenderImpl;
|
||||
import ca.uhn.fhir.rest.api.MethodOutcome;
|
||||
import ca.uhn.fhir.rest.server.mail.MailConfig;
|
||||
import ca.uhn.fhir.util.HapiExtensions;
|
||||
import com.icegreen.greenmail.junit5.GreenMailExtension;
|
||||
import com.icegreen.greenmail.store.FolderException;
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
import com.icegreen.greenmail.util.ServerSetup;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
import org.hl7.fhir.dstu3.model.Bundle;
|
||||
import org.hl7.fhir.dstu3.model.CodeableConcept;
|
||||
import org.hl7.fhir.dstu3.model.Coding;
|
||||
|
@ -15,11 +17,10 @@ import org.hl7.fhir.dstu3.model.Observation;
|
|||
import org.hl7.fhir.dstu3.model.StringType;
|
||||
import org.hl7.fhir.dstu3.model.Subscription;
|
||||
import org.hl7.fhir.instance.model.api.IIdType;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
@ -27,11 +28,11 @@ import javax.mail.internet.MimeMessage;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static ca.uhn.fhir.jpa.subscription.resthook.RestHookTestDstu3Test.logAllInterceptors;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Test the rest-hook subscriptions
|
||||
|
@ -40,11 +41,12 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(EmailSubscriptionDstu3Test.class);
|
||||
|
||||
@RegisterExtension
|
||||
static GreenMailExtension ourGreenMail = new GreenMailExtension(ServerSetupTest.SMTP);
|
||||
|
||||
@Autowired
|
||||
private SubscriptionTestUtil mySubscriptionTestUtil;
|
||||
|
||||
private static int ourListenerPort;
|
||||
private static GreenMail ourTestSmtp;
|
||||
private List<IIdType> mySubscriptionIds = new ArrayList<>();
|
||||
|
||||
@AfterEach
|
||||
|
@ -68,7 +70,7 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
|
||||
@BeforeEach
|
||||
public void beforeRegisterEmailListener() throws FolderException {
|
||||
ourTestSmtp.purgeEmailFromAllMailboxes();
|
||||
ourGreenMail.purgeEmailFromAllMailboxes();
|
||||
|
||||
ourLog.info("Before re-registering interceptors");
|
||||
logAllInterceptors(myInterceptorRegistry);
|
||||
|
@ -78,8 +80,6 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
ourLog.info("After re-registering interceptors");
|
||||
logAllInterceptors(myInterceptorRegistry);
|
||||
|
||||
mySubscriptionTestUtil.initEmailSender(ourListenerPort);
|
||||
|
||||
myDaoConfig.setEmailFromAddress("123@hapifhir.io");
|
||||
}
|
||||
|
||||
|
@ -133,24 +133,19 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml";
|
||||
Subscription subscription = createSubscription(criteria1, payload);
|
||||
waitForQueueToDrain();
|
||||
mySubscriptionTestUtil.setEmailSender(subscription.getIdElement());
|
||||
mySubscriptionTestUtil.setEmailSender(subscription.getIdElement(), new EmailSenderImpl(withMailConfig()));
|
||||
|
||||
sendObservation(code, "SNOMED-CT");
|
||||
waitForQueueToDrain();
|
||||
|
||||
waitForSize(1, 60000, new Callable<Number>() {
|
||||
@Override
|
||||
public Number call() {
|
||||
return ourTestSmtp.getReceivedMessages().length;
|
||||
}
|
||||
});
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
|
||||
List<MimeMessage> received = Arrays.asList(ourTestSmtp.getReceivedMessages());
|
||||
List<MimeMessage> received = Arrays.asList(ourGreenMail.getReceivedMessages());
|
||||
assertEquals(1, received.get(0).getFrom().length);
|
||||
assertEquals("123@hapifhir.io", ((InternetAddress) received.get(0).getFrom()[0]).getAddress());
|
||||
assertEquals(1, received.get(0).getAllRecipients().length);
|
||||
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
|
||||
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
|
||||
assertEquals("text/plain; charset=UTF-8", received.get(0).getContentType());
|
||||
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
|
||||
|
||||
// Expect the body of the email subscription to be an Observation formatted as XML
|
||||
|
@ -185,25 +180,20 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
|
||||
ourClient.update().resource(subscriptionTemp).withId(subscriptionTemp.getIdElement()).execute();
|
||||
waitForQueueToDrain();
|
||||
mySubscriptionTestUtil.setEmailSender(subscriptionTemp.getIdElement());
|
||||
mySubscriptionTestUtil.setEmailSender(subscriptionTemp.getIdElement(), new EmailSenderImpl(withMailConfig()));
|
||||
|
||||
sendObservation(code, "SNOMED-CT");
|
||||
waitForQueueToDrain();
|
||||
|
||||
waitForSize(1, 60000, new Callable<Number>() {
|
||||
@Override
|
||||
public Number call() throws Exception {
|
||||
return ourTestSmtp.getReceivedMessages().length;
|
||||
}
|
||||
});
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
|
||||
List<MimeMessage> received = Arrays.asList(ourTestSmtp.getReceivedMessages());
|
||||
List<MimeMessage> received = Arrays.asList(ourGreenMail.getReceivedMessages());
|
||||
assertEquals(1, received.size());
|
||||
assertEquals(1, received.get(0).getFrom().length);
|
||||
assertEquals("myfrom@from.com", ((InternetAddress) received.get(0).getFrom()[0]).getAddress());
|
||||
assertEquals(1, received.get(0).getAllRecipients().length);
|
||||
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
|
||||
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
|
||||
assertEquals("text/plain; charset=UTF-8", received.get(0).getContentType());
|
||||
assertEquals("This is a subject", received.get(0).getSubject().toString().trim());
|
||||
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
|
||||
|
||||
|
@ -239,25 +229,20 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
ourLog.info("Subscription ID is: {}", id.getValue());
|
||||
|
||||
waitForQueueToDrain();
|
||||
mySubscriptionTestUtil.setEmailSender(subscriptionTemp.getIdElement());
|
||||
mySubscriptionTestUtil.setEmailSender(subscriptionTemp.getIdElement(), new EmailSenderImpl(withMailConfig()));
|
||||
|
||||
sendObservation(code, "SNOMED-CT");
|
||||
waitForQueueToDrain();
|
||||
|
||||
waitForSize(1, 60000, new Callable<Number>() {
|
||||
@Override
|
||||
public Number call() throws Exception {
|
||||
return ourTestSmtp.getReceivedMessages().length;
|
||||
}
|
||||
});
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
|
||||
List<MimeMessage> received = Arrays.asList(ourTestSmtp.getReceivedMessages());
|
||||
List<MimeMessage> received = Arrays.asList(ourGreenMail.getReceivedMessages());
|
||||
assertEquals(1, received.size());
|
||||
assertEquals(1, received.get(0).getFrom().length);
|
||||
assertEquals("myfrom@from.com", ((InternetAddress) received.get(0).getFrom()[0]).getAddress());
|
||||
assertEquals(1, received.get(0).getAllRecipients().length);
|
||||
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
|
||||
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
|
||||
assertEquals("text/plain; charset=UTF-8", received.get(0).getContentType());
|
||||
assertEquals("This is a subject", received.get(0).getSubject().toString().trim());
|
||||
assertEquals("", received.get(0).getContent().toString().trim());
|
||||
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
|
||||
|
@ -273,20 +258,11 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
mySubscriptionTestUtil.waitForQueueToDrain();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
ourTestSmtp.stop();
|
||||
private MailConfig withMailConfig() {
|
||||
return new MailConfig()
|
||||
.setSmtpHostname(ServerSetupTest.SMTP.getBindAddress())
|
||||
.setSmtpPort(ServerSetupTest.SMTP.getPort());
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP);
|
||||
smtp.setServerStartupTimeout(2000);
|
||||
smtp.setReadTimeout(2000);
|
||||
smtp.setConnectionTimeout(2000);
|
||||
ourTestSmtp = new GreenMail(smtp);
|
||||
ourTestSmtp.start();
|
||||
ourListenerPort = ourTestSmtp.getSmtp().getPort();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
package ca.uhn.fhir.jpa.subscription.email;
|
||||
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.EmailDetails;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.email.JavaMailEmailSender;
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
import com.icegreen.greenmail.util.GreenMailUtil;
|
||||
import com.icegreen.greenmail.util.ServerSetup;
|
||||
import org.hl7.fhir.dstu3.model.IdType;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class JavaMailEmailSenderTest {
|
||||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(JavaMailEmailSenderTest.class);
|
||||
private static GreenMail ourTestSmtp;
|
||||
private static int ourPort;
|
||||
|
||||
@Test
|
||||
public void testSend() throws Exception {
|
||||
JavaMailEmailSender sender = new JavaMailEmailSender();
|
||||
sender.setSmtpServerHostname("localhost");
|
||||
sender.setSmtpServerPort(ourPort);
|
||||
sender.setSmtpServerUsername(null);
|
||||
sender.setSmtpServerPassword(null);
|
||||
sender.start();
|
||||
|
||||
String body = "foo";
|
||||
|
||||
EmailDetails details = new EmailDetails();
|
||||
details.setSubscription(new IdType("Subscription/123"));
|
||||
details.setFrom("foo@example.com ");
|
||||
details.setTo(Arrays.asList(" to1@example.com", "to2@example.com "));
|
||||
details.setSubjectTemplate("test subject");
|
||||
details.setBodyTemplate(body);
|
||||
sender.send(details);
|
||||
|
||||
MimeMessage[] messages = ourTestSmtp.getReceivedMessages();
|
||||
assertEquals(2, messages.length);
|
||||
ourLog.info("Received: " + GreenMailUtil.getWholeMessage(messages[0]));
|
||||
assertEquals("test subject", messages[0].getSubject());
|
||||
assertEquals(1, messages[0].getFrom().length);
|
||||
assertEquals("foo@example.com", ((InternetAddress)messages[0].getFrom()[0]).getAddress());
|
||||
assertEquals(2, messages[0].getAllRecipients().length);
|
||||
assertEquals("to1@example.com", ((InternetAddress)messages[0].getAllRecipients()[0]).getAddress());
|
||||
assertEquals("to2@example.com", ((InternetAddress)messages[0].getAllRecipients()[1]).getAddress());
|
||||
assertEquals(1, messages[0].getHeader("Content-Type").length);
|
||||
assertEquals("text/plain; charset=us-ascii", messages[0].getHeader("Content-Type")[0]);
|
||||
String foundBody = GreenMailUtil.getBody(messages[0]);
|
||||
assertEquals("foo", foundBody);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
ourTestSmtp.stop();
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP);
|
||||
smtp.setServerStartupTimeout(2000);
|
||||
ourTestSmtp = new GreenMail(smtp);
|
||||
ourTestSmtp.start();
|
||||
ourPort = ourTestSmtp.getSmtp().getPort();
|
||||
}
|
||||
|
||||
}
|
|
@ -65,18 +65,12 @@
|
|||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>javax.mail-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
|
|
@ -20,19 +20,38 @@ package ca.uhn.fhir.jpa.subscription.match.deliver.email;
|
|||
* #L%
|
||||
*/
|
||||
|
||||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hl7.fhir.instance.model.api.IIdType;
|
||||
import org.simplejavamail.api.email.Email;
|
||||
import org.simplejavamail.email.EmailBuilder;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.dialect.SpringStandardDialect;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.StringTemplateResolver;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class EmailDetails {
|
||||
private final SpringTemplateEngine myTemplateEngine;
|
||||
private final Context myContext;
|
||||
|
||||
private String mySubjectTemplate;
|
||||
private String myBodyTemplate;
|
||||
private List<String> myTo;
|
||||
private String myFrom;
|
||||
private IIdType mySubscription;
|
||||
|
||||
public String getBodyTemplate() {
|
||||
return myBodyTemplate;
|
||||
public EmailDetails() {
|
||||
myTemplateEngine = makeTemplateEngine();
|
||||
myContext = new Context();
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return myTemplateEngine.process(myBodyTemplate, myContext);
|
||||
}
|
||||
|
||||
public void setBodyTemplate(String theBodyTemplate) {
|
||||
|
@ -40,35 +59,65 @@ public class EmailDetails {
|
|||
}
|
||||
|
||||
public String getFrom() {
|
||||
return myFrom;
|
||||
return StringUtils.trim(myFrom);
|
||||
}
|
||||
|
||||
public void setFrom(String theFrom) {
|
||||
myFrom = theFrom;
|
||||
}
|
||||
|
||||
public String getSubjectTemplate() {
|
||||
return mySubjectTemplate;
|
||||
public String getSubject() {
|
||||
return myTemplateEngine.process(mySubjectTemplate, myContext);
|
||||
}
|
||||
|
||||
public void setSubjectTemplate(String theSubjectTemplate) {
|
||||
mySubjectTemplate = theSubjectTemplate;
|
||||
}
|
||||
|
||||
public IIdType getSubscription() {
|
||||
return mySubscription;
|
||||
public String getSubscriptionId() {
|
||||
return mySubscription.toUnqualifiedVersionless().getValue();
|
||||
}
|
||||
|
||||
public void setSubscription(IIdType theSubscription) {
|
||||
mySubscription = theSubscription;
|
||||
}
|
||||
|
||||
public List<String> getTo() {
|
||||
return myTo;
|
||||
public String getTo() {
|
||||
return myTo.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
public void setTo(List<String> theTo) {
|
||||
myTo = theTo;
|
||||
}
|
||||
|
||||
public Email toEmail() {
|
||||
try {
|
||||
return EmailBuilder.startingBlank()
|
||||
.from(getFrom())
|
||||
.to(getTo())
|
||||
.withSubject(getSubject())
|
||||
.withPlainText(getBody())
|
||||
.withHeader("X-FHIR-Subscription", getSubscriptionId())
|
||||
.buildEmail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new InternalErrorException("Failed to create email message", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private SpringTemplateEngine makeTemplateEngine() {
|
||||
StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
|
||||
stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);
|
||||
|
||||
SpringStandardDialect springStandardDialect = new SpringStandardDialect();
|
||||
springStandardDialect.setEnableSpringELCompiler(true);
|
||||
|
||||
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
|
||||
springTemplateEngine.setDialect(springStandardDialect);
|
||||
springTemplateEngine.setEnableSpringELCompiler(true);
|
||||
springTemplateEngine.setTemplateResolver(stringTemplateResolver);
|
||||
|
||||
return springTemplateEngine;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package ca.uhn.fhir.jpa.subscription.match.deliver.email;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* HAPI FHIR Subscription Server
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2021 Smile CDR, Inc.
|
||||
* %%
|
||||
* 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.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import ca.uhn.fhir.rest.server.mail.MailConfig;
|
||||
import ca.uhn.fhir.rest.server.mail.MailSvc;
|
||||
import ca.uhn.fhir.util.StopWatch;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.simplejavamail.api.email.Email;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class EmailSenderImpl implements IEmailSender {
|
||||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(EmailSenderImpl.class);
|
||||
private MailSvc myMailSvc = new MailSvc();
|
||||
private MailConfig myMailConfig;
|
||||
|
||||
public EmailSenderImpl(@Nonnull MailConfig theMailConfig) {
|
||||
Validate.notNull(theMailConfig);
|
||||
myMailConfig = theMailConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(EmailDetails theDetails) {
|
||||
Validate.notNull(myMailConfig, "Mail configuration is not set!");
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
|
||||
ourLog.info("Sending email for subscription {} from [{}] to recipients: [{}]", theDetails.getSubscriptionId(), theDetails.getFrom(), theDetails.getTo());
|
||||
|
||||
Email email = theDetails.toEmail();
|
||||
|
||||
myMailSvc.sendMail(myMailConfig, email,
|
||||
() -> ourLog.info("Done sending email for subscription {} from [{}] to recipients: [{}] (took {}ms)",
|
||||
theDetails.getSubscriptionId(), theDetails.getFrom(), theDetails.getTo(), stopWatch.getMillis()),
|
||||
(e) -> {
|
||||
ourLog.error("Error sending email for subscription {} from [{}] to recipients: [{}] (took {}ms)",
|
||||
theDetails.getSubscriptionId(), theDetails.getFrom(), theDetails.getTo(), stopWatch.getMillis());
|
||||
ourLog.error("Error sending email", e);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,194 +0,0 @@
|
|||
package ca.uhn.fhir.jpa.subscription.match.deliver.email;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* HAPI FHIR Subscription Server
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2021 Smile CDR, Inc.
|
||||
* %%
|
||||
* 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.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import ca.uhn.fhir.rest.api.Constants;
|
||||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
|
||||
import ca.uhn.fhir.util.StopWatch;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.dialect.SpringStandardDialect;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.StringTemplateResolver;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
import static org.apache.commons.lang3.StringUtils.trim;
|
||||
|
||||
public class JavaMailEmailSender implements IEmailSender {
|
||||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(JavaMailEmailSender.class);
|
||||
private String mySmtpServerHostname;
|
||||
private int mySmtpServerPort = 25;
|
||||
private JavaMailSenderImpl mySender;
|
||||
private String mySmtpServerUsername;
|
||||
private String mySmtpServerPassword;
|
||||
private final Properties myJavaMailProperties = new Properties();
|
||||
|
||||
public String getSmtpServerHostname() {
|
||||
return mySmtpServerHostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SMTP server host to use for outbound mail
|
||||
*/
|
||||
public void setSmtpServerHostname(String theSmtpServerHostname) {
|
||||
mySmtpServerHostname = theSmtpServerHostname;
|
||||
}
|
||||
|
||||
public String getSmtpServerPassword() {
|
||||
return mySmtpServerPassword;
|
||||
}
|
||||
|
||||
public void setSmtpServerPassword(String theSmtpServerPassword) {
|
||||
mySmtpServerPassword = theSmtpServerPassword;
|
||||
}
|
||||
|
||||
public int getSmtpServerPort() {
|
||||
return mySmtpServerPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SMTP server port to use for outbound mail
|
||||
*/
|
||||
public void setSmtpServerPort(int theSmtpServerPort) {
|
||||
mySmtpServerPort = theSmtpServerPort;
|
||||
}
|
||||
|
||||
public String getSmtpServerUsername() {
|
||||
return mySmtpServerUsername;
|
||||
}
|
||||
|
||||
public void setSmtpServerUsername(String theSmtpServerUsername) {
|
||||
mySmtpServerUsername = theSmtpServerUsername;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "mail.smtp.auth" Java Mail Property
|
||||
*/
|
||||
|
||||
public void setAuth(Boolean theAuth) {
|
||||
myJavaMailProperties.setProperty("mail.smtp.auth", theAuth.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "mail.smtp.starttls.enable" Java Mail Property
|
||||
*/
|
||||
|
||||
public void setStartTlsEnable(Boolean theStartTlsEnable) {
|
||||
myJavaMailProperties.setProperty("mail.smtp.starttls.enable", theStartTlsEnable.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "mail.smtp.starttls.required" Java Mail Property
|
||||
*/
|
||||
|
||||
public void setStartTlsRequired(Boolean theStartTlsRequired) {
|
||||
myJavaMailProperties.setProperty("mail.smtp.starttls.required", theStartTlsRequired.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "mail.smtp.quitwait" Java Mail Property
|
||||
*/
|
||||
|
||||
public void setQuitWait(Boolean theQuitWait) {
|
||||
myJavaMailProperties.setProperty("mail.smtp.quitwait", theQuitWait.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(EmailDetails theDetails) {
|
||||
String subscriptionId = theDetails.getSubscription().toUnqualifiedVersionless().getValue();
|
||||
StopWatch sw = new StopWatch();
|
||||
|
||||
StringTemplateResolver templateResolver = new StringTemplateResolver();
|
||||
templateResolver.setTemplateMode(TemplateMode.TEXT);
|
||||
|
||||
SpringStandardDialect dialect = new SpringStandardDialect();
|
||||
dialect.setEnableSpringELCompiler(true);
|
||||
|
||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||
engine.setDialect(dialect);
|
||||
engine.setEnableSpringELCompiler(true);
|
||||
engine.setTemplateResolver(templateResolver);
|
||||
|
||||
Context context = new Context();
|
||||
|
||||
String body = engine.process(theDetails.getBodyTemplate(), context);
|
||||
String subject = engine.process(theDetails.getSubjectTemplate(), context);
|
||||
|
||||
MimeMessage email = mySender.createMimeMessage();
|
||||
|
||||
String from = trim(theDetails.getFrom());
|
||||
ourLog.info("Sending email for subscription {} from [{}] to recipients: [{}]", subscriptionId, from, theDetails.getTo());
|
||||
|
||||
try {
|
||||
email.setFrom(from);
|
||||
email.setRecipients(Message.RecipientType.TO, toTrimmedCommaSeparatedString(theDetails.getTo()));
|
||||
email.setSubject(subject);
|
||||
email.setText(body);
|
||||
email.setSentDate(new Date());
|
||||
email.addHeader("X-FHIR-Subscription", subscriptionId);
|
||||
} catch (MessagingException e) {
|
||||
throw new InternalErrorException("Failed to create email message", e);
|
||||
}
|
||||
|
||||
mySender.send(email);
|
||||
|
||||
ourLog.info("Done sending email (took {}ms)", sw.getMillis());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void start() {
|
||||
Validate.notBlank(mySmtpServerHostname, "No SMTP host defined");
|
||||
|
||||
mySender = new JavaMailSenderImpl();
|
||||
mySender.setHost(getSmtpServerHostname());
|
||||
mySender.setPort(getSmtpServerPort());
|
||||
mySender.setUsername(getSmtpServerUsername());
|
||||
mySender.setPassword(getSmtpServerPassword());
|
||||
mySender.setDefaultEncoding(Constants.CHARSET_UTF8.name());
|
||||
mySender.setJavaMailProperties(myJavaMailProperties);
|
||||
}
|
||||
|
||||
private static String toTrimmedCommaSeparatedString(List<String> theTo) {
|
||||
List<String> to = new ArrayList<>();
|
||||
for (String next : theTo) {
|
||||
if (isNotBlank(next)) {
|
||||
to.add(next);
|
||||
}
|
||||
}
|
||||
|
||||
return StringUtils.join(to, ",");
|
||||
}
|
||||
}
|
|
@ -22,20 +22,21 @@ package ca.uhn.fhir.jpa.subscription.match.deliver.email;
|
|||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.jpa.model.entity.ModelConfig;
|
||||
import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
|
||||
import ca.uhn.fhir.jpa.subscription.match.deliver.BaseSubscriptionDeliverySubscriber;
|
||||
import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
|
||||
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryMessage;
|
||||
import ca.uhn.fhir.rest.api.EncodingEnum;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.*;
|
||||
import static org.apache.commons.lang3.StringUtils.defaultString;
|
||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
import static org.apache.commons.lang3.StringUtils.trim;
|
||||
|
||||
public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliverySubscriber {
|
||||
private Logger ourLog = LoggerFactory.getLogger(SubscriptionDeliveringEmailSubscriber.class);
|
||||
|
|
|
@ -87,6 +87,29 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- mail start -->
|
||||
<dependency>
|
||||
<groupId>org.simplejavamail</groupId>
|
||||
<artifactId>simple-java-mail</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>jakarta.annotation</groupId>
|
||||
<artifactId>jakarta.annotation-api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail-junit5</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- mail end -->
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package ca.uhn.fhir.rest.server.mail;
|
||||
|
||||
import org.apache.commons.lang3.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
|
||||
public class MailConfig {
|
||||
private String smtpHostname;
|
||||
private Integer smtpPort;
|
||||
private String smtpUsername;
|
||||
private String smtpPassword;
|
||||
private boolean smtpUseStartTLS;
|
||||
|
||||
public MailConfig() {
|
||||
}
|
||||
|
||||
public String getSmtpHostname() {
|
||||
return smtpHostname;
|
||||
}
|
||||
|
||||
public MailConfig setSmtpHostname(String theSmtpHostname) {
|
||||
smtpHostname = theSmtpHostname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public MailConfig setSmtpPort(Integer theSmtpPort) {
|
||||
smtpPort = theSmtpPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSmtpUsername() {
|
||||
return smtpUsername;
|
||||
}
|
||||
|
||||
public MailConfig setSmtpUsername(String theSmtpUsername) {
|
||||
smtpUsername = theSmtpUsername;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSmtpPassword() {
|
||||
return smtpPassword;
|
||||
}
|
||||
|
||||
public MailConfig setSmtpPassword(String theSmtpPassword) {
|
||||
smtpPassword = theSmtpPassword;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isSmtpUseStartTLS() {
|
||||
return smtpUseStartTLS;
|
||||
}
|
||||
|
||||
public MailConfig setSmtpUseStartTLS(boolean theSmtpUseStartTLS) {
|
||||
smtpUseStartTLS = theSmtpUseStartTLS;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object) {
|
||||
return true;
|
||||
}
|
||||
if (object == null || getClass() != object.getClass()) {
|
||||
return false;
|
||||
}
|
||||
return EqualsBuilder.reflectionEquals(this, object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
package ca.uhn.fhir.rest.server.mail;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.simplejavamail.MailException;
|
||||
import org.simplejavamail.api.email.Email;
|
||||
import org.simplejavamail.api.email.Recipient;
|
||||
import org.simplejavamail.api.mailer.AsyncResponse;
|
||||
import org.simplejavamail.api.mailer.AsyncResponse.ExceptionConsumer;
|
||||
import org.simplejavamail.api.mailer.Mailer;
|
||||
import org.simplejavamail.api.mailer.config.TransportStrategy;
|
||||
import org.simplejavamail.mailer.MailerBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MailSvc {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(MailSvc.class);
|
||||
|
||||
public void sendMail(@Nonnull MailConfig theMailConfig, @Nonnull List<Email> theEmails) {
|
||||
Validate.notNull(theMailConfig);
|
||||
Validate.notNull(theEmails);
|
||||
final Mailer mailer = makeMailer(theMailConfig);
|
||||
theEmails.forEach(theEmail -> sendMail(mailer, theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail)));
|
||||
}
|
||||
|
||||
public void sendMail(@Nonnull MailConfig theMailConfig, @Nonnull Email theEmail) {
|
||||
Validate.notNull(theMailConfig);
|
||||
final Mailer mailer = makeMailer(theMailConfig);
|
||||
sendMail(mailer, theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail));
|
||||
}
|
||||
|
||||
public void sendMail(@Nonnull MailConfig theMailConfig,
|
||||
@Nonnull Email theEmail,
|
||||
@Nonnull Runnable theOnSuccess,
|
||||
@Nonnull ExceptionConsumer theErrorHandler) {
|
||||
Validate.notNull(theMailConfig);
|
||||
final Mailer mailer = makeMailer(theMailConfig);
|
||||
sendMail(mailer, theEmail, theOnSuccess, theErrorHandler);
|
||||
}
|
||||
|
||||
private void sendMail(@Nonnull Mailer theMailer,
|
||||
@Nonnull Email theEmail,
|
||||
@Nonnull Runnable theOnSuccess,
|
||||
@Nonnull ExceptionConsumer theErrorHandler) {
|
||||
Validate.notNull(theMailer);
|
||||
Validate.notNull(theEmail);
|
||||
Validate.notNull(theOnSuccess);
|
||||
Validate.notNull(theErrorHandler);
|
||||
try {
|
||||
final AsyncResponse asyncResponse = theMailer.sendMail(theEmail, true);
|
||||
if (asyncResponse != null) {
|
||||
asyncResponse.onSuccess(theOnSuccess);
|
||||
asyncResponse.onException(theErrorHandler);
|
||||
}
|
||||
} catch (MailException e) {
|
||||
theErrorHandler.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Mailer makeMailer(@Nonnull MailConfig theMailConfig) {
|
||||
return MailerBuilder
|
||||
.withSMTPServer(
|
||||
theMailConfig.getSmtpHostname(),
|
||||
theMailConfig.getSmtpPort(),
|
||||
theMailConfig.getSmtpUsername(),
|
||||
theMailConfig.getSmtpPassword())
|
||||
.withTransportStrategy(theMailConfig.isSmtpUseStartTLS() ? TransportStrategy.SMTP_TLS : TransportStrategy.SMTP)
|
||||
.buildMailer();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private String makeMessage(@Nonnull Email theEmail) {
|
||||
return " with subject [" + theEmail.getSubject() + "] and recipients ["
|
||||
+ theEmail.getRecipients().stream().map(Recipient::getAddress).collect(Collectors.joining(",")) + "]";
|
||||
}
|
||||
|
||||
private class OnSuccess implements Runnable {
|
||||
private final Email myEmail;
|
||||
|
||||
private OnSuccess(@Nonnull Email theEmail) {
|
||||
myEmail = theEmail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ourLog.info("Email sent" + makeMessage(myEmail));
|
||||
}
|
||||
}
|
||||
|
||||
private class ErrorHandler implements ExceptionConsumer {
|
||||
private final Email myEmail;
|
||||
|
||||
private ErrorHandler(@Nonnull Email theEmail) {
|
||||
myEmail = theEmail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Exception t) {
|
||||
ourLog.error("Email not sent" + makeMessage(myEmail), t);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
package ca.uhn.fhir.rest.server.mail;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class MailConfigTest {
|
||||
|
||||
private static final String SMTP_HOST_NAME = "SMTP_HOST_NAME";
|
||||
private static final Integer SMTP_PORT = 1025;
|
||||
private static final String SMTP_USERNAME = "SMTP_USERNAME";
|
||||
private static final String SMTP_PASSWORD = "SMTP_PASSWORD";
|
||||
|
||||
private MailConfig fixture;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
fixture = withMainConfig();
|
||||
}
|
||||
|
||||
private MailConfig withMainConfig() {
|
||||
return new MailConfig()
|
||||
.setSmtpHostname(SMTP_HOST_NAME)
|
||||
.setSmtpPort(SMTP_PORT)
|
||||
.setSmtpUsername(SMTP_USERNAME)
|
||||
.setSmtpPassword(SMTP_PASSWORD)
|
||||
.setSmtpUseStartTLS(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmtpHostname() {
|
||||
// execute
|
||||
final String actual = fixture.getSmtpHostname();
|
||||
// validate
|
||||
assertEquals(SMTP_HOST_NAME, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmtpPort() {
|
||||
// execute
|
||||
final int actual = fixture.getSmtpPort();
|
||||
// validate
|
||||
assertEquals(SMTP_PORT, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmtpUsername() {
|
||||
// execute
|
||||
final String actual = fixture.getSmtpUsername();
|
||||
// validate
|
||||
assertEquals(SMTP_USERNAME, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmtpPassword() {
|
||||
// execute
|
||||
final String actual = fixture.getSmtpPassword();
|
||||
// validate
|
||||
assertEquals(SMTP_PASSWORD, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSmtpUseStartTLS() {
|
||||
// execute
|
||||
final boolean actual = fixture.isSmtpUseStartTLS();
|
||||
// validate
|
||||
assertTrue(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquality() {
|
||||
// setup
|
||||
final MailConfig other = withMainConfig();
|
||||
// execute & validate
|
||||
assertEquals(fixture, fixture);
|
||||
assertSame(fixture, fixture);
|
||||
assertEquals(fixture, other);
|
||||
assertNotSame(fixture, other);
|
||||
assertEquals(fixture.hashCode(), other.hashCode());
|
||||
assertNotEquals(fixture.toString(), other.toString());
|
||||
assertNotEquals(fixture, null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
package ca.uhn.fhir.rest.server.mail;
|
||||
|
||||
import com.icegreen.greenmail.junit5.GreenMailExtension;
|
||||
import com.icegreen.greenmail.util.GreenMailUtil;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.simplejavamail.MailException;
|
||||
import org.simplejavamail.api.email.Email;
|
||||
import org.simplejavamail.email.EmailBuilder;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class MailSvcIT {
|
||||
private static final String FROM_ADDRESS = "from_address@email.com";
|
||||
private static final String TO_ADDRESS = "to_address@email.com";
|
||||
private static final String SUBJECT = "Email Subject";
|
||||
private static final String BODY = "Email Body !!!";
|
||||
|
||||
@RegisterExtension
|
||||
static GreenMailExtension ourGreenMail = new GreenMailExtension(ServerSetupTest.SMTP);
|
||||
|
||||
private MailSvc fixture;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
fixture = new MailSvc();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSingleMail() throws Exception {
|
||||
// setup
|
||||
final MailConfig mailConfig = withMailConfig();
|
||||
final Email email = withEmail();
|
||||
// execute
|
||||
fixture.sendMail(mailConfig, email);
|
||||
// validate
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 1));
|
||||
final MimeMessage[] receivedMessages = ourGreenMail.getReceivedMessages();
|
||||
assertEquals(1, receivedMessages.length);
|
||||
assertEquals(SUBJECT, receivedMessages[0].getSubject());
|
||||
assertEquals(BODY, GreenMailUtil.getBody(receivedMessages[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMultipleMail() throws Exception {
|
||||
// setup
|
||||
final MailConfig mailConfig = withMailConfig();
|
||||
final List<Email> emails = Arrays.asList(withEmail(), withEmail(), withEmail());
|
||||
// execute
|
||||
fixture.sendMail(mailConfig, emails);
|
||||
// validate
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, emails.size()));
|
||||
final MimeMessage[] receivedMessages = ourGreenMail.getReceivedMessages();
|
||||
assertEquals(emails.size(), receivedMessages.length);
|
||||
assertEquals(SUBJECT, receivedMessages[0].getSubject());
|
||||
assertEquals(BODY, GreenMailUtil.getBody(receivedMessages[0]));
|
||||
assertEquals(SUBJECT, receivedMessages[1].getSubject());
|
||||
assertEquals(BODY, GreenMailUtil.getBody(receivedMessages[1]));
|
||||
assertEquals(SUBJECT, receivedMessages[2].getSubject());
|
||||
assertEquals(BODY, GreenMailUtil.getBody(receivedMessages[2]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMailWithInvalidToAddress() {
|
||||
// setup
|
||||
final MailConfig mailConfig = withMailConfig();
|
||||
final Email email = withEmail("xyz");
|
||||
// execute
|
||||
fixture.sendMail(mailConfig, email);
|
||||
// validate
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 0));
|
||||
final MimeMessage[] receivedMessages = ourGreenMail.getReceivedMessages();
|
||||
assertEquals(0, receivedMessages.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMailWithInvalidToAddressExpectErrorHandler() {
|
||||
// setup
|
||||
final MailConfig mailConfig = withMailConfig();
|
||||
final Email email = withEmail("xyz");
|
||||
// execute
|
||||
fixture.sendMail(mailConfig, email,
|
||||
() -> fail("Should not execute on Success"),
|
||||
(e) -> {
|
||||
assertTrue(e instanceof MailException);
|
||||
assertEquals("Invalid TO address: " + email, e.getMessage());
|
||||
});
|
||||
// validate
|
||||
assertTrue(ourGreenMail.waitForIncomingEmail(1000, 0));
|
||||
}
|
||||
|
||||
private MailConfig withMailConfig() {
|
||||
return new MailConfig()
|
||||
.setSmtpHostname(ServerSetupTest.SMTP.getBindAddress())
|
||||
.setSmtpPort(ServerSetupTest.SMTP.getPort());
|
||||
}
|
||||
|
||||
private Email withEmail() {
|
||||
return withEmail(TO_ADDRESS);
|
||||
}
|
||||
|
||||
private Email withEmail(@Nonnull String toAddress) {
|
||||
return EmailBuilder.startingBlank()
|
||||
.from(FROM_ADDRESS)
|
||||
.to(toAddress)
|
||||
.withSubject(SUBJECT)
|
||||
.withPlainText(BODY)
|
||||
.buildEmail();
|
||||
}
|
||||
|
||||
}
|
38
pom.xml
38
pom.xml
|
@ -11,6 +11,7 @@
|
|||
<description>An open-source implementation of the FHIR specification in Java.</description>
|
||||
<url>https://hapifhir.io</url>
|
||||
|
||||
|
||||
<organization>
|
||||
<name>Smile CDR, Inc.</name>
|
||||
<url>https://smilecdr.com</url>
|
||||
|
@ -893,16 +894,25 @@
|
|||
<artifactId>caffeine</artifactId>
|
||||
<version>${caffeine_version}</version>
|
||||
</dependency>
|
||||
<!-- mail start -->
|
||||
<dependency>
|
||||
<groupId>org.simplejavamail</groupId>
|
||||
<artifactId>simple-java-mail</artifactId>
|
||||
<version>6.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail</artifactId>
|
||||
<version>1.6.3</version>
|
||||
<version>1.6.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail-spring</artifactId>
|
||||
<version>1.5.10</version>
|
||||
<artifactId>greenmail-junit5</artifactId>
|
||||
<version>1.6.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- mail end -->
|
||||
<dependency>
|
||||
<groupId>com.github.dnault</groupId>
|
||||
<artifactId>xml-patch</artifactId>
|
||||
|
@ -963,11 +973,6 @@
|
|||
<artifactId>jakarta.activation</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.vladsch.flexmark</groupId>
|
||||
<artifactId>flexmark</artifactId>
|
||||
|
@ -1115,11 +1120,6 @@
|
|||
<artifactId>mssql-jdbc</artifactId>
|
||||
<version>9.2.1.jre8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>javax.mail-api</artifactId>
|
||||
<version>1.6.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
|
@ -1784,17 +1784,7 @@
|
|||
<artifactId>spring-websocket</artifactId>
|
||||
<version>${spring_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-core</artifactId>
|
||||
<version>${spring_batch_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-infrastructure</artifactId>
|
||||
<version>${spring_batch_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.retry</groupId>
|
||||
<artifactId>spring-retry</artifactId>
|
||||
<version>${spring_retry_version}</version>
|
||||
|
|
Loading…
Reference in New Issue