MessagingMngrImpl implements IMessagingMngr { @Override public void sendMessageSafely( ManagedApplication ma, Instance instance, Message message ) { ma.storeAwaitingMessage( instance, message ); sendStoredMessages( ma, instance ); }  void setMessagingClient( RCDm messagingClient ); @Override void sendMessageSafely( ManagedApplication ma, Instance instance, Message message ); @Override void sendMessageDirectly( ManagedApplication ma, Instance scopedInstance, Message message ); @Override void sendStoredMessages( ManagedApplication ma, Instance instance ); @Override void sendMessageToTheDm( Message message ); @Override IDmClient getMessagingClient(); @Override void checkMessagingConfiguration();  }
@Test public void testSendMessageSafely_theMessageIsSentWhenTheMachineIsThere() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Message msg = new MsgCmdRemoveInstance( "/" ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); Mockito.when( this.msgClient.isConnected()).thenReturn( true ); Mockito.verifyZeroInteractions( this.msgClient ); this.mngr.sendMessageSafely( ma, app.getMySqlVm(), msg ); Mockito.verify( this.msgClient, Mockito.times( 1 )).isConnected(); Mockito.verify( this.msgClient, Mockito.times( 1 )).sendMessageToAgent( Mockito.eq( app ), Mockito.eq( app.getMySqlVm()), Mockito.eq( msg )); Assert.assertEquals( 0, ma.removeAwaitingMessages( app.getMySqlVm()).size()); }
@Test public void testSendMessageSafely_theMessageIsSentWhenTheMachineIsThere_butMessagingIsNotReady() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Message msg = new MsgCmdRemoveInstance( "/" ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); Mockito.when( this.msgClient.isConnected()).thenReturn( false ); Mockito.verifyZeroInteractions( this.msgClient ); this.mngr.sendMessageSafely( ma, app.getMySqlVm(), msg ); Mockito.verify( this.msgClient, Mockito.times( 1 )).isConnected(); Mockito.verify( this.msgClient, Mockito.times( 0 )).sendMessageToAgent( Mockito.any( Application.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); List<Message> messages = ma.removeAwaitingMessages( app.getMySqlVm()); Assert.assertEquals( 1, messages.size()); Assert.assertEquals( msg, messages.iterator().next()); }
@Test public void testSendMessageSafely_theMessageIsStoredWhenTheMachineIsNotThere() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Message msg = new MsgCmdRemoveInstance( "/" ); List<InstanceStatus> statuses = new ArrayList<>( Arrays.asList( InstanceStatus.values())); statuses.remove( InstanceStatus.DEPLOYED_STARTED ); Mockito.when( this.msgClient.isConnected()).thenReturn( true ); for( InstanceStatus status : statuses ) { Mockito.reset( this.msgClient ); ma.removeAwaitingMessages( app.getMySqlVm()); app.getMySqlVm().setStatus( status ); Mockito.verifyZeroInteractions( this.msgClient ); this.mngr.sendMessageSafely( ma, app.getMySqlVm(), msg ); Mockito.verify( this.msgClient, Mockito.times( 1 )).isConnected(); Mockito.verify( this.msgClient, Mockito.times( 0 )).sendMessageToAgent( Mockito.any( Application.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); List<Message> messages = ma.removeAwaitingMessages( app.getMySqlVm()); Assert.assertEquals( 1, messages.size()); Assert.assertEquals( msg, messages.iterator().next()); } }
@Test public void testSendMessageSafely_theMessageIsStoredWhenTheMachineIsThere_andThatTheMessagingFails() throws Exception { TestApplication app = new TestApplication(); ManagedApplication ma = new ManagedApplication( app ); Message msg = new MsgCmdRemoveInstance( "/" ); app.getMySqlVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); Mockito.when( this.msgClient.isConnected()).thenReturn( true ); Mockito.doThrow( new IOException( "for test" )).when( this.msgClient ).sendMessageToAgent( Mockito.any( Application.class ), Mockito.any( Instance.class ), Mockito.any( Message.class )); Mockito.verifyZeroInteractions( this.msgClient ); this.mngr.sendMessageSafely( ma, app.getMySqlVm(), msg ); Mockito.verify( this.msgClient, Mockito.times( 1 )).isConnected(); Mockito.verify( this.msgClient, Mockito.times( 1 )).sendMessageToAgent( Mockito.eq( app ), Mockito.eq( app.getMySqlVm()), Mockito.eq( msg )); List<Message> messages = ma.removeAwaitingMessages( app.getMySqlVm()); Assert.assertEquals( 1, messages.size()); Assert.assertEquals( msg, messages.iterator().next()); }