### Question:
UpdateFailedHandler implements ApplicationListener<UpdateFailedEvent> { @Override public void onApplicationEvent(UpdateFailedEvent event) { long autoscaleClusterId = event.getClusterId(); Cluster cluster = clusterService.findById(autoscaleClusterId); if (cluster == null) { return; } MDCBuilder.buildMdcContext(cluster); Integer failed = Optional.ofNullable(updateFailures.get(autoscaleClusterId)) .map(failedCount -> failedCount + 1) .orElse(1); if (failed < RETRY_THRESHOLD) { updateFailures.put(autoscaleClusterId, failed); LOGGER.debug("Increased failed count '{}' for cluster '{}'", failed, cluster.getStackCrn()); } else { suspendCluster(cluster); updateFailures.remove(autoscaleClusterId); LOGGER.debug("Suspended cluster monitoring for cluster '{}' due to failing update attempts", cluster.getStackCrn()); } }  @Override void onApplicationEvent(UpdateFailedEvent event);  }### Answer:
@Test public void testOnApplicationEventWhenFailsFirstTime() { Cluster cluster = getARunningCluster(); when(clusterService.findById(anyLong())).thenReturn(cluster); underTest.onApplicationEvent(new UpdateFailedEvent(AUTOSCALE_CLUSTER_ID)); verify(clusterService).findById(AUTOSCALE_CLUSTER_ID); verify(clusterService, never()).setState(AUTOSCALE_CLUSTER_ID, ClusterState.SUSPENDED); }
@Test public void testOnApplicationEventWhenFailsFourTimes() { Cluster cluster = getARunningCluster(); when(clusterService.findById(anyLong())).thenReturn(cluster); IntStream.range(0, 4).forEach(i -> underTest.onApplicationEvent(new UpdateFailedEvent(AUTOSCALE_CLUSTER_ID))); verify(clusterService, times(4)).findById(AUTOSCALE_CLUSTER_ID); verify(clusterService, never()).setState(AUTOSCALE_CLUSTER_ID, ClusterState.SUSPENDED); }
@Test public void testOnApplicationEventWhenFailsFiveTimes() { Cluster cluster = getARunningCluster(); when(clusterService.findById(anyLong())).thenReturn(cluster); IntStream.range(0, 5).forEach(i -> underTest.onApplicationEvent(new UpdateFailedEvent(AUTOSCALE_CLUSTER_ID))); verify(clusterService, times(5)).findById(AUTOSCALE_CLUSTER_ID); verify(clusterService).setState(AUTOSCALE_CLUSTER_ID, ClusterState.SUSPENDED); }