ZoneToEndpoint implements Function<Object, URI> { @Override public URI apply(@Nullable Object from) { checkArgument(from != null && from instanceof String, "you must specify a zone, as a String argument"); Map<String, Supplier<URI>> zoneToEndpoint = zoneToEndpointSupplier.get(); checkState(zoneToEndpoint.size() > 0, "no zone name to endpoint mappings configured!"); checkArgument(zoneToEndpoint.containsKey(from), "requested location %s, which is not in the configured locations: %s", from, zoneToEndpoint); return zoneToEndpoint.get(from).get(); } @Inject  ZoneToEndpoint(@Zone Supplier<Map<String, Supplier<URI>>> zoneToEndpointSupplier); @Override URI apply(@Nullable Object from);  }
@Test public void testCorrect() { ZoneToEndpoint fn = new ZoneToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1", Suppliers.ofInstance(URI.create("http: assertEquals(fn.apply("1"), URI.create("http: }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeString() { ZoneToEndpoint fn = new ZoneToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1", Suppliers.ofInstance(URI.create("http: fn.apply(new File("foo")); }
@Test(expectedExceptions = IllegalStateException.class) public void testMustHaveEndpoints() { ZoneToEndpoint fn = new ZoneToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap .<String, Supplier<URI>> of())); fn.apply("1"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullIsIllegal() { ZoneToEndpoint fn = new ZoneToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1", Suppliers.ofInstance(URI.create("http: fn.apply(null); }