NIFI-9272: When determining if Property dependency is satisfied, consider property default values also

Signed-off-by: Joe Gresock <jgresock@gmail.com>

This closes #5432.
This commit is contained in:
Mark Payne 2021-10-02 15:24:41 -04:00 committed by Joe Gresock
parent 72660af479
commit a675023b71
No known key found for this signature in database
GPG Key ID: 37F5B9B6E258C8B7
3 changed files with 85 additions and 9 deletions

View File

@ -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();
}
};

View File

@ -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));
}
}

View File

@ -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;