From 5e649d15c63bd8e105c8c68e01081b5d4abf4d73 Mon Sep 17 00:00:00 2001 From: Jason King Date: Fri, 16 Sep 2011 22:45:10 +0100 Subject: [PATCH] Refactored SoftLayerUtils to ProductPackagePredicates --- .../ProductPackagePredicates.java} | 27 ++++++-- .../ProductPackageClientLiveTest.java | 10 ++- .../ProductPackagePredicatesTest.java | 45 +++++++++++++ .../softlayer/util/SoftLayerUtilsTest.java | 67 ------------------- 4 files changed, 71 insertions(+), 78 deletions(-) rename sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/{util/SoftLayerUtils.java => predicates/ProductPackagePredicates.java} (62%) create mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/predicates/ProductPackagePredicatesTest.java delete mode 100644 sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductPackagePredicates.java similarity index 62% rename from sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java rename to sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductPackagePredicates.java index 1b6a9840f9..5f3447d462 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/predicates/ProductPackagePredicates.java @@ -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() { + public boolean apply(ProductPackage productPackage) { + return productPackage.getName().equals(packageName); + } + }; + } + } diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java index d9b0ae5101..671563add0 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/ProductPackageClientLiveTest.java @@ -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 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); diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/predicates/ProductPackagePredicatesTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/predicates/ProductPackagePredicatesTest.java new file mode 100644 index 0000000000..5f72ebbd2d --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/predicates/ProductPackagePredicatesTest.java @@ -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)); + } +} diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java deleted file mode 100644 index 6637282707..0000000000 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java +++ /dev/null @@ -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); - } -}