mirror of https://github.com/apache/jclouds.git
Issue 158: Adding Predicates and functions to get capacity and category
This commit is contained in:
parent
64591e6d0e
commit
4fc4cb9a75
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
* 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 java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the capacity of the ProductItem.
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
public class CapacityFromProductItem implements Function<ProductItem,Float> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param productItem the productItem to use
|
||||||
|
* @return the capacity
|
||||||
|
* @throws NoSuchElementException if the capacity is missing
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Float apply(ProductItem productItem) {
|
||||||
|
Float result = productItem.getCapacity();
|
||||||
|
if(result==null) throw new NoSuchElementException();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
* 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 java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the description of the ProductItem.
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
public class DescriptionFromProductItem implements Function<ProductItem,String> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param productItem the productItem to use
|
||||||
|
* @return the description.
|
||||||
|
* @throws java.util.NoSuchElementException if the description is missing
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String apply(ProductItem productItem) {
|
||||||
|
String result = productItem.getDescription();
|
||||||
|
if(result==null) throw new NoSuchElementException();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
/**
|
||||||
|
* 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 com.google.common.base.Predicate;
|
||||||
|
import org.jclouds.softlayer.domain.ProductItem;
|
||||||
|
import org.jclouds.softlayer.domain.ProductItemCategory;
|
||||||
|
|
||||||
|
public class ProductItemPredicates {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if the ProductItem contains the required category.
|
||||||
|
* @param category
|
||||||
|
* @return true if it does, otherwise false.
|
||||||
|
*/
|
||||||
|
public static Predicate<ProductItem> categoryCode(final String category) {
|
||||||
|
return new Predicate<ProductItem>() {
|
||||||
|
@Override
|
||||||
|
public boolean apply(ProductItem productItem) {
|
||||||
|
for(ProductItemCategory productItemCategory: productItem.getCategories()) {
|
||||||
|
if(category.equals(productItemCategory.getCategoryCode())) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "categoryCode("+category+")";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if the ProductItem has the required capacity.
|
||||||
|
* @param capacity
|
||||||
|
* @return true if it does, otherwise false.
|
||||||
|
*/
|
||||||
|
public static Predicate<ProductItem> capacity(final Float capacity) {
|
||||||
|
return new Predicate<ProductItem>() {
|
||||||
|
@Override
|
||||||
|
public boolean apply(ProductItem productItem) {
|
||||||
|
Float productItemCapacity = productItem.getCapacity();
|
||||||
|
if (productItemCapacity == null) return false;
|
||||||
|
return productItemCapacity.equals(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "capacity("+capacity+")";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
/**
|
||||||
|
* 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 org.jclouds.softlayer.domain.ProductItem;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@code DescriptionFromProductItem}
|
||||||
|
*
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public class CapacityFromProductItemTest {
|
||||||
|
|
||||||
|
private CapacityFromProductItem function;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setup() {
|
||||||
|
function = new CapacityFromProductItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCapacity() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).capacity(2.0f)
|
||||||
|
.build();
|
||||||
|
assertEquals(function.apply(item),2.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = NoSuchElementException.class)
|
||||||
|
public void testCapacityMissing() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).build();
|
||||||
|
function.apply(item);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
/**
|
||||||
|
* 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 org.jclouds.softlayer.domain.ProductItem;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@code DescriptionFromProductItem}
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit")
|
||||||
|
public class DescriptionFromProductItemTest {
|
||||||
|
|
||||||
|
private DescriptionFromProductItem function;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setup() {
|
||||||
|
function = new DescriptionFromProductItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDescription() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).description("an item")
|
||||||
|
.build();
|
||||||
|
assertEquals(function.apply(item),"an item");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = NoSuchElementException.class)
|
||||||
|
public void testDescriptionMissing() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).build();
|
||||||
|
function.apply(item);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,95 @@
|
||||||
|
/**
|
||||||
|
* 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 com.google.common.collect.ImmutableSet;
|
||||||
|
import org.jclouds.softlayer.domain.ProductItem;
|
||||||
|
import org.jclouds.softlayer.domain.ProductItemCategory;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertFalse;
|
||||||
|
|
||||||
|
@Test(sequential = true,groups = "unit")
|
||||||
|
public class ProductItemPredicatesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCategoryCodePresent() {
|
||||||
|
ProductItemCategory category = ProductItemCategory.builder()
|
||||||
|
.id(1).categoryCode("ram")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.categories(ImmutableSet.of(category))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert ProductItemPredicates.categoryCode("ram").apply(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCategoryCodePresentTwoCategories() {
|
||||||
|
ProductItemCategory category1 = ProductItemCategory.builder()
|
||||||
|
.id(1).categoryCode("os")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ProductItemCategory category2 = ProductItemCategory.builder()
|
||||||
|
.id(2).categoryCode("ram")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.categories(ImmutableSet.of(category1, category2))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert ProductItemPredicates.categoryCode("ram").apply(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCategoryCodeMissing() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.categories(ImmutableSet.<ProductItemCategory>of())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertFalse(ProductItemPredicates.categoryCode("ram").apply(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCapacity() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).capacity(2.0f)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert ProductItemPredicates.capacity(2.0f).apply(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCapacityDifferent() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).capacity(2.0f)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertFalse(ProductItemPredicates.capacity(1.0f).apply(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCapacityMissing() {
|
||||||
|
ProductItem item = ProductItem.builder()
|
||||||
|
.id(1).build();
|
||||||
|
|
||||||
|
assertFalse(ProductItemPredicates.capacity(2.0f).apply(item));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue