EC2Context { public List<String> getAutoScalingMembers(String autoScalingGroupName) { Preconditions.checkArgument(autoScalingGroupName != null && !autoScalingGroupName.isEmpty()); AutoScalingGroup autoScalingGroup = this.getAutoScalingGroup(autoScalingGroupName); List<String> list = new ArrayList<>(); for (com.amazonaws.services.autoscaling.model.Instance instance : autoScalingGroup.getInstances()) { if (instance.getHealthStatus().equals("Healthy")) { list.add(instance.getInstanceId()); } } return list; }  String getInstanceId(); Map<String, String> getInstanceTags(); String getAutoScalingGroup(); List<String> getAutoScalingMembers(String autoScalingGroupName); List<String> getPrivateAutoScalingMemberIPs(String autoScalingGroupName); AutoScalingGroup getAutoScalingGroup(String autoScalingGroupName); Map<String, String> getAutoScalingGroupTags(String autoScalingGroupName); Instance getInstance();  }
@Test public void getServerGroupMembers() { Collection<com.amazonaws.services.autoscaling.model.Instance> instances = new ArrayList<>(); com.amazonaws.services.autoscaling.model.Instance healthyInstance = new com.amazonaws.services.autoscaling.model.Instance(); healthyInstance.setInstanceId("healthyOne"); healthyInstance.setHealthStatus("Healthy"); instances.add(healthyInstance); com.amazonaws.services.autoscaling.model.Instance unhealthyInstance = new com.amazonaws.services.autoscaling.model.Instance(); unhealthyInstance.setInstanceId("unhealthyOne"); unhealthyInstance.setHealthStatus("Unhealthy"); instances.add(unhealthyInstance); AutoScalingGroup grp = new AutoScalingGroup(); grp.setAutoScalingGroupName("awsGroup"); grp.setInstances(instances); DescribeAutoScalingGroupsResult res = new DescribeAutoScalingGroupsResult(); res.setAutoScalingGroups(Collections.singleton(grp)); Mockito.when(this.asgMock.describeAutoScalingGroups(Mockito.any(DescribeAutoScalingGroupsRequest.class))).thenReturn(res); try { this.context.getAutoScalingMembers(null); Assert.fail("Null check failed"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } try { this.context.getAutoScalingMembers(""); Assert.fail("Empty check failed"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } List<String> groupMembers = this.context.getAutoScalingMembers("awsGroup"); Assert.assertEquals(1, groupMembers.size()); Assert.assertEquals("healthyOne", groupMembers.get(0)); }
@Test(expected = IllegalStateException.class) public void getServerGroupMembersWithInvalidResponseMultipleGroups() { AutoScalingGroup grp = new AutoScalingGroup().withAutoScalingGroupName("awsGroup"); AutoScalingGroup grp2 = new AutoScalingGroup().withAutoScalingGroupName("awsGroup2"); DescribeAutoScalingGroupsResult res = new DescribeAutoScalingGroupsResult(); res.withAutoScalingGroups(grp, grp2); Mockito.when(this.asgMock.describeAutoScalingGroups(Mockito.any(DescribeAutoScalingGroupsRequest.class))).thenReturn(res); this.context.getAutoScalingMembers("awsGroup"); }
@Test(expected = IllegalStateException.class) public void getServerGroupMembersWithInvalidResponseMissingGroups() { DescribeAutoScalingGroupsResult res = new DescribeAutoScalingGroupsResult(); res.withAutoScalingGroups(); Mockito.when(this.asgMock.describeAutoScalingGroups(Mockito.any(DescribeAutoScalingGroupsRequest.class))).thenReturn(res); this.context.getAutoScalingMembers("awsGroup"); }