mirror of https://github.com/apache/jclouds.git
backfilled tests for org.jclouds.location.functions
This commit is contained in:
parent
3bd0be2c24
commit
cf4acc8000
|
@ -20,23 +20,22 @@ package org.jclouds.location.functions;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jclouds.javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.javax.annotation.Nullable;
|
||||||
import org.jclouds.location.Provider;
|
import org.jclouds.location.Provider;
|
||||||
import org.jclouds.location.Region;
|
import org.jclouds.location.Region;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If a mapping of regions to endpoints exists, return a uri corresponding to the name of the region
|
* Return a uri corresponding to the name of the region (passed argument).
|
||||||
* (passed argument). Otherwise, return the default location.
|
* Otherwise, return the default location.
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
|
@ -47,20 +46,20 @@ public class RegionToEndpointOrProviderIfNull implements Function<Object, URI> {
|
||||||
private final Map<String, URI> regionToEndpoint;
|
private final Map<String, URI> regionToEndpoint;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RegionToEndpointOrProviderIfNull(@Provider URI defaultUri, @Provider String defaultProvider,
|
public RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider URI defaultUri,
|
||||||
@Nullable @Region Map<String, URI> regionToEndpoint) {
|
@Region Map<String, URI> regionToEndpoint) {
|
||||||
this.defaultUri = checkNotNull(defaultUri, "defaultUri");
|
|
||||||
this.defaultProvider = checkNotNull(defaultProvider, "defaultProvider");
|
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
|
@Override
|
||||||
public URI apply(@Nullable Object from) {
|
public URI apply(@Nullable Object from) {
|
||||||
if (from == null || from.equals(defaultProvider))
|
if (from == null || from.equals(defaultProvider))
|
||||||
return defaultUri;
|
return defaultUri;
|
||||||
checkState(from.equals(defaultProvider) || regionToEndpoint != null, "requested location " + from
|
checkArgument(from instanceof String, "region is a String argument");
|
||||||
+ ", but only the default location " + defaultProvider + " is configured");
|
checkArgument(regionToEndpoint.containsKey(from),
|
||||||
checkArgument(from.equals(defaultProvider) || (regionToEndpoint != null && regionToEndpoint.containsKey(from)),
|
|
||||||
"requested location %s, which is not in the configured locations: %s", from, regionToEndpoint);
|
"requested location %s, which is not in the configured locations: %s", from, regionToEndpoint);
|
||||||
return regionToEndpoint.get(from);
|
return regionToEndpoint.get(from);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,15 @@
|
||||||
package org.jclouds.location.functions;
|
package org.jclouds.location.functions;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jclouds.javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.javax.annotation.Nullable;
|
||||||
import org.jclouds.location.Zone;
|
import org.jclouds.location.Zone;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
@ -42,12 +43,13 @@ public class ZoneToEndpoint implements Function<Object, URI> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ZoneToEndpoint(@Zone Map<String, URI> zoneToEndpoint) {
|
public ZoneToEndpoint(@Zone Map<String, URI> zoneToEndpoint) {
|
||||||
this.zoneToEndpoint = zoneToEndpoint;
|
this.zoneToEndpoint = checkNotNull(zoneToEndpoint, "zoneToEndpoint");
|
||||||
|
checkArgument(zoneToEndpoint.size() > 0, "no zone name to endpoint mappings configured!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public URI apply(@Nullable Object from) {
|
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);
|
return zoneToEndpoint.get(from);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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.<String, URI> of());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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.<String, URI> of());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||||
|
public void testNullIsIllegal() {
|
||||||
|
ZoneToEndpoint fn = new ZoneToEndpoint(ImmutableMap.of("1", URI.create("http://1")));
|
||||||
|
fn.apply(null);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue