diff --git a/.gitignore b/.gitignore index 14949e8..39ebe9d 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ hs_err_pid* /.classpath /.project +/target/ diff --git a/pom.xml b/pom.xml index 2b2c28b..cc117a4 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nhind direct-smtp-mq-gateway - 6.0 + 8.0.0 jar DirectProject Java RI Lightweight SMTP Server Gateway Standalone Spring Boot Micro-service Application DirectProject Java RI Lightweight SMTP Server Gateway Standalone Spring Boot Micro-service Application @@ -13,13 +13,14 @@ scm:git:https://github.com/DirectProject/nhin-d.git - 3.0.0 + 3.5.0 - + org.springframework.boot spring-boot-starter-parent - 2.1.9.RELEASE - + 2.5.2 + + Greg Meyer @@ -30,7 +31,6 @@ - New BSD License @@ -39,27 +39,29 @@ UTF-8 + 3.1.7 + 3.8.0 org.springframework.boot spring-boot-dependencies - 2.1.9.RELEASE + 2.5.2 pom import io.pivotal.spring.cloud spring-cloud-services-dependencies - 2.1.4.RELEASE + 3.3.0 pom import - + org.springframework.cloud spring-cloud-starter-parent - Greenwich.SR3 + 2020.0.3 pom import @@ -105,21 +107,37 @@ org.springframework.cloud spring-cloud-starter-stream-rabbit - + + + org.springframework.cloud + spring-cloud-starter-bootstrap + org.springframework.boot spring-boot-configuration-processor true + + + org.springframework.cloud + spring-cloud-stream + test-jar + test + test-binder + + + org.projectlombok + lombok + org.nhind direct-common - 6.0.1 + 8.0.0 org.subethamail subethasmtp - 3.1.7 + ${subethasmtp.version} mail @@ -130,13 +148,8 @@ commons-net commons-net - 3.6 + ${commons-net.version} compile - - - org.springframework.cloud - spring-cloud-stream-test-support - test org.springframework.boot @@ -186,27 +199,21 @@ - @@ -228,22 +236,21 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 2.9 - - org.apache.maven.plugins - maven-javadoc-plugin - 3.0.1 - + + org.apache.maven.plugins + maven-javadoc-plugin + + -Xdoclint:none UTF-8 UTF-8 true true true 1.8 - protected - - + public + + org.apache.maven.plugins maven-pmd-plugin @@ -258,11 +265,11 @@ org.apache.maven.plugins maven-jxr-plugin + 3.1.1 org.codehaus.mojo findbugs-maven-plugin - 1.2 Max diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/boot/SmtpGatewayApplication.java b/src/main/java/org/nhindirect/smtpmq/gateway/boot/SmtpGatewayApplication.java index e59ee9d..04c8131 100644 --- a/src/main/java/org/nhindirect/smtpmq/gateway/boot/SmtpGatewayApplication.java +++ b/src/main/java/org/nhindirect/smtpmq/gateway/boot/SmtpGatewayApplication.java @@ -1,6 +1,5 @@ package org.nhindirect.smtpmq.gateway.boot; -import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.WebApplicationType; @@ -15,9 +14,6 @@ @EnableScheduling public class SmtpGatewayApplication implements CommandLineRunner { - @Autowired - protected ConnectionFactory connectionFactory; - @Autowired protected SMTPServer smtpServer; diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/server/SMTPMessageHandler.java b/src/main/java/org/nhindirect/smtpmq/gateway/server/SMTPMessageHandler.java index bcab405..ec986f2 100644 --- a/src/main/java/org/nhindirect/smtpmq/gateway/server/SMTPMessageHandler.java +++ b/src/main/java/org/nhindirect/smtpmq/gateway/server/SMTPMessageHandler.java @@ -17,17 +17,16 @@ import org.apache.commons.io.input.CountingInputStream; import org.nhindirect.common.mail.SMTPMailMessage; import org.nhindirect.smtpmq.gateway.streams.SmtpGatewayMessageSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import org.subethamail.smtp.MessageHandler; import org.subethamail.smtp.RejectException; import org.subethamail.smtp.TooMuchDataException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class SMTPMessageHandler implements MessageHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(SMTPMessageHandler.class); - protected SmtpGatewayMessageSource messageSource; protected GetMessageHeaderStream getMessageHeaderStream; protected SizeLimitedStreamCreator sizeLimitedStreamCreator; @@ -54,15 +53,15 @@ public void from(String from) throws RejectException { try { - if (!StringUtils.isEmpty(from)) + if (StringUtils.hasText(from)) { this.from = new InternetAddress(from); // handle "<>" scenario - if (StringUtils.isEmpty(this.from.getAddress())) + if (!StringUtils.hasText(this.from.getAddress())) { this.from = null; - LOGGER.info("blank address... mailFrom will be null"); + log.info("blank address... mailFrom will be null"); } } @@ -86,7 +85,7 @@ public void recipient(String recipient) throws RejectException catch (AddressException e) { String errorMessage = "error parsing recipient address " + recipient; - LOGGER.error(errorMessage, e); + log.error(errorMessage, e); throw new RejectException(errorMessage); } @@ -131,7 +130,7 @@ protected void updateMessageID() throws MessagingException * 552 - Requested mail action aborted: exceeded storage allocation */ errorMessage += ": " + e.getCause().getMessage(); - LOGGER.error(errorMessage, e); + log.error(errorMessage, e); throw new RejectException(552, errorMessage); } else @@ -140,7 +139,7 @@ protected void updateMessageID() throws MessagingException * RFC821: * Service not available, closing transmission channel [This may be a reply to any command if the service knows it must shut down] */ - LOGGER.error(errorMessage, e); + log.error(errorMessage, e); throw new RejectException(421, errorMessage); } } @@ -152,7 +151,7 @@ protected void updateMessageID() throws MessagingException messageId = mimeMessage.getMessageID(); messageSource.forwardSMTPMessage(mailMessage); - LOGGER.info("successfully sent message with message id {} ({} bytes)", messageId, countingInputStream.getByteCount()); + log.info("successfully sent message with message id {} ({} bytes)", messageId, countingInputStream.getByteCount()); } catch (Throwable e) { @@ -160,7 +159,7 @@ protected void updateMessageID() throws MessagingException * RFC821: * Transaction failed */ - LOGGER.error("error sending message with message id " + messageId, e); + log.error("error sending message with message id " + messageId, e); throw new RejectException(554, "Error sending message: " + e.getMessage()); } } diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/springconfig/SMTPServerBeanConfig.java b/src/main/java/org/nhindirect/smtpmq/gateway/springconfig/SMTPServerBeanConfig.java index 4de9eb6..cdcdb32 100644 --- a/src/main/java/org/nhindirect/smtpmq/gateway/springconfig/SMTPServerBeanConfig.java +++ b/src/main/java/org/nhindirect/smtpmq/gateway/springconfig/SMTPServerBeanConfig.java @@ -42,7 +42,7 @@ public class SMTPServerBeanConfig private int maxMessageSize; - @Value("#{'${direct.smtpmqgateway.clientwhitelist.cidr:}'.split(',')}") + @Value("${direct.smtpmqgateway.clientwhitelist.cidr:}") private List clientWhitelistCidrs; @Autowired @@ -65,7 +65,7 @@ public MessageHandler create(MessageContext ctx) protected ServerSocket createServerSocket() throws IOException { if (clientWhitelistCidrs.isEmpty() || - (clientWhitelistCidrs.size() == 1 && StringUtils.isEmpty(clientWhitelistCidrs.get(0)))) + (clientWhitelistCidrs.size() == 1 && !StringUtils.hasText(clientWhitelistCidrs.get(0)))) return super.createServerSocket(); InetSocketAddress isa; diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageOutput.java b/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageOutput.java deleted file mode 100644 index e3a7901..0000000 --- a/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageOutput.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.nhindirect.smtpmq.gateway.streams; - -import org.springframework.cloud.stream.annotation.Output; -import org.springframework.messaging.MessageChannel; - -public interface SmtpGatewayMessageOutput -{ - public static final String SMTP_GATEWAY_MESSAGE_OUTPUT = "direct-smtp-gateway-message-output"; - - @Output(SMTP_GATEWAY_MESSAGE_OUTPUT) - MessageChannel txOutput(); -} diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageSource.java b/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageSource.java index 4f5d54b..75893f2 100644 --- a/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageSource.java +++ b/src/main/java/org/nhindirect/smtpmq/gateway/streams/SmtpGatewayMessageSource.java @@ -1,24 +1,47 @@ package org.nhindirect.smtpmq.gateway.streams; +import java.util.List; + +import javax.mail.internet.InternetAddress; + import org.nhindirect.common.mail.SMTPMailMessage; import org.nhindirect.common.mail.streams.SMTPMailMessageConverter; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.cloud.stream.annotation.EnableBinding; -import org.springframework.cloud.stream.annotation.Output; -import org.springframework.messaging.MessageChannel; +import org.springframework.cloud.stream.function.StreamBridge; +import org.springframework.stereotype.Component; + +import lombok.extern.slf4j.Slf4j; + -@EnableBinding(SmtpGatewayMessageOutput.class) +@Component +@Slf4j public class SmtpGatewayMessageSource { + // Maps to the Spring Cloud Stream functional output binding name. + protected static final String OUT_BINDING_NAME = "direct-smtp-gateway-message-out-0"; + @Autowired - @Qualifier(SmtpGatewayMessageOutput.SMTP_GATEWAY_MESSAGE_OUTPUT) - private MessageChannel smtpGatewayChannel; + private StreamBridge streamBridge; - @Output(SmtpGatewayMessageOutput.SMTP_GATEWAY_MESSAGE_OUTPUT) - public void forwardSMTPMessage(SMTPMailMessage msg) + public void forwardSMTPMessage(SMTPMailMessage msg) throws Exception { - this.smtpGatewayChannel.send(SMTPMailMessageConverter.toStreamMessage(msg)); + final String from = (msg.getMailFrom() == null) ? null : msg.getMailFrom().toString(); + + log.info("Handing off incoming message to smtp gateway for from {} to {} with message id {}", from, + toRecipsPrettingString(msg.getRecipientAddresses()), msg.getMimeMessage().getMessageID()); + + streamBridge.send(OUT_BINDING_NAME, SMTPMailMessageConverter.toStreamMessage(msg)); } + protected String toRecipsPrettingString(List recips) + { + final String[] addrs = new String[recips.size()]; + + int idx = 0; + for (InternetAddress addr : recips) + addrs[idx++] = addr.toString(); + + return String.join(",", addrs); + } + } diff --git a/src/main/java/org/nhindirect/smtpmq/gateway/task/SimulatedLoadTask.java b/src/main/java/org/nhindirect/smtpmq/gateway/task/SimulatedLoadTask.java index d333ddb..9ac1485 100644 --- a/src/main/java/org/nhindirect/smtpmq/gateway/task/SimulatedLoadTask.java +++ b/src/main/java/org/nhindirect/smtpmq/gateway/task/SimulatedLoadTask.java @@ -10,20 +10,19 @@ import org.nhindirect.common.mail.SMTPMailMessage; import org.nhindirect.smtpmq.gateway.streams.SmtpGatewayMessageSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import lombok.extern.slf4j.Slf4j; + @ConditionalOnProperty(name="direct.smtpmqgateway.loadgen.rate", matchIfMissing=false) @Component +@Slf4j public class SimulatedLoadTask { - private static final Logger LOGGER = LoggerFactory.getLogger(SimulatedLoadTask.class); - @Value("${direct.smtpmqgateway.loadgen.sender}") protected String sender; @@ -44,7 +43,7 @@ public void sendMessages() throws Exception SMTPMailMessage smptMsg = new SMTPMailMessage(msg, Arrays.asList(new InternetAddress(recipient)), new InternetAddress(sender)); - LOGGER.info("Sending generated load from " + sender + " to " + recipient); + log.info("Sending generated load from " + sender + " to " + recipient); msgSource.forwardSMTPMessage(smptMsg); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index de347b9..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,8 +0,0 @@ -# automatically expand info properties from the Maven project using resource filtering -info.build.artifact=@project.artifactId@ -info.build.name=@project.name@ -info.build.description=@project.description@ -info.build.version=@project.version@ - -spring.cloud.stream.bindings.direct-smtp-gateway-message-output.destination=direct-smtp-mq-gateway -#spring.cloud.stream.bindings.direct-smtp-gateway-message-output.content-type=message/rfc822 \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..00603da --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,16 @@ +# automatically expand info properties from the Maven project using resource filtering +info: + build: + artifact: '@project.artifactId@' + name: '@project.name@' + description: '@project.description@' + version: '@project.version@' + +spring: + cloud: + stream: + # Need to define an explicit source binding name since we are using a StreamBridge + source: direct-smtp-gateway-message + bindings: + direct-smtp-gateway-message-out-0: + destination: direct-smtp-mq-gateway diff --git a/src/main/resources/bootstrap.properties b/src/main/resources/bootstrap.properties deleted file mode 100644 index e69a9cc..0000000 --- a/src/main/resources/bootstrap.properties +++ /dev/null @@ -1 +0,0 @@ -spring.application.name=direct-smtp-mq-gateway \ No newline at end of file diff --git a/src/main/resources/bootstrap.yml b/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..0f2d83b --- /dev/null +++ b/src/main/resources/bootstrap.yml @@ -0,0 +1,4 @@ +spring: + application: + name: + direct-smtp-mq-gateway \ No newline at end of file diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_largeRecipsTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_largeRecipsTest.java index 54547da..e065c9f 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_largeRecipsTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_largeRecipsTest.java @@ -1,89 +1,69 @@ package org.nhindirect.smtpmq.gateway.boot; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.util.List; -import java.util.concurrent.BlockingQueue; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.net.smtp.SMTPClient; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.extension.ExtendWith; import org.nhindirect.common.mail.SMTPMailMessage; import org.nhindirect.common.mail.streams.SMTPMailMessageConverter; -import org.nhindirect.smtpmq.gateway.streams.SmtpGatewayMessageOutput; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.stream.test.binder.MessageCollector; -import org.springframework.context.annotation.Configuration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.stream.binder.test.OutputDestination; +import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.messaging.Message; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.subethamail.smtp.server.SMTPServer; -@WebAppConfiguration -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {SmtpGatewayApplication.class}) -@Configuration -@TestPropertySource("classpath:properties/testConfig.properties") -@DirtiesContext +@ExtendWith(SpringExtension.class) public class SMTPMessageHandler_largeRecipsTest { protected static MimeMessage sentMessage; - - @Autowired - protected SMTPServer smtpServer; - - @Autowired - protected SmtpGatewayMessageOutput source; - - @Autowired - private MessageCollector collector; - - @Before - public void setUp() - { - sentMessage = null; - if (!smtpServer.isRunning()) - { - smtpServer.start(); - } - } - + @Test public void testMaxRecipientsExceeded_assertRecipientsLimited() throws Exception - { - final String sender = "sender@localhost"; - - - final String body = "Subject: test\r\n\r\nTestmail"; - final SMTPClient client = new SMTPClient(); - client.connect("localhost", 1025); - client.helo("localhost"); - client.setSender(sender); - for (int i = 0; i < 550; ++i) - client.addRecipient("rcpt" + i + "@localhost.com"); - - assertTrue(client.sendShortMessageData(body)); - client.quit(); - client.disconnect(); - - //final Address[] recips = postProc.getRecipientAddresses().get(RabbitConfig.MAIL_RECIPIENTS_HEADER); - BlockingQueue> messages = collector.forChannel(source.txOutput()); - - Message msg = messages.poll(); - - final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); - - final List recips = smtpMailMessage.getRecipientAddresses(); - assertEquals(550, recips.size()); + { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder( + TestChannelBinderConfiguration.getCompleteConfiguration( + SmtpGatewayApplication.class)) + .run("")) + { + OutputDestination output = context.getBean(OutputDestination.class); + final SMTPServer smtpServer = context.getBean(SMTPServer.class); + if (!smtpServer.isRunning()) + { + smtpServer.start(); + } + + final String sender = "sender@localhost"; + + final String body = "Subject: test\r\n\r\nTestmail"; + final SMTPClient client = new SMTPClient(); + client.connect("localhost", 1025); + client.helo("localhost"); + client.setSender(sender); + for (int i = 0; i < 550; ++i) + client.addRecipient("rcpt" + i + "@localhost.com"); + + assertTrue(client.sendShortMessageData(body)); + client.quit(); + client.disconnect(); + + final Message msg = output.receive(); + + final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); + + final List recips = smtpMailMessage.getRecipientAddresses(); + assertEquals(550, recips.size()); + } + } } diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_maxSizeTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_maxSizeTest.java index 5fcee42..4e6362a 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_maxSizeTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_maxSizeTest.java @@ -1,27 +1,24 @@ package org.nhindirect.smtpmq.gateway.boot; +import static org.junit.jupiter.api.Assertions.assertFalse; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; - - -import static org.junit.Assert.assertFalse; - +import org.junit.jupiter.api.extension.ExtendWith; import org.apache.commons.net.smtp.SMTPClient; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.smtp.server.SMTPServer; @WebAppConfiguration -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(classes = {SmtpGatewayApplication.class}) @Configuration @TestPropertySource("classpath:properties/testConfig.properties") @@ -31,7 +28,7 @@ public class SMTPMessageHandler_maxSizeTest @Autowired protected SMTPServer smtpServer; - @Before + @BeforeEach public void setUp() { if (!smtpServer.isRunning()) diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_sendMessageTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_sendMessageTest.java index 599c886..e9c3572 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_sendMessageTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/SMTPMessageHandler_sendMessageTest.java @@ -1,157 +1,167 @@ package org.nhindirect.smtpmq.gateway.boot; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.nio.charset.Charset; import java.util.List; -import java.util.concurrent.BlockingQueue; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.io.IOUtils; import org.apache.commons.net.smtp.SMTPClient; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import org.nhindirect.common.mail.SMTPMailMessage; import org.nhindirect.common.mail.streams.SMTPMailMessageConverter; -import org.nhindirect.smtpmq.gateway.streams.SmtpGatewayMessageOutput; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.stream.test.binder.MessageCollector; -import org.springframework.context.annotation.Configuration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.stream.binder.test.OutputDestination; +import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.messaging.Message; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.subethamail.smtp.server.SMTPServer; -@WebAppConfiguration -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {SmtpGatewayApplication.class}) -@Configuration +@ExtendWith(SpringExtension.class) @TestPropertySource("classpath:properties/testConfig.properties") -@DirtiesContext public class SMTPMessageHandler_sendMessageTest { protected static MimeMessage sentMessage; - @Autowired - protected SMTPServer smtpServer; - - @Autowired - protected SmtpGatewayMessageOutput source; - - @Autowired - private MessageCollector collector; - - @Before - public void setUp() - { - sentMessage = null; - if (!smtpServer.isRunning()) - { - smtpServer.start(); - } - } - - @Test public void testGoodMessage_assertMessageSent() throws Exception { - final String sender = "sender@localhost"; + try (ConfigurableApplicationContext context = new SpringApplicationBuilder( + TestChannelBinderConfiguration.getCompleteConfiguration( + SmtpGatewayApplication.class)) + .run("")) + { + OutputDestination output = context.getBean(OutputDestination.class); + final SMTPServer smtpServer = context.getBean(SMTPServer.class); + if (!smtpServer.isRunning()) + { + smtpServer.start(); + } + + final String sender = "sender@localhost"; - final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; - final SMTPClient client = new SMTPClient(); - client.connect("localhost", 1025); - client.helo("localhost"); - client.setSender(sender); - for (int i = 0; i < 4; ++i) - client.addRecipient("rcpt" + i + "@localhost.com"); - - assertTrue(client.sendShortMessageData(data)); - client.quit(); - client.disconnect(); - - BlockingQueue> messages = collector.forChannel(source.txOutput()); - - Message msg = messages.poll(); - - final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); - - sentMessage = smtpMailMessage.getMimeMessage(); - - assertEquals("test", sentMessage.getSubject()); - assertEquals("rcpt@localhost.com", sentMessage.getHeader("To")[0]); - final String content = IOUtils.toString(sentMessage.getInputStream(), Charset.defaultCharset()); - assertEquals("Testmail\r\n", content); - - final List recips = smtpMailMessage.getRecipientAddresses(); - - assertEquals(4, recips.size()); + final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; + final SMTPClient client = new SMTPClient(); + client.connect("localhost", 1025); + client.helo("localhost"); + client.setSender(sender); + for (int i = 0; i < 4; ++i) + client.addRecipient("rcpt" + i + "@localhost.com"); + + assertTrue(client.sendShortMessageData(data)); + client.quit(); + client.disconnect(); + + final Message msg = output.receive(); + + final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); + + sentMessage = smtpMailMessage.getMimeMessage(); + + assertEquals("test", sentMessage.getSubject()); + assertEquals("rcpt@localhost.com", sentMessage.getHeader("To")[0]); + final String content = IOUtils.toString(sentMessage.getInputStream(), Charset.defaultCharset()); + assertEquals("Testmail\r\n", content); + + final List recips = smtpMailMessage.getRecipientAddresses(); + + assertEquals(4, recips.size()); + }; } @Test public void testGoodMessageEmptyFrom_assertMessageSent() throws Exception { - final String sender = ""; + try (ConfigurableApplicationContext context = new SpringApplicationBuilder( + TestChannelBinderConfiguration.getCompleteConfiguration( + SmtpGatewayApplication.class)) + .run("")) + { + OutputDestination output = context.getBean(OutputDestination.class); + final SMTPServer smtpServer = context.getBean(SMTPServer.class); + if (!smtpServer.isRunning()) + { + smtpServer.start(); + } + + final String sender = ""; - final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; - final SMTPClient client = new SMTPClient(); - client.connect("localhost", 1025); - client.helo("localhost"); - client.setSender(sender); - for (int i = 0; i < 4; ++i) - client.addRecipient("rcpt" + i + "@localhost.com"); - - assertTrue(client.sendShortMessageData(data)); - client.quit(); - client.disconnect(); - - BlockingQueue> messages = collector.forChannel(source.txOutput()); - - Message msg = messages.poll(); - - final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); - - assertNull(smtpMailMessage.getMailFrom()); - sentMessage = smtpMailMessage.getMimeMessage(); - - assertEquals("test", sentMessage.getSubject()); - assertEquals("rcpt@localhost.com", sentMessage.getHeader("To")[0]); - final String content = IOUtils.toString(sentMessage.getInputStream(), Charset.defaultCharset()); - assertEquals("Testmail\r\n", content); - - final List recips = smtpMailMessage.getRecipientAddresses(); - - assertEquals(4, recips.size()); + final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; + final SMTPClient client = new SMTPClient(); + client.connect("localhost", 1025); + client.helo("localhost"); + client.setSender(sender); + for (int i = 0; i < 4; ++i) + client.addRecipient("rcpt" + i + "@localhost.com"); + + assertTrue(client.sendShortMessageData(data)); + client.quit(); + client.disconnect(); + + final Message msg = output.receive(); + + final SMTPMailMessage smtpMailMessage = SMTPMailMessageConverter.fromStreamMessage(msg); + + assertNull(smtpMailMessage.getMailFrom()); + sentMessage = smtpMailMessage.getMimeMessage(); + + assertEquals("test", sentMessage.getSubject()); + assertEquals("rcpt@localhost.com", sentMessage.getHeader("To")[0]); + final String content = IOUtils.toString(sentMessage.getInputStream(), Charset.defaultCharset()); + assertEquals("Testmail\r\n", content); + + final List recips = smtpMailMessage.getRecipientAddresses(); + + assertEquals(4, recips.size()); + + } + } @Test public void testGoodInvalidFromMessage_assertMessageNotSent() throws Exception { - final String sender = "bl#@#$#.Localhost.com"; + try (ConfigurableApplicationContext context = new SpringApplicationBuilder( + TestChannelBinderConfiguration.getCompleteConfiguration( + SmtpGatewayApplication.class)) + .run("")) + { + final SMTPServer smtpServer = context.getBean(SMTPServer.class); + if (!smtpServer.isRunning()) + { + smtpServer.start(); + } + + final String sender = "bl#@#$#.Localhost.com"; - final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; - final SMTPClient client = new SMTPClient(); - client.connect("localhost", 1025); - client.helo("localhost"); - client.setSender(sender); - for (int i = 0; i < 4; ++i) - client.addRecipient("rcpt" + i + "@localhost.com"); - - assertFalse(client.sendShortMessageData(data)); - client.quit(); - client.disconnect(); + final String data = "To: rcpt@localhost.com\r\nSubject: test\r\n\r\nTestmail"; + final SMTPClient client = new SMTPClient(); + client.connect("localhost", 1025); + client.helo("localhost"); + client.setSender(sender); + for (int i = 0; i < 4; ++i) + client.addRecipient("rcpt" + i + "@localhost.com"); + + assertFalse(client.sendShortMessageData(data)); + client.quit(); + client.disconnect(); + } + + } diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_authCidrTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_authCidrTest.java index 013adc0..26f8d16 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_authCidrTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_authCidrTest.java @@ -1,26 +1,25 @@ package org.nhindirect.smtpmq.gateway.boot; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.smtp.server.SMTPServer; @WebAppConfiguration -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(classes = {SmtpGatewayApplication.class}) @Configuration @TestPropertySource("classpath:properties/testLocalAuthorizedCIDRConfig.properties") @@ -30,7 +29,7 @@ public class WhitelistedServerSocket_authCidrTest @Autowired protected SMTPServer smtpServer; - @Before + @BeforeEach public void setUp() { if (!smtpServer.isRunning()) diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_multiAuthCidrTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_multiAuthCidrTest.java index ee89dd7..452d1b9 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_multiAuthCidrTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_multiAuthCidrTest.java @@ -1,25 +1,26 @@ package org.nhindirect.smtpmq.gateway.boot; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.smtp.server.SMTPServer; @WebAppConfiguration -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(classes = {SmtpGatewayApplication.class}) @Configuration @TestPropertySource("classpath:properties/testLocalMultiAuthorizedCIDRConfig.properties") @@ -29,7 +30,7 @@ public class WhitelistedServerSocket_multiAuthCidrTest @Autowired protected SMTPServer smtpServer; - @Before + @BeforeEach public void setUp() { if (!smtpServer.isRunning()) diff --git a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_noauthCidrTest.java b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_noauthCidrTest.java index 58860d6..dd63e7e 100644 --- a/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_noauthCidrTest.java +++ b/src/test/java/org/nhindirect/smtpmq/gateway/boot/WhitelistedServerSocket_noauthCidrTest.java @@ -1,27 +1,27 @@ package org.nhindirect.smtpmq.gateway.boot; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.smtp.server.SMTPServer; @WebAppConfiguration -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(classes = {SmtpGatewayApplication.class}) @Configuration @TestPropertySource("classpath:properties/testLocalNoAuthorizedCIDRConfig.properties") @@ -31,7 +31,7 @@ public class WhitelistedServerSocket_noauthCidrTest @Autowired protected SMTPServer smtpServer; - @Before + @BeforeEach public void setUp() { if (!smtpServer.isRunning())