nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Instantiating a Controller Service in a Junit test
Date Sun, 09 Aug 2015 14:40:53 GMT
I have written a simple Cache Controller Service, this Controller Service has a property which
if populated allows the cache to be populated when it is intialized.  I have also written
a simple processor that allows me to utilize the Controller Service and checks some of the
preloaded values and also checks some of the cache methods.
I now want to write some Junit tests for my processor, and I want to instantiate my Cache
Controller Service.  I have looked at other Junit test classes in the nifi-0.2.1 source release
for some guidance on how to do this, looking particularly at the test classes for the DetectDuplicate
I have imported the Controller Service API and based on what I saw in the DetectDuplicate
tests I have created a test shown below:
public class TestCacheTester {
    public void  checkCache() throws InitializationException, IOException {
        final TestRunner runner = TestRunners.newTestRunner(CacheTester.class);
        final StandardCacheService  testCache = createService();
        runner.addControllerService("my-cache", testCache );
        runner.setProperty("Cache Service", "my-cache");        
     private StandardCacheService createService() throws InitializationException {

            final StandardCacheService cacheService = new StandardCacheService();
            final ComponentLog logger = new MockProcessorLog("cacheService", cacheService);
            final MockControllerServiceInitializationContext clientInitContext =
new                       MockControllerServiceInitializationContext(cacheService,
"cacheService", logger);

            return cacheService;
     static final class StandardCacheService extends AbstractControllerService implements
CacheServiceAPI {

         public static Map<String, String> cacheMap = new HashMap<String,

            public void onPropertyModified(final PropertyDescriptor descriptor,
final String oldValue, final String                 newValue) {

            protected java.util.List<PropertyDescriptor> getSupportedPropertyDescriptors()
                final List<PropertyDescriptor> props = new ArrayList<>();
                return props;
            public void onConfigured(final ConfigurationContext context) throws
InitializationException {
               // configContext = context;                


            private void initializeCache(String fileName) {           
                try {
                    BufferedReader br = new BufferedReader(new FileReader(fileName));   
                    String line = "";                    
                    while((line = br.readLine()) != null){           
                        String [] values = line.split(",");
                        cacheMap.put(values[0], values[1]);
                } catch (IOException e) {                    
                    System.out.println("IO Exception " + e);
........ Plus other Cache Methods snipped for brevity................

My question is should I actually be importing my StandardCacheService  class and setting
its property descriptor or is the above listing where I have effectively rewritten StandardCacheService
class in the Junit Test Class the correct way of using a Controller Service in a Junit test?
If the former is correct how do I set the PropertyDescriptor as when I did try this option
the StandardCacheService.DATAFILE PropertyDescriptor was never visible?
Many thanksDave

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message