Search in sources :

Example 1 with ModifiesClasspathProcessor

use of org.apache.nifi.test.processors.ModifiesClasspathProcessor in project nifi by apache.

the class TestStandardProcessorNode method testMultiplePropertiesDynamicallyModifyClasspathWithExpressionLanguage.

@Test
public void testMultiplePropertiesDynamicallyModifyClasspathWithExpressionLanguage() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    final PropertyDescriptor classpathProp1 = new PropertyDescriptor.Builder().name("Classpath Resource 1").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final PropertyDescriptor classpathProp2 = new PropertyDescriptor.Builder().name("Classpath Resource 2").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp1, classpathProp2));
    final StandardProcessorNode procNode = createProcessorNode(processor, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }
        // Simulate setting the properties pointing to two of the resources
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp1.getName(), "src/test/resources/TestClasspathResources/resource1.txt");
        properties.put(classpathProp2.getName(), "src/test/resources/TestClasspathResources/${myResource}");
        variableRegistry.setVariable(new VariableDescriptor("myResource"), "resource3.txt");
        procNode.setProperties(properties);
        // Should have resources 1 and 3 loaded into the InstanceClassLoader now
        assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResources[0]));
        assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResources[2]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[1]));
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) VariableDescriptor(org.apache.nifi.registry.VariableDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 2 with ModifiesClasspathProcessor

use of org.apache.nifi.test.processors.ModifiesClasspathProcessor in project nifi by apache.

the class TestStandardProcessorNode method testSinglePropertyDynamicallyModifiesClasspath.

@Test
public void testSinglePropertyDynamicallyModifiesClasspath() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    final PropertyDescriptor classpathProp = new PropertyDescriptor.Builder().name("Classpath Resources").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp));
    final StandardProcessorNode procNode = createProcessorNode(processor, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }
        // Simulate setting the properties of the processor to point to the test resources directory
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp.getName(), "src/test/resources/TestClasspathResources");
        procNode.setProperties(properties);
        // Should have all of the resources loaded into the InstanceClassLoader now
        for (URL testResource : testResources) {
            assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResource));
        }
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) URL(java.net.URL) Test(org.junit.Test)

Example 3 with ModifiesClasspathProcessor

use of org.apache.nifi.test.processors.ModifiesClasspathProcessor in project nifi by apache.

the class TestStandardProcessorNode method testUpdateOtherPropertyDoesNotImpactClasspath.

@Test
public void testUpdateOtherPropertyDoesNotImpactClasspath() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    final PropertyDescriptor classpathProp = new PropertyDescriptor.Builder().name("Classpath Resources").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final PropertyDescriptor otherProp = new PropertyDescriptor.Builder().name("My Property").addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp, otherProp));
    final StandardProcessorNode procNode = createProcessorNode(processor, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }
        // Simulate setting the properties of the processor to point to the test resources directory
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp.getName(), "src/test/resources/TestClasspathResources");
        procNode.setProperties(properties);
        // Should have all of the resources loaded into the InstanceClassLoader now
        for (URL testResource : testResources) {
            assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResource));
        }
        // Should pass validation
        assertTrue(procNode.isValid());
        // Simulate setting updating the other property which should not change the classpath
        final Map<String, String> otherProperties = new HashMap<>();
        otherProperties.put(otherProp.getName(), "foo");
        procNode.setProperties(otherProperties);
        // Should STILL have all of the resources loaded into the InstanceClassLoader now
        for (URL testResource : testResources) {
            assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResource));
        }
        // Should STILL pass validation
        assertTrue(procNode.isValid());
        // Lets update the classpath property and make sure the resources get updated
        final Map<String, String> newClasspathProperties = new HashMap<>();
        newClasspathProperties.put(classpathProp.getName(), "src/test/resources/TestClasspathResources/resource1.txt");
        procNode.setProperties(newClasspathProperties);
        // Should only have resource1 loaded now
        assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResources[0]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[1]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[2]));
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should STILL pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) URL(java.net.URL) Test(org.junit.Test)

Example 4 with ModifiesClasspathProcessor

use of org.apache.nifi.test.processors.ModifiesClasspathProcessor in project nifi by apache.

the class TestStandardProcessorNode method testSomeNonExistentPropertiesDynamicallyModifyClasspath.

@Test
public void testSomeNonExistentPropertiesDynamicallyModifyClasspath() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    final PropertyDescriptor classpathProp1 = new PropertyDescriptor.Builder().name("Classpath Resource 1").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final PropertyDescriptor classpathProp2 = new PropertyDescriptor.Builder().name("Classpath Resource 2").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp1, classpathProp2));
    final StandardProcessorNode procNode = createProcessorNode(processor, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }
        // Simulate setting the properties pointing to two of the resources
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp1.getName(), "src/test/resources/TestClasspathResources/resource1.txt");
        properties.put(classpathProp2.getName(), "src/test/resources/TestClasspathResources/DoesNotExist.txt");
        procNode.setProperties(properties);
        // Should have resources 1 and 3 loaded into the InstanceClassLoader now
        assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResources[0]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[1]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[2]));
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) URL(java.net.URL) Test(org.junit.Test)

Aggregations

URL (java.net.URL)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4 NarCloseable (org.apache.nifi.nar.NarCloseable)4 ModifiesClasspathProcessor (org.apache.nifi.test.processors.ModifiesClasspathProcessor)4 Test (org.junit.Test)4 VariableDescriptor (org.apache.nifi.registry.VariableDescriptor)1