### Question:
LoadFloatingIpsForInstance extends CacheLoader<RegionAndId, Iterable<? extends FloatingIpForServer>> { @Override public Iterable<? extends FloatingIpForServer> load(final RegionAndId key) throws Exception { String region = key.getRegion(); Optional<? extends FloatingIPApi> ipApiOptional = api.getFloatingIPApi(region); if (ipApiOptional.isPresent()) { return ipApiOptional.get().list().filter( new Predicate<FloatingIP>() { @Override public boolean apply(FloatingIP input) { return key.getId().equals(input.getInstanceId()); } }) .transform(new Function<FloatingIP, FloatingIpForServer>() { @Override public FloatingIpForServer apply(FloatingIP input) { return FloatingIpForServer.create(key, input.getId(), input.getIp()); } }); } return ImmutableSet.of(); } @Inject  LoadFloatingIpsForInstance(NovaApi api); @Override Iterable<? extends FloatingIpForServer> load(final RegionAndId key);  }### Answer:
@Test public void testReturnsPublicIpOnMatch() throws Exception { NovaApi api = createMock(NovaApi.class); FloatingIPApi ipApi = createMock(FloatingIPApi.class); FloatingIP testIp = FloatingIP.builder().id("1").ip("1.1.1.1").fixedIp("10.1.1.1").instanceId("i-blah").build(); expect(api.getFloatingIPApi("RegionOne")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce(); expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of(testIp))) .atLeastOnce(); replay(api); replay(ipApi); LoadFloatingIpsForInstance parser = new LoadFloatingIpsForInstance(api); FloatingIpForServer floatingIpForServer = FloatingIpForServer.create(RegionAndId.fromRegionAndId("RegionOne", "i-blah"), "1", "1.1.1.1"); assertEquals(ImmutableSet.copyOf(parser.load(RegionAndId.fromRegionAndId("RegionOne", "i-blah"))), ImmutableSet.of(floatingIpForServer)); verify(api); verify(ipApi); }
@Test public void testReturnsNullWhenNotFound() throws Exception { NovaApi api = createMock(NovaApi.class); FloatingIPApi ipApi = createMock(FloatingIPApi.class); expect(api.getFloatingIPApi("region")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce(); expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of())) .atLeastOnce(); replay(api); replay(ipApi); LoadFloatingIpsForInstance parser = new LoadFloatingIpsForInstance(api); assertFalse(parser.load(RegionAndId.fromRegionAndId("region", "i-blah")).iterator().hasNext()); verify(api); verify(ipApi); }
@Test public void testReturnsNullWhenNotAssigned() throws Exception { NovaApi api = createMock(NovaApi.class); FloatingIPApi ipApi = createMock(FloatingIPApi.class); expect(api.getFloatingIPApi("region")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce(); expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of(FloatingIP.builder().id("1").ip("1.1.1.1").build()))) .atLeastOnce(); replay(api); replay(ipApi); LoadFloatingIpsForInstance parser = new LoadFloatingIpsForInstance(api); assertFalse(parser.load(RegionAndId.fromRegionAndId("region", "i-blah")).iterator().hasNext()); verify(api); verify(ipApi); }