Issue 158: Change and move prices method to static function, tests

This commit is contained in:
Jason King 2011-09-20 16:57:43 +01:00
parent d5df2d2057
commit 0c6845986d
4 changed files with 58 additions and 54 deletions

View File

@ -1,46 +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.compute.functions;
import com.google.common.base.Function;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* Returns the ProductItemPrice for the ProductItem.
* @author Jason King
*/
public class ProductItemPriceFromProductItem implements Function<ProductItem,ProductItemPrice> {
/**
*
* @param productItem the productItem to use
* @return the productItemPrice
* @throws java.util.NoSuchElementException if not only 1 price
*/
@Override
public ProductItemPrice apply(ProductItem productItem) {
Set<ProductItemPrice> prices = productItem.getPrices();
if(prices.size()!=1) throw new NoSuchElementException();
return prices.iterator().next();
}
}

View File

@ -19,7 +19,11 @@
package org.jclouds.softlayer.compute.functions;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import java.util.NoSuchElementException;
public class ProductItems {
@ -46,4 +50,19 @@ public class ProductItems {
}
};
}
/**
* Creates a function to get the ProductItemPrice for the ProductItem.
* Currently returns the first price.
* This will need to be changed if more than one price is returned.
*/
public static Function<ProductItem,ProductItemPrice> price() {
return new Function<ProductItem,ProductItemPrice>() {
@Override
public ProductItemPrice apply(ProductItem productItem) {
if(productItem.getPrices().size()<1) throw new NoSuchElementException("ProductItem has no prices:"+productItem);
return Iterables.get(productItem.getPrices(), 0);
}
};
}
}

View File

@ -18,12 +18,17 @@
*/
package org.jclouds.softlayer.compute.functions;
import com.google.common.collect.ImmutableSet;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.NoSuchElementException;
import static org.jclouds.softlayer.compute.functions.ProductItems.capacity;
import static org.jclouds.softlayer.compute.functions.ProductItems.description;
import static org.jclouds.softlayer.compute.functions.ProductItems.price;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
@ -35,11 +40,19 @@ import static org.testng.Assert.assertNull;
@Test(groups = "unit")
public class ProductItemsTest {
private ProductItemPrice price;
private ProductItem item;
@BeforeMethod
public void setup() {
item = ProductItem.builder().id(1).capacity(2.0f).description("an item").build();
price = ProductItemPrice.builder().id(1).build();
item = ProductItem.builder().id(1)
.capacity(2.0f)
.description("an item")
.price(price)
.build();
}
@Test
public void testCapacity() {
@ -57,8 +70,27 @@ public class ProductItemsTest {
assertEquals(description().apply(item),"an item");
}
@Test
public void testDescriptionMissing() {
ProductItem item = ProductItem.builder().id(1).build();
assertNull(description().apply(item));
}
@Test
public void testPrice() {
assertEquals(price().apply(item),price);
}
@Test
public void testPriceMultiplePrices() {
ImmutableSet<ProductItemPrice> prices = ImmutableSet.of(price, ProductItemPrice.builder().id(2).build());
ProductItem item2 = ProductItem.builder().prices(prices).build();
assertEquals(price().apply(item2),price);
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testPriceMissing() {
ProductItem noPriceItem = ProductItem.builder().id(1).build();
price().apply(noPriceItem);
}
}

View File

@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jclouds.softlayer.compute.functions.ProductItemPriceFromProductItem;
import org.jclouds.softlayer.compute.functions.ProductItems;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.reference.SoftLayerConstants;
@ -119,7 +118,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
// capacity is key in GB (1Gb = 1.0f)
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
ProductItemPrice price = ProductItems.price().apply(ramToProductItem.get(1.0f));
assert new Long(1644L).equals(price.getId());
}
@ -131,7 +130,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
// number of cores is the key
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
ProductItemPrice price = ProductItems.price().apply(coresToProductItem.get(2.0f));
assert new Long(1963L).equals(price.getId());
}
@ -141,7 +140,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
ProductItemPrice price = ProductItems.price().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
assert new Long(1693L).equals(price.getId());
}
@ -152,16 +151,16 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice ramPrice = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
ProductItemPrice ramPrice = ProductItems.price().apply(ramToProductItem.get(1.0f));
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice cpuPrice = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
ProductItemPrice cpuPrice = ProductItems.price().apply(coresToProductItem.get(2.0f));
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice osPrice = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
ProductItemPrice osPrice = ProductItems.price().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
Set<Long> prices = Sets.<Long>newLinkedHashSet();
prices.addAll(SoftLayerConstants.DEFAULT_VIRTUAL_GUEST_PRICES);