From cb0a105171f4348cd68a5f6dc5786175aed85f74 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 7 Mar 2012 15:13:54 -0800 Subject: [PATCH] added predicates handy for finding vcloud references --- .../predicates/ReferenceTypePredicates.java | 85 +++++++++++++++++++ .../ReferenceTypePredicatesTest.java | 56 ++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicates.java create mode 100644 labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicatesTest.java diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicates.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicates.java new file mode 100644 index 0000000000..f616df5f93 --- /dev/null +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicates.java @@ -0,0 +1,85 @@ +/* + * 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.vcloud.director.v1_5.predicates; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.jclouds.vcloud.director.v1_5.domain.ReferenceType; + +import com.google.common.base.Predicate; + +/** + * Predicates handy when working with Reference Types + * + * @author Adrian Cole + */ + +public class ReferenceTypePredicates { + + /** + * matches references of the given name + * + * @param + * type of the ReferenceType, ex. {@link Link} + * @param name + * ex. {@code context.getApi().getCurrentSession().getOrg()} + * @return predicate that will match references of the given name + */ + public static > Predicate nameEquals(final String name) { + checkNotNull(name, "name must be defined"); + + return new Predicate() { + @Override + public boolean apply(T reference) { + return name.equals(reference.getName()); + } + + @Override + public String toString() { + return "nameEquals(" + name + ")"; + } + }; + } + + /** + * matches references of the given type + * + * @param + * type of the ReferenceType, ex. {@link Link} + * @param type + * ex. {@link VCloudDirectorMediaType#CATALOG} + * @return predicate that will match references of the given type + * @see VCloudDirectorMediaType + */ + public static > Predicate typeEquals(final String type) { + checkNotNull(type, "type must be defined"); + + return new Predicate() { + @Override + public boolean apply(T reference) { + return type.equals(reference.getType()); + } + + @Override + public String toString() { + return "typeEquals(" + type + ")"; + } + }; + } +} diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicatesTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicatesTest.java new file mode 100644 index 0000000000..ac611153d6 --- /dev/null +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicatesTest.java @@ -0,0 +1,56 @@ +/** + * 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.vcloud.director.v1_5.predicates; + +import java.net.URI; + +import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; +import org.jclouds.vcloud.director.v1_5.domain.Reference; +import org.jclouds.vcloud.director.v1_5.predicates.ReferenceTypePredicates; +import org.testng.annotations.Test; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ReferenceTypePredicatesTest") +public class ReferenceTypePredicatesTest { + Reference ref = Reference.builder().type("application/vnd.vmware.vcloud.catalogItem+xml").name("image").href( + URI.create("https://vcloudbeta.bluelock.com/api/catalogItem/67a469a1-aafe-4b5b-bb31-a6202ad8961f")).build(); + + @Test + public void testNameEqualsWhenEqual() { + assert ReferenceTypePredicates. nameEquals("image").apply(ref); + } + + @Test + public void testNameEqualsWhenNotEqual() { + assert !ReferenceTypePredicates. nameEquals("foo").apply(ref); + } + + @Test + public void testTypeEqualsWhenEqual() { + assert ReferenceTypePredicates. typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref); + } + + @Test + public void testTypeEqualsWhenNotEqual() { + assert !ReferenceTypePredicates. typeEquals("foo").apply(ref); + } +}