Search in sources :

Example 6 with ProcessorInitializationContext

use of org.apache.nifi.processor.ProcessorInitializationContext in project kylo by Teradata.

the class SpringSecurityContextLoader method create.

/**
 * Creates a new Security Context Loader from the specified processor context.
 *
 * @param processorContext the processor initialization context
 * @return the Security Context Loader
 */
@Nonnull
public static SpringSecurityContextLoader create(@Nonnull final ProcessorInitializationContext processorContext) {
    final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerSingleton(ProcessorInitializationContext.class.getName(), processorContext);
    final GenericApplicationContext context = new GenericApplicationContext(beanFactory);
    context.refresh();
    return create(context);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Nonnull(javax.annotation.Nonnull)

Example 7 with ProcessorInitializationContext

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

the class TestStandardProcessorNode method createProcessorNode.

private StandardProcessorNode createProcessorNode(final Processor processor, final ReloadComponent reloadComponent) {
    final String uuid = UUID.randomUUID().toString();
    final ValidationContextFactory validationContextFactory = createValidationContextFactory();
    final NiFiProperties niFiProperties = NiFiProperties.createBasicNiFiProperties("src/test/resources/conf/nifi.properties", null);
    final ProcessScheduler processScheduler = Mockito.mock(ProcessScheduler.class);
    final TerminationAwareLogger componentLog = Mockito.mock(TerminationAwareLogger.class);
    final Bundle systemBundle = SystemBundle.create(niFiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    ExtensionManager.createInstanceClassLoader(processor.getClass().getName(), uuid, systemBundle, null);
    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, componentLog, null, null, null);
    processor.initialize(initContext);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), componentLog);
    return new StandardProcessorNode(loggableComponent, uuid, validationContextFactory, processScheduler, null, niFiProperties, new StandardComponentVariableRegistry(variableRegistry), reloadComponent);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) ModifiesClasspathNoAnnotationProcessor(org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor) Processor(org.apache.nifi.processor.Processor) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) Bundle(org.apache.nifi.bundle.Bundle) SystemBundle(org.apache.nifi.nar.SystemBundle) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext)

Example 8 with ProcessorInitializationContext

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

the class TestStandardProcessorNode method testStart.

@Test(timeout = 10000)
public void testStart() throws InterruptedException {
    final ProcessorThatThrowsExceptionOnScheduled processor = new ProcessorThatThrowsExceptionOnScheduled();
    final String uuid = UUID.randomUUID().toString();
    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, null, null, null, null);
    processor.initialize(initContext);
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final BundleCoordinate coordinate = Mockito.mock(BundleCoordinate.class);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, coordinate, null);
    final StandardProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, createValidationContextFactory(), null, null, NiFiProperties.createBasicNiFiProperties(null, null), new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent);
    final ScheduledExecutorService taskScheduler = new FlowEngine(1, "TestClasspathResources", true);
    final StandardProcessContext processContext = new StandardProcessContext(procNode, null, null, null, () -> false);
    final SchedulingAgentCallback schedulingAgentCallback = new SchedulingAgentCallback() {

        @Override
        public void onTaskComplete() {
        }

        @Override
        public Future<?> scheduleTask(final Callable<?> task) {
            return taskScheduler.submit(task);
        }

        @Override
        public void trigger() {
            Assert.fail("Should not have completed");
        }
    };
    procNode.start(taskScheduler, 20000L, processContext, schedulingAgentCallback, true);
    Thread.sleep(1000L);
    assertEquals(1, processor.onScheduledCount);
    assertEquals(1, processor.onUnscheduledCount);
    assertEquals(1, processor.onStoppedCount);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ModifiesClasspathNoAnnotationProcessor(org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor) Processor(org.apache.nifi.processor.Processor) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) FlowEngine(org.apache.nifi.engine.FlowEngine) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) Callable(java.util.concurrent.Callable) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) Test(org.junit.Test)

Example 9 with ProcessorInitializationContext

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

the class ITConsumeKafka method validateConsumerRetainer.

@Test
public void validateConsumerRetainer() throws Exception {
    // skip if on windows
    assumeFalse(isWindowsEnvironment());
    final ConsumerPool consumerPool = mock(ConsumerPool.class);
    final ConsumeKafka processor = new ConsumeKafka() {

        @Override
        protected ConsumerPool createConsumerPool(ProcessContext context, ComponentLog log) {
            return consumerPool;
        }
    };
    final ComponentLog logger = mock(ComponentLog.class);
    final ProcessorInitializationContext initializationContext = mock(ProcessorInitializationContext.class);
    when(initializationContext.getLogger()).thenReturn(logger);
    processor.initialize(initializationContext);
    final ProcessContext processContext = mock(ProcessContext.class);
    final PropertyValue heartbeatInternalMsConfig = mock(PropertyValue.class);
    when(heartbeatInternalMsConfig.isSet()).thenReturn(true);
    when(heartbeatInternalMsConfig.asInteger()).thenReturn(100);
    when(processContext.getProperty(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG)).thenReturn(heartbeatInternalMsConfig);
    processor.onScheduled(processContext);
    // retainConsumers should be called at least 1 time if it passed longer than heartbeat interval milliseconds.
    Thread.sleep(200);
    verify(consumerPool, atLeast(1)).retainConsumers();
    processor.stopConnectionRetainer();
    // After stopping connection retainer, it shouldn't interact with consumerPool.
    Thread.sleep(200);
    verifyNoMoreInteractions(consumerPool);
}
Also used : PropertyValue(org.apache.nifi.components.PropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Test(org.junit.Test)

Example 10 with ProcessorInitializationContext

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

the class TestJmsConsumer method testMap2FlowFileTextMessage.

@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);
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) TestRunner(org.apache.nifi.util.TestRunner) InputStream(java.io.InputStream) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) IOException(java.io.IOException) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Test(org.junit.Test)

Aggregations

ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)12 Processor (org.apache.nifi.processor.Processor)6 Test (org.junit.Test)5 ProcessContext (org.apache.nifi.processor.ProcessContext)4 ComponentLog (org.apache.nifi.logging.ComponentLog)3 ProcessSession (org.apache.nifi.processor.ProcessSession)3 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)3 JmsProcessingSummary (org.apache.nifi.processors.standard.util.JmsProcessingSummary)3 MockProcessContext (org.apache.nifi.util.MockProcessContext)3 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)3 TestRunner (org.apache.nifi.util.TestRunner)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Bundle (org.apache.nifi.bundle.Bundle)2 MockProcessorInitializationContext (org.apache.nifi.mock.MockProcessorInitializationContext)2 NarCloseable (org.apache.nifi.nar.NarCloseable)2 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)2 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 ModifiesClasspathNoAnnotationProcessor (org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor)2