Search in sources :

Example 66 with TestRunner

use of org.apache.nifi.util.TestRunner in project nifi by apache.

the class TestConsumeEmail method validateUrl.

@Test
public void validateUrl() throws Exception {
    Field displayUrlField = AbstractEmailProcessor.class.getDeclaredField("displayUrl");
    displayUrlField.setAccessible(true);
    AbstractEmailProcessor<? extends AbstractMailReceiver> consume = new ConsumeIMAP();
    TestRunner runner = TestRunners.newTestRunner(consume);
    runner.setProperty(ConsumeIMAP.HOST, "foo.bar.com");
    runner.setProperty(ConsumeIMAP.PORT, "1234");
    runner.setProperty(ConsumeIMAP.USER, "jon");
    runner.setProperty(ConsumeIMAP.PASSWORD, "qhgwjgehr");
    runner.setProperty(ConsumeIMAP.FOLDER, "MYBOX");
    runner.setProperty(ConsumeIMAP.USE_SSL, "false");
    assertEquals("imap://jon:qhgwjgehr@foo.bar.com:1234/MYBOX", consume.buildUrl(runner.getProcessContext()));
    assertEquals("imap://jon:[password]@foo.bar.com:1234/MYBOX", displayUrlField.get(consume));
}
Also used : Field(java.lang.reflect.Field) TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 67 with TestRunner

use of org.apache.nifi.util.TestRunner in project nifi by apache.

the class TestExtractEmailAttachments method testValidEmailWithoutAttachments.

@Test
public void testValidEmailWithoutAttachments() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailAttachments());
    // Create the message dynamically
    byte[] simpleEmail = attachmentGenerator.SimpleEmail();
    runner.enqueue(simpleEmail);
    runner.run();
    runner.assertTransferCount(ExtractEmailAttachments.REL_ORIGINAL, 1);
    runner.assertTransferCount(ExtractEmailAttachments.REL_FAILURE, 0);
    runner.assertTransferCount(ExtractEmailAttachments.REL_ATTACHMENTS, 0);
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test)

Example 68 with TestRunner

use of org.apache.nifi.util.TestRunner in project nifi by apache.

the class TestExtractEmailAttachments method testValidEmailWithMultipleAttachments.

@Test
public void testValidEmailWithMultipleAttachments() throws Exception {
    Random rnd = new Random();
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailAttachments());
    // Create the message dynamically
    int amount = rnd.nextInt(10) + 1;
    byte[] withAttachment = attachmentGenerator.WithAttachments(amount);
    runner.enqueue(withAttachment);
    runner.run();
    runner.assertTransferCount(ExtractEmailAttachments.REL_ORIGINAL, 1);
    runner.assertTransferCount(ExtractEmailAttachments.REL_FAILURE, 0);
    runner.assertTransferCount(ExtractEmailAttachments.REL_ATTACHMENTS, amount);
    // Have a look at the attachments...
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailAttachments.REL_ATTACHMENTS);
    List<String> filenames = new ArrayList<>();
    for (int a = 0; a < amount; a++) {
        filenames.add(splits.get(a).getAttribute("filename").toString());
    }
    Assert.assertTrue(filenames.containsAll(Arrays.asList("pom.xml1", "pom.xml" + amount)));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Random(java.util.Random) TestRunner(org.apache.nifi.util.TestRunner) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 69 with TestRunner

use of org.apache.nifi.util.TestRunner in project nifi by apache.

the class TestExtractEmailAttachments method testValidEmailWithAttachments.

@Test
public void testValidEmailWithAttachments() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailAttachments());
    // Create the message dynamically
    byte[] withAttachment = attachmentGenerator.WithAttachments(1);
    runner.enqueue(withAttachment);
    runner.run();
    runner.assertTransferCount(ExtractEmailAttachments.REL_ORIGINAL, 1);
    runner.assertTransferCount(ExtractEmailAttachments.REL_FAILURE, 0);
    runner.assertTransferCount(ExtractEmailAttachments.REL_ATTACHMENTS, 1);
    // Have a look at the attachments...
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailAttachments.REL_ATTACHMENTS);
    splits.get(0).assertAttributeEquals("filename", "pom.xml1");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test)

Example 70 with TestRunner

use of org.apache.nifi.util.TestRunner in project nifi by apache.

the class TestExtractEmailHeaders method testStrictParsingFailsForInvalidAddresses.

/**
 * NIFI-4326 adds a new feature to disable strict address parsing for
 * mailbox list header fields. This is a test case that asserts that
 * strict address parsing fails (when set to "strict=true") for malformed
 * addresses.
 */
@Test
public void testStrictParsingFailsForInvalidAddresses() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.STRICT_PARSING, "true");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.setHeader("From", "<bad_email>");
    simpleEmailMimeMessage.setHeader("To", "<>, Joe, <invalid>");
    ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
    try {
        simpleEmailMimeMessage.writeTo(messageBytes);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
    runner.enqueue(messageBytes.toByteArray());
    runner.run();
    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 0);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 1);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TestRunner(org.apache.nifi.util.TestRunner) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TestRunner (org.apache.nifi.util.TestRunner)1337 Test (org.junit.Test)1315 MockFlowFile (org.apache.nifi.util.MockFlowFile)741 HashMap (java.util.HashMap)297 File (java.io.File)137 ProcessContext (org.apache.nifi.processor.ProcessContext)56 Connection (java.sql.Connection)44 Statement (java.sql.Statement)37 UpdateAttribute (org.apache.nifi.processors.attributes.UpdateAttribute)32 ProcessSession (org.apache.nifi.processor.ProcessSession)31 ResultSet (java.sql.ResultSet)30 Schema (org.apache.avro.Schema)28 Relationship (org.apache.nifi.processor.Relationship)27 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)27 ByteArrayInputStream (java.io.ByteArrayInputStream)26 MockProcessContext (org.apache.nifi.util.MockProcessContext)25 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)24 Ignore (org.junit.Ignore)24 InputStream (java.io.InputStream)22 PutFlowFile (org.apache.nifi.hbase.put.PutFlowFile)22