TargetsMngrImpl implements ITargetsMngr { @Override public String createTarget( String targetContent ) throws IOException { TargetValidator tv = new TargetValidator( targetContent ); tv.validate(); if( RoboconfErrorHelpers.containsCriticalErrors( tv.getErrors())) throw new IOException( "There are errors in the target definition." ); String targetId = tv.getProperties().getProperty( Constants.TARGET_PROPERTY_ID ); String creator = tv.getProperties().getProperty( CREATED_BY ); if( this.targetIds.putIfAbsent( targetId, Boolean.TRUE ) != null ) { if( creator == null ) throw new IOException( "ID " + targetId + " is already used." ); File createdByFile = new File( findTargetDirectory( targetId ), CREATED_BY ); String storedCreator = null; if( createdByFile.exists()) storedCreator = Utils.readFileContent( createdByFile ); if( ! Objects.equals( creator, storedCreator )) throw new IOException( "ID " + targetId + " is already used." ); } targetContent = targetContent.replaceAll( Constants.TARGET_PROPERTY_ID + "\\s*(:|=)[^\n]*(\n|$)", "" ); targetContent = targetContent.replaceAll( "\n\n" + Pattern.quote( CREATED_BY ) + "\\s*(:|=)[^\n]*(\n|$)", "" ); File targetFile = new File( findTargetDirectory( targetId ), Constants.TARGET_PROPERTIES_FILE_NAME ); Utils.createDirectory( targetFile.getParentFile()); Utils.writeStringInto( targetContent, targetFile ); if( creator != null ) { File createdByFile = new File( findTargetDirectory( targetId ), CREATED_BY ); Utils.writeStringInto( creator, createdByFile ); } return targetId; } TargetsMngrImpl( IConfigurationMngr configurationMngr ); @Override String createTarget( String targetContent ); @Override String createTarget( File targetPropertiesFile, ApplicationTemplate creator ); @Override void updateTarget( String targetId, String newTargetContent ); @Override void deleteTarget( String targetId ); @Override void associateTargetWith( String targetId, AbstractApplication app, String instancePathOrComponentName ); @Override void dissociateTargetFrom( AbstractApplication app, String instancePathOrComponentName ); @Override void copyOriginalMapping( Application app ); @Override void applicationWasDeleted( AbstractApplication app ); @Override TargetProperties findTargetProperties( AbstractApplication app, String instancePath ); @Override TargetProperties findTargetProperties( String targetId ); @Override String findTargetId( AbstractApplication app, String instancePath, boolean strict ); @Override String findTargetId( AbstractApplication app, String instancePath ); @Override Map<String,byte[]> findScriptResourcesForAgent( String targetId ); @Override Map<String,byte[]> findScriptResourcesForAgent( AbstractApplication app, Instance scopedInstance ); @Override File findScriptForDm( AbstractApplication app, Instance scopedInstance ); @Override List<TargetWrapperDescriptor> listAllTargets(); @Override TargetWrapperDescriptor findTargetById( String targetId ); @Override List<TargetWrapperDescriptor> listPossibleTargets( AbstractApplication app ); @Override void addHint( String targetId, AbstractApplication app ); @Override void removeHint( String targetId, AbstractApplication app ); @Override TargetProperties lockAndGetTarget( Application app, Instance scopedInstance ); @Override void unlockTarget( Application app, Instance scopedInstance ); @Override List<TargetUsageItem> findUsageStatistics( String targetId );  }
@Test( expected = IOException.class ) public void testCreateTargetFromFile_noApplicationTemplate_conflict() throws Exception { File f = this.folder.newFile("toto.properties"); try { Utils.writeStringInto( "id: tid\nprop: value\nhandler: h", f ); File targetDir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/tid" ); Assert.assertFalse( targetDir.exists()); String targetId = this.mngr.createTarget( f, null ); Assert.assertEquals( "tid", targetId ); Assert.assertTrue( targetDir.exists()); Assert.assertEquals( 1, targetDir.listFiles().length); } catch( Exception e ) { Assert.fail( "No exception was expected here." ); } this.mngr.createTarget( f, null ); }
@Test public void testCreateTargetFromFile_withApplicationTemplate_noConflict() throws Exception { File f = this.folder.newFile("toto.properties"); TestApplicationTemplate tpl = new TestApplicationTemplate(); try { Utils.writeStringInto( "id: tid\nprop: value\nhandler: h", f ); File targetDir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/tid" ); Assert.assertFalse( targetDir.exists()); String targetId = this.mngr.createTarget( f, tpl ); Assert.assertEquals( "tid", targetId ); Assert.assertTrue( targetDir.exists()); Assert.assertEquals( 2, targetDir.listFiles().length); } catch( Exception e ) { Assert.fail( "No exception was expected here." ); } this.mngr.createTarget( f, tpl ); }
@Test( expected = IOException.class ) public void testCreateTargetFromFile_withApplicationTemplate_conflict() throws Exception { File f = this.folder.newFile("toto.properties"); TestApplicationTemplate tpl1 = new TestApplicationTemplate(); try { Utils.writeStringInto( "id: tid\nprop: value\nhandler: h", f ); File targetDir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/tid" ); Assert.assertFalse( targetDir.exists()); String targetId = this.mngr.createTarget( f, tpl1 ); Assert.assertEquals( "tid", targetId ); Assert.assertTrue( targetDir.exists()); Assert.assertEquals( 2, targetDir.listFiles().length); } catch( Exception e ) { Assert.fail( "No exception was expected here." ); } TestApplicationTemplate tpl2 = new TestApplicationTemplate(); tpl2.setName( "whatever" ); this.mngr.createTarget( f, tpl2 ); }
@Test( expected = IOException.class ) public void testCreateTargetFromFile_mix_conflict() throws Exception { File f = this.folder.newFile("toto.properties"); try { Utils.writeStringInto( "id: tid\nprop: value\nhandler: h", f ); File targetDir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/tid" ); Assert.assertFalse( targetDir.exists()); String targetId = this.mngr.createTarget( f, null ); Assert.assertEquals( "tid", targetId ); Assert.assertTrue( targetDir.exists()); Assert.assertEquals( 1, targetDir.listFiles().length); } catch( Exception e ) { Assert.fail( "No exception was expected here." ); } TestApplicationTemplate tpl2 = new TestApplicationTemplate(); this.mngr.createTarget( f, tpl2 ); }
@Test public void testCopyScriptsInTargetDirectory() throws IOException { File f = new File( this.folder.newFolder(), "toto.properties" ); Assert.assertTrue( new File( f.getParentFile(), "toto" ).mkdir()); Utils.writeStringInto( "id: tid\nprop: value\nhandler: h", f ); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( f.getParentFile(), "toto/toto.sh" )); Utils.writeStringInto( "#!/bin/bash\ntouch titi.txt", new File( f.getParentFile(), "titi.sh" )); Utils.writeStringInto( "#!/bin/bash\necho tototo", new File( f.getParentFile(), "toto/script.sh" )); File targetDir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/tid" ); Assert.assertFalse( targetDir.exists()); TestApplicationTemplate appT = new TestApplicationTemplate(); String targetId = this.mngr.createTarget( f, appT ); Assert.assertEquals( "tid", targetId ); Assert.assertTrue( targetDir.exists()); Assert.assertEquals( 3, targetDir.list().length); Assert.assertTrue( new File( targetDir, "target.properties" ).exists()); Assert.assertTrue( new File( targetDir, "created.from" ).exists()); Assert.assertTrue( new File( targetDir, Constants.PROJECT_SUB_DIR_SCRIPTS ).isDirectory()); targetDir = new File( targetDir, Constants.PROJECT_SUB_DIR_SCRIPTS ); Assert.assertEquals( 2, targetDir.list().length); Assert.assertTrue( new File( targetDir, "toto.sh" ).exists()); Assert.assertTrue( new File( targetDir, "script.sh" ).exists()); Assert.assertFalse( new File( targetDir, "titi.sh").exists()); }