ReplicateCommandExecution extends AbstractCommandExecution { @Override public void execute() throws CommandException { Instance rootInstance = resolveInstance( this.instr, this.instr.getReplicatedInstancePath(), true ); ManagedApplication ma = resolveManagedApplication( this.manager, this.instr ); CreateInstanceCommandExecution.verify( this.executionContext, rootInstance.getComponent()); try { Instance copy = InstanceHelpers.replicateInstance( rootInstance ); copy.setName( this.instr.getNewInstanceName()); this.manager.instancesMngr().addInstance( ma, null, copy ); String targetId = this.manager.targetsMngr().findTargetId( ma.getApplication(), "/" + rootInstance.getName(), true ); if( targetId != null ) this.manager.targetsMngr().associateTargetWith( targetId, ma.getApplication(), "/" + copy.getName()); CreateInstanceCommandExecution.update( this.executionContext, copy ); } catch( Exception e ) { throw new CommandException( e ); } } ReplicateCommandExecution( ReplicateCommandInstruction instr, Manager manager ); @Override void execute();  }
@Test public void testExecute_success_noDefaultTarget() throws Exception { String instancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); Mockito.verifyZeroInteractions( this.instancesMngr ); Mockito.verifyZeroInteractions( this.targetsMngr ); executor.execute(); Mockito.verify( this.instancesMngr, Mockito.times( 1 )).addInstance( this.ma, null, new Instance( "toto" )); Mockito.verify( this.targetsMngr, Mockito.times( 1 )).findTargetId( this.app, instancePath, true ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).findTargetId( this.app, null ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).associateTargetWith( Mockito.anyString(), Mockito.any( Application.class ), Mockito.anyString()); }
@Test public void testExecute_noDefaultTarget_withAutonomicContext_success() throws Exception { String instancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); executor.setExecutionContext( new CommandExecutionContext( new AtomicInteger( 4 ), new AtomicInteger( 3 ), 2, false, "marker", null )); Mockito.verifyZeroInteractions( this.instancesMngr ); Mockito.verifyZeroInteractions( this.targetsMngr ); executor.execute(); Mockito.verify( this.instancesMngr, Mockito.times( 1 )).addInstance( this.ma, null, new Instance( "toto" )); Mockito.verify( this.targetsMngr, Mockito.times( 1 )).findTargetId( this.app, instancePath, true ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).findTargetId( this.app, null ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).associateTargetWith( Mockito.anyString(), Mockito.any( Application.class ), Mockito.anyString()); }
@Test( expected = CommandException.class ) public void testExecute_noDefaultTarget_withAutonomicContext_failure() throws Exception { String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); executor.setExecutionContext( new CommandExecutionContext( new AtomicInteger( 6 ), new AtomicInteger( 3 ), 5, true, null, null )); Mockito.verifyZeroInteractions( this.instancesMngr ); Mockito.verifyZeroInteractions( this.targetsMngr ); executor.execute(); }
@Test public void testExecute_success_nonDefaultTargetIsCopied() throws Exception { String instancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); Mockito.when( this.targetsMngr.findTargetId( this.app, null )).thenReturn( null ); Mockito.when( this.targetsMngr.findTargetId( this.app, instancePath, true )).thenReturn( "my-target-id" ); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); Mockito.verifyZeroInteractions( this.instancesMngr ); Mockito.verifyZeroInteractions( this.targetsMngr ); executor.execute(); Mockito.verify( this.instancesMngr, Mockito.times( 1 )).addInstance( this.ma, null, new Instance( "toto" )); Mockito.verify( this.targetsMngr, Mockito.times( 1 )).findTargetId( this.app, instancePath, true ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).findTargetId( this.app, null ); Mockito.verify( this.targetsMngr, Mockito.times( 1 )).associateTargetWith( "my-target-id", this.app, "/toto" ); }
@Test public void testExecute_success_defaultTargetIsNotCopied() throws Exception { String instancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); Mockito.when( this.targetsMngr.findTargetId( this.app, null )).thenReturn( "my-target-id" ); Mockito.when( this.targetsMngr.findTargetId( this.app, instancePath, true )).thenReturn( null ); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); Mockito.verifyZeroInteractions( this.instancesMngr ); Mockito.verifyZeroInteractions( this.targetsMngr ); executor.execute(); Mockito.verify( this.instancesMngr, Mockito.times( 1 )).addInstance( this.ma, null, new Instance( "toto" )); Mockito.verify( this.targetsMngr, Mockito.times( 1 )).findTargetId( this.app, instancePath, true ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).findTargetId( this.app, null ); Mockito.verify( this.targetsMngr, Mockito.times( 0 )).associateTargetWith( Mockito.anyString(), Mockito.any( Application.class ), Mockito.anyString()); }
@Test( expected = CommandException.class ) public void testExecute_failure() throws Exception { Mockito .doThrow( new IOException( "for test" )) .when( this.instancesMngr ) .addInstance( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), Mockito.any( Instance.class )); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); executor.execute(); }
@Test( expected = CommandException.class ) public void testExecute_failure_applicationNotFound() throws Exception { Mockito.reset( this.applicationsMngr ); String line = "replicate /" + this.app.getTomcatVm().getName() + " as toto"; ReplicateCommandExecution executor = buildExecutor( line ); executor.execute(); }
@Test( expected = CommandException.class ) public void testExecute_failure_inexistingInstance() throws Exception { ReplicateCommandExecution executor = buildExecutor( "replicate /inexisting as toto", 1 ); executor.execute(); }