DmUtils { public static void markScopedInstanceAsNotDeployed( Instance scopedInstance, ManagedApplication ma, INotificationMngr notificationMngr, IInstancesMngr instanceMngr ) { scopedInstance.data.remove( Instance.IP_ADDRESS ); scopedInstance.data.remove( Instance.MACHINE_ID ); scopedInstance.data.remove( Instance.TARGET_ACQUIRED ); scopedInstance.data.remove( Instance.RUNNING_FROM ); scopedInstance.data.remove( Instance.READY_FOR_CFG_MARKER ); List<Instance> instancesToDelete = new ArrayList<> (); for( Instance i : InstanceHelpers.buildHierarchicalList( scopedInstance )) { InstanceStatus oldstatus = i.getStatus(); i.setStatus( InstanceStatus.NOT_DEPLOYED ); if( oldstatus != InstanceStatus.NOT_DEPLOYED ) notificationMngr.instance( i, ma.getApplication(), EventType.CHANGED ); i.getImports().clear(); if( i.data.containsKey( Instance.DELETE_WHEN_NOT_DEPLOYED )) instancesToDelete.add( i ); } Collections.reverse( instancesToDelete ); for( Instance i : instancesToDelete ) { try { instanceMngr.removeInstance( ma, i, true ); } catch( Exception e ) { Logger logger = Logger.getLogger( DmUtils.class.getName()); logger.severe( "An error occurred while deleting an instance marked with DELETE_WHEN_NOT_DEPLOYED. Instance = " + InstanceHelpers.computeInstancePath( i ) + " App = " + ma.getName()); Utils.logException( logger, e ); } } } private  DmUtils(); static void markScopedInstanceAsNotDeployed(
			Instance scopedInstance,
			ManagedApplication ma,
			INotificationMngr notificationMngr,
			IInstancesMngr instanceMngr );  }
@Test public void testDeletionOfInstances_noInstanceMarked() throws Exception { INotificationMngr notificationMngr = Mockito.mock( INotificationMngr.class ); IInstancesMngr instanceMngr = Mockito.mock( IInstancesMngr.class ); TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); DmUtils.markScopedInstanceAsNotDeployed( app.getMySqlVm(), ma, notificationMngr, instanceMngr ); Assert.assertEquals( InstanceStatus.NOT_DEPLOYED, app.getMySqlVm().getStatus()); Mockito.verifyZeroInteractions( instanceMngr ); }
@Test public void testDeletionOfInstances_withInstanceMarked() throws Exception { INotificationMngr notificationMngr = Mockito.mock( INotificationMngr.class ); IInstancesMngr instanceMngr = Mockito.mock( IInstancesMngr.class ); TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); app.getMySqlVm().data.put( Instance.DELETE_WHEN_NOT_DEPLOYED, "whatever" ); DmUtils.markScopedInstanceAsNotDeployed( app.getMySqlVm(), ma, notificationMngr, instanceMngr ); Assert.assertEquals( InstanceStatus.NOT_DEPLOYED, app.getMySqlVm().getStatus()); Mockito.verify( instanceMngr ).removeInstance( ma, app.getMySqlVm(), true ); }
@Test public void testDeletionOfInstances_withInstanceMarked_andExceptions() throws Exception { INotificationMngr notificationMngr = Mockito.mock( INotificationMngr.class ); IInstancesMngr instanceMngr = Mockito.mock( IInstancesMngr.class ); TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Mockito.doThrow( new RuntimeException( "for test" )).when( instanceMngr ).removeInstance( ma, app.getMySqlVm(), true ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); app.getMySqlVm().data.put( Instance.DELETE_WHEN_NOT_DEPLOYED, "whatever" ); DmUtils.markScopedInstanceAsNotDeployed( app.getMySqlVm(), ma, notificationMngr, instanceMngr ); Assert.assertEquals( InstanceStatus.NOT_DEPLOYED, app.getMySqlVm().getStatus()); Mockito.verify( instanceMngr ).removeInstance( ma, app.getMySqlVm(), true ); }