DebugMngrImpl implements IDebugMngr { @Override public int pingAgent( ManagedApplication app, Instance scopedInstance, String message ) { int result; MsgEcho ping = new MsgEcho( "PING:" + message ); try { if( scopedInstance.getStatus() != InstanceStatus.NOT_DEPLOYED ) { this.messagingMngr.sendMessageDirectly( app, scopedInstance, ping ); this.logger.fine( "Sent PING request message=" + message + " to application=" + app + ", agent=" + scopedInstance ); result = 0; } else { this.logger.fine( "No PING request was sent to application=" + app + ", agent=" + scopedInstance + ". The agent was not started." ); result = 1; } } catch( IOException e ) { this.logger.fine( "No PING request was sent to application=" + app + ", agent=" + scopedInstance + ". An error occurred with the messaging." ); Utils.logException( this.logger, e ); result = 2; } return result; } DebugMngrImpl( IMessagingMngr messagingMngr, INotificationMngr notificationMngr ); @Override boolean pingMessageQueue( String message ); @Override int pingAgent( ManagedApplication app, Instance scopedInstance, String message ); @Override void notifyMsgEchoReceived( MsgEcho message );  }
@Test public void testSendPingAgent_normal_rootsAreDeployed() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Mockito.verifyZeroInteractions( this.messagingMngr ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); app.getTomcatVm().setStatus( InstanceStatus.PROBLEM ); for( Instance i : app.getRootInstances()) Assert.assertEquals( 0, this.mngr.pingAgent( ma, i, "TEST " + i.getName())); int inv = app.getRootInstances().size(); ArgumentCaptor<Message> argMsg = ArgumentCaptor.forClass( Message.class ); ArgumentCaptor<ManagedApplication> argApp = ArgumentCaptor.forClass( ManagedApplication.class ); Mockito.verify( this.messagingMngr, Mockito.times( inv )).sendMessageDirectly( argApp.capture(), Mockito.any( Instance.class ), argMsg.capture()); List<Message> sentMessages = argMsg.getAllValues(); Assert.assertEquals( app.getRootInstances().size(), sentMessages.size()); int index = 0; for( Instance i : app.getRootInstances()) { Message message = sentMessages.get( index++ ); Assert.assertTrue( message instanceof MsgEcho ); MsgEcho echo = (MsgEcho) message; Assert.assertEquals( "PING:TEST " + i.getName(), echo.getContent()); } for( ManagedApplication t : argApp.getAllValues()) { Assert.assertEquals( ma, t ); } }
@Test public void testSendPingAgent_normal_rootsAreNotDeployed() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Mockito.verifyZeroInteractions( this.messagingMngr ); for (Instance i : app.getRootInstances()) { i.setStatus( InstanceStatus.NOT_DEPLOYED ); Assert.assertEquals( 1, this.mngr.pingAgent( ma, i, "TEST " + i.getName())); } Mockito.verify( this.messagingMngr, Mockito.times( 0 )).sendMessageDirectly( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); }
@Test public void testSendPingAgent_normal_rootsAreDeployed_messagingError() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Mockito.verifyZeroInteractions( this.messagingMngr ); Mockito.doThrow( new IOException( "for test" ) ).when( this.messagingMngr ).sendMessageDirectly( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); for( Instance i : app.getRootInstances()) { i.setStatus( InstanceStatus.DEPLOYED_STARTED ); Assert.assertEquals( 2, this.mngr.pingAgent( ma, i, "TEST " + i.getName())); } int inv = app.getRootInstances().size(); Mockito.verify( this.messagingMngr, Mockito.times( inv )).sendMessageDirectly( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); }