Issue 830: Tidy up reference predicates

This commit is contained in:
Andrew Donald Kennedy 2012-03-23 12:16:40 +00:00
parent c76ad955ed
commit c71ff11fc5
7 changed files with 95 additions and 221 deletions

View File

@ -59,13 +59,13 @@ public class LinkPredicates {
* @see ReferenceTypePredicates#nameEquals * @see ReferenceTypePredicates#nameEquals
*/ */
public static Predicate<Link> nameEquals(String name) { public static Predicate<Link> nameEquals(String name) {
return ReferenceTypePredicates.nameEquals(name); return ReferencePredicates.nameEquals(name);
} }
/** /**
* @see ReferenceTypePredicates#typeEquals * @see ReferenceTypePredicates#typeEquals
*/ */
public static Predicate<Link> typeEquals(String type) { public static Predicate<Link> typeEquals(String type) {
return ReferenceTypePredicates.typeEquals(type); return ReferencePredicates.typeEquals(type);
} }
} }

View File

@ -20,27 +20,23 @@ package org.jclouds.vcloud.director.v1_5.predicates;
import static com.google.common.base.Preconditions.checkNotNull; 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 org.jclouds.vcloud.director.v1_5.domain.Reference;
import com.google.common.base.Predicate; 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 * @author Adrian Cole
*/ */
public class ReferencePredicates { public class ReferencePredicates {
/** /**
* matches references of the given name * Matches {@link Reference}s with the given name.
* *
* @param <T> * @param T type of the reference, for example {@link Link}
* type of the Reference, ex. {@link Link} * @param name value of the name attribute of the referenced object
* @param name
* ex. {@code context.getApi().getCurrentSession().getOrg()}
* @return predicate that will match references of the given name * @return predicate that will match references of the given name
*/ */
public static <T extends Reference> Predicate<T> nameEquals(final String name) { public static <T extends Reference> Predicate<T> 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 <T> * @param T type of the reference, for example {@link Link}
* type of the Reference, ex. {@link Link} * @param name prefix of the name attribute of the referenced object
* @param type * @return predicate that will match references with names starting with the given prefix
* ex. {@link VCloudDirectorMediaType#CATALOG} */
public static <T extends Reference> Predicate<T> nameStartsWith(final String prefix) {
checkNotNull(prefix, "prefix must be defined");
return new Predicate<T>() {
@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 <T extends Reference> Predicate<T> nameIn(final Iterable<String> names) {
checkNotNull(names, "names must be defined");
return new Predicate<T>() {
@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 * @return predicate that will match references of the given type
* @see VCloudDirectorMediaType * @see VCloudDirectorMediaType
*/ */

View File

@ -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 <T extends Reference> Predicate<T> nameEquals(final String name) {
checkNotNull(name, "name must be defined");
return new Predicate<T>() {
@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 <T extends Reference> Predicate<T> nameStartsWith(final String prefix) {
checkNotNull(prefix, "prefix must be defined");
return new Predicate<T>() {
@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 <T extends Reference> Predicate<T> nameIn(final Iterable<String> names) {
checkNotNull(names, "names must be defined");
return new Predicate<T>() {
@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 <T extends Reference> Predicate<T> typeEquals(final String type) {
checkNotNull(type, "type must be defined");
return new Predicate<T>() {
@Override
public boolean apply(T reference) {
return type.equals(reference.getType());
}
@Override
public String toString() {
return "typeEquals(" + type + ")";
}
};
}
}

View File

@ -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.CimUnsignedLong;
import org.jclouds.vcloud.director.v1_5.domain.cim.ResourceAllocationSettingData; 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.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.jclouds.xml.internal.JAXBParser;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
@ -186,8 +186,8 @@ public abstract class AbstractVAppClientLiveTest extends BaseVCloudDirectorClien
Iterable<Reference> vApps = Iterables.filter( Iterable<Reference> vApps = Iterables.filter(
vdc.getResourceEntities(), vdc.getResourceEntities(),
Predicates.and( Predicates.and(
ReferenceTypePredicates.<Reference>typeEquals(VCloudDirectorMediaType.VAPP), ReferencePredicates.<Reference>typeEquals(VCloudDirectorMediaType.VAPP),
ReferenceTypePredicates.<Reference>nameIn(vAppNames) ReferencePredicates.<Reference>nameIn(vAppNames)
) )
); );

View File

@ -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.VAppClient;
import org.jclouds.vcloud.director.v1_5.features.VAppTemplateClient; 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.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.TaskStatusEquals;
import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess; import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess;
import org.testng.annotations.BeforeClass; 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())) { if (Iterables.any(Lists.newArrayList(vAppTemplateURI, networkURI, vdcURI), Predicates.isNull())) {
Org thisOrg = context.getApi().getOrgClient().getOrg( Org thisOrg = context.getApi().getOrgClient().getOrg(
Iterables.find(context.getApi().getOrgClient().getOrgList().getOrgs(), Iterables.find(context.getApi().getOrgClient().getOrgList().getOrgs(),
ReferenceTypePredicates.<Reference> nameEquals(session.getOrg())).getHref()); ReferencePredicates.<Reference> 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.<Link> typeEquals(VCloudDirectorMediaType.VDC)).getHref();
if (vdcURI == null) if (vdcURI == null)
vdcURI = Iterables.find(thisOrg.getLinks(), vdcURI = Iterables.find(thisOrg.getLinks(),
ReferenceTypePredicates.<Link> typeEquals(VCloudDirectorMediaType.VDC)).getHref(); ReferencePredicates.<Link> typeEquals(VCloudDirectorMediaType.VDC)).getHref();
if (networkURI == null) if (networkURI == null)
networkURI = Iterables.find(thisOrg.getLinks(), networkURI = Iterables.find(thisOrg.getLinks(),
ReferenceTypePredicates.<Link> typeEquals(VCloudDirectorMediaType.ORG_NETWORK)).getHref(); ReferencePredicates.<Link> typeEquals(VCloudDirectorMediaType.ORG_NETWORK)).getHref();
// FIXME the URI should be opaque // FIXME the URI should be opaque
if (Strings.isNullOrEmpty(catalogId)) { if (Strings.isNullOrEmpty(catalogId)) {
String uri = Iterables.find(thisOrg.getLinks(), String uri = Iterables.find(thisOrg.getLinks(),
ReferenceTypePredicates.<Link> typeEquals(VCloudDirectorMediaType.CATALOG)).getHref().toASCIIString(); ReferencePredicates.<Link> typeEquals(VCloudDirectorMediaType.CATALOG)).getHref().toASCIIString();
catalogId = Iterables.getLast(Splitter.on('/').split(uri)); catalogId = Iterables.getLast(Splitter.on('/').split(uri));
} }
} }

View File

@ -18,14 +18,18 @@
*/ */
package org.jclouds.vcloud.director.v1_5.predicates; 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 java.net.URI;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Reference; import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
/** /**
*
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "unit", testName = "ReferencePredicatesTest") @Test(groups = "unit", testName = "ReferencePredicatesTest")
@ -35,21 +39,41 @@ public class ReferencePredicatesTest {
@Test @Test
public void testNameEqualsWhenEqual() { public void testNameEqualsWhenEqual() {
assert ReferencePredicates.<Reference> nameEquals("image").apply(ref); assertTrue(ReferencePredicates.<Reference> nameEquals("image").apply(ref));
} }
@Test @Test
public void testNameEqualsWhenNotEqual() { public void testNameEqualsWhenNotEqual() {
assert !ReferencePredicates.<Reference> nameEquals("foo").apply(ref); assertFalse(ReferencePredicates.<Reference> nameEquals("foo").apply(ref));
}
@Test
public void testNameStartsWithWhenStartsWith() {
assertTrue(ReferencePredicates.<Reference> nameStartsWith("i").apply(ref));
}
@Test
public void testNameStartsWithWhenNotStartsWith() {
assertFalse(ReferencePredicates.<Reference> nameStartsWith("f").apply(ref));
}
@Test
public void testNameInWhenIn() {
assertTrue(ReferencePredicates.<Reference> nameIn(ImmutableSet.of("one", "two", "image")).apply(ref));
}
@Test
public void testNameInWhenNotIn() {
assertFalse(ReferencePredicates.<Reference> nameIn(ImmutableSet.of("one", "two", "foo")).apply(ref));
} }
@Test @Test
public void testTypeEqualsWhenEqual() { public void testTypeEqualsWhenEqual() {
assert ReferencePredicates.<Reference> typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref); assertTrue(ReferencePredicates.<Reference> typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref));
} }
@Test @Test
public void testTypeEqualsWhenNotEqual() { public void testTypeEqualsWhenNotEqual() {
assert !ReferencePredicates.<Reference> typeEquals("foo").apply(ref); assertFalse(ReferencePredicates.<Reference> typeEquals("foo").apply(ref));
} }
} }

View File

@ -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.<Reference> nameEquals("image").apply(ref);
}
@Test
public void testNameEqualsWhenNotEqual() {
assert !ReferenceTypePredicates.<Reference> nameEquals("foo").apply(ref);
}
@Test
public void testTypeEqualsWhenEqual() {
assert ReferenceTypePredicates.<Reference> typeEquals(VCloudDirectorMediaType.CATALOG_ITEM).apply(ref);
}
@Test
public void testTypeEqualsWhenNotEqual() {
assert !ReferenceTypePredicates.<Reference> typeEquals("foo").apply(ref);
}
}