### Question:
Ec2LookupService { public Map<String, Boolean> checkIfInstancesExistOrTerminated(AWSEnvironment environment, List<String> instanceIds){ Map<String, Boolean> outcome = instanceIds.stream() .collect(Collectors.toMap(Function.identity(), instance -> Boolean.TRUE)); Filter instanceFilter = createAwsFilter("instance-id", instanceIds); Filter runningFilter = createAwsFilter("instance-state-code", Arrays.asList(INSTANCE_RUNNING, STOPPED, STOPPING, SHUTTING_DOWN)); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); describeInstancesRequest.setFilters(Arrays.asList(instanceFilter, runningFilter)); awsSessionService.getEC2Session(environment) .describeInstances(describeInstancesRequest) .getReservations() .forEach(reservation -> { reservation.getInstances() .forEach(instance -> { outcome.put(instance.getInstanceId(), Boolean.FALSE); }); }); return outcome; } @Autowired  Ec2LookupService(SsmService ssmService,
                            AwsSessionService awsSessionService,
                            GatekeeperEC2Properties gatekeeperEc2Properties); List<GatekeeperAWSInstance> getInstances(AWSEnvironment environment, String platform, String searchLabel, String searchString); Map<String, Boolean> checkIfInstancesExistOrTerminated(AWSEnvironment environment, List<String> instanceIds);  }### Answer:
@Test public void testCheckIfInstancesExistOrTerminatedAllFalse(){ mockedResult.setReservations(Arrays.asList(new Reservation[]{ fakeInstance("i-12345", "1.2.3.4", "TestOne", "TEP","Linux"), fakeInstance("i-abcde", "123.2.3.4", "TestOneTwo", "TST", "Linux"), fakeInstance("i-123ab", "456.2.3.4", "HelloOne", "TEP", "Linux"), fakeInstance("i-456cd", "123.22.3.4", "HelloTwo", "TEP", "Linux"), })); Mockito.when(amazonEC2Client.describeInstances(any())).thenReturn(mockedResult); List<String> instanceIds = Arrays.asList("i-12345", "i-abcde", "i-456cd", "i-123ab"); Map<String, Boolean> result = Ec2LookupService.checkIfInstancesExistOrTerminated(awsEnvironment, instanceIds); Assert.assertEquals("There should be 4 elements in the map", instanceIds.size(), result.size()); Assert.assertTrue("No entry should be set to true", result.entrySet().stream().noneMatch(Map.Entry::getValue)); }
@Test public void testCheckIfInstancesExistOrTerminatedMixed(){ mockedResult.setReservations(Arrays.asList(new Reservation[]{ fakeInstance("i-12345", "1.2.3.4", "TestOne", "TEP","Linux"), fakeInstance("i-abcde", "123.2.3.4", "TestOneTwo", "TST", "Linux"), fakeInstance("i-456cd", "123.22.3.4", "HelloTwo", "TEP", "Linux"), })); Mockito.when(amazonEC2Client.describeInstances(any())).thenReturn(mockedResult); List<String> instanceIds = Arrays.asList( "i-12345", "i-abcde", "i-456cd", "i-123ab"); Map<String, Boolean> result = Ec2LookupService.checkIfInstancesExistOrTerminated(awsEnvironment, instanceIds); Assert.assertEquals("The elements in the instance ID list are in the result map", instanceIds.size(), result.size()); Assert.assertTrue("The instance i-123ab is 'terminated'", result.get("i-123ab")); Assert.assertFalse("The instance i-12345 'exists'", result.get("i-12345")); Assert.assertFalse("The instance i-abcde 'exists'", result.get("i-abcde")); Assert.assertFalse("The instance i-456cd 'exists'", result.get("i-456cd")); }