Do I need to specifically enable a ControllerService in a unit test?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Do I need to specifically enable a ControllerService in a unit test?

Dan Bress
Lets say I have a ControllerService that loads some data into memory, and lets say that is happening during OnEnabled.  If I want to test this behavior using the NiFi Mock TestRunner, do I need to explicitly call "enableControllerService" to cause the "@OnEnabled" method on my ControllerService to be called?


Looking at StandardSSLContextService [1] this appears to be the case.  I was not aware of this requirement, and wanted to make sure I understood it.


Is this part of allowing full life cycle support for controller services and NIFI-250?  Any reason to not automatically enable controller services during mock testing?  Or is the thinking that explicitly calling it allows you finer grained testing of difference scenarios.


Thanks!


[1] https://github.com/apache/incubator-nifi/blob/develop/nifi/nifi-nar-bundles/nifi-standard-services/nifi-ssl-context-bundle/nifi-ssl-context-service/src/test/java/org/apache/nifi/ssl/SSLContextServiceTest.java#L106


Dan Bress
Software Engineer
ONYX Consulting Services
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to specifically enable a ControllerService in a unit test?

Mark Payne
Dan,


Yes, you should be calling enableControllerService on the TestRunner. This was refactored a big in the 0.1.0 version because previously the only option you had was to create the Controller Service and then add it to the test runner with a Map or all properties. With the test runner, you can now set properties via TestRunner.setProperty(ControllerService, String, String); The test runner does not automatically enable the service because the service is not valid until the properties are set, and the properties will often no longer be set when creating the service. You are correct in that it is all a result of the full lifecycle support added for Controller Services via NIFI-250.


Thanks

-Mark







From: Dan Bress
Sent: ‎Thursday‎, ‎April‎ ‎2‎, ‎2015 ‎11‎:‎18‎ ‎AM
To: [hidden email]





Lets say I have a ControllerService that loads some data into memory, and lets say that is happening during OnEnabled.  If I want to test this behavior using the NiFi Mock TestRunner, do I need to explicitly call "enableControllerService" to cause the "@OnEnabled" method on my ControllerService to be called?


Looking at StandardSSLContextService [1] this appears to be the case.  I was not aware of this requirement, and wanted to make sure I understood it.


Is this part of allowing full life cycle support for controller services and NIFI-250?  Any reason to not automatically enable controller services during mock testing?  Or is the thinking that explicitly calling it allows you finer grained testing of difference scenarios.


Thanks!


[1] https://github.com/apache/incubator-nifi/blob/develop/nifi/nifi-nar-bundles/nifi-standard-services/nifi-ssl-context-bundle/nifi-ssl-context-service/src/test/java/org/apache/nifi/ssl/SSLContextServiceTest.java#L106


Dan Bress
Software Engineer
ONYX Consulting Services
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to specifically enable a ControllerService in a unit test?

Dan Bress
Mark,
   Cool.  Thanks for the explanation.

   Is there any significance to the order of the following events in a Mock test:
   
   A) Adding a service to a processor or other service(TestRunner.addControllerService)
   B) Setting properties on a controller service(TestRunner.setProperty(ControllerService service, String key, String value)
   C) Enabling a service(TestRunner.enableControllerService)

I would assume enabling depends on the correct properties being set, so C must happen after B.  But it seems like A can happen at any point.  Correct?

Dan Bress
Software Engineer
ONYX Consulting Services

________________________________________
From: Mark Payne <[hidden email]>
Sent: Thursday, April 2, 2015 11:38 AM
To: [hidden email]
Subject: Re: Do I need to specifically enable a ControllerService in a unit test?

Dan,


Yes, you should be calling enableControllerService on the TestRunner. This was refactored a big in the 0.1.0 version because previously the only option you had was to create the Controller Service and then add it to the test runner with a Map or all properties. With the test runner, you can now set properties via TestRunner.setProperty(ControllerService, String, String); The test runner does not automatically enable the service because the service is not valid until the properties are set, and the properties will often no longer be set when creating the service. You are correct in that it is all a result of the full lifecycle support added for Controller Services via NIFI-250.


Thanks

-Mark







From: Dan Bress
Sent: ‎Thursday‎, ‎April‎ ‎2‎, ‎2015 ‎11‎:‎18‎ ‎AM
To: [hidden email]





Lets say I have a ControllerService that loads some data into memory, and lets say that is happening during OnEnabled.  If I want to test this behavior using the NiFi Mock TestRunner, do I need to explicitly call "enableControllerService" to cause the "@OnEnabled" method on my ControllerService to be called?


Looking at StandardSSLContextService [1] this appears to be the case.  I was not aware of this requirement, and wanted to make sure I understood it.


Is this part of allowing full life cycle support for controller services and NIFI-250?  Any reason to not automatically enable controller services during mock testing?  Or is the thinking that explicitly calling it allows you finer grained testing of difference scenarios.


Thanks!


[1] https://github.com/apache/incubator-nifi/blob/develop/nifi/nifi-nar-bundles/nifi-standard-services/nifi-ssl-context-bundle/nifi-ssl-context-service/src/test/java/org/apache/nifi/ssl/SSLContextServiceTest.java#L106


Dan Bress
Software Engineer
ONYX Consulting Services
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to specifically enable a ControllerService in a unit test?

Mark Payne
In reply to this post by Dan Bress
Dan,


You’ll need to add the Controller Service (A) before you can set a property on it (B).


Also, you’ll need to be sure to enable the service (C) before you can call runner.run() if your processor is referencing the service (otherwise the processor will be invalid).


Thanks

-Mark







From: Dan Bress
Sent: ‎Thursday‎, ‎April‎ ‎2‎, ‎2015 ‎12‎:‎18‎ ‎PM
To: [hidden email]





Mark,
   Cool.  Thanks for the explanation.

   Is there any significance to the order of the following events in a Mock test:
   
   A) Adding a service to a processor or other service(TestRunner.addControllerService)
   B) Setting properties on a controller service(TestRunner.setProperty(ControllerService service, String key, String value)
   C) Enabling a service(TestRunner.enableControllerService)

I would assume enabling depends on the correct properties being set, so C must happen after B.  But it seems like A can happen at any point.  Correct?

Dan Bress
Software Engineer
ONYX Consulting Services

________________________________________
From: Mark Payne <[hidden email]>
Sent: Thursday, April 2, 2015 11:38 AM
To: [hidden email]
Subject: Re: Do I need to specifically enable a ControllerService in a unit test?

Dan,


Yes, you should be calling enableControllerService on the TestRunner. This was refactored a big in the 0.1.0 version because previously the only option you had was to create the Controller Service and then add it to the test runner with a Map or all properties. With the test runner, you can now set properties via TestRunner.setProperty(ControllerService, String, String); The test runner does not automatically enable the service because the service is not valid until the properties are set, and the properties will often no longer be set when creating the service. You are correct in that it is all a result of the full lifecycle support added for Controller Services via NIFI-250.


Thanks

-Mark







From: Dan Bress
Sent: ‎Thursday‎, ‎April‎ ‎2‎, ‎2015 ‎11‎:‎18‎ ‎AM
To: [hidden email]





Lets say I have a ControllerService that loads some data into memory, and lets say that is happening during OnEnabled.  If I want to test this behavior using the NiFi Mock TestRunner, do I need to explicitly call "enableControllerService" to cause the "@OnEnabled" method on my ControllerService to be called?


Looking at StandardSSLContextService [1] this appears to be the case.  I was not aware of this requirement, and wanted to make sure I understood it.


Is this part of allowing full life cycle support for controller services and NIFI-250?  Any reason to not automatically enable controller services during mock testing?  Or is the thinking that explicitly calling it allows you finer grained testing of difference scenarios.


Thanks!


[1] https://github.com/apache/incubator-nifi/blob/develop/nifi/nifi-nar-bundles/nifi-standard-services/nifi-ssl-context-bundle/nifi-ssl-context-service/src/test/java/org/apache/nifi/ssl/SSLContextServiceTest.java#L106


Dan Bress
Software Engineer
ONYX Consulting Services