Search in sources :

Example 71 with TestRunner

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

the class TestExtractEmailHeaders method testValidEmailWithAttachments.

@Test
public void testValidEmailWithAttachments() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    // Create the message dynamically
    byte[] withAttachment = attachmentGenerator.WithAttachments(1);
    runner.enqueue(withAttachment);
    runner.run();
    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);
    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", from);
    splits.get(0).assertAttributeEquals("email.headers.to.0", to);
    splits.get(0).assertAttributeEquals("email.headers.subject", subject);
    splits.get(0).assertAttributeEquals("email.attachment_count", "1");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test)

Example 72 with TestRunner

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

the class TestExtractEmailHeaders method testValidEmailWithNoRecipients.

/**
 * Test case added for NIFI-4326 for a potential NPE bug
 * if the email message contains no recipient header fields, ie,
 * TO, CC, BCC.
 */
@Test
public void testValidEmailWithNoRecipients() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.CAPTURED_HEADERS, "MIME-Version");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.removeHeader("To");
    simpleEmailMimeMessage.removeHeader("Cc");
    simpleEmailMimeMessage.removeHeader("Bcc");
    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, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);
    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", from);
    splits.get(0).assertAttributeExists("email.headers.mime-version");
    splits.get(0).assertAttributeNotExists("email.headers.to");
    splits.get(0).assertAttributeNotExists("email.headers.cc");
    splits.get(0).assertAttributeNotExists("email.headers.bcc");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) 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)

Example 73 with TestRunner

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

the class TestExtractEmailHeaders method testNonStrictParsingPassesForInvalidAddresses.

/**
 * NIFI-4326 adds a new feature to disable strict address parsing for
 * mailbox list header fields. This is a test case that asserts that
 * lax address parsing passes (when set to "strict=false") for malformed
 * addresses.
 */
@Test
public void testNonStrictParsingPassesForInvalidAddresses() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.STRICT_PARSING, "false");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.setHeader("From", "<bad_email>");
    simpleEmailMimeMessage.setHeader("To", "<>, Joe, \"\" <>");
    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, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);
    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", "bad_email");
    splits.get(0).assertAttributeEquals("email.headers.to.0", "");
    splits.get(0).assertAttributeEquals("email.headers.to.1", "Joe");
    splits.get(0).assertAttributeEquals("email.headers.to.2", "");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) 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)

Example 74 with TestRunner

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

the class TestExtractTNEFAttachments method testValidTNEFWithoutAttachment.

@Test
public void testValidTNEFWithoutAttachment() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractTNEFAttachments());
    runner.enqueue(Paths.get("src/test/resources/winmail-simple.dat"));
    runner.run();
    runner.assertTransferCount(ExtractEmailAttachments.REL_ORIGINAL, 1);
    runner.assertTransferCount(ExtractEmailAttachments.REL_FAILURE, 0);
    runner.assertTransferCount(ExtractEmailAttachments.REL_ATTACHMENTS, 0);
    // Have a look at the attachments...
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailAttachments.REL_ATTACHMENTS);
    Assert.assertTrue(splits.size() == 0);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test)

Example 75 with TestRunner

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

the class TestExtractTNEFAttachments method testValidTNEFWithAttachment.

@Test
public void testValidTNEFWithAttachment() throws Exception {
    Random rnd = new Random();
    final TestRunner runner = TestRunners.newTestRunner(new ExtractTNEFAttachments());
    runner.enqueue(Paths.get("src/test/resources/winmail-with-attachment.dat"));
    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(ExtractTNEFAttachments.REL_ATTACHMENTS);
    List<String> filenames = new ArrayList<>();
    for (int a = 0; a < splits.size(); a++) {
        filenames.add(splits.get(a).getAttribute("filename").toString());
    }
    Assert.assertTrue(filenames.containsAll(Arrays.asList("nifiDrop.svg")));
}
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)

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