Search in sources :

Example 1 with MockProcessContext

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

the class TestStandardRemoteGroupPort method setupMockProcessSession.

private void setupMockProcessSession() {
    // Construct a RemoteGroupPort as a processor to use NiFi mock library.
    final Processor remoteGroupPort = mock(Processor.class);
    final Set<Relationship> relationships = new HashSet<>();
    relationships.add(Relationship.ANONYMOUS);
    when(remoteGroupPort.getRelationships()).thenReturn(relationships);
    when(remoteGroupPort.getIdentifier()).thenReturn("remote-group-port-id");
    sessionState = new SharedSessionState(remoteGroupPort, new AtomicLong(0));
    processSession = new MockProcessSession(sessionState, remoteGroupPort);
    processContext = new MockProcessContext(remoteGroupPort);
}
Also used : SharedSessionState(org.apache.nifi.util.SharedSessionState) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) Relationship(org.apache.nifi.processor.Relationship) MockProcessSession(org.apache.nifi.util.MockProcessSession) MockProcessContext(org.apache.nifi.util.MockProcessContext) HashSet(java.util.HashSet)

Example 2 with MockProcessContext

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

the class TestReportLineageToAtlas method validateAtlasUrls.

@Test
public void validateAtlasUrls() throws Exception {
    final ReportLineageToAtlas reportingTask = new ReportLineageToAtlas();
    final MockProcessContext processContext = new MockProcessContext(reportingTask);
    final MockValidationContext validationContext = new MockValidationContext(processContext);
    processContext.setProperty(ATLAS_NIFI_URL, "http://nifi.example.com:8080/nifi");
    processContext.setProperty(ATLAS_USER, "admin");
    processContext.setProperty(ATLAS_PASSWORD, "admin");
    BiConsumer<Collection<ValidationResult>, Consumer<ValidationResult>> assertResults = (rs, a) -> {
        assertTrue(rs.iterator().hasNext());
        for (ValidationResult r : rs) {
            logger.info("{}", r);
            final String subject = r.getSubject();
            if (ATLAS_URLS.getDisplayName().equals(subject)) {
                a.accept(r);
            }
        }
    };
    // Default setting.
    assertResults.accept(reportingTask.validate(validationContext), r -> assertTrue("Atlas URLs is required", !r.isValid()));
    // Invalid URL.
    processContext.setProperty(ATLAS_URLS, "invalid");
    assertResults.accept(reportingTask.validate(validationContext), r -> assertTrue("Atlas URLs is invalid", !r.isValid()));
    // Valid URL
    processContext.setProperty(ATLAS_URLS, "http://atlas.example.com:21000");
    assertTrue(processContext.isValid());
    // Valid URL with Expression
    processContext.setProperty(ATLAS_URLS, "http://atlas.example.com:${literal(21000)}");
    assertTrue(processContext.isValid());
    // Valid URLs
    processContext.setProperty(ATLAS_URLS, "http://atlas1.example.com:21000, http://atlas2.example.com:21000");
    assertTrue(processContext.isValid());
    // Invalid and Valid URLs
    processContext.setProperty(ATLAS_URLS, "invalid, http://atlas2.example.com:21000");
    assertResults.accept(reportingTask.validate(validationContext), r -> assertTrue("Atlas URLs is invalid", !r.isValid()));
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ATLAS_NIFI_URL(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_NIFI_URL) Consumer(java.util.function.Consumer) MockValidationContext(org.apache.nifi.util.MockValidationContext) ATLAS_USER(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_USER) MockProcessContext(org.apache.nifi.util.MockProcessContext) ATLAS_URLS(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_URLS) BiConsumer(java.util.function.BiConsumer) ATLAS_PASSWORD(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_PASSWORD) ValidationResult(org.apache.nifi.components.ValidationResult) Consumer(java.util.function.Consumer) BiConsumer(java.util.function.BiConsumer) MockValidationContext(org.apache.nifi.util.MockValidationContext) Collection(java.util.Collection) ValidationResult(org.apache.nifi.components.ValidationResult) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 3 with MockProcessContext

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

the class TestInvokeGroovy method testScriptDefinedAttribute.

/**
 * Tests a script that has a Groovy Processor that that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedAttribute() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_reader.groovy");
    context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/groovy");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
    assertNotNull(descriptors);
    assertTrue(descriptors.size() > 0);
    boolean found = false;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals("test-attribute")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 4 with MockProcessContext

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

the class TestInvokeGroovy method testScriptDefinedRelationship.

/**
 * Tests a script that has a Groovy Processor that that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedRelationship() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_reader.groovy");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    Set<Relationship> relationships = processor.getRelationships();
    assertNotNull(relationships);
    assertTrue(relationships.size() > 0);
    boolean found = false;
    for (Relationship relationship : relationships) {
        if (relationship.getName().equals("test")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) Relationship(org.apache.nifi.processor.Relationship) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 5 with MockProcessContext

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

the class TestInvokeJavascript method testScriptDefinedAttribute.

/**
 * Tests a scripted processor written in Javascript that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 * Confirms that the scripted processor can return property descriptors defined in it.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedAttribute() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/javascript/test_reader.js");
    context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/javascript");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
    assertNotNull(descriptors);
    assertTrue(descriptors.size() > 0);
    boolean found = false;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals("test-attribute")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Aggregations

MockProcessContext (org.apache.nifi.util.MockProcessContext)39 Test (org.junit.Test)35 ValidationResult (org.apache.nifi.components.ValidationResult)25 TestRunner (org.apache.nifi.util.TestRunner)24 ProcessContext (org.apache.nifi.processor.ProcessContext)16 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)10 MockValidationContext (org.apache.nifi.util.MockValidationContext)6 HashSet (java.util.HashSet)4 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)4 Relationship (org.apache.nifi.processor.Relationship)4 ProcessSession (org.apache.nifi.processor.ProcessSession)3 JmsProcessingSummary (org.apache.nifi.processors.standard.util.JmsProcessingSummary)3 Matchers.anyString (org.mockito.Matchers.anyString)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 NullSink (org.apache.flume.sink.NullSink)2 AvroSource (org.apache.flume.source.AvroSource)2