Search in sources :

Example 86 with ProcessContext

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

the class PutMongoIT method testValidators.

@Test
public void testValidators() {
    TestRunner runner = TestRunners.newTestRunner(PutMongo.class);
    Collection<ValidationResult> results;
    ProcessContext pc;
    // missing uri, db, collection
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(3, results.size());
    Iterator<ValidationResult> it = results.iterator();
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo URI is required"));
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo Database Name is required"));
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo Collection Name is required"));
    // invalid write concern
    runner.setProperty(AbstractMongoProcessor.URI, MONGO_URI);
    runner.setProperty(AbstractMongoProcessor.DATABASE_NAME, DATABASE_NAME);
    runner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, COLLECTION_NAME);
    runner.setProperty(PutMongo.WRITE_CONCERN, "xyz");
    runner.setProperty(PutMongo.UPDATE_QUERY_KEY, "_id");
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(1, results.size());
    Assert.assertTrue(results.iterator().next().toString().matches("'Write Concern' .* is invalid because Given value not found in allowed set .*"));
    // valid write concern
    runner.setProperty(PutMongo.WRITE_CONCERN, PutMongo.WRITE_CONCERN_UNACKNOWLEDGED);
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(0, results.size());
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ValidationResult(org.apache.nifi.components.ValidationResult) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 87 with ProcessContext

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

the class PutMongoRecordIT method testValidators.

@Test
public void testValidators() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(PutMongoRecord.class);
    runner.addControllerService("reader", recordReader);
    runner.enableControllerService(recordReader);
    Collection<ValidationResult> results;
    ProcessContext pc;
    // missing uri, db, collection, RecordReader
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(4, results.size());
    Iterator<ValidationResult> it = results.iterator();
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo URI is required"));
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo Database Name is required"));
    Assert.assertTrue(it.next().toString().contains("is invalid because Mongo Collection Name is required"));
    Assert.assertTrue(it.next().toString().contains("is invalid because Record Reader is required"));
    // invalid write concern
    runner.setProperty(AbstractMongoProcessor.URI, MONGO_URI);
    runner.setProperty(AbstractMongoProcessor.DATABASE_NAME, DATABASE_NAME);
    runner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, COLLECTION_NAME);
    runner.setProperty(PutMongoRecord.RECORD_READER_FACTORY, "reader");
    runner.setProperty(PutMongoRecord.WRITE_CONCERN, "xyz");
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(1, results.size());
    Assert.assertTrue(results.iterator().next().toString().matches("'Write Concern' .* is invalid because Given value not found in allowed set .*"));
    // valid write concern
    runner.setProperty(PutMongoRecord.WRITE_CONCERN, PutMongoRecord.WRITE_CONCERN_UNACKNOWLEDGED);
    runner.enqueue(new byte[0]);
    pc = runner.getProcessContext();
    results = new HashSet<>();
    if (pc instanceof MockProcessContext) {
        results = ((MockProcessContext) pc).validate();
    }
    Assert.assertEquals(0, results.size());
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ValidationResult(org.apache.nifi.components.ValidationResult) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 88 with ProcessContext

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

the class TestConsumeKafkaRecord_0_10 method setup.

@Before
public void setup() throws InitializationException {
    mockLease = mock(ConsumerLease.class);
    mockConsumerPool = mock(ConsumerPool.class);
    ConsumeKafkaRecord_0_10 proc = new ConsumeKafkaRecord_0_10() {

        @Override
        protected ConsumerPool createConsumerPool(final ProcessContext context, final ComponentLog log) {
            return mockConsumerPool;
        }
    };
    runner = TestRunners.newTestRunner(proc);
    runner.setProperty(KafkaProcessorUtils.BOOTSTRAP_SERVERS, "okeydokey:1234");
    final String readerId = "record-reader";
    final MockRecordParser readerService = new MockRecordParser();
    readerService.addSchemaField("name", RecordFieldType.STRING);
    readerService.addSchemaField("age", RecordFieldType.INT);
    runner.addControllerService(readerId, readerService);
    runner.enableControllerService(readerService);
    final String writerId = "record-writer";
    final RecordSetWriterFactory writerService = new MockRecordWriter("name, age");
    runner.addControllerService(writerId, writerService);
    runner.enableControllerService(writerService);
    runner.setProperty(ConsumeKafkaRecord_0_10.RECORD_READER, readerId);
    runner.setProperty(ConsumeKafkaRecord_0_10.RECORD_WRITER, writerId);
}
Also used : RecordSetWriterFactory(org.apache.nifi.serialization.RecordSetWriterFactory) MockRecordWriter(org.apache.nifi.processors.kafka.pubsub.util.MockRecordWriter) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) MockRecordParser(org.apache.nifi.processors.kafka.pubsub.util.MockRecordParser) Before(org.junit.Before)

Example 89 with ProcessContext

use of org.apache.nifi.processor.ProcessContext 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 90 with ProcessContext

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

the class TestConsumeKafkaRecord_0_11 method setup.

@Before
public void setup() throws InitializationException {
    mockLease = mock(ConsumerLease.class);
    mockConsumerPool = mock(ConsumerPool.class);
    ConsumeKafkaRecord_0_11 proc = new ConsumeKafkaRecord_0_11() {

        @Override
        protected ConsumerPool createConsumerPool(final ProcessContext context, final ComponentLog log) {
            return mockConsumerPool;
        }
    };
    runner = TestRunners.newTestRunner(proc);
    runner.setProperty(KafkaProcessorUtils.BOOTSTRAP_SERVERS, "okeydokey:1234");
    final String readerId = "record-reader";
    final MockRecordParser readerService = new MockRecordParser();
    readerService.addSchemaField("name", RecordFieldType.STRING);
    readerService.addSchemaField("age", RecordFieldType.INT);
    runner.addControllerService(readerId, readerService);
    runner.enableControllerService(readerService);
    final String writerId = "record-writer";
    final RecordSetWriterFactory writerService = new MockRecordWriter("name, age");
    runner.addControllerService(writerId, writerService);
    runner.enableControllerService(writerService);
    runner.setProperty(ConsumeKafkaRecord_0_11.RECORD_READER, readerId);
    runner.setProperty(ConsumeKafkaRecord_0_11.RECORD_WRITER, writerId);
}
Also used : RecordSetWriterFactory(org.apache.nifi.serialization.RecordSetWriterFactory) MockRecordWriter(org.apache.nifi.processors.kafka.pubsub.util.MockRecordWriter) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) MockRecordParser(org.apache.nifi.processors.kafka.pubsub.util.MockRecordParser) Before(org.junit.Before)

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