ResourceUtils { public static Map<String,byte[]> storeInstanceResources( File applicationFilesDirectory, Instance instance ) throws IOException { Map<String,byte[]> result = new HashMap<> (); File instanceResourcesDirectory = findInstanceResourcesDirectory( applicationFilesDirectory, instance ); if( instanceResourcesDirectory.exists() && instanceResourcesDirectory.isDirectory()) result.putAll( Utils.storeDirectoryResourcesAsBytes( instanceResourcesDirectory )); result.putAll( storeInstanceProbeResources( applicationFilesDirectory, instance )); return result; } private  ResourceUtils(); static Map<String,byte[]> storeInstanceResources( File applicationFilesDirectory, Instance instance ); static Map<String,byte[]> storeInstanceProbeResources( File applicationFilesDirectory, Instance instance ); static File findInstanceResourcesDirectory( File applicationFilesDirectory, Instance instance ); static File findInstanceResourcesDirectory( File applicationFilesDirectory, Component component ); static Map<Component,File> findScopedInstancesDirectories( AbstractApplication absApp );  }
@Test public void testStoreInstanceResources_inexistingDirectory() throws Exception { Instance instance = new Instance( "whatever" ).component( new Component( "comp" )); Map<?,?> map = ResourceUtils.storeInstanceResources( new File( "file/does/not/exist" ), instance ); Assert.assertEquals( 0, map.size()); }
@Test public void testStoreInstanceResources_notADirectory() throws Exception { final File appDir = this.folder.newFolder(); final String componentName = "my-component"; final File componentDirectory = new File( appDir, Constants.PROJECT_DIR_GRAPH + File.separator + componentName ); Assert.assertTrue( componentDirectory.getParentFile().mkdirs()); Assert.assertTrue( componentDirectory.createNewFile()); Instance instance = new Instance( "whatever" ).component( new Component( componentName )); Map<?,?> map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 0, map.size()); }
@Test public void testStoreInstanceResources() throws Exception { final File appDir = this.folder.newFolder(); final String componentName = "my-component"; final File componentDirectory = new File( appDir, Constants.PROJECT_DIR_GRAPH + File.separator + componentName ); Assert.assertTrue( componentDirectory.mkdirs()); Instance instance = new Instance( "whatever" ).component( new Component( componentName )); Map<?,?> map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 0, map.size()); File recipeFile = new File( componentDirectory, "recipe.txt" ); Assert.assertTrue( recipeFile.createNewFile()); map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 1, map.size()); File autonomicDir = new File( appDir, Constants.PROJECT_DIR_PROBES ); Assert.assertTrue( autonomicDir.mkdir()); File propertiesFile = new File( autonomicDir, componentName + Constants.FILE_EXT_MEASURES + ".properties" ); Assert.assertTrue( propertiesFile.createNewFile()); map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 1, map.size()); File measuresFile = new File( autonomicDir, componentName + Constants.FILE_EXT_MEASURES ); Assert.assertTrue( measuresFile.createNewFile()); map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 3, map.size()); Utils.deleteFilesRecursively( propertiesFile ); map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 2, map.size()); }