use of org.apache.nifi.util.TestRunner in project nifi by apache.
the class TestListenSMTP method validateSuccessfulInteraction.
@Test
public void validateSuccessfulInteraction() throws Exception, EmailException {
int port = NetworkUtils.availablePort();
TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class);
runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
runner.assertValid();
runner.run(5, false);
final int numMessages = 5;
CountDownLatch latch = new CountDownLatch(numMessages);
this.executor.schedule(() -> {
for (int i = 0; i < numMessages; i++) {
try {
Email email = new SimpleEmail();
email.setHostName("localhost");
email.setSmtpPort(port);
email.setFrom("alice@nifi.apache.org");
email.setSubject("This is a test");
email.setMsg("MSG-" + i);
email.addTo("bob@nifi.apache.org");
email.send();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
latch.countDown();
}
}
}, 1500, TimeUnit.MILLISECONDS);
boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
runner.shutdown();
assertTrue(complete);
runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, numMessages);
}
use of org.apache.nifi.util.TestRunner in project nifi by apache.
the class ConsumeAMQPTest method validateSuccessfullConsumeAndTransferToSuccess.
@Test
public void validateSuccessfullConsumeAndTransferToSuccess() throws Exception {
final Map<String, List<String>> routingMap = Collections.singletonMap("key1", Arrays.asList("queue1", "queue2"));
final Map<String, String> exchangeToRoutingKeymap = Collections.singletonMap("myExchange", "key1");
final Connection connection = new TestConnection(exchangeToRoutingKeymap, routingMap);
try (AMQPPublisher sender = new AMQPPublisher(connection, mock(ComponentLog.class))) {
sender.publish("hello".getBytes(), MessageProperties.PERSISTENT_TEXT_PLAIN, "key1", "myExchange");
ConsumeAMQP pubProc = new LocalConsumeAMQP(connection);
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(ConsumeAMQP.HOST, "injvm");
runner.setProperty(ConsumeAMQP.QUEUE, "queue1");
runner.run();
final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishAMQP.REL_SUCCESS).get(0);
assertNotNull(successFF);
}
}
use of org.apache.nifi.util.TestRunner in project nifi by apache.
the class PublishAMQPTest method validateSuccessfullPublishAndTransferToSuccess.
@Test
public void validateSuccessfullPublishAndTransferToSuccess() throws Exception {
final PublishAMQP pubProc = new LocalPublishAMQP();
final TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(PublishAMQP.HOST, "injvm");
runner.setProperty(PublishAMQP.EXCHANGE, "myExchange");
runner.setProperty(PublishAMQP.ROUTING_KEY, "key1");
final Map<String, String> attributes = new HashMap<>();
attributes.put("foo", "bar");
attributes.put("amqp$contentType", "foo/bar");
attributes.put("amqp$contentEncoding", "foobar123");
attributes.put("amqp$headers", "foo=bar,foo2=bar2,foo3");
attributes.put("amqp$deliveryMode", "1");
attributes.put("amqp$priority", "2");
attributes.put("amqp$correlationId", "correlationId123");
attributes.put("amqp$replyTo", "replyTo123");
attributes.put("amqp$expiration", "expiration123");
attributes.put("amqp$messageId", "messageId123");
attributes.put("amqp$timestamp", "123456789");
attributes.put("amqp$type", "type123");
attributes.put("amqp$userId", "userId123");
attributes.put("amqp$appId", "appId123");
attributes.put("amqp$clusterId", "clusterId123");
runner.enqueue("Hello Joe".getBytes(), attributes);
runner.run();
final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishAMQP.REL_SUCCESS).get(0);
assertNotNull(successFF);
final Channel channel = ((LocalPublishAMQP) pubProc).getConnection().createChannel();
final GetResponse msg1 = channel.basicGet("queue1", true);
assertNotNull(msg1);
assertEquals("foo/bar", msg1.getProps().getContentType());
assertEquals("foobar123", msg1.getProps().getContentEncoding());
final Map<String, Object> headerMap = msg1.getProps().getHeaders();
final Object foo = headerMap.get("foo");
final Object foo2 = headerMap.get("foo2");
final Object foo3 = headerMap.get("foo3");
assertEquals("bar", foo.toString());
assertEquals("bar2", foo2.toString());
assertNull(foo3);
assertEquals((Integer) 1, msg1.getProps().getDeliveryMode());
assertEquals((Integer) 2, msg1.getProps().getPriority());
assertEquals("correlationId123", msg1.getProps().getCorrelationId());
assertEquals("replyTo123", msg1.getProps().getReplyTo());
assertEquals("expiration123", msg1.getProps().getExpiration());
assertEquals("messageId123", msg1.getProps().getMessageId());
assertEquals(new Date(123456789L), msg1.getProps().getTimestamp());
assertEquals("type123", msg1.getProps().getType());
assertEquals("userId123", msg1.getProps().getUserId());
assertEquals("appId123", msg1.getProps().getAppId());
assertEquals("clusterId123", msg1.getProps().getClusterId());
assertNotNull(channel.basicGet("queue2", true));
}
use of org.apache.nifi.util.TestRunner in project nifi by apache.
the class PublishAMQPTest method validateFailedPublishAndTransferToFailure.
@Test
public void validateFailedPublishAndTransferToFailure() throws Exception {
PublishAMQP pubProc = new LocalPublishAMQP();
TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(PublishAMQP.HOST, "injvm");
runner.setProperty(PublishAMQP.EXCHANGE, "badToTheBone");
runner.setProperty(PublishAMQP.ROUTING_KEY, "key1");
runner.enqueue("Hello Joe".getBytes());
runner.run();
assertTrue(runner.getFlowFilesForRelationship(PublishAMQP.REL_SUCCESS).isEmpty());
assertNotNull(runner.getFlowFilesForRelationship(PublishAMQP.REL_FAILURE).get(0));
}
use of org.apache.nifi.util.TestRunner in project nifi by apache.
the class ParseEvtxTest method recordGranularityLifecycleTest.
@Test
public void recordGranularityLifecycleTest() throws IOException, ParserConfigurationException, SAXException {
String baseName = "testFileName";
String name = baseName + ".evtx";
TestRunner testRunner = TestRunners.newTestRunner(ParseEvtx.class);
testRunner.setProperty(ParseEvtx.GRANULARITY, ParseEvtx.RECORD);
Map<String, String> attributes = new HashMap<>();
attributes.put(CoreAttributes.FILENAME.key(), name);
testRunner.enqueue(this.getClass().getClassLoader().getResourceAsStream("application-logs.evtx"), attributes);
testRunner.run();
List<MockFlowFile> originalFlowFiles = testRunner.getFlowFilesForRelationship(ParseEvtx.REL_ORIGINAL);
assertEquals(1, originalFlowFiles.size());
MockFlowFile originalFlowFile = originalFlowFiles.get(0);
originalFlowFile.assertAttributeEquals(CoreAttributes.FILENAME.key(), name);
originalFlowFile.assertContentEquals(this.getClass().getClassLoader().getResourceAsStream("application-logs.evtx"));
// We expect the same bad chunks no matter the granularity
List<MockFlowFile> badChunkFlowFiles = testRunner.getFlowFilesForRelationship(ParseEvtx.REL_BAD_CHUNK);
assertEquals(2, badChunkFlowFiles.size());
badChunkFlowFiles.get(0).assertAttributeEquals(CoreAttributes.FILENAME.key(), parseEvtx.getName(baseName, 1, null, ParseEvtx.EVTX_EXTENSION));
badChunkFlowFiles.get(1).assertAttributeEquals(CoreAttributes.FILENAME.key(), parseEvtx.getName(baseName, 2, null, ParseEvtx.EVTX_EXTENSION));
List<MockFlowFile> failureFlowFiles = testRunner.getFlowFilesForRelationship(ParseEvtx.REL_FAILURE);
assertEquals(0, failureFlowFiles.size());
// Whole file fails if there is a failure parsing
List<MockFlowFile> successFlowFiles = testRunner.getFlowFilesForRelationship(ParseEvtx.REL_SUCCESS);
assertEquals(960, successFlowFiles.size());
// We expect the same number of records to come out no matter the granularity
assertEquals(960, validateFlowFiles(successFlowFiles));
}
Aggregations