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 e001d5a863..705fc13b72 100644 --- a/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java +++ b/core/src/main/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNull.java @@ -20,23 +20,22 @@ 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 org.jclouds.javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; +import org.jclouds.javax.annotation.Nullable; import org.jclouds.location.Provider; import org.jclouds.location.Region; import com.google.common.base.Function; /** - * If a mapping of regions to endpoints exists, return a uri corresponding to the name of the region - * (passed argument). Otherwise, return the default location. + * Return a uri corresponding to the name of the region (passed argument). + * Otherwise, return the default location. * * @author Adrian Cole */ @@ -47,20 +46,20 @@ public class RegionToEndpointOrProviderIfNull implements Function { private final Map regionToEndpoint; @Inject - public RegionToEndpointOrProviderIfNull(@Provider URI defaultUri, @Provider String defaultProvider, - @Nullable @Region Map regionToEndpoint) { - this.defaultUri = checkNotNull(defaultUri, "defaultUri"); + public RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider URI defaultUri, + @Region Map regionToEndpoint) { this.defaultProvider = checkNotNull(defaultProvider, "defaultProvider"); - this.regionToEndpoint = regionToEndpoint; + this.defaultUri = checkNotNull(defaultUri, "defaultUri"); + this.regionToEndpoint = checkNotNull(regionToEndpoint, "regionToEndpoint"); + checkArgument(regionToEndpoint.size() > 0, "no region name to endpoint mappings configured!"); } @Override public URI apply(@Nullable Object from) { if (from == null || from.equals(defaultProvider)) return defaultUri; - checkState(from.equals(defaultProvider) || regionToEndpoint != null, "requested location " + from - + ", but only the default location " + defaultProvider + " is configured"); - checkArgument(from.equals(defaultProvider) || (regionToEndpoint != null && regionToEndpoint.containsKey(from)), + checkArgument(from instanceof String, "region is a String argument"); + checkArgument(regionToEndpoint.containsKey(from), "requested location %s, which is not in the configured locations: %s", from, regionToEndpoint); return regionToEndpoint.get(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 a650508719..51ae51cbf3 100644 --- a/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java +++ b/core/src/main/java/org/jclouds/location/functions/ZoneToEndpoint.java @@ -19,14 +19,15 @@ 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 org.jclouds.javax.annotation.Nullable; 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; @@ -42,12 +43,13 @@ public class ZoneToEndpoint implements Function { @Inject public ZoneToEndpoint(@Zone Map zoneToEndpoint) { - this.zoneToEndpoint = zoneToEndpoint; + this.zoneToEndpoint = checkNotNull(zoneToEndpoint, "zoneToEndpoint"); + checkArgument(zoneToEndpoint.size() > 0, "no zone name to endpoint mappings configured!"); } @Override public URI apply(@Nullable Object from) { - checkArgument(from != null, "you must specify a zone"); + checkArgument(from != null && from instanceof String, "you must specify a zone, as a String argument"); return zoneToEndpoint.get(from); } } \ No newline at end of file diff --git a/core/src/test/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNullTest.java b/core/src/test/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNullTest.java new file mode 100644 index 0000000000..35102b5069 --- /dev/null +++ b/core/src/test/java/org/jclouds/location/functions/RegionToEndpointOrProviderIfNullTest.java @@ -0,0 +1,70 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.location.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.File; +import java.net.URI; + +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * Tests behavior of {@code RegionToEndpointOrProviderIfNull} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "RegionToEndpointOrProviderIfNullTest") +public class RegionToEndpointOrProviderIfNullTest { + + @Test + public void testWhenFindsRegion() throws SecurityException, NoSuchMethodException { + RegionToEndpointOrProviderIfNull fn = new RegionToEndpointOrProviderIfNull("leader", URI.create("http://leader"), + ImmutableMap.of("1", URI.create("http://1"))); + assertEquals(fn.apply("1"), URI.create("http://1")); + } + + public void testNullReturnsProvider() { + RegionToEndpointOrProviderIfNull fn = new RegionToEndpointOrProviderIfNull("leader", URI.create("http://leader"), + ImmutableMap.of("1", URI.create("http://1"))); + assertEquals(fn.apply("leader"), URI.create("http://leader")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMustBeString() { + RegionToEndpointOrProviderIfNull fn = new RegionToEndpointOrProviderIfNull("leader", URI.create("http://leader"), + ImmutableMap.of("1", URI.create("http://1"))); + fn.apply(new File("foo")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMustBeInRegionMapIfSpecified() { + RegionToEndpointOrProviderIfNull fn = new RegionToEndpointOrProviderIfNull("leader", URI.create("http://leader"), + ImmutableMap.of("1", URI.create("http://1"))); + fn.apply("2"); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMustHaveEndpoints() { + new RegionToEndpointOrProviderIfNull("leader", URI.create("http://leader"), ImmutableMap. of()); + } + +} diff --git a/core/src/test/java/org/jclouds/location/functions/ZoneToEndpointTest.java b/core/src/test/java/org/jclouds/location/functions/ZoneToEndpointTest.java new file mode 100644 index 0000000000..a90d258db4 --- /dev/null +++ b/core/src/test/java/org/jclouds/location/functions/ZoneToEndpointTest.java @@ -0,0 +1,60 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.location.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.File; +import java.net.URI; + +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * Tests behavior of {@code ZoneToEndpoint} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ZoneToEndpointTest") +public class ZoneToEndpointTest { + + @Test + public void testCorrect() throws SecurityException, NoSuchMethodException { + ZoneToEndpoint fn = new ZoneToEndpoint(ImmutableMap.of("1", URI.create("http://1"))); + assertEquals(fn.apply("1"), URI.create("http://1")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMustBeString() { + ZoneToEndpoint fn = new ZoneToEndpoint(ImmutableMap.of("1", URI.create("http://1"))); + fn.apply(new File("foo")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMustHaveEndpoints() { + new ZoneToEndpoint(ImmutableMap. of()); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testNullIsIllegal() { + ZoneToEndpoint fn = new ZoneToEndpoint(ImmutableMap.of("1", URI.create("http://1"))); + fn.apply(null); + } +}