### Question:
ScalingHandler implements ApplicationListener<ScalingEvent> { @Override public void onApplicationEvent(ScalingEvent event) { BaseAlert alert = event.getAlert(); Cluster cluster = clusterService.findById(alert.getCluster().getId()); MDCBuilder.buildMdcContext(cluster); ScalingPolicy policy = alert.getScalingPolicy(); int hostGroupNodeCount = event.getHostGroupNodeCount(); int desiredAbsoluteHostGroupNodeCount = event.getDesiredAbsoluteHostGroupNodeCount(); if (hostGroupNodeCount != desiredAbsoluteHostGroupNodeCount) { Runnable scalingRequest = (Runnable) applicationContext.getBean("ScalingRequest", cluster, policy, hostGroupNodeCount, desiredAbsoluteHostGroupNodeCount, event.getDecommissionNodeIds()); executorService.submit(scalingRequest); rejectedThreadService.remove(cluster.getId()); clusterService.setLastScalingActivity(cluster.getId(), System.currentTimeMillis()); } else { LOGGER.info("Autoscaling activity not required for config '{}', cluster '{}'.", alert.getName(), cluster.getStackCrn()); if (alert instanceof TimeAlert) { historyService.createEntry(ScalingStatus.SUCCESS, messagesService.getMessage(MessageCode.AUTOSCALING_ACTIVITY_NOT_REQUIRED, List.of(alert.getAlertType(), alert.getName(), alert.getScalingPolicy().getHostGroup(), hostGroupNodeCount)), hostGroupNodeCount, 0, policy); } } }  @Override void onApplicationEvent(ScalingEvent event);  }### Answer:
@Test public void testOnApplicationEventWhenLoadAlertDecommissionNodes() { LoadAlert loadAlertMock = mock(LoadAlert.class); Cluster cluster = getARunningCluster(); int hostGroupNodeCount = 10; List nodeIds = List.of("nodeId1", "nodeId2", "nodeId3"); int expectedNodeCount = hostGroupNodeCount - nodeIds.size(); when(scalingEventMock.getAlert()).thenReturn(loadAlertMock); when(loadAlertMock.getCluster()).thenReturn(cluster); when(loadAlertMock.getScalingPolicy()).thenReturn(scalingPolicyMock); when(clusterService.findById(anyLong())).thenReturn(cluster); when(scalingPolicyMock.getAdjustmentType()).thenReturn(LOAD_BASED); when(scalingEventMock.getHostGroupNodeCount()).thenReturn(hostGroupNodeCount); when(scalingEventMock.getDesiredAbsoluteHostGroupNodeCount()).thenReturn(expectedNodeCount); when(scalingEventMock.getDecommissionNodeIds()).thenReturn(nodeIds); when(applicationContext.getBean("ScalingRequest", cluster, scalingPolicyMock, hostGroupNodeCount, expectedNodeCount, nodeIds)).thenReturn(runnableMock); underTest.onApplicationEvent(scalingEventMock); verify(rejectedThreadService).remove(AUTOSCALE_CLUSTER_ID); verify(clusterService).setLastScalingActivity(eq(AUTOSCALE_CLUSTER_ID), anyLong()); verify(executorService).submit(runnableMock); }
@Test public void testOnApplicationEventWhenNoScaling() { Cluster cluster = getARunningCluster(); TimeAlert timeAlertMock = mock(TimeAlert.class); when(scalingEventMock.getAlert()).thenReturn(timeAlertMock); when(scalingEventMock.getHostGroupNodeCount()).thenReturn(2); when(scalingEventMock.getDesiredAbsoluteHostGroupNodeCount()).thenReturn(2); when(scalingPolicyMock.getHostGroup()).thenReturn("compute"); when(clusterService.findById(anyLong())).thenReturn(cluster); when(messagesService.getMessage(anyString(), any(List.class))).thenReturn(""); when(timeAlertMock.getCluster()).thenReturn(cluster); when(timeAlertMock.getScalingPolicy()).thenReturn(scalingPolicyMock); when(timeAlertMock.getName()).thenReturn("testalert"); when(timeAlertMock.getAlertType()).thenCallRealMethod(); underTest.onApplicationEvent(scalingEventMock); verify(historyService, times(1)).createEntry( eq(ScalingStatus.SUCCESS), anyString(), eq(2), eq(0), eq(scalingPolicyMock)); verify(clusterService, never()).setLastScalingActivity(eq(AUTOSCALE_CLUSTER_ID), anyLong()); verify(applicationContext, never()).getBean("ScalingRequest"); }