Github user trixpan commented on a diff in the pull request: https://github.com/apache/nifi/pull/827#discussion_r74364045 --- Diff: nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/test/java/org/apache/nifi/processors/email/TestListenSMTP.java --- @@ -13,307 +13,174 @@ * 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. -*/ + */ package org.apache.nifi.processors.email; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + import org.apache.commons.mail.Email; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; -import org.apache.nifi.processor.ProcessContext; -import org.apache.nifi.processor.ProcessSessionFactory; +import org.apache.nifi.remote.io.socket.NetworkUtils; +import org.apache.nifi.ssl.SSLContextService; import org.apache.nifi.ssl.StandardSSLContextService; -import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; - -import org.apache.nifi.ssl.SSLContextService; - -import org.junit.Assert; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; - public class TestListenSMTP { - @Test(timeout=15000) - public void ValidEmailTls() throws Exception { - boolean[] failed = {false}; - ListenSMTP listenSmtp = new ListenSMTP(); - final TestRunner runner = TestRunners.newTestRunner(listenSmtp); - - runner.setProperty(ListenSMTP.SMTP_PORT, "0"); - runner.setProperty(ListenSMTP.SMTP_HOSTNAME, "bermudatriangle"); - runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3"); - runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds"); - - // Setup the SSL Context - final SSLContextService sslContextService = new StandardSSLContextService(); - runner.addControllerService("ssl-context", sslContextService); - runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE, "src/test/resources/localhost-ts.jks"); - runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_PASSWORD, "localtest"); - runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_TYPE, "JKS"); - runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE, "src/test/resources/localhost-ks.jks"); - runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_PASSWORD, "localtest"); - runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_TYPE, "JKS"); - runner.enableControllerService(sslContextService); - - // and add the SSL context to the runner - runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, "ssl-context"); - runner.setProperty(ListenSMTP.CLIENT_AUTH, SSLContextService.ClientAuth.NONE.name()); - + private ScheduledExecutorService executor; + /** + * + */ + @Before + public void before() { + this.executor = Executors.newScheduledThreadPool(2); + } - final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory(); - final ProcessContext context = runner.getProcessContext(); - - // NOTE: This test routine uses the same strategy used by TestListenAndPutSyslog - // where listenSmtp method calls are used to allow the processor to be started using - // port "0" without triggering a violation of PORT_VALIDATOR - - listenSmtp.onScheduled(context); - listenSmtp.initializeSMTPServer(context); - - final int port = listenSmtp.getPort(); - - try { - final Thread clientThread = new Thread(new Runnable() { - @Override - public void run() { - try { - - - System.setProperty("mail.smtp.ssl.trust", "*"); - System.setProperty("javax.net.ssl.keyStore", "src/test/resources/localhost-ks.jks"); - System.setProperty("javax.net.ssl.keyStorePassword", "localtest"); - - Email email = new SimpleEmail(); - - email.setHostName("127.0.0.1"); - email.setSmtpPort(port); - - // Enable STARTTLS but ignore the cert - email.setStartTLSEnabled(true); - email.setStartTLSRequired(true); - email.setSSLCheckServerIdentity(false); - - email.setFrom("al...@nifi.apache.org"); - email.setSubject("This is a test"); - email.setMsg("Test test test chocolate"); - email.addTo("b...@nifi.apache.org"); - - email.send(); - } catch (final Throwable t) { - failed[0] = true; - } - } - }); - clientThread.start(); - - while (runner.getFlowFilesForRelationship(ListenSMTP.REL_SUCCESS).isEmpty()) { - // process the request. - listenSmtp.onTrigger(context, processSessionFactory); - } - - // Checks if client experienced Exception - Assert.assertFalse("Client experienced exception", failed[0]); - - runner.assertTransferCount(ListenSMTP.REL_SUCCESS, 1); - clientThread.stop(); - - Assert.assertFalse("Sending email failed", failed[0]); - - runner.assertQueueEmpty(); - final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ListenSMTP.REL_SUCCESS); - splits.get(0).assertAttributeEquals("smtp.from", "al...@nifi.apache.org"); - splits.get(0).assertAttributeEquals("smtp.to", "b...@nifi.apache.org"); - - Thread.sleep(100); - } finally { - // shut down the server - listenSmtp.startShutdown(); - } + /** + * + */ + @After + public void after() { + this.executor.shutdown(); } - @Test(timeout=15000) - public void ValidEmail() throws Exception, EmailException { - final boolean[] failed = {false}; - ListenSMTP listenSmtp = new ListenSMTP(); - final TestRunner runner = TestRunners.newTestRunner(listenSmtp); + /** + * + */ + @Test + public void validateSuccessfulInteraction() throws Exception, EmailException { + int port = NetworkUtils.availablePort(); - runner.setProperty(ListenSMTP.SMTP_PORT, "0"); - runner.setProperty(ListenSMTP.SMTP_HOSTNAME, "bermudatriangle"); + TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class); + runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port)); runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3"); runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds"); - final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory(); - final ProcessContext context = runner.getProcessContext(); + runner.assertValid(); - // NOTE: This test routine uses the same strategy used by TestListenAndPutSyslog - // where listenSmtp method calls are used to allow the processor to be started using - // port "0" without triggering a violation of PORT_VALIDATOR - listenSmtp.onScheduled(context); - listenSmtp.initializeSMTPServer(context); + int messageCount = 5; + CountDownLatch latch = new CountDownLatch(messageCount); - final int port = listenSmtp.getPort(); + this.executor.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + runner.run(1, false); + } + }, 0, 500, TimeUnit.MILLISECONDS); - try { - final Thread clientThread = new Thread(new Runnable() { - @Override - public void run() { + this.executor.schedule(new Runnable() { + @Override + public void run() { + for (int i = 0; i < messageCount; i++) { try { Email email = new SimpleEmail(); - email.setHostName("127.0.0.1"); + email.setHostName("localhost"); email.setSmtpPort(port); - email.setStartTLSEnabled(false); email.setFrom("al...@nifi.apache.org"); email.setSubject("This is a test"); - email.setMsg("Test test test chocolate"); + email.setMsg("MSG-" + i); --- End diff -- /joke on NOOOOOOOOOO!!!! What have you done!??!! :-) https://github.com/apache/nifi/search?utf8=%E2%9C%93&q=chocolate /joke off
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---