diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/MockPropertyValue.java b/nifi-mock/src/main/java/org/apache/nifi/util/MockPropertyValue.java index 45dbfde38bf6..cde952929703 100644 --- a/nifi-mock/src/main/java/org/apache/nifi/util/MockPropertyValue.java +++ b/nifi-mock/src/main/java/org/apache/nifi/util/MockPropertyValue.java @@ -93,14 +93,14 @@ private void ensureExpressionsEvaluated() { } } - private void validateExpressionScope(boolean attributesAvailable) { + private void validateExpressionScope(boolean flowFileProvided, boolean additionalAttributesAvailable) { if (expressionLanguageScope == null) { return; } // language scope is not null, we have attributes available but scope is not equal to FF attributes // it means that we're not evaluating against flow file attributes even though attributes are available - if (attributesAvailable && !ExpressionLanguageScope.FLOWFILE_ATTRIBUTES.equals(expressionLanguageScope)) { + if (flowFileProvided && !ExpressionLanguageScope.FLOWFILE_ATTRIBUTES.equals(expressionLanguageScope)) { throw new IllegalStateException("Attempting to evaluate expression language for " + propertyDescriptor.getName() + " using flow file attributes but the scope evaluation is set to " + expressionLanguageScope + ". The" + " proper scope should be set in the property descriptor using" @@ -124,8 +124,8 @@ private void validateExpressionScope(boolean attributesAvailable) { return; } - // we're trying to evaluate against flow files attributes but we don't have any attributes available. - if (!attributesAvailable && ExpressionLanguageScope.FLOWFILE_ATTRIBUTES.equals(expressionLanguageScope)) { + // we're trying to evaluate against flow files attributes but we don't have a FlowFile available. + if (!flowFileProvided && !additionalAttributesAvailable && ExpressionLanguageScope.FLOWFILE_ATTRIBUTES.equals(expressionLanguageScope)) { throw new IllegalStateException("Attempting to evaluate expression language for " + propertyDescriptor.getName() + " without using flow file attributes but the scope evaluation is set to " + expressionLanguageScope + ". The" + " proper scope should be set in the property descriptor using" @@ -263,7 +263,7 @@ public PropertyValue evaluateAttributeExpressions(FlowFile flowFile, Map getSupportedPropertyDescriptors() { + return List.of(PROP_ENV_SCOPE, PROP_FF_SCOPE, PROP_NONE_SCOPE); + } + + @Override + public Set getRelationships() { + return Set.of(SUCCESS); + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile ff = session.get(); + + final Map map = Map.of("test", "test"); + + switch (getTestCase()) { + case FF_SCOPE_WITH_FF -> context.getProperty(PROP_FF_SCOPE).evaluateAttributeExpressions(ff); + case ENV_SCOPE_WITH_FF -> context.getProperty(PROP_ENV_SCOPE).evaluateAttributeExpressions(ff); + case NONE_SCOPE_WITH_FF -> context.getProperty(PROP_NONE_SCOPE).evaluateAttributeExpressions(ff); + case FF_SCOPE_NO_FF -> context.getProperty(PROP_FF_SCOPE).evaluateAttributeExpressions(); + case ENV_SCOPE_NO_FF -> context.getProperty(PROP_ENV_SCOPE).evaluateAttributeExpressions(); + case NONE_SCOPE_NO_FF -> context.getProperty(PROP_NONE_SCOPE).evaluateAttributeExpressions(); + case FF_SCOPE_WITH_MAP -> context.getProperty(PROP_FF_SCOPE).evaluateAttributeExpressions(map); + case ENV_SCOPE_WITH_MAP -> context.getProperty(PROP_ENV_SCOPE).evaluateAttributeExpressions(map); + case NONE_SCOPE_WITH_MAP -> context.getProperty(PROP_NONE_SCOPE).evaluateAttributeExpressions(map); + } + + if (ff != null) { + session.transfer(ff, SUCCESS); + } + } + + public String getTestCase() { + return testCase; + } + + public void setTestCase(String testCase) { + this.testCase = testCase; + } + } + + @InputRequirement(Requirement.INPUT_FORBIDDEN) + private static class DummyProcessorNoInput extends AbstractProcessor { + static final PropertyDescriptor PROP_FF_SCOPE = new PropertyDescriptor.Builder() + .name("Property with FF scope") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .build(); + static final PropertyDescriptor PROP_ENV_SCOPE = new PropertyDescriptor.Builder() + .name("Property with Env scope") + .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .build(); + static final PropertyDescriptor PROP_NONE_SCOPE = new PropertyDescriptor.Builder() + .name("Property with None scope") + .expressionLanguageSupported(ExpressionLanguageScope.NONE) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .build(); + + static final Relationship SUCCESS = new Relationship.Builder() + .name("success") + .build(); + + private String testCase; + + @Override + protected List getSupportedPropertyDescriptors() { + return List.of(PROP_ENV_SCOPE, PROP_FF_SCOPE, PROP_NONE_SCOPE); + } + + @Override + public Set getRelationships() { + return Set.of(SUCCESS); + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + final Map map = Map.of("test", "test"); + + switch (getTestCase()) { + case FF_SCOPE_NO_FF -> context.getProperty(PROP_FF_SCOPE).evaluateAttributeExpressions(); + case ENV_SCOPE_NO_FF -> context.getProperty(PROP_ENV_SCOPE).evaluateAttributeExpressions(); + case NONE_SCOPE_NO_FF -> context.getProperty(PROP_NONE_SCOPE).evaluateAttributeExpressions(); + case FF_SCOPE_WITH_MAP -> context.getProperty(PROP_FF_SCOPE).evaluateAttributeExpressions(map); + case ENV_SCOPE_WITH_MAP -> context.getProperty(PROP_ENV_SCOPE).evaluateAttributeExpressions(map); + case NONE_SCOPE_WITH_MAP -> context.getProperty(PROP_NONE_SCOPE).evaluateAttributeExpressions(map); + } + } + + public String getTestCase() { + return testCase; + } + + public void setTestCase(String testCase) { + this.testCase = testCase; + } + } +} \ No newline at end of file