@Test void testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing() { List<CloudInstance> cloudInstances = new LinkedList<>(); List<List<String>> deleteAlarmNames = new LinkedList<>(); List<String> alarmNames1 = new LinkedList<>(); List<String> alarmNames2 = new LinkedList<>(); List<String> alarmNames3 = new LinkedList<>(); for (int i = 1; i <= 100; i++) { String alarmName = "i-" + i + "-Status-Check-Failed-System"; alarmNames1.add(alarmName); deleteAlarmNames.add(List.of(alarmName)); } for (int i = 101; i <= 200; i++) { String alarmName = "i-" + i + "-Status-Check-Failed-System"; alarmNames2.add(alarmName); deleteAlarmNames.add(List.of(alarmName)); } for (int i = 201; i <= 210; i++) { String alarmName = "i-" + i + "-Status-Check-Failed-System"; alarmNames3.add(alarmName); deleteAlarmNames.add(List.of(alarmName)); } for (int i = 1; i <= 210; i++) { String instanceId = "i-" + i; CloudInstance cloudInstance = mock(CloudInstance.class); when(cloudInstance.getInstanceId()).thenReturn(instanceId); cloudInstances.add(cloudInstance); } AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class); AwsCredentialView credentialView = mock(AwsCredentialView.class); CloudStack stack = mock(CloudStack.class); Group group = mock(Group.class); List<Group> groups = List.of(group); when(stack.getParameters()).thenReturn(PARAMETERS); when(stack.getGroups()).thenReturn(groups); when(group.getInstances()).thenReturn(cloudInstances); when(awsClient.createCloudWatchClient(credentialView, REGION)).thenReturn(cloudWatchClient); AmazonCloudWatchException exception = new AmazonCloudWatchException("No permissions to descrive cloudwatch alarms"); when(cloudWatchClient.describeAlarms(any())).thenThrow(exception); underTest.deleteCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView); ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class); ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class); verify(cloudWatchClient, times(3)).describeAlarms(captorDescribe.capture()); verify(cloudWatchClient, times(210)).deleteAlarms(captorDelete.capture()); assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList())); assertEquals(deleteAlarmNames, captorDelete.getAllValues().stream().map(DeleteAlarmsRequest::getAlarmNames).collect(Collectors.toList())); }
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);  }