DiagnosticMessagingCommand implements Action { @Override public Object execute() throws Exception { RbcfInfo info = KarafDmCommandsUtils.findInstances( this.manager, this.applicationName, this.scopedInstancePath, this.out ); if( ! info.getScopedInstances().isEmpty()) { DiagnosticListener listener = new DiagnosticListener( this.uuidToFound ); try { this.manager.listenerAppears( listener ); this.out.println( "Pinging the DM..." ); String uuid = UUID.randomUUID().toString(); this.uuidToTarget.put( uuid, "The DM" ); this.manager.debugMngr().pingMessageQueue( uuid ); for( Instance inst : info.getScopedInstances()) { String path = InstanceHelpers.computeInstancePath( inst ); if( inst.getStatus() != InstanceStatus.DEPLOYED_STARTED ) { this.out.println( "Skipping agent " + path + " as it is not deployed and started." ); continue; } this.out.println( "Pinging agent " + path ); uuid = UUID.randomUUID().toString(); this.uuidToTarget.put( uuid, "Agent " + path ); this.manager.debugMngr().pingAgent( info.getManagedApplication(), inst, uuid ); } this.out.println( "" ); this.out.println( "Waiting few seconds to get PING results..." ); this.out.println( "" ); Thread.sleep( this.waitingDelay * 1000 ); } finally { this.manager.listenerDisappears( listener ); listener = null; this.out.println( "" ); this.out.print( "The " + this.waitingDelay + " second" ); if( this.waitingDelay > 1 ) this.out.print( "s" ); this.out.print( " delay elapsed." ); this.out.println( "" ); for( Map.Entry<String,String> entry : this.uuidToTarget.entrySet()) { Boolean b = this.uuidToFound.get( entry.getKey()); if( b != null && b ) this.out.println( "[ SUCCESS ] " + entry.getValue() + " responded to the ping." ); } this.out.println( "" ); for( Map.Entry<String,String> entry : this.uuidToTarget.entrySet()) { Boolean b = this.uuidToFound.get( entry.getKey()); if( b == null || ! b ) this.out.println( "[ FAILURE ] " + entry.getValue() + " did not respond to the ping." ); } } } return null; }  @Override Object execute();  }
@Test public void testExecute_invalidApplicationName() throws Exception { this.cmd.applicationName = "invalid"; this.cmd.scopedInstancePath = null; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "Unknown application" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verifyZeroInteractions( this.debugMngr ); }
@Test public void testExecute_invalidInstance() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = "not here"; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "There is no" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verifyZeroInteractions( this.debugMngr ); }
@Test public void testExecute_notAScopedInstance() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getWar()); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "is not a scoped instance" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verifyZeroInteractions( this.debugMngr ); }
@Test public void testExecute_valid_noSpecifiedInstance_notDeployed() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = null; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingMessageQueue( Mockito.anyString()); }
@Test public void testExecute_valid_noSpecifiedInstance_allDeployed() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = null; for( Instance inst : InstanceHelpers.findAllScopedInstances( this.app )) inst.setStatus( InstanceStatus.DEPLOYED_STARTED ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingMessageQueue( Mockito.anyString()); Mockito.verify( this.debugMngr, Mockito.times( 2 )).pingAgent( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), Mockito.anyString()); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "Pinging the DM..." )); Assert.assertTrue( s.contains( "Pinging agent" )); Assert.assertFalse( s.contains( "null" )); Assert.assertTrue( s.contains( "[ FAILURE ]" )); Assert.assertFalse( s.contains( "[ SUCCESS ]" )); }
@Test public void testExecute_valid_withSpecifiedInstance_deployed() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); this.app.getTomcatVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingMessageQueue( Mockito.anyString()); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingAgent( Mockito.eq( this.ma ), Mockito.eq( this.app.getTomcatVm()), Mockito.anyString()); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "Pinging the DM..." )); Assert.assertTrue( s.contains( "Pinging agent" )); Assert.assertFalse( s.contains( "null" )); Assert.assertTrue( s.contains( "[ FAILURE ]" )); Assert.assertFalse( s.contains( "[ SUCCESS ]" )); }
@Test public void testExecute_valid_withSpecifiedInstance_deployed_pingResponded() throws Exception { this.cmd.applicationName = this.app.getName(); this.cmd.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); this.app.getTomcatVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.cmd.out = new PrintStream( os, true, "UTF-8" ); this.cmd.waitingDelay = 2; new Thread() { @Override public void run() { try { Thread.sleep( 1000 ); for( String uuid : DiagnosticMessagingCommandTest.this.cmd.uuidToTarget.keySet()) DiagnosticMessagingCommandTest.this.cmd.uuidToFound.put( uuid, Boolean.TRUE ); } catch( InterruptedException e ) { e.printStackTrace(); } }; }.start(); this.cmd.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.cmd.applicationName ); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingMessageQueue( Mockito.anyString()); Mockito.verify( this.debugMngr, Mockito.times( 1 )).pingAgent( Mockito.eq( this.ma ), Mockito.eq( this.app.getTomcatVm()), Mockito.anyString()); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "Pinging the DM..." )); Assert.assertTrue( s.contains( "Pinging agent" )); Assert.assertFalse( s.contains( "null" )); Assert.assertFalse( s.contains( "[ FAILURE ]" )); Assert.assertTrue( s.contains( "[ SUCCESS ]" )); }