Search in sources :

Example 1 with MockValidationContext

use of org.apache.nifi.util.MockValidationContext 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 2 with MockValidationContext

use of org.apache.nifi.util.MockValidationContext 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 3 with MockValidationContext

use of org.apache.nifi.util.MockValidationContext 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 4 with MockValidationContext

use of org.apache.nifi.util.MockValidationContext 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)

Example 5 with MockValidationContext

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

the class TestInvokeJavascript method testScriptDefinedRelationship.

/**
 * Tests a scripted processor written in Javascript 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 relationships defined in it.
 *
 * @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, "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);
    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)

Aggregations

MockProcessContext (org.apache.nifi.util.MockProcessContext)6 MockValidationContext (org.apache.nifi.util.MockValidationContext)6 Test (org.junit.Test)6 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)2 ValidationResult (org.apache.nifi.components.ValidationResult)2 Relationship (org.apache.nifi.processor.Relationship)2 File (java.io.File)1 Collection (java.util.Collection)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 ATLAS_NIFI_URL (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_NIFI_URL)1 ATLAS_PASSWORD (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_PASSWORD)1 ATLAS_URLS (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_URLS)1 ATLAS_USER (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_USER)1 ValidationContext (org.apache.nifi.components.ValidationContext)1 TestRunner (org.apache.nifi.util.TestRunner)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1