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

The following examples show how to use org.apache.nifi.util.TestRunner#getProcessContext() . 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: TestLogAttribute.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogPropertyWithCSVAndRegexNoIgnore() {
    final LogAttribute logAttribute = new LogAttribute();
    final TestRunner runner = TestRunners.newTestRunner(logAttribute);
    final ProcessContext context = runner.getProcessContext();
    final ProcessSession session = runner.getProcessSessionFactory().createSession();
    final MockComponentLog LOG = runner.getLogger();

    // there's an AND relationship between like properties, so only foo should be logged in this case
    runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo, bar");
    runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_REGEX, "foo*");

    final Map<String,String> attrs = Maps.newHashMap();
    attrs.put("foo", "foo-value");
    attrs.put("bar", "bar-value");
    attrs.put("foobaz", "foobaz-value");

    final MockFlowFile flowFile = runner.enqueue("content", attrs);

    final String logMessage = logAttribute.processFlowFile(LOG, LogAttribute.DebugLevels.info, flowFile, session, context);
    assertThat(logMessage, not(containsString("foobaz-value")));
    assertThat(logMessage, containsString("foo-value"));
    assertThat(logMessage, not(containsString("bar-value")));
}
 
Example 2
Source File: TestListenSyslog.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testParsingError() throws IOException {
    final FailParseProcessor proc = new FailParseProcessor();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.UDP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");

    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);

    try {
        final int port = proc.getPort();
        final DatagramSender sender = new DatagramSender(port, 1, 1, INVALID_MESSAGE);
        sender.run();

        // should keep re-processing event1 from the error queue
        proc.onTrigger(context, processSessionFactory);
        runner.assertTransferCount(ListenSyslog.REL_INVALID, 1);
        runner.assertTransferCount(ListenSyslog.REL_SUCCESS, 0);
    } finally {
        proc.onUnscheduled();
    }
}
 
Example 3
Source File: TestEncryptContent.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldValidatePGPPublicKeyringContainsUserId() {
    // Arrange
    final TestRunner runner = TestRunners.newTestRunner(EncryptContent.class);
    Collection<ValidationResult> results;
    MockProcessContext pc;

    runner.setProperty(EncryptContent.MODE, EncryptContent.ENCRYPT_MODE);
    runner.setProperty(EncryptContent.ENCRYPTION_ALGORITHM, EncryptionMethod.PGP.name());
    runner.setProperty(EncryptContent.PUBLIC_KEYRING, "src/test/resources/TestEncryptContent/pubring.gpg");
    runner.setProperty(EncryptContent.PUBLIC_KEY_USERID, "USERID");
    runner.enqueue(new byte[0]);
    pc = (MockProcessContext) runner.getProcessContext();

    // Act
    results = pc.validate();

    // Assert
    Assert.assertEquals(1, results.size());
    ValidationResult vr = (ValidationResult) results.toArray()[0];
    String expectedResult = "PGPException: Could not find a public key with the given userId";
    String message = "'" + vr.toString() + "' contains '" + expectedResult + "'";
    Assert.assertTrue(message, vr.toString().contains(expectedResult));
}
 
Example 4
Source File: TestMergeContent.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileDelimitersValidation() throws IOException, InterruptedException {
    final String doesNotExistFile = "src/test/resources/TestMergeContent/does_not_exist";
    final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
    runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
    runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_CONCAT);
    runner.setProperty(MergeContent.DELIMITER_STRATEGY, MergeContent.DELIMITER_STRATEGY_FILENAME);
    runner.setProperty(MergeContent.HEADER, doesNotExistFile);
    runner.setProperty(MergeContent.DEMARCATOR, doesNotExistFile);
    runner.setProperty(MergeContent.FOOTER, doesNotExistFile);

    Collection<ValidationResult> results = new HashSet<>();
    ProcessContext context = runner.getProcessContext();
    if (context instanceof MockProcessContext) {
        MockProcessContext mockContext = (MockProcessContext)context;
        results = mockContext.validate();
    }

    Assert.assertEquals(3, results.size());
    for (ValidationResult vr : results) {
        Assert.assertTrue(vr.toString().contains("is invalid because File " + new File(doesNotExistFile).toString() + " does not exist"));
    }
}
 
Example 5
Source File: TestExecuteProcess.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testRedirectErrorStreamWithExpressions() {
    final TestRunner runner = TestRunners.newTestRunner(ExecuteProcess.class);
    runner.setProperty(ExecuteProcess.COMMAND, "ls");
    runner.setProperty(ExecuteProcess.COMMAND_ARGUMENTS, "${literal('does-not-exist'):toUpper()}");
    runner.setProperty(ExecuteProcess.REDIRECT_ERROR_STREAM, "true");

    ProcessContext processContext = runner.getProcessContext();

    ExecuteProcess processor = (ExecuteProcess) runner.getProcessor();
    processor.updateScheduledTrue();
    processor.setupExecutor(processContext);

    processor.onTrigger(processContext, runner.getProcessSessionFactory());

    if (isCommandFailed(runner)) return;

    final List<LogMessage> warnMessages = runner.getLogger().getWarnMessages();
    assertEquals("If redirect error stream is true " +
            "the output should be sent as a content of flow-file.", 0, warnMessages.size());
    final List<MockFlowFile> succeeded = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS);
    assertEquals(1, succeeded.size());
    assertTrue(new String(succeeded.get(0).toByteArray()).contains("DOES-NOT-EXIST"));
    assertEquals(succeeded.get(0).getAttribute(ExecuteProcess.ATTRIBUTE_COMMAND), "ls");
    assertEquals(succeeded.get(0).getAttribute(ExecuteProcess.ATTRIBUTE_COMMAND_ARGS), "DOES-NOT-EXIST");
}
 
Example 6
Source File: MoveHDFSTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testOutputDirectoryValidator() {
    MoveHDFS proc = new TestableMoveHDFS(kerberosProperties);
    TestRunner runner = TestRunners.newTestRunner(proc);
    Collection<ValidationResult> results;
    ProcessContext pc;

    results = new HashSet<>();
    runner.setProperty(MoveHDFS.INPUT_DIRECTORY_OR_FILE, "/source");
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(1, results.size());
    for (ValidationResult vr : results) {
        assertTrue(vr.toString().contains("Output Directory is required"));
    }
}
 
Example 7
Source File: TestMergeContent.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTextDelimitersValidation() throws IOException, InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
    runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
    runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_CONCAT);
    runner.setProperty(MergeContent.DELIMITER_STRATEGY, MergeContent.DELIMITER_STRATEGY_TEXT);
    runner.setProperty(MergeContent.HEADER, "");
    runner.setProperty(MergeContent.DEMARCATOR, "");
    runner.setProperty(MergeContent.FOOTER, "");

    Collection<ValidationResult> results = new HashSet<>();
    ProcessContext context = runner.getProcessContext();
    if (context instanceof MockProcessContext) {
        MockProcessContext mockContext = (MockProcessContext)context;
        results = mockContext.validate();
    }

    Assert.assertEquals(3, results.size());
    for (ValidationResult vr : results) {
        Assert.assertTrue(vr.toString().contains("cannot be empty"));
    }
}
 
Example 8
Source File: TestJmsConsumer.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Test MapMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileMapMessage() throws Exception {

    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    MapMessage mapMessage = createMapMessage();

    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());

    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, mapMessage, true, pic.getLogger());

    assertEquals("MapMessage should not create FlowFile content", 0, summary.getBytesReceived());

    Map<String, String> attributes = summary.getLastFlowFile().getAttributes();
    assertEquals("", "Arnold", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "name"));
    assertEquals("", "97", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "age"));
    assertEquals("", "89686.564", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "xyz"));
    assertEquals("", "true", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "good"));
}
 
Example 9
Source File: TestEncryptContent.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldValidatePGPPublicKeyringIsProperFormat() {
    // Arrange
    final TestRunner runner = TestRunners.newTestRunner(EncryptContent.class);
    Collection<ValidationResult> results;
    MockProcessContext pc;

    runner.setProperty(EncryptContent.MODE, EncryptContent.ENCRYPT_MODE);
    runner.setProperty(EncryptContent.ENCRYPTION_ALGORITHM, EncryptionMethod.PGP.name());
    runner.setProperty(EncryptContent.PUBLIC_KEYRING, "src/test/resources/TestEncryptContent/text.txt");
    runner.setProperty(EncryptContent.PUBLIC_KEY_USERID, "USERID");
    runner.enqueue(new byte[0]);
    pc = (MockProcessContext) runner.getProcessContext();

    // Act
    results = pc.validate();

    // Assert
    Assert.assertEquals(1, results.size());
    ValidationResult vr = (ValidationResult) results.toArray()[0];
    String expectedResult = " java.io.IOException: invalid header encountered";
    String message = "'" + vr.toString() + "' contains '" + expectedResult + "'";
    Assert.assertTrue(message, vr.toString().contains(expectedResult));
}
 
Example 10
Source File: TestLogAttribute.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogPropertyRegexNoIgnore() {
    final LogAttribute logAttribute = new LogAttribute();
    final TestRunner runner = TestRunners.newTestRunner(logAttribute);
    final ProcessContext context = runner.getProcessContext();
    final ProcessSession session = runner.getProcessSessionFactory().createSession();
    final MockComponentLog LOG = runner.getLogger();

    runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_REGEX, "foo.*");

    final Map<String,String> attrs = Maps.newHashMap();
    attrs.put("foo", "foo-value");
    attrs.put("bar", "bar-value");
    attrs.put("foobaz", "foobaz-value");

    final MockFlowFile flowFile = runner.enqueue("content", attrs);

    final String logMessage = logAttribute.processFlowFile(LOG, LogAttribute.DebugLevels.info, flowFile, session, context);
    assertThat(logMessage, containsString("foobaz-value"));
    assertThat(logMessage, containsString("foo-value"));
    assertThat(logMessage, not(containsString("bar-value")));
}
 
Example 11
Source File: TestConfigurationProperty.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigurationCanary() throws IOException {
    TestRunner runner = TestRunners.newTestRunner(StoreInKiteDataset.class);
    runner.setProperty(
            AbstractKiteProcessor.CONF_XML_FILES, confLocation.toString());

    Assert.assertFalse("Should not contain canary value",
            DefaultConfiguration.get().getBoolean("nifi.config.canary", false));

    AbstractKiteProcessor processor = new StoreInKiteDataset();
    ProcessContext context = runner.getProcessContext();
    processor.setDefaultConfiguration(context);

    Assert.assertTrue("Should contain canary value",
            DefaultConfiguration.get().getBoolean("nifi.config.canary", false));
}
 
Example 12
Source File: TestExtractMediaMetadata.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testRelationships() {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata());
    ProcessContext context = runner.getProcessContext();
    Set<Relationship> relationships = context.getAvailableRelationships();
    assertEquals(2, relationships.size());
    assertTrue(relationships.contains(ExtractMediaMetadata.SUCCESS));
    assertTrue(relationships.contains(ExtractMediaMetadata.FAILURE));
}
 
Example 13
Source File: TestExecuteProcess.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotRedirectErrorStream() {
    final TestRunner runner = TestRunners.newTestRunner(ExecuteProcess.class);
    runner.setProperty(ExecuteProcess.COMMAND, "cd");
    runner.setProperty(ExecuteProcess.COMMAND_ARGUMENTS, "does-not-exist");

    ProcessContext processContext = runner.getProcessContext();

    ExecuteProcess processor = (ExecuteProcess) runner.getProcessor();
    processor.updateScheduledTrue();
    processor.setupExecutor(processContext);

    processor.onTrigger(processContext, runner.getProcessSessionFactory());

    if (isCommandFailed(runner)) return;

    // ExecuteProcess doesn't wait for finishing to drain error stream if it's configure NOT to redirect stream.
    // This causes test failure when draining the error stream didn't finish
    // fast enough before the thread of this test case method checks the warn msg count.
    // So, this loop wait for a while until the log msg count becomes expected number, otherwise let it fail.
    final int expectedWarningMessages = 1;
    final int maxRetry = 5;
    for (int i = 0; i < maxRetry
        && (runner.getLogger().getWarnMessages().size() < expectedWarningMessages); i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    final List<LogMessage> warnMessages = runner.getLogger().getWarnMessages();
    assertEquals("If redirect error stream is false, " +
            "the output should be logged as a warning so that user can notice on bulletin.", expectedWarningMessages, warnMessages.size());
    final List<MockFlowFile> succeeded = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS);
    assertEquals(0, succeeded.size());
}
 
Example 14
Source File: TestExtractMediaMetadata.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProperties() {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata());
    ProcessContext context = runner.getProcessContext();
    Map<PropertyDescriptor, String> propertyValues = context.getProperties();
    assertEquals(4, propertyValues.size());
}
 
Example 15
Source File: TestJmsConsumer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMap2FlowFileTextMessage() throws Exception {

    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    TextMessage textMessage = new ActiveMQTextMessage();

    String payload = "Hello world!";
    textMessage.setText(payload);

    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());

    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, textMessage, true, pic.getLogger());

    assertEquals("TextMessage content length should equal to FlowFile content size", payload.length(), summary.getLastFlowFile().getSize());

    final byte[] buffer = new byte[payload.length()];
    runner.clearTransferState();

    session.read(summary.getLastFlowFile(), new InputStreamCallback() {
        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });

    String contentString = new String(buffer, "UTF-8");
    assertEquals("", payload, contentString);
}
 
Example 16
Source File: TestJmsConsumer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Test BytesMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileBytesMessage() throws Exception {

    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    BytesMessage bytesMessage = new ActiveMQBytesMessage();

    String sourceString = "Apache NiFi is an easy to use, powerful, and reliable system to process and distribute data.!";
    byte[] payload = sourceString.getBytes("UTF-8");
    bytesMessage.writeBytes(payload);
    bytesMessage.reset();

    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());

    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, bytesMessage, true, pic.getLogger());

    assertEquals("BytesMessage content length should equal to FlowFile content size", payload.length, summary.getLastFlowFile().getSize());

    final byte[] buffer = new byte[payload.length];
    runner.clearTransferState();

    session.read(summary.getLastFlowFile(), new InputStreamCallback() {
        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });

    String contentString = new String(buffer, "UTF-8");
    assertEquals("", sourceString, contentString);
}
 
Example 17
Source File: ITListenGRPC.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecureTwoWaySSLPassAuthorizedDNCheck() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
    final int randPort = TestGRPCClient.randomPort();
    final Map<String, String> sslProperties = getKeystoreProperties();
    sslProperties.putAll(getTruststoreProperties());
    final ManagedChannel channel = TestGRPCClient.buildChannel(HOST, randPort, sslProperties);
    final FlowFileServiceGrpc.FlowFileServiceBlockingStub stub = FlowFileServiceGrpc.newBlockingStub(channel);

    final ListenGRPC listenGRPC = new ListenGRPC();
    final TestRunner runner = TestRunners.newTestRunner(listenGRPC);
    runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, String.valueOf(randPort));
    runner.setProperty(ListenGRPC.PROP_USE_SECURE, "true");
    runner.setProperty(ListenGRPC.PROP_AUTHORIZED_DN_PATTERN, "CN=localhost.*");
    useSSLContextService(runner, sslProperties);

    final ProcessContext processContext = runner.getProcessContext();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();

    try {
        // start the server. The order of the following statements shouldn't matter, because the
        // startServer() method waits for a processSessionFactory to be available to it.
        listenGRPC.startServer(processContext);
        listenGRPC.onTrigger(processContext, processSessionFactory);


        final FlowFileRequest ingestFile = FlowFileRequest.newBuilder()
                .putAttributes("FOO", "BAR")
                .setContent(ByteString.copyFrom("content".getBytes()))
                .build();
        final FlowFileReply reply = stub.send(ingestFile);
        assertThat(reply.getResponseCode(), equalTo(FlowFileReply.ResponseCode.SUCCESS));
        assertThat(reply.getBody(), equalTo("FlowFile successfully received."));

        runner.assertTransferCount(ListenGRPC.REL_SUCCESS, 1);
        final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ListenGRPC.REL_SUCCESS);
        assertThat(successFiles.size(), equalTo(1));
        final MockFlowFile mockFlowFile = successFiles.get(0);
        assertThat(mockFlowFile.getAttribute("FOO"), equalTo("BAR"));
        assertThat(mockFlowFile.getAttribute(ListenGRPC.REMOTE_HOST), equalTo("127.0.0.1"));
        assertThat(mockFlowFile.getAttribute(ListenGRPC.REMOTE_USER_DN), equalTo(CERT_DN));

    } finally {
        // stop the server
        listenGRPC.stopServer(processContext);
        channel.shutdown();
    }
}
 
Example 18
Source File: TestListenSyslog.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTCPMultipleConnection() throws IOException, InterruptedException {
    final ListenSyslog proc = new ListenSyslog();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.TCP_VALUE.getValue());
    runner.setProperty(ListenSyslog.MAX_CONNECTIONS, "5");
    runner.setProperty(ListenSyslog.PORT, "0");

    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);

    final int numMessages = 20;
    final int port = proc.getPort();
    Assert.assertTrue(port > 0);

    // write some TCP messages to the port in the background
    final Thread sender = new Thread(new MultiConnectionSocketSender(port, numMessages, 10, VALID_MESSAGE_TCP));
    sender.setDaemon(true);
    sender.start();

    // call onTrigger until we read all messages, or 30 seconds passed
    try {
        int nubTransferred = 0;
        long timeout = System.currentTimeMillis() + 30000;

        while (nubTransferred < numMessages && System.currentTimeMillis() < timeout) {
            Thread.sleep(10);
            proc.onTrigger(context, processSessionFactory);
            nubTransferred = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).size();
        }
        Assert.assertEquals("Did not process all the messages", numMessages, nubTransferred);

        MockFlowFile flowFile = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0);
        checkFlowFile(flowFile, 0, ListenSyslog.TCP_VALUE.getValue());

        final List<ProvenanceEventRecord> events = runner.getProvenanceEvents();
        Assert.assertNotNull(events);
        Assert.assertEquals(numMessages, events.size());

        final ProvenanceEventRecord event = events.get(0);
        Assert.assertEquals(ProvenanceEventType.RECEIVE, event.getEventType());
        Assert.assertTrue("transit uri must be set and start with proper protocol", event.getTransitUri().toLowerCase().startsWith("tcp"));

    } finally {
        // unschedule to close connections
        proc.onUnscheduled();
    }
}
 
Example 19
Source File: ITListenSyslog.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testUDP() throws IOException, InterruptedException {
    final ListenSyslog proc = new ListenSyslog();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.UDP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");

    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);

    final int numMessages = 20;
    final int port = proc.getPort();
    Assert.assertTrue(port > 0);

    // write some UDP messages to the port in the background
    final Thread sender = new Thread(new DatagramSender(port, numMessages, 10, VALID_MESSAGE));
    sender.setDaemon(true);
    sender.start();

    // call onTrigger until we read all datagrams, or 30 seconds passed
    try {
        int numTransferred = 0;
        long timeout = System.currentTimeMillis() + 30000;

        while (numTransferred < numMessages && System.currentTimeMillis() < timeout) {
            Thread.sleep(10);
            proc.onTrigger(context, processSessionFactory);
            numTransferred = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).size();
        }
        Assert.assertEquals("Did not process all the datagrams", numMessages, numTransferred);

        MockFlowFile flowFile = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0);
        checkFlowFile(flowFile, 0, ListenSyslog.UDP_VALUE.getValue());

        final List<ProvenanceEventRecord> events = runner.getProvenanceEvents();
        Assert.assertNotNull(events);
        Assert.assertEquals(numMessages, events.size());

        final ProvenanceEventRecord event = events.get(0);
        Assert.assertEquals(ProvenanceEventType.RECEIVE, event.getEventType());
        Assert.assertTrue("transit uri must be set and start with proper protocol", event.getTransitUri().toLowerCase().startsWith("udp"));

    } finally {
        // unschedule to close connections
        proc.onUnscheduled();
    }
}
 
Example 20
Source File: ITListenSyslog.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTCPSingleConnectionWithNewLines() throws IOException, InterruptedException {
    final ListenSyslog proc = new ListenSyslog();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.TCP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");

    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);

    final int numMessages = 3;
    final int port = proc.getPort();
    Assert.assertTrue(port > 0);

    // send 3 messages as 1
    final String multipleMessages = VALID_MESSAGE_TCP + "\n" + VALID_MESSAGE_TCP + "\n" + VALID_MESSAGE_TCP + "\n";
    final Thread sender = new Thread(new SingleConnectionSocketSender(port, 1, 10, multipleMessages));
    sender.setDaemon(true);
    sender.start();

    // call onTrigger until we read all messages, or 30 seconds passed
    try {
        int nubTransferred = 0;
        long timeout = System.currentTimeMillis() + 30000;

        while (nubTransferred < numMessages && System.currentTimeMillis() < timeout) {
            Thread.sleep(10);
            proc.onTrigger(context, processSessionFactory);
            nubTransferred = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).size();
        }
        Assert.assertEquals("Did not process all the messages", numMessages, nubTransferred);

        MockFlowFile flowFile = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0);
        checkFlowFile(flowFile, 0, ListenSyslog.TCP_VALUE.getValue());

        final List<ProvenanceEventRecord> events = runner.getProvenanceEvents();
        Assert.assertNotNull(events);
        Assert.assertEquals(numMessages, events.size());

        final ProvenanceEventRecord event = events.get(0);
        Assert.assertEquals(ProvenanceEventType.RECEIVE, event.getEventType());
        Assert.assertTrue("transit uri must be set and start with proper protocol", event.getTransitUri().toLowerCase().startsWith("tcp"));

    } finally {
        // unschedule to close connections
        proc.onUnscheduled();
    }
}