nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From DAVID SMITH <davidrsm...@btinternet.com>
Subject Instantiating a Controller Service in a Junit test
Date Sun, 09 Aug 2015 14:40:53 GMT
Hi
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
processor.
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 {
    
    @Test
    public void  checkCache() throws InitializationException, IOException {
        
        final TestRunner runner = TestRunners.newTestRunner(CacheTester.class);
        final StandardCacheService  testCache = createService();
        
        runner.addControllerService("my-cache", testCache );
        runner.enableControllerService(testCache);        
        runner.setProperty("Cache Service", "my-cache");        
        runner.enqueue(Paths.get("src/test/resources/hello.txt"));
        runner.run();        
    }
        
     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);
            cacheService.initialize(clientInitContext);

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

         public static Map<String, String> cacheMap = new HashMap<String,
String>();

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

            @Override
            protected java.util.List<PropertyDescriptor> getSupportedPropertyDescriptors()
{
                final List<PropertyDescriptor> props = new ArrayList<>();
                //props.add(StandardCacheService.DATAFILE);
                return props;
            } 
            
            @OnEnabled
            public void onConfigured(final ConfigurationContext context) throws
InitializationException {
               // configContext = context;                
                initializeCache("/data/TEST_FILE");             

            }

            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]);
                    }                    
                    br.close();                    
                } 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



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