### Question:
StackScalingService { public Map<String, String> getUnusedInstanceIds(String instanceGroupName, Integer scalingAdjustment, Stack stack) { if (scalingAdjustment > 0) { LOGGER.error("Scaling adjustment shouldn't be a positive number, we are trying to downscaling.."); return new HashMap<>(); } InstanceGroup instanceGroup = stack.getInstanceGroupByInstanceGroupName(instanceGroupName); List<InstanceMetaData> unattachedInstanceMetaDatas = new ArrayList<>(instanceGroup.getUnattachedInstanceMetaDataSet()); return unattachedInstanceMetaDatas.stream() .filter(instanceMetaData -> instanceMetaData.getInstanceId() != null && instanceMetaData.getDiscoveryFQDN() != null) .sorted(Comparator.comparing(InstanceMetaData::getStartDate)) .limit(Math.abs(scalingAdjustment)) .collect(Collectors.toMap(InstanceMetaData::getInstanceId, InstanceMetaData::getDiscoveryFQDN)); }  int updateRemovedResourcesState(Collection<String> instanceIds, InstanceGroup instanceGroup); Map<String, String> getUnusedInstanceIds(String instanceGroupName, Integer scalingAdjustment, Stack stack);  }### Answer:
@Test public void testGetUnusedInstanceIds() { Stack stack = mock(Stack.class); InstanceGroup instanceGroup = mock(InstanceGroup.class); when(stack.getInstanceGroupByInstanceGroupName("worker")).thenReturn(instanceGroup); Set<InstanceMetaData> instanceMetaDataSet = new HashSet<>(); InstanceMetaData instanceMetaData1 = new InstanceMetaData(); instanceMetaData1.setStartDate(1L); instanceMetaData1.setInstanceId("instance-id-1"); instanceMetaData1.setDiscoveryFQDN("fqdn1"); instanceMetaDataSet.add(instanceMetaData1); InstanceMetaData instanceMetaData2 = new InstanceMetaData(); instanceMetaData2.setStartDate(3L); instanceMetaData2.setInstanceId("instance-id-2"); instanceMetaData2.setDiscoveryFQDN("fqdn2"); instanceMetaDataSet.add(instanceMetaData2); InstanceMetaData instanceMetaData3 = new InstanceMetaData(); instanceMetaData3.setStartDate(2L); instanceMetaData3.setInstanceId("instance-id-3"); instanceMetaData3.setDiscoveryFQDN("fqdn3"); instanceMetaDataSet.add(instanceMetaData3); InstanceMetaData instanceMetaData4 = new InstanceMetaData(); instanceMetaData4.setStartDate(4L); instanceMetaData4.setInstanceId("instance-id-4"); instanceMetaData4.setDiscoveryFQDN("fqdn4"); instanceMetaDataSet.add(instanceMetaData4); instanceMetaDataSet.add(new InstanceMetaData()); when(instanceGroup.getUnattachedInstanceMetaDataSet()).thenReturn(instanceMetaDataSet); Map<String, String> unusedInstanceIds = stackScalingService.getUnusedInstanceIds("worker", -3, stack); assertTrue(unusedInstanceIds.containsKey("instance-id-1")); assertTrue(unusedInstanceIds.containsKey("instance-id-2")); assertTrue(unusedInstanceIds.containsKey("instance-id-3")); assertFalse(unusedInstanceIds.containsKey("instance-id-4")); }
@Test public void testGetUnusedInstanceIdsButWithPositiveScalingAdjustment() { Stack stack = mock(Stack.class); Map<String, String> unusedInstanceIds = stackScalingService.getUnusedInstanceIds("worker", 5, stack); assertEquals(0, unusedInstanceIds.keySet().size()); }