diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/LinkPredicates.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/LinkPredicates.java index 96ac2dd1ae..51e9a063b0 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/LinkPredicates.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/LinkPredicates.java @@ -59,13 +59,13 @@ public class LinkPredicates { * @see ReferenceTypePredicates#nameEquals */ public static Predicate nameEquals(String name) { - return ReferenceTypePredicates.nameEquals(name); + return ReferencePredicates.nameEquals(name); } /** * @see ReferenceTypePredicates#typeEquals */ public static Predicate typeEquals(String type) { - return ReferenceTypePredicates.typeEquals(type); + return ReferencePredicates.typeEquals(type); } } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicates.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicates.java index 6b4925e7aa..60339ee333 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicates.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicates.java @@ -20,27 +20,23 @@ package org.jclouds.vcloud.director.v1_5.predicates; import static com.google.common.base.Preconditions.checkNotNull; -import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; -import org.jclouds.vcloud.director.v1_5.domain.Link; import org.jclouds.vcloud.director.v1_5.domain.Reference; import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; /** - * Predicates handy when working with Reference Types + * Predicates for working with {@link Reference} collections. * * @author Adrian Cole */ - public class ReferencePredicates { /** - * matches references of the given name + * Matches {@link Reference}s with the given name. * - * @param - * type of the Reference, ex. {@link Link} - * @param name - * ex. {@code context.getApi().getCurrentSession().getOrg()} + * @param T type of the reference, for example {@link Link} + * @param name value of the name attribute of the referenced object * @return predicate that will match references of the given name */ public static Predicate nameEquals(final String name) { @@ -60,12 +56,58 @@ public class ReferencePredicates { } /** - * matches references of the given type + * Matches {@link Reference}s with names starting with the given prefix. * - * @param - * type of the Reference, ex. {@link Link} - * @param type - * ex. {@link VCloudDirectorMediaType#CATALOG} + * @param T type of the reference, for example {@link Link} + * @param name prefix of the name attribute of the referenced object + * @return predicate that will match references with names starting with the given prefix + */ + public static Predicate nameStartsWith(final String prefix) { + checkNotNull(prefix, "prefix must be defined"); + + return new Predicate() { + @Override + public boolean apply(T reference) { + String name = reference.getName(); + return name != null && name.startsWith(prefix); + } + + @Override + public String toString() { + return "nameStartsWith(" + prefix + ")"; + } + }; + } + + /** + * Matches {@link Reference}s with names in the given collection. + * + * @param T type of the reference, for example {@link Link} + * @param names collection of values for the name attribute of the referenced object + * @return predicate that will match references with names starting with the given prefix + */ + public static Predicate nameIn(final Iterable names) { + checkNotNull(names, "names must be defined"); + + return new Predicate() { + @Override + public boolean apply(T reference) { + String name = reference.getName(); + return Iterables.contains(names, name); + } + + @Override + public String toString() { + return "nameIn(" + Iterables.toString(names) + ")"; + } + }; + } + + /** + * Matches {@link Reference}s of the given type. + * + * @param T type of the reference, for example {@link Link} + * @param type the media type string of the referenced object, for example {@link VCloudDirectorMediaType#CATALOG} * @return predicate that will match references of the given type * @see VCloudDirectorMediaType */ 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 deleted file mode 100644 index b1a3ca3484..0000000000 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicates.java +++ /dev/null @@ -1,129 +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.vcloud.director.v1_5.predicates; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.vcloud.director.v1_5.domain.Reference; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; - -/** - * Predicates for working with {@link Reference} collections. - * - * @author Adrian Cole - */ -public class ReferenceTypePredicates { - - /** - * Matches {@link Reference}s with the given name. - * - * @param T type of the reference, for example {@link Link} - * @param name value of the name attribute of the referenced object - * @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 {@link Reference}s with names starting with the given prefix. - * - * @param T type of the reference, for example {@link Link} - * @param name prefix of the name attribute of the referenced object - * @return predicate that will match references with names starting with the given prefix - */ - public static Predicate nameStartsWith(final String prefix) { - checkNotNull(prefix, "prefix must be defined"); - - return new Predicate() { - @Override - public boolean apply(T reference) { - String name = reference.getName(); - return name != null && name.startsWith(prefix); - } - - @Override - public String toString() { - return "nameStartsWith(" + prefix + ")"; - } - }; - } - - /** - * Matches {@link Reference}s with names in the given collection. - * - * @param T type of the reference, for example {@link Link} - * @param names collection of values for the name attribute of the referenced object - * @return predicate that will match references with names starting with the given prefix - */ - public static Predicate nameIn(final Iterable names) { - checkNotNull(names, "names must be defined"); - - return new Predicate() { - @Override - public boolean apply(T reference) { - String name = reference.getName(); - return Iterables.contains(names, name); - } - - @Override - public String toString() { - return "nameIn(" + Iterables.toString(names) + ")"; - } - }; - } - - /** - * Matches {@link Reference}s of the given type. - * - * @param T type of the reference, for example {@link Link} - * @param type the media type string of the referenced object, for example {@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/features/AbstractVAppClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/AbstractVAppClientLiveTest.java index b2c4ee4176..fdc34fcc20 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/AbstractVAppClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/AbstractVAppClientLiveTest.java @@ -51,7 +51,7 @@ import org.jclouds.vcloud.director.v1_5.domain.cim.CimUnsignedInt; import org.jclouds.vcloud.director.v1_5.domain.cim.CimUnsignedLong; import org.jclouds.vcloud.director.v1_5.domain.cim.ResourceAllocationSettingData; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; -import org.jclouds.vcloud.director.v1_5.predicates.ReferenceTypePredicates; +import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates; import org.jclouds.xml.internal.JAXBParser; import org.testng.Assert; import org.testng.annotations.AfterClass; @@ -186,8 +186,8 @@ public abstract class AbstractVAppClientLiveTest extends BaseVCloudDirectorClien Iterable vApps = Iterables.filter( vdc.getResourceEntities(), Predicates.and( - ReferenceTypePredicates.typeEquals(VCloudDirectorMediaType.VAPP), - ReferenceTypePredicates.nameIn(vAppNames) + ReferencePredicates.typeEquals(VCloudDirectorMediaType.VAPP), + ReferencePredicates.nameIn(vAppNames) ) ); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java index eaa51964b9..56e345219b 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java @@ -66,7 +66,7 @@ import org.jclouds.vcloud.director.v1_5.features.TaskClient; import org.jclouds.vcloud.director.v1_5.features.VAppClient; import org.jclouds.vcloud.director.v1_5.features.VAppTemplateClient; import org.jclouds.vcloud.director.v1_5.features.VdcClient; -import org.jclouds.vcloud.director.v1_5.predicates.ReferenceTypePredicates; +import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates; import org.jclouds.vcloud.director.v1_5.predicates.TaskStatusEquals; import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess; import org.testng.annotations.BeforeClass; @@ -187,28 +187,20 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ if (Iterables.any(Lists.newArrayList(vAppTemplateURI, networkURI, vdcURI), Predicates.isNull())) { Org thisOrg = context.getApi().getOrgClient().getOrg( Iterables.find(context.getApi().getOrgClient().getOrgList().getOrgs(), - ReferenceTypePredicates. nameEquals(session.getOrg())).getHref()); - - //TODO: can we create new objects via (admin) operations instead of looking things up? - //TODO: lookup mediaURI, userURI - - // FIXME: lookup vAppTemplate -// if (vAppTemplateURI == null) -// vAppTemplateURI = Iterables.find(context.getApi().getQueryClient().vAppTemplatesReferenceQueryAll(), -// ReferenceTypePredicates. typeEquals(VCloudDirectorMediaType.VDC)).getHref(); + ReferencePredicates. nameEquals(session.getOrg())).getHref()); if (vdcURI == null) vdcURI = Iterables.find(thisOrg.getLinks(), - ReferenceTypePredicates. typeEquals(VCloudDirectorMediaType.VDC)).getHref(); + ReferencePredicates. typeEquals(VCloudDirectorMediaType.VDC)).getHref(); if (networkURI == null) networkURI = Iterables.find(thisOrg.getLinks(), - ReferenceTypePredicates. typeEquals(VCloudDirectorMediaType.ORG_NETWORK)).getHref(); + ReferencePredicates. typeEquals(VCloudDirectorMediaType.ORG_NETWORK)).getHref(); // FIXME the URI should be opaque if (Strings.isNullOrEmpty(catalogId)) { String uri = Iterables.find(thisOrg.getLinks(), - ReferenceTypePredicates. typeEquals(VCloudDirectorMediaType.CATALOG)).getHref().toASCIIString(); + ReferencePredicates. typeEquals(VCloudDirectorMediaType.CATALOG)).getHref().toASCIIString(); catalogId = Iterables.getLast(Splitter.on('/').split(uri)); } } diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicatesTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicatesTest.java index a3cf706772..ca4b33af43 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicatesTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferencePredicatesTest.java @@ -18,14 +18,18 @@ */ package org.jclouds.vcloud.director.v1_5.predicates; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + import java.net.URI; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.domain.Reference; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableSet; + /** - * * @author Adrian Cole */ @Test(groups = "unit", testName = "ReferencePredicatesTest") @@ -35,21 +39,41 @@ public class ReferencePredicatesTest { @Test public void testNameEqualsWhenEqual() { - assert ReferencePredicates. nameEquals("image").apply(ref); + assertTrue(ReferencePredicates. nameEquals("image").apply(ref)); } @Test public void testNameEqualsWhenNotEqual() { - assert !ReferencePredicates. nameEquals("foo").apply(ref); + assertFalse(ReferencePredicates. nameEquals("foo").apply(ref)); + } + + @Test + public void testNameStartsWithWhenStartsWith() { + assertTrue(ReferencePredicates. nameStartsWith("i").apply(ref)); + } + + @Test + public void testNameStartsWithWhenNotStartsWith() { + assertFalse(ReferencePredicates. nameStartsWith("f").apply(ref)); + } + + @Test + public void testNameInWhenIn() { + assertTrue(ReferencePredicates. nameIn(ImmutableSet.of("one", "two", "image")).apply(ref)); + } + + @Test + public void testNameInWhenNotIn() { + assertFalse(ReferencePredicates. nameIn(ImmutableSet.of("one", "two", "foo")).apply(ref)); } @Test public void testTypeEqualsWhenEqual() { - assert ReferencePredicates. typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref); + assertTrue(ReferencePredicates. typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref)); } @Test public void testTypeEqualsWhenNotEqual() { - assert !ReferencePredicates. typeEquals("foo").apply(ref); + assertFalse(ReferencePredicates. typeEquals("foo").apply(ref)); } } 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 deleted file mode 100644 index 44dc169daf..0000000000 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/predicates/ReferenceTypePredicatesTest.java +++ /dev/null @@ -1,55 +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.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.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); - } -}