private void startWebServerAndSendRequests(Runnable sendRequestToWebserver, int numberOfExpectedFlowFiles, int returnCode) throws Exception { final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory(); final ProcessContext context = runner.getProcessContext(); proc.createHttpServer(context); new Thread(sendRequestToWebserver).start(); long responseTimeout = 10000; int numTransferred = 0; long startTime = System.currentTimeMillis(); while (numTransferred < numberOfExpectedFlowFiles && (System.currentTimeMillis() - startTime < responseTimeout)) { proc.onTrigger(context, processSessionFactory); numTransferred = runner.getFlowFilesForRelationship(RELATIONSHIP_SUCCESS).size(); Thread.sleep(100); } runner.assertTransferCount(ListenHTTP.RELATIONSHIP_SUCCESS, numberOfExpectedFlowFiles); }
@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"))); }
@Test public void testLogPropertyWithIgnoreCSV() { 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_IGNORE_CSV, "bar"); 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"))); }
@Test public void testLogPropertyCSVNoIgnore() { 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_CSV, "foo, bar"); 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, containsString("bar-value")); }
@Test public void testLogPropertyWithIgnoreRegex() { 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_IGNORE_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, not(containsString("foobaz-value"))); assertThat(logMessage, not(containsString("foo-value"))); assertThat(logMessage, containsString("bar-value")); }
@Test public void testDefaultUserAgent() throws Exception { setup(null); Assert.assertTrue(runner.getProcessContext().getProperty(PostHTTP.USER_AGENT).getValue().startsWith("Apache-HttpClient")); }
@Before public void setUp() throws Exception { processor = new ListFile(); runner = TestRunners.newTestRunner(processor); runner.setProperty(AbstractListProcessor.TARGET_SYSTEM_TIMESTAMP_PRECISION, AbstractListProcessor.PRECISION_SECONDS.getValue()); context = runner.getProcessContext(); deleteDirectory(testDir); assertTrue("Unable to create test data directory " + testDir.getAbsolutePath(), testDir.exists() || testDir.mkdirs()); resetAges(); }
@Test public void testLogPropertyWithCSVAndRegexNoIgnore() { 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(); // there's an AND relationship between like properties, so only foo should be logged in this case runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo, bar"); 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, not(containsString("foobaz-value"))); assertThat(logMessage, containsString("foo-value")); assertThat(logMessage, not(containsString("bar-value"))); }
@Test public void testLogPropertyCSVWithIgnoreCSV() { 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(); // add foo,foobaz and remove foobaz runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo,foobaz"); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_CSV, "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"))); }
@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(); runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_REGEX, "foo.*"); // includes foo,foobaz runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_REGEX, "foobaz.*"); // includes 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"))); } }
@Test public void testLogPropertyWithIgnoreCSVAndRegex() { 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(); // there's an OR relationship between like properties, so anything starting with foo or bar are removed. that's everything we're adding runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_CSV, "foo,bar"); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_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, not(containsString("foobaz-value"))); assertThat(logMessage, not(containsString("foo-value"))); assertThat(logMessage, not(containsString("bar-value"))); }
@Test public void testLogPropertyCSVWithIgnoreRegex() { 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(); // we're saying add and remove the same properties, so the net result should be nothing runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo"); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_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, not(containsString("foobaz-value"))); assertThat(logMessage, not(containsString("foo-value"))); assertThat(logMessage, not(containsString("bar-value"))); }
@Test public void testCleanupResources() throws JMSException, NoSuchFieldException, IllegalAccessException { final PutJMS putJMS = new PutJMS(); final TestRunner runnerPut = TestRunners.newTestRunner(putJMS); runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER); runnerPut.setProperty(JmsProperties.URL, TEST_URL); runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE); runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix()); final Queue<WrappedMessageProducer> wrappedMessageProducerQueue = spy(new LinkedBlockingQueue<>()); injectFieldValue(PutJMS.class, putJMS, "producerQueue", wrappedMessageProducerQueue); final WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runnerPut.getProcessContext(), true); wrappedMessageProducerQueue.offer(wrappedProducer); assertNotNull(wrappedMessageProducerQueue.peek()); putJMS.cleanupResources(); assertNull(wrappedMessageProducerQueue.peek()); }
/** * Test MapMessage to FlowFile conversion * * @throws java.lang.Exception ex */ @Test public void testMap2FlowFileMapMessage() throws Exception { TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class); MapMessage mapMessage = createMapMessage(); ProcessContext context = runner.getProcessContext(); ProcessSession session = runner.getProcessSessionFactory().createSession(); ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext()); JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, mapMessage, true, pic.getLogger()); assertEquals("MapMessage should not create FlowFile content", 0, summary.getBytesReceived()); Map<String, String> attributes = summary.getLastFlowFile().getAttributes(); assertEquals("", "Arnold", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "name")); assertEquals("", "97", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "age")); assertEquals("", "89686.564", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "xyz")); assertEquals("", "true", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "good")); }
@Test(expected = NumberFormatException.class) public void testBadMessagePriorityValueFails() throws JMSException { final PutJMS putJMS = spy(new PutJMS()); final TestRunner runnerPut = TestRunners.newTestRunner(putJMS); runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER); runnerPut.setProperty(JmsProperties.URL, TEST_URL); runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE); runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix()); runnerPut.setProperty(JmsProperties.MESSAGE_PRIORITY, "negative one"); assertEquals(PutJMS.DEFAULT_MESSAGE_PRIORITY, runnerPut.getProcessContext().getProperty(JmsProperties.MESSAGE_PRIORITY).asInteger().intValue()); }
@org.junit.Ignore public void testSendObjectToQueue() throws Exception { final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class); runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER); runner.setProperty(JmsProperties.URL, "tcp://localhost:61616"); runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE); runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing"); runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO); WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true); final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); // Revision class is used because test just needs any Serializable class in core NiFi final ObjectMessage message = jmsSession.createObjectMessage(new Revision(1L, "ID", "COMP_ID")); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); } }
@Test public void testTextDelimitersValidation() throws IOException, InterruptedException { final TestRunner runner = TestRunners.newTestRunner(new MergeContent()); runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec"); runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_CONCAT); runner.setProperty(MergeContent.DELIMITER_STRATEGY, MergeContent.DELIMITER_STRATEGY_TEXT); runner.setProperty(MergeContent.HEADER, ""); runner.setProperty(MergeContent.DEMARCATOR, ""); runner.setProperty(MergeContent.FOOTER, ""); Collection<ValidationResult> results = new HashSet<>(); ProcessContext context = runner.getProcessContext(); if (context instanceof MockProcessContext) { MockProcessContext mockContext = (MockProcessContext)context; results = mockContext.validate(); } Assert.assertEquals(3, results.size()); for (ValidationResult vr : results) { Assert.assertTrue(vr.toString().contains("cannot be empty")); } }
@org.junit.Ignore public void testSendMapToQueue() throws Exception { final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class); runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER); runner.setProperty(JmsProperties.URL, "tcp://localhost:61616"); runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE); runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing"); runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO); WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true); final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); final MapMessage message = jmsSession.createMapMessage(); message.setString("foo!", "bar"); message.setString("bacon", "meat"); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); }
@Test public void testShouldExtractPGPPublicKeyFromKeyring() { // Arrange final TestRunner runner = TestRunners.newTestRunner(EncryptContent.class); Collection<ValidationResult> results; MockProcessContext pc; runner.setProperty(EncryptContent.MODE, EncryptContent.ENCRYPT_MODE); runner.setProperty(EncryptContent.ENCRYPTION_ALGORITHM, EncryptionMethod.PGP.name()); runner.setProperty(EncryptContent.PUBLIC_KEYRING, "src/test/resources/TestEncryptContent/pubring.gpg"); runner.setProperty(EncryptContent.PUBLIC_KEY_USERID, "NiFi PGP Test Key (Short test key for NiFi PGP unit tests) <alopresto.apache+test@gmail.com>"); runner.enqueue(new byte[0]); pc = (MockProcessContext) runner.getProcessContext(); // Act results = pc.validate(); // Assert Assert.assertEquals(0, results.size()); }
@Test public void testRedirectErrorStream() { final TestRunner runner = TestRunners.newTestRunner(ExecuteProcess.class); runner.setProperty(ExecuteProcess.COMMAND, "cd"); runner.setProperty(ExecuteProcess.COMMAND_ARGUMENTS, "does-not-exist"); runner.setProperty(ExecuteProcess.REDIRECT_ERROR_STREAM, "true"); ProcessContext processContext = runner.getProcessContext(); ExecuteProcess processor = (ExecuteProcess) runner.getProcessor(); processor.updateScheduledTrue(); processor.setupExecutor(processContext); processor.onTrigger(processContext, runner.getProcessSessionFactory()); if (isCommandFailed(runner)) return; final List<LogMessage> warnMessages = runner.getLogger().getWarnMessages(); assertEquals("If redirect error stream is true " + "the output should be sent as a content of flow-file.", 0, warnMessages.size()); final List<MockFlowFile> succeeded = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS); assertEquals(1, succeeded.size()); }