added predicates handy for finding vcloud references

This commit is contained in:
Adrian Cole 2012-03-07 15:13:54 -08:00
parent e8f5b049d8
commit cb0a105171
2 changed files with 141 additions and 0 deletions

View File

@ -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 <T>
* 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 <T extends ReferenceType<T>> 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 references of the given type
*
* @param <T>
* 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 <T extends ReferenceType<T>> 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

@ -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.<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);
}
}