AutonomicMngrImpl implements IAutonomicMngr { @Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } } AutonomicMngrImpl( ICommandsMngr commandsMngr ); void setPreferencesMngr( IPreferencesMngr preferencesMngr ); @Override void loadApplicationRules( Application app ); @Override void refreshApplicationRules( Application app, String ruleFileName ); @Override void unloadApplicationRules( Application app ); @Override void notifyVmWasDeletedByHand( Instance rootInstance ); @Override int getAutonomicInstancesCount(); @Override void handleEvent( ManagedApplication ma, MsgNotifAutonomic event );  }
@Test public void testHandleEvent_withContext_noRule() throws Exception { List<Rule> rules = new ArrayList<>( 0 ); Application app = new Application( "app", new TestApplicationTemplate()); AutonomicApplicationContext ctx = Mockito.spy( new AutonomicApplicationContext( app )); Mockito.when( ctx.findRulesToExecute()).thenReturn( rules ); this.autonomicMngr.appNameToContext.put( "app", ctx ); ManagedApplication ma = new ManagedApplication( app ); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); Mockito.verifyZeroInteractions( this.commandsMngr ); Mockito.verifyZeroInteractions( this.preferencesMngr ); Map<?,?> eventNameToLastRecordTime = TestUtils.getInternalField( ctx, "eventNameToLastRecordTime", Map.class ); Assert.assertEquals( 1, eventNameToLastRecordTime.size()); Assert.assertNotNull( eventNameToLastRecordTime.get( "event" )); }
@Test public void testHandleEvent_withContext_withRule() throws Exception { Mockito.when( this.preferencesMngr.get( Mockito.anyString(), Mockito.anyString())).thenReturn( "5" ); Rule rule1 = Mockito.mock( Rule.class ); Mockito.when( rule1.getCommandsToInvoke()).thenReturn( Arrays.asList( "cmd1" )); Rule rule2 = Mockito.mock( Rule.class ); Mockito.when( rule2.getCommandsToInvoke()).thenReturn( Arrays.asList( "cmd2", "cmd3" )); Application app = new Application( "app", new TestApplicationTemplate()); AutonomicApplicationContext ctx = Mockito.spy( new AutonomicApplicationContext( app )); Mockito.when( ctx.findRulesToExecute()).thenReturn( Arrays.asList( rule1, rule2 )); this.autonomicMngr.appNameToContext.put( "app", ctx ); ManagedApplication ma = new ManagedApplication( app ); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); Mockito.verify( this.preferencesMngr, Mockito.times( 2 )).get( Mockito.anyString(), Mockito.anyString()); Map<?,?> eventNameToLastRecordTime = TestUtils.getInternalField( ctx, "eventNameToLastRecordTime", Map.class ); Assert.assertEquals( 1, eventNameToLastRecordTime.size()); Assert.assertNotNull( eventNameToLastRecordTime.get( "event" )); Mockito.verify( rule1, Mockito.times( 1 )).getCommandsToInvoke(); Mockito.verify( rule2, Mockito.times( 1 )).getCommandsToInvoke(); ArgumentCaptor<CommandExecutionContext> execCtx = ArgumentCaptor.forClass( CommandExecutionContext.class ); Mockito.verify( this.commandsMngr, Mockito.times( 3 )).execute( Mockito.any( Application.class ), Mockito.anyString(), Mockito.any( CommandExecutionContext.class ), Mockito.anyInt(), Mockito.anyString()); for( CommandExecutionContext c : execCtx.getAllValues()) { Assert.assertEquals( 5, c.getMaxVm()); Assert.assertFalse( c.isStrictMaxVm()); } }
@Test public void testHandleEvent_withContext_withRule_negativeMaxVmNumber() throws Exception { Mockito.when( this.preferencesMngr.get( Mockito.anyString(), Mockito.anyString())).thenReturn( "-1" ); ManagedApplication ma = factorizeConfiguration(); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); ArgumentCaptor<CommandExecutionContext> execCtx = ArgumentCaptor.forClass( CommandExecutionContext.class ); Mockito.verify( this.commandsMngr, Mockito.times( 3 )).execute( Mockito.any( Application.class ), Mockito.anyString(), execCtx.capture(), Mockito.anyInt(), Mockito.anyString()); for( CommandExecutionContext c : execCtx.getAllValues()) { Assert.assertEquals( Integer.MAX_VALUE, c.getMaxVm()); Assert.assertFalse( c.isStrictMaxVm()); } }
@Test public void testHandleEvent_withContext_withRule_strictMaximum() throws Exception { Mockito.when( this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE )).thenReturn( "4" ); Mockito.when( this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" )).thenReturn( "true" ); ManagedApplication ma = factorizeConfiguration(); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); Mockito.verify( this.preferencesMngr, Mockito.times( 1 )).get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); Mockito.verify( this.preferencesMngr, Mockito.times( 1 )).get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); ArgumentCaptor<CommandExecutionContext> execCtx = ArgumentCaptor.forClass( CommandExecutionContext.class ); Mockito.verify( this.commandsMngr, Mockito.times( 3 )).execute( Mockito.any( Application.class ), Mockito.anyString(), execCtx.capture(), Mockito.anyInt(), Mockito.anyString()); for( CommandExecutionContext c : execCtx.getAllValues()) { Assert.assertEquals( 4, c.getMaxVm()); Assert.assertTrue( c.isStrictMaxVm()); } }
@Test public void testHandleEvent_withContext_withRule_strictMaximumEnabledAutomatically() throws Exception { TestUtils.getInternalField( this.autonomicMngr, "autonomicVmCount", AtomicInteger.class ).set( 10 ); Mockito.when( this.preferencesMngr.get( Mockito.anyString(), Mockito.anyString())).thenReturn( "5" ); ManagedApplication ma = factorizeConfiguration(); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); ArgumentCaptor<CommandExecutionContext> execCtx = ArgumentCaptor.forClass( CommandExecutionContext.class ); Mockito.verify( this.commandsMngr, Mockito.times( 3 )).execute( Mockito.any( Application.class ), Mockito.anyString(), execCtx.capture(), Mockito.anyInt(), Mockito.anyString()); for( CommandExecutionContext c : execCtx.getAllValues()) { Assert.assertEquals( 5, c.getMaxVm()); Assert.assertTrue( c.isStrictMaxVm()); } }
@Test public void testHandleEvent_execption() throws Exception { Mockito.doThrow( new CommandException( "for test" )).when( this.commandsMngr ).execute( Mockito.any( Application.class ), Mockito.anyString(), Mockito.any( CommandExecutionContext.class ), Mockito.anyInt(), Mockito.anyString()); ManagedApplication ma = factorizeConfiguration(); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); Mockito.verifyZeroInteractions( this.commandsMngr ); }