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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations