@Test void testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted() { String alarm1Name = "i-1-Status-Check-Failed-System"; String alarm2Name = "i-2-Status-Check-Failed-System"; String alarm3Name = "i-3-Status-Check-Failed-System"; String instanceId1 = "i-1"; String instanceId2 = "i-2"; String instanceId3 = "i-3"; MetricAlarm alarm1 = mock(MetricAlarm.class); MetricAlarm alarm2 = mock(MetricAlarm.class); DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class); AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class); AwsCredentialView credentialView = mock(AwsCredentialView.class); CloudStack stack = mock(CloudStack.class); Group group = mock(Group.class); CloudInstance instance1 = mock(CloudInstance.class); CloudInstance instance2 = mock(CloudInstance.class); CloudInstance instance3 = mock(CloudInstance.class); List<Group> groups = List.of(group); when(stack.getParameters()).thenReturn(PARAMETERS); when(stack.getGroups()).thenReturn(groups); when(group.getInstances()).thenReturn(List.of(instance1, instance2, instance3)); when(instance1.getInstanceId()).thenReturn(instanceId1); when(instance2.getInstanceId()).thenReturn(instanceId2); when(instance3.getInstanceId()).thenReturn(instanceId3); when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient); when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult); when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2)); when(alarm1.getAlarmName()).thenReturn(alarm1Name); when(alarm2.getAlarmName()).thenReturn(alarm2Name); underTest.deleteCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView); ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class); ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class); verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture()); verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture()); assertEquals(List.of(alarm1Name, alarm2Name, alarm3Name), captorDescribe.getValue().getAlarmNames()); assertEquals(List.of(alarm1Name, alarm2Name), captorDelete.getValue().getAlarmNames()); }
public void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView) { List<String> instanceIds = stack.getGroups().stream() .flatMap(group -> group.getInstances().stream()) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIds); }
AwsCloudWatchService { public void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView) { List<String> instanceIds = stack.getGroups().stream() .flatMap(group -> group.getInstances().stream()) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIds); } }
AwsCloudWatchService { public void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView) { List<String> instanceIds = stack.getGroups().stream() .flatMap(group -> group.getInstances().stream()) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIds); }  }
AwsCloudWatchService { public void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView) { List<String> instanceIds = stack.getGroups().stream() .flatMap(group -> group.getInstances().stream()) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIds); }  void addCloudWatchAlarmsForSystemFailures(List<CloudResource> instances, CloudStack stack, String regionName, AwsCredentialView credentialView); void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView); void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView, List<String> instanceIds); }
AwsCloudWatchService { public void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView) { List<String> instanceIds = stack.getGroups().stream() .flatMap(group -> group.getInstances().stream()) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIds); }  void addCloudWatchAlarmsForSystemFailures(List<CloudResource> instances, CloudStack stack, String regionName, AwsCredentialView credentialView); void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView); void deleteCloudWatchAlarmsForSystemFailures(CloudStack stack, String regionName, AwsCredentialView credentialView, List<String> instanceIds);  }