Issue 158: Extracted getProductPackageId to SoftLayerUtils and added tests

This commit is contained in:
Jason King 2011-09-16 11:37:40 +01:00
parent c52c13e625
commit 0f31b1cfb7
3 changed files with 108 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -27,6 +27,7 @@ import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice; import org.jclouds.softlayer.domain.ProductItemPrice;
import org.jclouds.softlayer.domain.ProductPackage; import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.util.SoftLayerUtils;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -87,7 +88,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Set<Datacenter> expected = builder.build(); Set<Datacenter> expected = builder.build();
Long productPackageId = getProductPackageId(CLOUD_SERVER_PACKAGE_NAME); Long productPackageId = SoftLayerUtils.getProductPackageId(accountClient,CLOUD_SERVER_PACKAGE_NAME);
assertNotNull(productPackageId); assertNotNull(productPackageId);
ProductPackage productPackage = client.getProductPackage(productPackageId); ProductPackage productPackage = client.getProductPackage(productPackageId);
@ -96,12 +97,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
assertTrue(datacenters.containsAll(expected)); 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) { private void checkProductItem(ProductItem item) {
assert item.getId() > 0 : item; assert item.getId() > 0 : item;

View File

@ -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);
}
}