LoadFloatingIpsForInstance extends CacheLoader<ZoneAndId, Iterable<? extends FloatingIP>> { @Override public Iterable<? extends FloatingIP> load(final ZoneAndId key) throws Exception { String zone = key.getZone(); Optional<? extends FloatingIPApi> ipApiOptional = api.getFloatingIPExtensionForZone(zone); if (ipApiOptional.isPresent()) { return ipApiOptional.get().list().filter( new Predicate<FloatingIP>() { @Override public boolean apply(FloatingIP input) { return key.getId().equals(input.getInstanceId()); } }); } return ImmutableSet.of(); } @Inject  LoadFloatingIpsForInstance(NovaApi api); @Override Iterable<? extends FloatingIP> load(final ZoneAndId key);  }
@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.getFloatingIPExtensionForZone("Zone")).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); assertEquals(ImmutableSet.copyOf(parser.load(ZoneAndId.fromZoneAndId("Zone", "i-blah"))), ImmutableSet.of(testIp)); verify(api); verify(ipApi); }
@Test public void testReturnsNullWhenNotFound() throws Exception { NovaApi api = createMock(NovaApi.class); FloatingIPApi ipApi = createMock(FloatingIPApi.class); expect(api.getFloatingIPExtensionForZone("Zone")).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(ZoneAndId.fromZoneAndId("Zone", "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.getFloatingIPExtensionForZone("Zone")).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(ZoneAndId.fromZoneAndId("Zone", "i-blah")).iterator().hasNext()); verify(api); verify(ipApi); }