TargetsMngrImpl implements ITargetsMngr { @Override public void applicationWasDeleted( AbstractApplication app ) throws IOException { String name = app.getName(); String qualifier = app instanceof ApplicationTemplate ? ((ApplicationTemplate) app).getVersion() : null; List<InstanceContext> toClean = new ArrayList<> (); Set<String> targetIds = new HashSet<> (); for( Map.Entry<InstanceContext,String> entry : this.instanceToCachedId.entrySet()) { if( Objects.equals( name, entry.getKey().getName()) && Objects.equals( qualifier, entry.getKey().getQualifier())) { targetIds.add( entry.getValue()); toClean.add( entry.getKey()); } } for( Instance scopedInstance : InstanceHelpers.findAllScopedInstances( app )) { InstanceContext ctx = new InstanceContext( app, scopedInstance ); toClean.add( ctx ); } for( String targetId : targetIds ) { File targetDirectory = findTargetDirectory( targetId ); File[] files = new File[] { new File( targetDirectory, TARGETS_ASSOC_FILE ), new File( targetDirectory, TARGETS_HINTS_FILE ), new File( targetDirectory, TARGETS_USAGE_FILE ) }; for( File f : files ) { for( InstanceContext key : toClean ) { Properties props = Utils.readPropertiesFileQuietly( f, this.logger ); props.remove( key.toString()); writeProperties( props, f ); } } } for( InstanceContext key : toClean ) this.instanceToCachedId.remove( key ); } 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 public void testApplicationWasDeleted() throws Exception { TestApplication app1 = new TestApplication(); app1.name( "app1" ); TestApplication app2 = new TestApplication(); app2.name( "app2" ); String t1 = this.mngr.createTarget( "id: t1\nprop: ok\nname: target 1\ndescription: t1's target\nhandler: h" ); String t2 = this.mngr.createTarget( "id: t2\nprop: ok\nhandler: docker" ); String path = InstanceHelpers.computeInstancePath( app1.getTomcatVm()); this.mngr.associateTargetWith( t1, app1.getTemplate(), null ); this.mngr.associateTargetWith( t1, app1, null ); this.mngr.associateTargetWith( t2, app1, path ); this.mngr.associateTargetWith( t2, app2, null ); this.mngr.addHint( t1, app1 ); this.mngr.addHint( t2, app1 ); this.mngr.addHint( t2, app2 ); this.mngr.lockAndGetTarget( app2, app2.getTomcatVm()); Assert.assertEquals( t1, this.mngr.findTargetId( app1, null )); Assert.assertEquals( t2, this.mngr.findTargetId( app1, path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app1 ).size()); Assert.assertEquals( t2, this.mngr.findTargetId( app2, null )); Assert.assertEquals( t2, this.mngr.findTargetId( app2, path )); Assert.assertEquals( 1, this.mngr.listPossibleTargets( app2 ).size()); Assert.assertEquals( t1, this.mngr.findTargetId( app1.getTemplate(), path )); Assert.assertEquals( 0, this.mngr.listPossibleTargets( app1.getTemplate()).size()); this.mngr.applicationWasDeleted( app1 ); Assert.assertNull( this.mngr.findTargetId( app1, null )); Assert.assertNull( this.mngr.findTargetId( app1, path )); List<TargetWrapperDescriptor> hints = this.mngr.listPossibleTargets( app1 ); Assert.assertEquals( 1, hints.size()); Assert.assertEquals( t1, hints.get( 0 ).getId()); Assert.assertEquals( t2, this.mngr.findTargetId( app2, null )); Assert.assertEquals( t2, this.mngr.findTargetId( app2, path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app2 ).size()); Assert.assertEquals( t1, this.mngr.findTargetId( app1.getTemplate(), path )); Assert.assertEquals( 1, this.mngr.listPossibleTargets( app1.getTemplate()).size()); this.mngr.applicationWasDeleted( app1.getTemplate()); Assert.assertNull( this.mngr.findTargetId( app1, null )); Assert.assertNull( this.mngr.findTargetId( app1, path )); Assert.assertEquals( 1, this.mngr.listPossibleTargets( app1 ).size()); Assert.assertEquals( t2, this.mngr.findTargetId( app2, null )); Assert.assertEquals( t2, this.mngr.findTargetId( app2, path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app2 ).size()); Assert.assertNull( this.mngr.findTargetId( app1.getTemplate(), path )); Assert.assertEquals( 1, this.mngr.listPossibleTargets( app1.getTemplate()).size()); this.mngr.applicationWasDeleted( app2 ); Assert.assertNull( this.mngr.findTargetId( app1, null )); Assert.assertNull( this.mngr.findTargetId( app1, path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app1 ).size()); Assert.assertNull( this.mngr.findTargetId( app2, null )); Assert.assertNull( this.mngr.findTargetId( app2, path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app2 ).size()); Assert.assertNull( this.mngr.findTargetId( app1.getTemplate(), path )); Assert.assertEquals( 2, this.mngr.listPossibleTargets( app1.getTemplate()).size()); this.mngr.deleteTarget( t1 ); this.mngr.deleteTarget( t2 ); }