### Question:
ClusterStatusUpdater { public void updateClusterStatus(Stack stack, Cluster cluster) { if (isStackOrClusterStatusInvalid(stack, cluster)) { if ((stack.isStackInStopPhase() || stack.isDeleteCompleted()) && cluster != null) { updateClusterStatus(stack.getId(), cluster, stack.getStatus()); cluster.setStatus(stack.getStatus()); } List<String> eventMessageArgs = Arrays.asList(stack.getStatus().name(), cluster == null ? "" : cluster.getStatus().name()); cloudbreakEventService.fireCloudbreakEvent(stack.getId(), stack.getStatus().name(), CLUSTER_AMBARI_CLUSTER_COULD_NOT_SYNC, eventMessageArgs); } else if (cluster != null && cluster.getClusterManagerIp() != null) { Long stackId = stack.getId(); clusterService.updateClusterMetadata(stackId); String blueprintName = cluster.getBlueprint().getStackName(); ClusterStatusResult clusterStatusResult = clusterApiConnectors.getConnector(stack).clusterStatusService().getStatus(StringUtils.isNotBlank(blueprintName)); LOGGER.debug("Cluster status: [{}] Status reason: [{}]", clusterStatusResult.getClusterStatus(), clusterStatusResult.getStatusReason()); updateClusterStatus(stackId, stack.getStatus(), cluster, clusterStatusResult); } }  void updateClusterStatus(Stack stack, Cluster cluster);  }### Answer:
@Test public void testUpdateClusterStatusShouldUpdateStackStatusWhenStackStatusChanged() { Stack stack = createStack(Status.AVAILABLE, Status.AVAILABLE); when(clusterStatusService.getStatus(anyBoolean())).thenReturn(ClusterStatusResult.of(ClusterStatus.INSTALLED)); underTest.updateClusterStatus(stack, stack.getCluster()); Mockito.verify(clusterService, Mockito.times(1)).updateClusterStatusByStackId(stack.getId(), Status.STOPPED); }
@Test public void testUpdateClusterStatusShouldUpdateStackStatusWhenThereAreStoppedServices() { Stack stack = createStack(Status.AVAILABLE, Status.UPDATE_FAILED); underTest.updateClusterStatus(stack, stack.getCluster()); Mockito.verify(clusterService, Mockito.times(1)).updateClusterStatusByStackId(stack.getId(), Status.AVAILABLE); }
@Test public void testUpdateClusterStatusShouldNotUpdateStackStatusWhenMaintenanceModeIsEnabledAndCLusterIsAvailable() { Stack stack = createStack(Status.AVAILABLE, Status.MAINTENANCE_MODE_ENABLED); underTest.updateClusterStatus(stack, stack.getCluster()); Mockito.verify(clusterService, Mockito.never()).updateClusterStatusByStackId(eq(stack.getId()), any(Status.class)); }
@Test public void testUpdateClusterStatusShouldUpdateStackStatusWhenMaintenanceModeIsEnabledButClusterIsStopped() { Stack stack = createStack(Status.AVAILABLE, Status.MAINTENANCE_MODE_ENABLED); when(clusterStatusService.getStatus(anyBoolean())).thenReturn(ClusterStatusResult.of(ClusterStatus.INSTALLED)); underTest.updateClusterStatus(stack, stack.getCluster()); Mockito.verify(clusterService, Mockito.times(1)).updateClusterStatusByStackId(stack.getId(), Status.STOPPED); }
@Test public void testUpdateClusterStatusShouldOnlyNotifyWhenStackStatusNotChanged() { Stack stack = createStack(Status.AVAILABLE, Status.AVAILABLE); underTest.updateClusterStatus(stack, stack.getCluster()); Mockito.verify(clusterService, Mockito.times(0)).updateClusterStatusByStackId(any(Long.class), any(Status.class)); }
@Test public void testUpdateClusterStatusShouldUpdateWhenStackStatusStopped() { Stack stack = createStack(Status.STOPPED, Status.AVAILABLE); underTest.updateClusterStatus(stack, stack.getCluster()); BDDMockito.verify(clusterService, BDDMockito.times(1)).updateClusterStatusByStackId(any(Long.class), eq(stack.getStatus())); }