Support Questions

Find answers, ask questions, and share your expertise

How to access custom properties in custom NiFi Controller Service?

Explorer

I have custom controller service with few properties. Here is sample of one of them:

public static final PropertyDescriptor CONFIG_DELIMITER = new PropertyDescriptor.Builder()
        .name("Field delimiter to be used")
        .description("Field delimiter used to separate the fields in the cache data file...")
        .defaultValue(",")
        .expressionLanguageSupported(true)
        .required(true)
        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
        .build();

I have expressionLanguageSupported set to true for them with the idea of resolving them in onConfigred method of the service.

@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {
    configDelim = context.getProperty(CONFIG_DELIMITER).evaluateAttributeExpressions().getValue();
	..........    
}

I have created a custom properties file custom.properties with following content:

nifi.iSOS.MyService.delim=,

I have setup properties in my test as :

@Test
public void testService() throws InitializationException {
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    final MyService service = new MyService();
    runner.addControllerService("MyService", service);
    runner.setProperty(service, LookupService.CONFIG_DELIMITER , "${nifi.iSOS.MyService.delim}");
    ..........
}

I need help with 2 things:

  1. How to pass the custom properties file to the test for MyService?
  2. Is context.getProperty(CONFIG_DELIMITER).evaluateAttributeExpressions().getValue(); correct way to evaluate the value of the custom property in the controller service?

Big thank you for all the help...

2 REPLIES 2

1) You can't set a whole properties file during unit testing, but TestRunner has the following method:

void setVariable(String name, String value);

So just call setVariable("nifi.iSOS.MyService.delim", ",");

2) Yes that is correct.

Explorer

Thanks Bryan, it helped a lot.

Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.