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/util/SoftLayerUtils.java new file mode 100644 index 0000000000..1b6a9840f9 --- /dev/null +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/util/SoftLayerUtils.java @@ -0,0 +1,38 @@ +/** + * 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 org.jclouds.softlayer.domain.ProductPackage; +import org.jclouds.softlayer.features.AccountClient; + + +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) { + for (ProductPackage productPackage : accountClient.getActivePackages()) { + if (productPackage.getName().equals(packageName)) return productPackage.getId(); + } + return null; + } +} 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 260f8274ee..cbd187a57f 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 @@ -27,6 +27,7 @@ import org.jclouds.softlayer.domain.Datacenter; import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItemPrice; import org.jclouds.softlayer.domain.ProductPackage; +import org.jclouds.softlayer.util.SoftLayerUtils; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -87,7 +88,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest { Set expected = builder.build(); - Long productPackageId = getProductPackageId(CLOUD_SERVER_PACKAGE_NAME); + Long productPackageId = SoftLayerUtils.getProductPackageId(accountClient,CLOUD_SERVER_PACKAGE_NAME); assertNotNull(productPackageId); ProductPackage productPackage = client.getProductPackage(productPackageId); @@ -96,12 +97,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest { assertTrue(datacenters.containsAll(expected)); } - private Long getProductPackageId(String name) { - for (ProductPackage productPackage : accountClient.getActivePackages()) { - if (productPackage.getName().equals(name)) return productPackage.getId(); - } - return null; - } + private void checkProductItem(ProductItem item) { assert item.getId() > 0 : item; 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 new file mode 100644 index 0000000000..6637282707 --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/util/SoftLayerUtilsTest.java @@ -0,0 +1,67 @@ +/** + * 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); + } +}