PresentSpotRequestsAndInstances extends PresentInstances { @Override public Set<RunningInstance> apply(Set<RegionAndName> regionAndIds) { if (checkNotNull(regionAndIds, "regionAndIds").isEmpty()) return ImmutableSet.of(); if (any(regionAndIds, compose(containsPattern("sir-"), nameFunction()))) return getSpots(regionAndIds); return super.apply(regionAndIds); } @Inject  PresentSpotRequestsAndInstances(AWSEC2Client client, Function<SpotInstanceRequest, AWSRunningInstance> spotConverter); @Override Set<RunningInstance> apply(Set<RegionAndName> regionAndIds); @Override String toString();  }
@SuppressWarnings("unchecked") @Test public void testWhenInstancesPresentSingleCall() { AWSEC2Client client = createMock(AWSEC2Client.class); AWSInstanceClient instanceClient = createMock(AWSInstanceClient.class); Function<SpotInstanceRequest, AWSRunningInstance> converter = createMock(Function.class); expect(client.getInstanceServices()).andReturn(instanceClient); expect(instanceClient.describeInstancesInRegion("us-east-1", "i-aaaa", "i-bbbb")).andReturn( Set.class.cast(ImmutableSet.of(Reservation.<AWSRunningInstance> builder().region("us-east-1") .instances(ImmutableSet.of(instance1, instance2)).build()))); replay(client, instanceClient, converter); PresentSpotRequestsAndInstances fn = new PresentSpotRequestsAndInstances(client, converter); assertEquals(fn.apply(ImmutableSet.of(new RegionAndName("us-east-1", "i-aaaa"), new RegionAndName("us-east-1", "i-bbbb"))), ImmutableSet.of(instance1, instance2)); verify(client, instanceClient, converter); }
@Test public void testWhenSpotsPresentSingleCall() { Function<SpotInstanceRequest, AWSRunningInstance> converter = Functions.forMap(ImmutableMap.of(spot1, instance1, spot2, instance2)); AWSEC2Client client = createMock(AWSEC2Client.class); SpotInstanceClient spotClient = createMock(SpotInstanceClient.class); expect(client.getSpotInstanceServices()).andReturn(spotClient); expect(spotClient.describeSpotInstanceRequestsInRegion("us-east-1", "sir-aaaa", "sir-bbbb")).andReturn( ImmutableSet.of(spot1, spot2)); replay(client, spotClient); PresentSpotRequestsAndInstances fn = new PresentSpotRequestsAndInstances(client, converter); assertEquals(fn.apply(ImmutableSet.of(new RegionAndName("us-east-1", "sir-aaaa"), new RegionAndName("us-east-1", "sir-bbbb"))), ImmutableSet.of(instance1, instance2)); verify(client, spotClient); }