Refactored SoftLayerUtils to ProductPackagePredicates

This commit is contained in:
Jason King 2011-09-16 22:45:10 +01:00
parent af5cd63dfc
commit 5e649d15c6
4 changed files with 71 additions and 78 deletions

View File

@ -16,23 +16,40 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.softlayer.util;
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;
public class ProductPackagePredicates {
public class SoftLayerUtils {
/**
* 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) {
public static Long getProductPackageId(AccountClient accountClient,String packageName) {
for (ProductPackage productPackage : accountClient.getActivePackages()) {
if (productPackage.getName().equals(packageName)) return productPackage.getId();
if (named(packageName).apply(productPackage)) return productPackage.getId();
}
return null;
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) {
return new Predicate<ProductPackage>() {
public boolean apply(ProductPackage productPackage) {
return productPackage.getName().equals(packageName);
}
};
}
}

View File

@ -18,18 +18,16 @@
*/
package org.jclouds.softlayer.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import com.google.common.collect.ImmutableSet;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.util.SoftLayerUtils;
import org.jclouds.softlayer.predicates.ProductPackagePredicates;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.util.Set;
import static org.testng.Assert.*;
/**
* Tests behavior of {@code ProductPackageClient}
*
@ -85,7 +83,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Set<Datacenter> expected = builder.build();
Long productPackageId = SoftLayerUtils.getProductPackageId(accountClient,CLOUD_SERVER_PACKAGE_NAME);
Long productPackageId = ProductPackagePredicates.getProductPackageId(accountClient, CLOUD_SERVER_PACKAGE_NAME);
assertNotNull(productPackageId);
ProductPackage productPackage = client.getProductPackage(productPackageId);

View File

@ -0,0 +1,45 @@
/**
* 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.softlayer.predicates;
import org.jclouds.softlayer.domain.ProductPackage;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
/**
* Tests {@code ProductPackagePredicates}
*
* @author Jason King
*/
@Test(sequential = true,groups = "unit")
public class ProductPackagePredicatesTest {
@Test
public void testMatches() {
ProductPackage productPackage = ProductPackage.builder().name("foo").build();
assertTrue(ProductPackagePredicates.named("foo").apply(productPackage));
}
@Test
public void testDoesNotMatch() {
ProductPackage productPackage = ProductPackage.builder().name("foo").build();
assertFalse(ProductPackagePredicates.named("bar").apply(productPackage));
}
}

View File

@ -1,67 +0,0 @@
/**
* 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.softlayer.util;
import com.google.common.collect.ImmutableSet;
import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.features.AccountClient;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
/**
* Tests {@code SoftLayerUtils}
*
* @author Jason King
*/
@Test(sequential = true,groups = "unit")
public class SoftLayerUtilsTest {
private AccountClient accountClient;
private ProductPackage productPackage;
@BeforeMethod
public void setup() {
accountClient = createMock(AccountClient.class);
ProductPackage.Builder builder = ProductPackage.builder();
productPackage = builder.name("product package").id(123L).build();
}
@Test
public void testGetProductPackageIdMissing() {
expect(accountClient.getActivePackages()).andReturn(ImmutableSet.of(productPackage));
replay(accountClient);
Long result = SoftLayerUtils.getProductPackageId(accountClient,"missing package");
assertNull(result);
verify(accountClient);
}
@Test
public void testGetProductPackageIdFound() {
expect(accountClient.getActivePackages()).andReturn(ImmutableSet.of(productPackage));
replay(accountClient);
Long result = SoftLayerUtils.getProductPackageId(accountClient,"product package");
assertEquals(result,new Long(123L));
verify(accountClient);
}
}