### Question:
PresentSpotRequestsAndInstances extends PresentInstances { @Override public Set<RunningInstance> apply(Set<RegionAndName> regionAndIds) { if (checkNotNull(regionAndIds, "regionAndIds").isEmpty()) return ImmutableSet.of(); if (any(regionAndIds, Predicates.compose(containsPattern("sir-"), nameFunction()))) return getSpots(regionAndIds); return super.apply(regionAndIds); } @Inject  PresentSpotRequestsAndInstances(AWSEC2Api client, Function<SpotInstanceRequest, AWSRunningInstance> spotConverter); @Override Set<RunningInstance> apply(Set<RegionAndName> regionAndIds); @Override String toString();  }### Answer:
@SuppressWarnings("unchecked") @Test public void testWhenInstancesPresentSingleCall() { AWSEC2Api client = createMock(AWSEC2Api.class); AWSInstanceApi instanceApi = createMock(AWSInstanceApi.class); Function<SpotInstanceRequest, AWSRunningInstance> converter = createMock(Function.class); expect(client.getInstanceApi()).andReturn((Optional) Optional.of(instanceApi)); expect(instanceApi.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, instanceApi, 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, instanceApi, converter); }
@Test public void testWhenSpotsPresentSingleCall() { Function<SpotInstanceRequest, AWSRunningInstance> converter = Functions.forMap(ImmutableMap.of(spot1, instance1, spot2, instance2)); AWSEC2Api client = createMock(AWSEC2Api.class); SpotInstanceApi spotApi = createMock(SpotInstanceApi.class); expect(client.getSpotInstanceApi()).andReturn((Optional) Optional.of(spotApi)); expect(spotApi.describeSpotInstanceRequestsInRegion("us-east-1", "sir-aaaa", "sir-bbbb")).andReturn( ImmutableSet.of(spot1, spot2)); replay(client, spotApi); 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, spotApi); }