functions for regions

This commit is contained in:
Adrian Cole 2012-04-19 23:14:13 -07:00
parent 3672e7c762
commit b87e1397ee
2 changed files with 128 additions and 0 deletions

View File

@ -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 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.Region;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
*
* @author Adrian Cole
*/
@Singleton
public class RegionToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier;
@Inject
public RegionToEndpoint(@Region Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier) {
this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier");
}
@Override
public URI apply(@Nullable Object from) {
checkArgument(from != null && from instanceof String, "you must specify a region, as a String argument");
Map<String, Supplier<URI>> regionToEndpoint = regionToEndpointSupplier.get();
checkState(regionToEndpoint.size() > 0, "no region name to endpoint mappings configured!");
checkArgument(regionToEndpoint.containsKey(from),
"requested location %s, which is not in the configured locations: %s", from, regionToEndpoint);
return regionToEndpoint.get(from).get();
}
}

View File

@ -0,0 +1,68 @@
/**
* 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 java.util.Map;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
/**
* Tests behavior of {@code RegionToEndpoint}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "RegionToEndpointTest")
public class RegionToEndpointTest {
@Test
public void testCorrect() {
RegionToEndpoint fn = new RegionToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1",
Suppliers.ofInstance(URI.create("http://1")))));
assertEquals(fn.apply("1"), URI.create("http://1"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeString() {
RegionToEndpoint fn = new RegionToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1",
Suppliers.ofInstance(URI.create("http://1")))));
fn.apply(new File("foo"));
}
@Test(expectedExceptions = IllegalStateException.class)
public void testMustHaveEndpoints() {
RegionToEndpoint fn = new RegionToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap
.<String, Supplier<URI>> of()));
fn.apply("1");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNullIsIllegal() {
RegionToEndpoint fn = new RegionToEndpoint(Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("1",
Suppliers.ofInstance(URI.create("http://1")))));
fn.apply(null);
}
}