diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/RegionToAdminEndpointURI.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/RegionToAdminEndpointURI.java index e69fd19cdc..c6c1a34f14 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/RegionToAdminEndpointURI.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/RegionToAdminEndpointURI.java @@ -16,14 +16,34 @@ */ package org.jclouds.openstack.keystone.v2_0.functions; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + +import java.net.URI; +import java.util.Map; + import javax.inject.Inject; -import org.jclouds.location.functions.RegionToEndpoint; import org.jclouds.openstack.keystone.v2_0.suppliers.RegionIdToAdminURISupplier; -public class RegionToAdminEndpointURI extends RegionToEndpoint { +import com.google.common.base.Function; +import com.google.common.base.Supplier; + +public final class RegionToAdminEndpointURI implements Function { + + private final RegionIdToAdminURISupplier regionToAdminEndpoints; + @Inject - public RegionToAdminEndpointURI(RegionIdToAdminURISupplier regionToEndpointSupplier) { - super(regionToEndpointSupplier); + RegionToAdminEndpointURI(RegionIdToAdminURISupplier regionToAdminEndpoints) { + this.regionToAdminEndpoints = regionToAdminEndpoints; + } + + @Override + public URI apply(Object from) { + Map> regionToAdminEndpoint = regionToAdminEndpoints.get(); + checkState(!regionToAdminEndpoint.isEmpty(), "no region name to admin endpoint mappings in keystone!"); + checkArgument(regionToAdminEndpoint.containsKey(from), + "requested location %s, which is not in the keystone admin endpoints: %s", from, regionToAdminEndpoint); + return regionToAdminEndpoint.get(from).get(); } } diff --git a/apis/s3/src/main/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucket.java b/apis/s3/src/main/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucket.java index 0aadaebe1d..3a203b9be4 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucket.java +++ b/apis/s3/src/main/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucket.java @@ -18,41 +18,33 @@ package org.jclouds.s3.functions; import java.net.URI; -import javax.annotation.Resource; import javax.inject.Inject; -import javax.inject.Singleton; -import org.jclouds.javax.annotation.Nullable; import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; -import org.jclouds.logging.Logger; import org.jclouds.s3.Bucket; import com.google.common.base.Function; import com.google.common.base.Optional; -@Singleton -public class AssignCorrectHostnameForBucket implements Function { - @Resource - protected Logger logger = Logger.NULL; +public final class AssignCorrectHostnameForBucket implements Function { - protected final RegionToEndpointOrProviderIfNull r2; - protected final Function> bucketToRegion; + private final RegionToEndpointOrProviderIfNull delegate; + private final Function> bucketToRegion; @Inject - public AssignCorrectHostnameForBucket(RegionToEndpointOrProviderIfNull r2, + AssignCorrectHostnameForBucket(RegionToEndpointOrProviderIfNull delegate, @Bucket Function> bucketToRegion) { this.bucketToRegion = bucketToRegion; - this.r2 = r2; + this.delegate = delegate; } @Override - public URI apply(@Nullable Object from) { + public URI apply(Object from) { String bucket = from.toString(); Optional region = bucketToRegion.apply(bucket); if (region.isPresent()) { - return r2.apply(region.get()); + return delegate.apply(region.get()); } - return r2.apply(null); + return delegate.apply(null); } - } diff --git a/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java b/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java index 07f70def6f..8b087c6dba 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java +++ b/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java @@ -19,7 +19,6 @@ package org.jclouds.s3.functions; import java.net.URI; import javax.inject.Inject; -import javax.inject.Singleton; import org.jclouds.javax.annotation.Nullable; import org.jclouds.s3.Bucket; @@ -28,23 +27,22 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.cache.LoadingCache; -@Singleton -public class DefaultEndpointThenInvalidateRegion implements Function { +public final class DefaultEndpointThenInvalidateRegion implements Function { + private final Function delegate; private final LoadingCache> bucketToRegionCache; - private final AssignCorrectHostnameForBucket r2; @Inject - public DefaultEndpointThenInvalidateRegion(AssignCorrectHostnameForBucket r2, - @Bucket LoadingCache> bucketToRegionCache) { - this.r2 = r2; + DefaultEndpointThenInvalidateRegion(AssignCorrectHostnameForBucket delegate, + @Bucket LoadingCache> bucketToRegionCache) { + this.delegate = delegate; this.bucketToRegionCache = bucketToRegionCache; } @Override public URI apply(@Nullable Object from) { try { - return r2.apply(from); + return delegate.apply(from); } finally { bucketToRegionCache.invalidate(from.toString()); } diff --git a/apis/s3/src/test/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucketTest.java b/apis/s3/src/test/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucketTest.java index 5b9f7a5786..476f536163 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucketTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/functions/AssignCorrectHostnameForBucketTest.java @@ -21,6 +21,8 @@ import static org.testng.Assert.assertEquals; import java.net.URI; import java.util.Map; +import org.jclouds.location.Provider; +import org.jclouds.location.Region; import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; import org.testng.annotations.Test; @@ -29,42 +31,41 @@ import com.google.common.base.Optional; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Module; +import com.google.inject.Provides; -/** - * Tests behavior of {@code AssignCorrectHostnameForBucket} - */ -// NOTE:without testName, this will not call @Before* and fail w/NPE during -// surefire -@Test(groups = "unit", testName = "AssignCorrectHostnameForBucketTest") +@Test public class AssignCorrectHostnameForBucketTest { + static final RegionToEndpointOrProviderIfNull REGION_TO_ENDPOINT = Guice.createInjector(new Module() { + @Override public void configure(Binder binder) { + binder.bindConstant().annotatedWith(Provider.class).to("s3"); + } + + @Provides @Provider Supplier defaultUri() { + return Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")); + } + + @Provides @Region Supplier>> regionToEndpoints() { + Map> regionToEndpoint = ImmutableMap.of( // + "us-standard", defaultUri(), // + "us-west-1", Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com"))); + return Suppliers.ofInstance(regionToEndpoint); + } + }).getInstance(RegionToEndpointOrProviderIfNull.class); public void testWhenNoBucketRegionMappingInCache() { - - AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(new RegionToEndpointOrProviderIfNull( - "aws-s3", Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), - - Suppliers.>> ofInstance(ImmutableMap.of("us-standard", - Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), "us-west-1", - Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com"))))), - - Functions.forMap(ImmutableMap.> of("bucket", Optional. absent()))); + AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT, + Functions.forMap(ImmutableMap.of("bucket", Optional.absent()))); assertEquals(fn.apply("bucket"), URI.create("https://s3.amazonaws.com")); - } public void testWhenBucketRegionMappingInCache() { - - AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(new RegionToEndpointOrProviderIfNull( - "aws-s3", Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), - - Suppliers.>> ofInstance(ImmutableMap.of("us-standard", - Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), "us-west-1", - Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com"))))), - - Functions.forMap(ImmutableMap.> of("bucket", Optional.of("us-west-1")))); + AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT, + Functions.forMap(ImmutableMap.of("bucket", Optional.of("us-west-1")))); assertEquals(fn.apply("bucket"), URI.create("https://s3-us-west-1.amazonaws.com")); - } } diff --git a/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java b/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java index b601a8f157..485dd0e28c 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java @@ -17,34 +17,30 @@ package org.jclouds.s3.functions; import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; - -import java.net.URI; +import static org.jclouds.s3.functions.AssignCorrectHostnameForBucketTest.REGION_TO_ENDPOINT; import org.testng.annotations.Test; +import com.google.common.base.Functions; import com.google.common.base.Optional; import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; -@Test(testName = "DefaultEndpointThenInvalidateRegionTest") public class DefaultEndpointThenInvalidateRegionTest { - @SuppressWarnings("unchecked") @Test - void testInvalidate() throws Exception { - AssignCorrectHostnameForBucket r2 = createMock(AssignCorrectHostnameForBucket.class); + public void testInvalidate() throws Exception { LoadingCache> bucketToRegionCache = createMock(LoadingCache.class); - expect(r2.apply("mybucket")).andReturn(URI.create("http://east-url")); bucketToRegionCache.invalidate("mybucket"); + replay(bucketToRegionCache); - replay(r2, bucketToRegionCache); - - new DefaultEndpointThenInvalidateRegion(r2, bucketToRegionCache).apply("mybucket"); - verify(r2, bucketToRegionCache); + AssignCorrectHostnameForBucket delegate = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT, + Functions.forMap(ImmutableMap.of("mybucket", Optional.of("us-west-1")))); + new DefaultEndpointThenInvalidateRegion(delegate, bucketToRegionCache).apply("mybucket"); + verify(bucketToRegionCache); } - } diff --git a/core/src/main/java/org/jclouds/location/functions/RegionToEndpoint.java b/core/src/main/java/org/jclouds/location/functions/RegionToEndpoint.java index 01d4fcb82b..4180f324e2 100644 --- a/core/src/main/java/org/jclouds/location/functions/RegionToEndpoint.java +++ b/core/src/main/java/org/jclouds/location/functions/RegionToEndpoint.java @@ -17,36 +17,33 @@ package org.jclouds.location.functions; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import java.net.URI; import java.util.Map; import javax.inject.Inject; -import javax.inject.Singleton; import org.jclouds.location.Region; import com.google.common.base.Function; import com.google.common.base.Supplier; -@Singleton -public class RegionToEndpoint implements Function { +public final class RegionToEndpoint implements Function { - private final Supplier>> regionToEndpointSupplier; + private final Supplier>> regionToEndpoints; @Inject - public RegionToEndpoint(@Region Supplier>> regionToEndpointSupplier) { - this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier"); + RegionToEndpoint(@Region Supplier>> regionToEndpoints) { + this.regionToEndpoints = regionToEndpoints; } @Override public URI apply(Object from) { - Map> regionToEndpoint = regionToEndpointSupplier.get(); - checkState(regionToEndpoint.size() > 0, "no region name to endpoint mappings configured!"); + Map> regionToEndpoint = regionToEndpoints.get(); + checkState(!regionToEndpoint.isEmpty(), "no region name to endpoint mappings configured!"); checkArgument(regionToEndpoint.containsKey(from), - "requested location %s, which is not in the configured locations: %s", from, regionToEndpoint); + "requested location %s, which is not a configured region: %s", from, regionToEndpoint); return regionToEndpoint.get(from).get(); } } diff --git a/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java b/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java index f77f1a30e9..a59c3cebec 100644 --- a/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java +++ b/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java @@ -17,13 +17,11 @@ package org.jclouds.location.functions; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; import java.util.Map; import javax.inject.Inject; -import javax.inject.Singleton; import org.jclouds.javax.annotation.Nullable; import org.jclouds.location.Provider; @@ -36,25 +34,23 @@ import com.google.common.base.Supplier; * Return a uri corresponding to the name of the region (passed argument). * Otherwise, return the default location. */ -@Singleton -public class RegionToEndpointOrProviderIfNull implements Function { +public final class RegionToEndpointOrProviderIfNull implements Function { private final Supplier defaultUri; private final String defaultProvider; private final Supplier>> regionToEndpointSupplier; @Inject - public RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider Supplier defaultUri, + RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider Supplier defaultUri, @Region Supplier>> regionToEndpointSupplier) { - this.defaultProvider = checkNotNull(defaultProvider, "defaultProvider"); - this.defaultUri = checkNotNull(defaultUri, "defaultUri"); - this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier"); + this.defaultProvider = defaultProvider; + this.defaultUri = defaultUri; + this.regionToEndpointSupplier = regionToEndpointSupplier; } @Override public URI apply(@Nullable Object from) { if (from == null) return defaultUri.get(); - checkArgument(from instanceof String, "region is a String argument"); Map> regionToEndpoint = regionToEndpointSupplier.get(); if (from.equals(defaultProvider)) { if (regionToEndpoint.containsKey(from)) diff --git a/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java b/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java index a308dbef64..cb844dc2b1 100644 --- a/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java +++ b/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java @@ -17,38 +17,33 @@ package org.jclouds.location.functions; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import java.net.URI; import java.util.Map; import javax.inject.Inject; -import javax.inject.Singleton; -import org.jclouds.javax.annotation.Nullable; import org.jclouds.location.Zone; import com.google.common.base.Function; import com.google.common.base.Supplier; -@Singleton -public class ZoneToEndpoint implements Function { +public final class ZoneToEndpoint implements Function { - private final Supplier>> zoneToEndpointSupplier; + private final Supplier>> zoneToEndpoints; @Inject - public ZoneToEndpoint(@Zone Supplier>> zoneToEndpointSupplier) { - this.zoneToEndpointSupplier = checkNotNull(zoneToEndpointSupplier, "zoneToEndpointSupplier"); + ZoneToEndpoint(@Zone Supplier>> zoneToEndpoints) { + this.zoneToEndpoints = zoneToEndpoints; } @Override - public URI apply(@Nullable Object from) { - checkArgument(from != null && from instanceof String, "you must specify a zone, as a String argument"); - Map> zoneToEndpoint = zoneToEndpointSupplier.get(); - checkState(zoneToEndpoint.size() > 0, "no zone name to endpoint mappings configured!"); + public URI apply(Object from) { + Map> zoneToEndpoint = zoneToEndpoints.get(); + checkState(!zoneToEndpoint.isEmpty(), "no zone name to endpoint mappings configured!"); checkArgument(zoneToEndpoint.containsKey(from), - "requested location %s, which is not in the configured locations: %s", from, zoneToEndpoint); + "requested location %s, which is not a configured zone: %s", from, zoneToEndpoint); return zoneToEndpoint.get(from).get(); } }