Java Code Examples for org.apache.nifi.util.TestRunner#shutdown()

The following examples show how to use org.apache.nifi.util.TestRunner#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PutKafkaTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validatePartitionOutOfBounds() {
    String topicName = "validatePartitionOutOfBounds";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");
    runner.setProperty(PutKafka.PARTITION, "${partition}");

    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("partition", "123");
    runner.enqueue("Hello World\nGoodbye".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run(1, false);

    assertTrue("Error message should be logged", runner.getLogger().getErrorMessages().size() > 0);
    runner.assertTransferCount(PutKafka.REL_SUCCESS, 0);
    runner.assertTransferCount(PutKafka.REL_FAILURE, 1);

    runner.shutdown();
}
 
Example 2
Source File: PutKafkaTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validatePartitionOutOfBounds() {
    String topicName = "validatePartitionOutOfBounds";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");
    runner.setProperty(PutKafka.PARTITION, "${partition}");

    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("partition", "123");
    runner.enqueue("Hello World\nGoodbye".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run(1, false);

    assertTrue("Error message should be logged", runner.getLogger().getErrorMessages().size() > 0);
    runner.assertTransferCount(PutKafka.REL_SUCCESS, 0);
    runner.assertTransferCount(PutKafka.REL_FAILURE, 1);

    runner.shutdown();
}
 
Example 3
Source File: PutKafkaTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateSingleCharacterDemarcatedMessages() {
    String topicName = "validateSingleCharacterDemarcatedMessages";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");

    runner.enqueue("Hello World\nGoodbye\n1\n2\n3\n4\n5".getBytes(StandardCharsets.UTF_8));
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("1", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("2", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("3", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("4", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("5", new String(consumer.next().message(), StandardCharsets.UTF_8));

    runner.shutdown();
}
 
Example 4
Source File: PutKafkaTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateComplexPartialMatchDemarcatedMessages() {
    String topicName = "validateComplexPartialMatchDemarcatedMessages";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "僠<僠WILDSTUFF僠>僠");

    runner.enqueue("Hello World僠<僠WILDSTUFF僠>僠Goodbye僠<僠WILDBOOMSTUFF僠>僠".getBytes(StandardCharsets.UTF_8));
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye僠<僠WILDBOOMSTUFF僠>僠", new String(consumer.next().message(), StandardCharsets.UTF_8));
    runner.shutdown();
}
 
Example 5
Source File: SpringContextProcessorTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void validateOneWayFromSpring() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    runner.setProperty(SpringContextProcessor.CTX_CONFIG_PATH, "fromSpringOnly.xml");
    runner.setProperty(SpringContextProcessor.CTX_LIB_PATH, ".");
    runner.assertValid();

    TestProcessor processor = (TestProcessor) runner.getProcessor();
    SpringDataExchanger delegate = processor.getMockedDelegate();
    SpringResponse<Object> r = new SpringResponse<Object>("hello".getBytes(),
            Collections.<String, Object> emptyMap());
    when(delegate.receive(Mockito.anyLong())).thenReturn(r);
    when(delegate.send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong())).thenReturn(true);

    runner.run(1, false);

    verify(delegate, never()).send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong());
    verify(delegate, times(1)).receive(0);
    assertTrue(runner.getFlowFilesForRelationship(TestProcessor.REL_SUCCESS).size() == 1);
    runner.shutdown();
}
 
Example 6
Source File: SpringContextProcessorTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void validateOneWayFromNiFi() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    runner.setProperty(SpringContextProcessor.CTX_CONFIG_PATH, "toSpringOnly.xml");
    runner.setProperty(SpringContextProcessor.CTX_LIB_PATH, ".");
    runner.setProperty(SpringContextProcessor.RECEIVE_TIMEOUT, "100 millis");
    runner.assertValid();
    runner.enqueue("Hello".getBytes());

    TestProcessor processor = (TestProcessor) runner.getProcessor();
    SpringDataExchanger delegate = processor.getMockedDelegate();
    when(delegate.receive(Mockito.anyLong())).thenReturn(null);
    when(delegate.send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong())).thenReturn(true);

    runner.run(1, false);
    verify(delegate, times(1)).send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong());
    verify(delegate, times(1)).receive(100);
    assertTrue(runner.getFlowFilesForRelationship(TestProcessor.REL_SUCCESS).isEmpty());
    runner.shutdown();
}
 
Example 7
Source File: SpringContextProcessorTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void validateOneWayFromNiFi() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    runner.setProperty(SpringContextProcessor.CTX_CONFIG_PATH, "toSpringOnly.xml");
    runner.setProperty(SpringContextProcessor.CTX_LIB_PATH, ".");
    runner.setProperty(SpringContextProcessor.RECEIVE_TIMEOUT, "100 millis");
    runner.assertValid();
    runner.enqueue("Hello".getBytes());

    TestProcessor processor = (TestProcessor) runner.getProcessor();
    SpringDataExchanger delegate = processor.getMockedDelegate();
    when(delegate.receive(Mockito.anyLong())).thenReturn(null);
    when(delegate.send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong())).thenReturn(true);

    runner.run(1, false);
    verify(delegate, times(1)).send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong());
    verify(delegate, times(1)).receive(100);
    assertTrue(runner.getFlowFilesForRelationship(TestProcessor.REL_SUCCESS).isEmpty());
    runner.shutdown();
}
 
Example 8
Source File: PutKafkaTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateComplexRightPartialDemarcatedMessages() {
    String topicName = "validateComplexRightPartialDemarcatedMessages";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "僠<僠WILDSTUFF僠>僠");

    runner.enqueue("Hello World僠<僠WILDSTUFF僠>僠Goodbye僠<僠WILDSTUFF僠>僠I Mean IT!僠<僠WILDSTUFF僠>".getBytes(StandardCharsets.UTF_8));
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("I Mean IT!僠<僠WILDSTUFF僠>", new String(consumer.next().message(), StandardCharsets.UTF_8));
    runner.shutdown();
}
 
Example 9
Source File: PutKafkaTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateMultiCharacterDelimitedMessages() {
    String topicName = "validateMultiCharacterDemarcatedMessagesAndCustomPartitioner";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "foo");

    runner.enqueue("Hello WorldfooGoodbyefoo1foo2foo3foo4foo5".getBytes(StandardCharsets.UTF_8));
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("1", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("2", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("3", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("4", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("5", new String(consumer.next().message(), StandardCharsets.UTF_8));

    runner.shutdown();
}
 
Example 10
Source File: PutKafkaTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateSingleCharacterDemarcatedMessages() {
    String topicName = "validateSingleCharacterDemarcatedMessages";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");

    runner.enqueue("Hello World\nGoodbye\n1\n2\n3\n4\n5".getBytes(StandardCharsets.UTF_8));
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("1", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("2", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("3", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("4", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("5", new String(consumer.next().message(), StandardCharsets.UTF_8));

    runner.shutdown();
}
 
Example 11
Source File: PutKafkaTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateDeprecatedPartitionStrategy() {
    String topicName = "validateDeprecatedPartitionStrategy";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");

    // Old configuration using deprecated property still work.
    runner.setProperty(PutKafka.PARTITION_STRATEGY, PutKafka.USER_DEFINED_PARTITIONING);
    runner.setProperty(PutKafka.PARTITION, "${partition}");

    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("partition", "0");
    runner.enqueue("Hello World\nGoodbye".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));

    runner.shutdown();
}
 
Example 12
Source File: ExecuteFlumeSourceTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Does not work on Windows")
public void testSourceWithConfig() throws IOException {
    File spoolDirectory = temp.newFolder("spooldir");
    File dst = new File(spoolDirectory, "records.txt");
    FileUtils.copyFile(getClass().getResourceAsStream("/testdata/records.txt"), dst, true, false);

    TestRunner runner = TestRunners.newTestRunner(ExecuteFlumeSource.class);
    runner.setProperty(ExecuteFlumeSource.SOURCE_TYPE, "spooldir");
    runner.setProperty(ExecuteFlumeSink.FLUME_CONFIG,
        "tier1.sources.src-1.spoolDir = " + spoolDirectory.getAbsolutePath());
    runner.run(1, false, true);
    // Because the spool directory source is an event driven source, it may take some time for flow files to get
    // produced. I'm willing to wait up to 5 seconds, but will bail out early if possible. If it takes longer than
    // that then there is likely a bug.
    int numWaits = 10;
    while (runner.getFlowFilesForRelationship(ExecuteFlumeSource.SUCCESS).size() < 4 && --numWaits > 0) {
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException ex) {
            logger.warn("Sleep interrupted");
        }
    }
    runner.shutdown();
    runner.assertTransferCount(ExecuteFlumeSource.SUCCESS, 4);
    int i = 1;
    for (MockFlowFile flowFile : runner.getFlowFilesForRelationship(ExecuteFlumeSource.SUCCESS)) {
        flowFile.assertContentEquals("record " + i);
        i++;
    }
}
 
Example 13
Source File: ExecuteFlumeSourceTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Does not work on Windows")
public void testSourceWithConfig() throws IOException {
    File spoolDirectory = temp.newFolder("spooldir");
    File dst = new File(spoolDirectory, "records.txt");
    FileUtils.copyFile(getClass().getResourceAsStream("/testdata/records.txt"), dst, true, false);

    TestRunner runner = TestRunners.newTestRunner(ExecuteFlumeSource.class);
    runner.setProperty(ExecuteFlumeSource.SOURCE_TYPE, "spooldir");
    runner.setProperty(ExecuteFlumeSink.FLUME_CONFIG,
        "tier1.sources.src-1.spoolDir = " + spoolDirectory.getAbsolutePath());
    runner.run(1, false, true);
    // Because the spool directory source is an event driven source, it may take some time for flow files to get
    // produced. I'm willing to wait up to 5 seconds, but will bail out early if possible. If it takes longer than
    // that then there is likely a bug.
    int numWaits = 10;
    while (runner.getFlowFilesForRelationship(ExecuteFlumeSource.SUCCESS).size() < 4 && --numWaits > 0) {
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException ex) {
            logger.warn("Sleep interrupted");
        }
    }
    runner.shutdown();
    runner.assertTransferCount(ExecuteFlumeSource.SUCCESS, 4);
    int i = 1;
    for (MockFlowFile flowFile : runner.getFlowFilesForRelationship(ExecuteFlumeSource.SUCCESS)) {
        flowFile.assertContentEquals("record " + i);
        i++;
    }
}
 
Example 14
Source File: SpringContextProcessorTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void validateBiDirectional() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    runner.setProperty(SpringContextProcessor.CTX_CONFIG_PATH, "requestReply.xml");
    runner.setProperty(SpringContextProcessor.CTX_LIB_PATH, ".");
    runner.setProperty(SpringContextProcessor.RECEIVE_TIMEOUT, "100 millis");
    runner.assertValid();
    runner.enqueue("Hello".getBytes());

    TestProcessor processor = (TestProcessor) runner.getProcessor();
    SpringDataExchanger delegate = processor.getMockedDelegate();
    Map<String, Object> headers = new HashMap<>();
    headers.put("foo", "foo");
    headers.put("bar", new Object());
    SpringResponse<Object> r = new SpringResponse<Object>("hello".getBytes(), headers);
    when(delegate.receive(Mockito.anyLong())).thenReturn(r);
    when(delegate.send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong())).thenReturn(true);

    runner.run(1, false);
    verify(delegate, times(1)).send(Mockito.any(), Mockito.any(Map.class), Mockito.anyLong());
    verify(delegate, times(1)).receive(100);
    List<MockFlowFile> ffList = runner.getFlowFilesForRelationship(TestProcessor.REL_SUCCESS);
    assertTrue(ffList.size() == 1);
    assertEquals("foo", ffList.get(0).getAttribute("foo"));
    assertNull(ffList.get(0).getAttribute("bar"));
    runner.shutdown();
}
 
Example 15
Source File: PutKafkaTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateDeprecatedPartitionStrategy() {
    String topicName = "validateDeprecatedPartitionStrategy";
    PutKafka putKafka = new PutKafka();
    TestRunner runner = TestRunners.newTestRunner(putKafka);
    runner.setProperty(PutKafka.TOPIC, topicName);
    runner.setProperty(PutKafka.CLIENT_NAME, "foo");
    runner.setProperty(PutKafka.KEY, "key1");
    runner.setProperty(PutKafka.SEED_BROKERS, "localhost:" + kafkaLocal.getKafkaPort());
    runner.setProperty(PutKafka.MESSAGE_DELIMITER, "\n");

    // Old configuration using deprecated property still work.
    runner.setProperty(PutKafka.PARTITION_STRATEGY, PutKafka.USER_DEFINED_PARTITIONING);
    runner.setProperty(PutKafka.PARTITION, "${partition}");

    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("partition", "0");
    runner.enqueue("Hello World\nGoodbye".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run(1, false);

    runner.assertAllFlowFilesTransferred(PutKafka.REL_SUCCESS, 1);
    ConsumerIterator<byte[], byte[]> consumer = this.buildConsumer(topicName);
    assertEquals("Hello World", new String(consumer.next().message(), StandardCharsets.UTF_8));
    assertEquals("Goodbye", new String(consumer.next().message(), StandardCharsets.UTF_8));

    runner.shutdown();
}
 
Example 16
Source File: TestListenSMTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testListenSMTPwithTLS() throws Exception {
    final ListenSMTP processor = new ListenSMTP();
    final TestRunner runner = TestRunners.newTestRunner(processor);

    final int port = NetworkUtils.availablePort();
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");

    // Setup the SSL Context
    final SSLContextService sslContextService = new StandardRestrictedSSLContextService();
    runner.addControllerService("ssl-context", sslContextService);
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE, "src/test/resources/truststore.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_PASSWORD, "passwordpassword");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_TYPE, "JKS");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE, "src/test/resources/keystore.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_PASSWORD, "passwordpassword");
    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, SslContextFactory.ClientAuth.NONE.name());
    runner.assertValid();

    runner.run(1, false);

    assertTrue(String.format("expected server listening on %s:%d", "localhost", port), NetworkUtils.isListening("localhost", port, 5000));

    final Properties config = new Properties();
    config.put("mail.smtp.host", "localhost");
    config.put("mail.smtp.port", String.valueOf(port));
    config.put("mail.smtp.auth", "false");
    config.put("mail.smtp.starttls.enable", "true");
    config.put("mail.smtp.starttls.required", "true");
    config.put("mail.smtp.ssl.trust", "*");
    config.put("mail.smtp.connectiontimeout", "5000");
    config.put("mail.smtp.timeout", "5000");
    config.put("mail.smtp.writetimeout", "5000");

    final Session session = Session.getInstance(config);
    session.setDebug(true);

    final int numMessages = 5;
    for (int i = 0; i < numMessages; i++) {
        final Message email = new MimeMessage(session);
        email.setFrom(new InternetAddress("[email protected]"));
        email.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
        email.setSubject("This is a test");
        email.setText("MSG-" + i);
        Transport.send(email);
    }

    runner.shutdown();
    runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, numMessages);
}
 
Example 17
Source File: TestListenSMTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(expected = MessagingException.class)
public void testListenSMTPwithTooLargeMessage() throws Exception {
    final ListenSMTP processor = new ListenSMTP();
    final TestRunner runner = TestRunners.newTestRunner(processor);

    final int port = NetworkUtils.availablePort();
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_MSG_SIZE, "10 B");

    runner.run(1, false);

    assertTrue(String.format("expected server listening on %s:%d", "localhost", port), NetworkUtils.isListening("localhost", port, 5000));

    final Properties config = new Properties();
    config.put("mail.smtp.host", "localhost");
    config.put("mail.smtp.port", String.valueOf(port));
    config.put("mail.smtp.connectiontimeout", "5000");
    config.put("mail.smtp.timeout", "5000");
    config.put("mail.smtp.writetimeout", "5000");

    final Session session = Session.getInstance(config);
    session.setDebug(true);

    MessagingException messagingException = null;
    try {
        final Message email = new MimeMessage(session);
        email.setFrom(new InternetAddress("[email protected]"));
        email.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
        email.setSubject("This is a test");
        email.setText("MSG-0");
        Transport.send(email);
    } catch (final MessagingException e) {
        messagingException = e;
    }

    runner.shutdown();
    runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, 0);

    if (messagingException != null) throw messagingException;
}
 
Example 18
Source File: TestListenSMTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void validateSuccessfulInteraction() throws Exception, EmailException {
    int port = NetworkUtils.availablePort();

    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");

    runner.assertValid();
    runner.run(5, false);
    final int numMessages = 5;
    CountDownLatch latch = new CountDownLatch(numMessages);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < numMessages; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("[email protected]");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("[email protected]");
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1500, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, numMessages);
}
 
Example 19
Source File: TestListenSMTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void validateSuccessfulInteractionWithTls() throws Exception, EmailException {
    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");
    int port = NetworkUtils.availablePort();

    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");

    // Setup the SSL Context
    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());
    runner.assertValid();

    int messageCount = 5;
    CountDownLatch latch = new CountDownLatch(messageCount);
    runner.run(messageCount, false);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < messageCount; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("[email protected]");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("[email protected]");

                    // Enable STARTTLS but ignore the cert
                    email.setStartTLSEnabled(true);
                    email.setStartTLSRequired(true);
                    email.setSSLCheckServerIdentity(false);
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1500, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred("success", messageCount);
}
 
Example 20
Source File: TestListenSMTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void validateTooLargeMessage() throws Exception, EmailException {
    int port = NetworkUtils.availablePort();

    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");
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_MSG_SIZE, "10 B");

    runner.assertValid();

    int messageCount = 1;
    CountDownLatch latch = new CountDownLatch(messageCount);

    runner.run(messageCount, false);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < messageCount; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("[email protected]");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("[email protected]");
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1000, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred("success", 0);
}