Issue 550: Added support to filter location-based filters further by type.

* core/src/main/java/org/jclouds/providers/Providers.java
  (boundedByIso3166Code): Added another implementation that takes a type to
   further filter the results.
  (collocatedWith): Added another implementation that takes a type to
   further filter the results.

* core/src/test/java/org/jclouds/providers/ProvidersTest.java
  (testBoundedByIso3166Code, testCollocatedWith): Updated to test the versions
   that now take a type as an argument.
This commit is contained in:
Jeremy Whitlock 2011-05-26 10:56:43 -06:00
parent 86817b7612
commit d4ca5c1960
2 changed files with 66 additions and 45 deletions

View File

@ -21,6 +21,8 @@ package org.jclouds.providers;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import com.google.common.base.Predicates;
import java.util.NoSuchElementException;
import java.util.ServiceLoader;
@ -127,20 +129,34 @@ public class Providers {
}
/**
* Returns the providers that are bound to the same location as the given ISO 3166 code.
* Returns the providers that are bound to the same location as the given ISO 3166 code regardless of type.
*
* @param isoCode
* the ISO 3166 code to filter providers by
*
* @return the providers with the given ISO 3166 code
* @return the providers bound by the given ISO 3166 code
*/
public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code) {
return filter(all(), ProviderPredicates.boundedByIso3166Code(iso3166Code));
}
/**
* Returns the providers that have at least one common ISO 3166 code in common
* regardless of type.
* Returns the providers that are bound to the same location as the given ISO 3166 code and of the given type.
*
* @param iso3166Code
* the ISO 3166 code to filter providers by
* @param type
* the type to filter providers by
*
* @return the providers bound by the given ISO 3166 code and of the proper type
*/
public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code, String type) {
return filter(all(), Predicates.and(ProviderPredicates.boundedByIso3166Code(iso3166Code),
ProviderPredicates.type(type)));
}
/**
* Returns the providers that have at least one common ISO 3166 code in common regardless of type.
*
* @param providerMetadata
* the provider metadata to use to filter providers by
@ -150,4 +166,19 @@ public class Providers {
public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata) {
return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata));
}
/**
* Returns the providers that have at least one common ISO 3166 code and are of the given type.
*
* @param providerMetadata
* the provider metadata to use to filter providers by
* @param type
* the type to filter providers by
*
* @return the providers that share at least one common ISO 3166 code and of the given type
*/
public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata, String type) {
return filter(all(), Predicates.and(ProviderPredicates.intersectingIso3166Code(providerMetadata),
ProviderPredicates.type(type)));
}
}

View File

@ -21,8 +21,8 @@ package org.jclouds.providers;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Iterables;
import java.util.NoSuchElementException;
import org.testng.annotations.Test;
@ -96,50 +96,40 @@ public class ProvidersTest {
@Test
public void testBoundedByIso3166Code() {
@SuppressWarnings("serial")
Map<String, Integer> expectedResults = new HashMap<String, Integer>() {{
put("US-CA", 2);
put("US-FL", 1);
put("US", 2);
put("JP-13", 1);
put("JP", 1);
put("SOME-FAKE-CODE", 0);
}};
// Test filtering by ISO 3166 code alone
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA")), 2);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL")), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US")), 2);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("JP-13")), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("JP")), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE")), 0);
for (Map.Entry<String, Integer> result : expectedResults.entrySet()) {
Iterable<ProviderMetadata> providersMetadata = Providers.boundedByIso3166Code(result.getKey());
int providersFound = 0;
for (ProviderMetadata providerMetadata : providersMetadata) {
if (providerMetadata != null) {
providersFound++;
}
}
assertEquals(providersFound, result.getValue().intValue());
}
// Test filtering by ISO 3166 code and type
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA", ProviderMetadata.BLOBSTORE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA", ProviderMetadata.COMPUTE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL", ProviderMetadata.BLOBSTORE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL", ProviderMetadata.COMPUTE_TYPE)), 0);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US", ProviderMetadata.BLOBSTORE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US", ProviderMetadata.COMPUTE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE", ProviderMetadata.BLOBSTORE_TYPE)), 0);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE", ProviderMetadata.COMPUTE_TYPE)), 0);
}
@Test
public void testCollocatedWith() {
@SuppressWarnings("serial")
Map<ProviderMetadata, Integer> expectedResults = new HashMap<ProviderMetadata, Integer>() {{
put(testBlobstoreProvider, 1);
put(testComputeProvider, 1);
put(testYetAnotherComputeProvider, 0);
}};
// Test filtering by collocation alone
assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider)), 1);
assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider)), 1);
assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider)), 0);
for (Map.Entry<ProviderMetadata, Integer> result : expectedResults.entrySet()) {
Iterable<ProviderMetadata> providersMetadata = Providers.collocatedWith(result.getKey());
int providersFound = 0;
for (ProviderMetadata providerMetadata : providersMetadata) {
if (providerMetadata != null) {
providersFound++;
}
}
assertEquals(providersFound, result.getValue().intValue());
}
// Test filtering by collocation and type
assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider, ProviderMetadata.BLOBSTORE_TYPE)), 0);
assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider, ProviderMetadata.COMPUTE_TYPE)), 1);
assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider, ProviderMetadata.COMPUTE_TYPE)), 0);
assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider, ProviderMetadata.BLOBSTORE_TYPE)), 1);
assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider,
ProviderMetadata.COMPUTE_TYPE)), 0);
assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider,
ProviderMetadata.BLOBSTORE_TYPE)), 0);
}
}