Search in sources :

Example 96 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestListenHTTP method startWebServerAndSendMessages.

private void startWebServerAndSendMessages(final List<String> messages, int returnCode) throws Exception {
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.createHttpServer(context);
    Runnable sendMessagestoWebServer = () -> {
        try {
            for (final String message : messages) {
                if (executePOST(message) != returnCode) {
                    fail("HTTP POST failed.");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail("Not expecting error here.");
        }
    };
    new Thread(sendMessagestoWebServer).start();
    long responseTimeout = 10000;
    int numTransferred = 0;
    long startTime = System.currentTimeMillis();
    while (numTransferred < messages.size() && (System.currentTimeMillis() - startTime < responseTimeout)) {
        proc.onTrigger(context, processSessionFactory);
        numTransferred = runner.getFlowFilesForRelationship(RELATIONSHIP_SUCCESS).size();
        Thread.sleep(100);
    }
    runner.assertTransferCount(ListenHTTP.RELATIONSHIP_SUCCESS, messages.size());
}
Also used : ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) ProcessContext(org.apache.nifi.processor.ProcessContext) InitializationException(org.apache.nifi.reporting.InitializationException) IOException(java.io.IOException)

Example 97 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestListenRELP method run.

protected void run(final List<RELPFrame> frames, final int expectedTransferred, final int expectedResponses, final SSLContextService sslContextService) throws IOException, InterruptedException {
    Socket socket = null;
    try {
        // schedule to start listening on a random port
        final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
        final ProcessContext context = runner.getProcessContext();
        proc.onScheduled(context);
        // create a client connection to the port the dispatcher is listening on
        final int realPort = proc.getDispatcherPort();
        // create either a regular socket or ssl socket based on context being passed in
        if (sslContextService != null) {
            final SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED);
            socket = sslContext.getSocketFactory().createSocket("localhost", realPort);
        } else {
            socket = new Socket("localhost", realPort);
        }
        Thread.sleep(100);
        // send the frames to the port the processors is listening on
        sendFrames(frames, socket);
        long responseTimeout = 30000;
        // this first loop waits until the internal queue of the processor has the expected
        // number of messages ready before proceeding, we want to guarantee they are all there
        // before onTrigger gets a chance to run
        long startTimeQueueSizeCheck = System.currentTimeMillis();
        while (proc.getQueueSize() < expectedResponses && (System.currentTimeMillis() - startTimeQueueSizeCheck < responseTimeout)) {
            Thread.sleep(100);
        }
        // want to fail here if the queue size isn't what we expect
        Assert.assertEquals(expectedResponses, proc.getQueueSize());
        // call onTrigger until we got a respond for all the frames, or a certain amount of time passes
        long startTimeProcessing = System.currentTimeMillis();
        while (proc.responses.size() < expectedResponses && (System.currentTimeMillis() - startTimeProcessing < responseTimeout)) {
            proc.onTrigger(context, processSessionFactory);
            Thread.sleep(100);
        }
        // should have gotten a response for each frame
        Assert.assertEquals(expectedResponses, proc.responses.size());
        // should have transferred the expected events
        runner.assertTransferCount(ListenRELP.REL_SUCCESS, expectedTransferred);
    } finally {
        // unschedule to close connections
        proc.onUnscheduled();
        IOUtils.closeQuietly(socket);
    }
}
Also used : ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) SSLContext(javax.net.ssl.SSLContext) Socket(java.net.Socket) ProcessContext(org.apache.nifi.processor.ProcessContext)

Example 98 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestListenTCPRecord method runTCP.

protected void runTCP(final List<String> messages, final int expectedTransferred, final SSLContext sslContext) throws IOException, InterruptedException {
    SocketSender sender = null;
    try {
        // schedule to start listening on a random port
        final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
        final ProcessContext context = runner.getProcessContext();
        proc.onScheduled(context);
        Thread.sleep(100);
        sender = new SocketSender(proc.getDispatcherPort(), "localhost", sslContext, messages, 0);
        final Thread senderThread = new Thread(sender);
        senderThread.setDaemon(true);
        senderThread.start();
        long timeout = 10000;
        // call onTrigger until we processed all the records, or a certain amount of time passes
        int numTransferred = 0;
        long startTime = System.currentTimeMillis();
        while (numTransferred < expectedTransferred && (System.currentTimeMillis() - startTime < timeout)) {
            proc.onTrigger(context, processSessionFactory);
            numTransferred = runner.getFlowFilesForRelationship(ListenTCPRecord.REL_SUCCESS).size();
            Thread.sleep(100);
        }
        // should have transferred the expected events
        runner.assertTransferCount(ListenTCPRecord.REL_SUCCESS, expectedTransferred);
    } finally {
        // unschedule to close connections
        proc.onStopped();
        IOUtils.closeQuietly(sender);
    }
}
Also used : ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) ProcessContext(org.apache.nifi.processor.ProcessContext)

Example 99 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestLogAttribute method testLogPropertyRegexNoIgnore.

@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")));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockComponentLog(org.apache.nifi.util.MockComponentLog) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 100 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestLogAttribute method testLogPropertyRegexWithIgnoreRegex.

@Test
public void testLogPropertyRegexWithIgnoreRegex() {
    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();
    // includes foo,foobaz
    runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_REGEX, "foo.*");
    // includes foobaz
    runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_REGEX, "foobaz.*");
    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")));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockComponentLog(org.apache.nifi.util.MockComponentLog) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Aggregations

ProcessContext (org.apache.nifi.processor.ProcessContext)115 Test (org.junit.Test)67 TestRunner (org.apache.nifi.util.TestRunner)56 ProcessSession (org.apache.nifi.processor.ProcessSession)49 FlowFile (org.apache.nifi.flowfile.FlowFile)40 MockFlowFile (org.apache.nifi.util.MockFlowFile)39 HashSet (java.util.HashSet)35 Relationship (org.apache.nifi.processor.Relationship)35 List (java.util.List)34 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)34 ArrayList (java.util.ArrayList)33 Set (java.util.Set)33 Tags (org.apache.nifi.annotation.documentation.Tags)31 IOException (java.io.IOException)30 HashMap (java.util.HashMap)30 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)30 ProcessException (org.apache.nifi.processor.exception.ProcessException)30 Collections (java.util.Collections)29 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)29 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)29