This is an automated email from the ASF dual-hosted git repository.

jgresock pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new a675023  NIFI-9272: When determining if Property dependency is 
satisfied, consider property default values also
a675023 is described below

commit a675023b71fc8ffcb9bc2c73dd29abed7cd9f3c6
Author: Mark Payne <[email protected]>
AuthorDate: Sat Oct 2 15:24:41 2021 -0400

    NIFI-9272: When determining if Property dependency is satisfied, consider 
property default values also
    
    Signed-off-by: Joe Gresock <[email protected]>
    
    This closes #5432.
---
 .../apache/nifi/util/MockValidationContext.java    |  4 +-
 .../processor/TestStandardValidationContext.java   | 80 ++++++++++++++++++++++
 .../validation/AbstractValidationContext.java      | 10 +--
 3 files changed, 85 insertions(+), 9 deletions(-)

diff --git 
a/nifi-mock/src/main/java/org/apache/nifi/util/MockValidationContext.java 
b/nifi-mock/src/main/java/org/apache/nifi/util/MockValidationContext.java
index 364df82..a73d6eb 100644
--- a/nifi-mock/src/main/java/org/apache/nifi/util/MockValidationContext.java
+++ b/nifi-mock/src/main/java/org/apache/nifi/util/MockValidationContext.java
@@ -293,12 +293,12 @@ public class MockValidationContext extends 
MockControllerServiceLookup implement
 
             @Override
             public PropertyValue getProperty(final PropertyDescriptor 
descriptor) {
-                return null;
+                return MockValidationContext.this.getProperty(descriptor);
             }
 
             @Override
             public Map<String, String> getAllProperties() {
-                return null;
+                return MockValidationContext.this.getAllProperties();
             }
         };
 
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestStandardValidationContext.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestStandardValidationContext.java
new file mode 100644
index 0000000..70f6b3f
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestStandardValidationContext.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.processor;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.PropertyConfiguration;
+import org.apache.nifi.controller.service.ControllerServiceProvider;
+import org.apache.nifi.parameter.StandardParameterTokenList;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.registry.VariableRegistry;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public class TestStandardValidationContext {
+
+    @Test
+    public void testPropertyDependencySatisfied() {
+        final ControllerServiceProvider csProvider = 
mock(ControllerServiceProvider.class);
+
+        final PropertyDescriptor descriptorA = new PropertyDescriptor.Builder()
+            .name("A")
+            .allowableValues("abc", "xyz")
+            .defaultValue("abc")
+            .build();
+
+        final PropertyDescriptor descriptorB = new PropertyDescriptor.Builder()
+            .name("B")
+            .required(true)
+            .dependsOn(descriptorA, "abc")
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+        final PropertyConfiguration configurationB = null;
+
+        final Function<String, PropertyDescriptor> propertyLookup = propName 
-> propName.equals("A") ? descriptorA : null;
+
+        final Map<PropertyDescriptor, PropertyConfiguration> properties = new 
HashMap<>();
+        properties.put(descriptorB, configurationB);
+
+        StandardValidationContext context = new 
StandardValidationContext(csProvider, properties, null, "1234", "12345", 
VariableRegistry.EMPTY_REGISTRY, null, false);
+
+        // Property B's dependency should be satisfied because A = "abc"
+        assertTrue(context.isDependencySatisfied(descriptorB, propertyLookup));
+
+        // Property A's dependency is always satisfied b/c no dependency
+        assertTrue(context.isDependencySatisfied(descriptorA, propertyLookup));
+
+        properties.put(descriptorA, new PropertyConfiguration("xyz", new 
StandardParameterTokenList("xyz", Collections.emptyList()), 
Collections.emptyList()));
+        context = new StandardValidationContext(csProvider, properties, null, 
"1234", "12345", VariableRegistry.EMPTY_REGISTRY, null, false);
+
+        // Should not be satisfied because A = "xyz".
+        assertFalse(context.isDependencySatisfied(descriptorB, 
propertyLookup));
+
+        // Property A's dependency should still (always) satisfied b/c no 
dependency
+        assertTrue(context.isDependencySatisfied(descriptorA, propertyLookup));
+    }
+
+}
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/components/validation/AbstractValidationContext.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/components/validation/AbstractValidationContext.java
index d49c10b..ea9dd79 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/components/validation/AbstractValidationContext.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/components/validation/AbstractValidationContext.java
@@ -19,6 +19,7 @@ package org.apache.nifi.components.validation;
 
 import org.apache.nifi.components.PropertyDependency;
 import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.PropertyValue;
 import org.apache.nifi.components.ValidationContext;
 import org.apache.nifi.controller.PropertyConfiguration;
 import org.apache.nifi.parameter.ParameterLookup;
@@ -70,13 +71,8 @@ public abstract class AbstractValidationContext implements 
ValidationContext {
                     return false;
                 }
 
-                final PropertyConfiguration dependencyConfiguration = 
properties.get(dependencyDescriptor);
-                if (dependencyConfiguration == null) {
-                    logger.debug("Dependency for {} is not satisfied because 
it has a dependency on {}, which does not have a value", propertyDescriptor, 
dependencyName);
-                    return false;
-                }
-
-                final String dependencyValue = 
dependencyConfiguration.getEffectiveValue(parameterLookup);
+                final PropertyValue propertyValue = 
getProperty(dependencyDescriptor);
+                final String dependencyValue = propertyValue == null ? 
dependencyDescriptor.getDefaultValue() : propertyValue.getValue();
                 if (dependencyValue == null) {
                     logger.debug("Dependency for {} is not satisfied because 
it has a dependency on {}, which has a null value", propertyDescriptor, 
dependencyName);
                     return false;

Reply via email to