Issue 158: refactored to find package using Iterables

This commit is contained in:
Adrian Cole 2011-09-16 16:28:32 -07:00
parent a8348fcb17
commit e80786a1b2
2 changed files with 8 additions and 19 deletions

View File

@ -18,33 +18,18 @@
*/
package org.jclouds.softlayer.predicates;
import com.google.common.base.Predicate;
import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.features.AccountClient;
import java.util.NoSuchElementException;
import com.google.common.base.Predicate;
public class ProductPackagePredicates {
/**
* Attempts to obtain the packageId for the supplied packageName.
* @param accountClient @see AccountClient
* @param packageName The name field of the @see ProductPackage to find
* @return The id of the package or null if no match found
*/
public static Long getProductPackageId(AccountClient accountClient,String packageName) {
for (ProductPackage productPackage : accountClient.getActivePackages()) {
if (named(packageName).apply(productPackage)) return productPackage.getId();
}
throw new NoSuchElementException("ProductPackage:"+packageName+" not found");
}
/**
* Tests if the product package name equals the packageName
* @param packageName
* @return true if the name is equal, otherwise false.
*/
public static Predicate named(final String packageName) {
public static Predicate<ProductPackage> named(final String packageName) {
return new Predicate<ProductPackage>() {
public boolean apply(ProductPackage productPackage) {
return productPackage.getName().equals(packageName);

View File

@ -19,6 +19,8 @@
package org.jclouds.softlayer.features;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.predicates.ProductPackagePredicates;
import org.testng.annotations.BeforeGroups;
@ -83,8 +85,10 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Set<Datacenter> expected = builder.build();
Long productPackageId = ProductPackagePredicates.getProductPackageId(accountClient, CLOUD_SERVER_PACKAGE_NAME);
assertNotNull(productPackageId);
// note we don't need to check null, as this will throw NoSuchElementException if
// a product package named as below isn't found.
long productPackageId = Iterables.find(accountClient.getActivePackages(),
ProductPackagePredicates.named(CLOUD_SERVER_PACKAGE_NAME)).getId();
ProductPackage productPackage = client.getProductPackage(productPackageId);
Set<Datacenter> datacenters = productPackage.getDatacenters();