Search in sources :

Example 6 with MockComponentLog

use of org.apache.nifi.util.MockComponentLog in project nifi by apache.

the class TestLogAttribute method testLogPropertyCSVWithIgnoreRegex.

@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")));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockComponentLog(org.apache.nifi.util.MockComponentLog) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 7 with MockComponentLog

use of org.apache.nifi.util.MockComponentLog in project nifi by apache.

the class TestLogAttribute method testLogPropertyCSVNoIgnore.

@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"));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockComponentLog(org.apache.nifi.util.MockComponentLog) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 8 with MockComponentLog

use of org.apache.nifi.util.MockComponentLog in project nifi by apache.

the class TestLogAttribute method testLogPropertyWithIgnoreRegex.

@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"));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockComponentLog(org.apache.nifi.util.MockComponentLog) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 9 with MockComponentLog

use of org.apache.nifi.util.MockComponentLog in project nifi by apache.

the class TestLogMessage method testInfoMessageWithPrefixLogged.

@Test
public void testInfoMessageWithPrefixLogged() throws InitializationException, IOException {
    runner.setProperty(LogMessage.LOG_PREFIX, "FOOBAR>>>");
    runner.setProperty(LogMessage.LOG_MESSAGE, "This should help the operator to follow the flow: ${foobar}");
    runner.setProperty(LogMessage.LOG_LEVEL, LogMessage.MessageLogLevel.info.toString());
    HashMap<String, String> flowAttributes = new HashMap<>();
    flowAttributes.put("foobar", "baz");
    runner.enqueue("This is a message!", flowAttributes);
    runner.setValidateExpressionUsage(false);
    runner.run();
    List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship(LogMessage.REL_SUCCESS);
    Assert.assertEquals(1, successFlowFiles.size());
    MockComponentLog mockComponentLog = testableLogMessage.getMockComponentLog();
    List<org.apache.nifi.util.LogMessage> infoMessages = mockComponentLog.getInfoMessages();
    Assert.assertEquals(1, infoMessages.size());
    Assert.assertTrue(infoMessages.get(0).getMsg().endsWith("FOOBAR>>>This should help the operator to follow the flow: baz"));
    Assert.assertTrue(mockComponentLog.getTraceMessages().isEmpty());
    Assert.assertTrue(mockComponentLog.getDebugMessages().isEmpty());
    Assert.assertTrue(mockComponentLog.getWarnMessages().isEmpty());
    Assert.assertTrue(mockComponentLog.getErrorMessages().isEmpty());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) MockComponentLog(org.apache.nifi.util.MockComponentLog) Test(org.junit.Test)

Example 10 with MockComponentLog

use of org.apache.nifi.util.MockComponentLog in project nifi by apache.

the class TestRollbackOnFailure method processInputs.

private void processInputs(RollbackOnFailure context, Integer[][] inputs, List<Integer> results) {
    final ExternalProcedure p = new ExternalProcedure();
    final MockComponentLog componentLog = new MockComponentLog("processor-id", this);
    final ExceptionHandler<RollbackOnFailure> handler = getContextAwareExceptionHandler(componentLog);
    for (Integer[] input : inputs) {
        if (!handler.execute(context, input, (in) -> {
            results.add(p.divide(in[0], in[1]));
            context.proceed();
        })) {
            continue;
        }
        assertEquals(input[2], results.get(results.size() - 1));
    }
}
Also used : Logger(org.slf4j.Logger) ExternalProcedure(org.apache.nifi.processor.util.pattern.TestExceptionHandler.ExternalProcedure) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) TestExceptionHandler.createArrayInputErrorHandler(org.apache.nifi.processor.util.pattern.TestExceptionHandler.createArrayInputErrorHandler) List(java.util.List) TestExceptionHandler.exceptionMapping(org.apache.nifi.processor.util.pattern.TestExceptionHandler.exceptionMapping) Assert.fail(org.junit.Assert.fail) MockComponentLog(org.apache.nifi.util.MockComponentLog) Assert.assertEquals(org.junit.Assert.assertEquals) MockComponentLog(org.apache.nifi.util.MockComponentLog) ExternalProcedure(org.apache.nifi.processor.util.pattern.TestExceptionHandler.ExternalProcedure)

Aggregations

MockComponentLog (org.apache.nifi.util.MockComponentLog)19 Test (org.junit.Test)15 MockFlowFile (org.apache.nifi.util.MockFlowFile)13 ProcessContext (org.apache.nifi.processor.ProcessContext)9 ProcessSession (org.apache.nifi.processor.ProcessSession)9 TestRunner (org.apache.nifi.util.TestRunner)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 HashMap (java.util.HashMap)4 MockStateManager (org.apache.nifi.state.MockStateManager)3 List (java.util.List)2 ComponentLog (org.apache.nifi.logging.ComponentLog)2 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)2 Before (org.junit.Before)2 PartitionContext (com.microsoft.azure.eventprocessorhost.PartitionContext)1 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Connection (com.rabbitmq.client.Connection)1 ReturnListener (com.rabbitmq.client.ReturnListener)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1