You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2016/02/01 18:50:32 UTC
[16/50] [abbrv] brooklyn-server git commit: CatalogPredicates: test +
not anonymous classes
CatalogPredicates: test + not anonymous classes
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/6ea18917
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/6ea18917
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/6ea18917
Branch: refs/heads/0.8.0-incubating
Commit: 6ea1891747dd5f8954aebfd773f5a6a19417127c
Parents: e50fc99
Author: Aled Sage <al...@gmail.com>
Authored: Wed Aug 19 14:38:40 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Aug 26 09:24:31 2015 +0100
----------------------------------------------------------------------
.../core/catalog/CatalogPredicates.java | 145 ++++++++++++++++--
.../core/catalog/CatalogPredicatesTest.java | 153 +++++++++++++++++++
2 files changed, 288 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6ea18917/core/src/main/java/org/apache/brooklyn/core/catalog/CatalogPredicates.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/CatalogPredicates.java b/core/src/main/java/org/apache/brooklyn/core/catalog/CatalogPredicates.java
index f5ed326..e30d278 100644
--- a/core/src/main/java/org/apache/brooklyn/core/catalog/CatalogPredicates.java
+++ b/core/src/main/java/org/apache/brooklyn/core/catalog/CatalogPredicates.java
@@ -39,21 +39,49 @@ import com.google.common.base.Predicate;
public class CatalogPredicates {
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> isCatalogItemType(final CatalogItemType ciType) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && item.getCatalogItemType()==ciType;
}
};
+ return new CatalogItemTypeEquals<T, SpecT>(ciType);
+ }
+
+ private static class CatalogItemTypeEquals<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final CatalogItemType ciType;
+
+ public CatalogItemTypeEquals(final CatalogItemType ciType) {
+ this.ciType = ciType;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && item.getCatalogItemType()==ciType;
+ }
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> deprecated(final boolean deprecated) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && item.isDeprecated() == deprecated;
}
};
+ return new DeprecatedEquals<T, SpecT>(deprecated);
+ }
+
+ private static class DeprecatedEquals<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final boolean deprecated;
+
+ public DeprecatedEquals(boolean deprecated) {
+ this.deprecated = deprecated;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && item.isDeprecated() == deprecated;
+ }
}
public static final Predicate<CatalogItem<Application,EntitySpec<? extends Application>>> IS_TEMPLATE =
@@ -65,7 +93,20 @@ public class CatalogPredicates {
public static final Predicate<CatalogItem<Location,LocationSpec<?>>> IS_LOCATION =
CatalogPredicates.<Location,LocationSpec<?>>isCatalogItemType(CatalogItemType.LOCATION);
- public static final Function<CatalogItem<?,?>,String> ID_OF_ITEM_TRANSFORMER = new Function<CatalogItem<?,?>, String>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ @SuppressWarnings("unused")
+ private static final Function<CatalogItem<?,?>,String> ID_OF_ITEM_TRANSFORMER_ANONYMOUS = new Function<CatalogItem<?,?>, String>() {
+ @Override @Nullable
+ public String apply(@Nullable CatalogItem<?,?> input) {
+ if (input==null) return null;
+ return input.getId();
+ }
+ };
+
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ public static final Function<CatalogItem<?,?>,String> ID_OF_ITEM_TRANSFORMER = new IdOfItemTransformer();
+
+ private static class IdOfItemTransformer implements Function<CatalogItem<?,?>,String> {
@Override @Nullable
public String apply(@Nullable CatalogItem<?,?> input) {
if (input==null) return null;
@@ -80,12 +121,26 @@ public class CatalogPredicates {
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> displayName(final Predicate<? super String> filter) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && filter.apply(item.getDisplayName());
}
};
+ return new DisplayNameMatches<T,SpecT>(filter);
+ }
+
+ private static class DisplayNameMatches<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final Predicate<? super String> filter;
+
+ public DisplayNameMatches(Predicate<? super String> filter) {
+ this.filter = filter;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && filter.apply(item.getDisplayName());
+ }
}
@Deprecated
@@ -94,49 +149,119 @@ public class CatalogPredicates {
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> symbolicName(final Predicate<? super String> filter) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && filter.apply(item.getSymbolicName());
}
};
+ return new SymbolicNameMatches<T,SpecT>(filter);
+ }
+
+ private static class SymbolicNameMatches<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final Predicate<? super String> filter;
+
+ public SymbolicNameMatches(Predicate<? super String> filter) {
+ this.filter = filter;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && filter.apply(item.getSymbolicName());
+ }
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> javaType(final Predicate<? super String> filter) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && filter.apply(item.getJavaType());
}
};
+ return new JavaTypeMatches<T, SpecT>(filter);
+ }
+
+ private static class JavaTypeMatches<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final Predicate<? super String> filter;
+
+ public JavaTypeMatches(Predicate<? super String> filter) {
+ this.filter = filter;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && filter.apply(item.getJavaType());
+ }
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> xml(final Predicate<? super String> filter) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) && filter.apply(item.toXmlString());
}
};
+ return new XmlMatches<T,SpecT>(filter);
+ }
+
+ private static class XmlMatches<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final Predicate<? super String> filter;
+
+ public XmlMatches(Predicate<? super String> filter) {
+ this.filter = filter;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) && filter.apply(item.toXmlString());
+ }
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> entitledToSee(final ManagementContext mgmt) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return (item != null) &&
Entitlements.isEntitled(mgmt.getEntitlementManager(), Entitlements.SEE_CATALOG_ITEM, item.getCatalogItemId());
}
};
+ return new EntitledToSee<T, SpecT>(mgmt);
+ }
+
+ private static class EntitledToSee<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final ManagementContext mgmt;
+
+ public EntitledToSee(ManagementContext mgmt) {
+ this.mgmt = mgmt;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return (item != null) &&
+ Entitlements.isEntitled(mgmt.getEntitlementManager(), Entitlements.SEE_CATALOG_ITEM, item.getCatalogItemId());
+ }
}
public static <T,SpecT> Predicate<CatalogItem<T,SpecT>> isBestVersion(final ManagementContext mgmt) {
- return new Predicate<CatalogItem<T,SpecT>>() {
+ // TODO PERSISTENCE WORKAROUND kept anonymous function in case referenced in persisted state
+ new Predicate<CatalogItem<T,SpecT>>() {
@Override
public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
return CatalogUtils.isBestVersion(mgmt, item);
}
};
+ return new IsBestVersion<T, SpecT>(mgmt);
+ }
+
+ private static class IsBestVersion<T,SpecT> implements Predicate<CatalogItem<T,SpecT>> {
+ private final ManagementContext mgmt;
+
+ public IsBestVersion(ManagementContext mgmt) {
+ this.mgmt = mgmt;
+ }
+ @Override
+ public boolean apply(@Nullable CatalogItem<T,SpecT> item) {
+ return CatalogUtils.isBestVersion(mgmt, item);
+ }
}
-
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6ea18917/core/src/test/java/org/apache/brooklyn/core/catalog/CatalogPredicatesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/catalog/CatalogPredicatesTest.java b/core/src/test/java/org/apache/brooklyn/core/catalog/CatalogPredicatesTest.java
new file mode 100644
index 0000000..32860a4
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/core/catalog/CatalogPredicatesTest.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.brooklyn.core.catalog;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.brooklyn.api.catalog.BrooklynCatalog;
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.catalog.CatalogItem.CatalogItemType;
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.proxying.EntitySpec;
+import org.apache.brooklyn.core.catalog.internal.CatalogItemBuilder;
+import org.apache.brooklyn.core.management.internal.LocalManagementContext;
+import org.apache.brooklyn.test.entity.LocalManagementContextForTests;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Predicates;
+
+import brooklyn.entity.basic.Entities;
+
+public class CatalogPredicatesTest {
+ private LocalManagementContext mgmt;
+ private BrooklynCatalog catalog;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception {
+ mgmt = LocalManagementContextForTests.newInstance();
+ catalog = mgmt.getCatalog();
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception {
+ if (mgmt != null) Entities.destroyAll(mgmt);
+ }
+
+ @Test
+ public void testDisplayName() {
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .displayName("myname")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>displayName(Predicates.equalTo("myname")).apply(item));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>displayName(Predicates.equalTo("wrongname")).apply(item));
+ }
+
+ @Test
+ public void testDeprecated() {
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>deprecated(false).apply(item));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>deprecated(true).apply(item));
+
+ item = deprecateItem(item);
+
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>deprecated(false).apply(item));
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>deprecated(true).apply(item));
+ }
+
+ @Test
+ public void testIsCatalogItemType() {
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>isCatalogItemType(CatalogItemType.ENTITY).apply(item));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>isCatalogItemType(CatalogItemType.LOCATION).apply(item));
+ }
+
+ @Test
+ public void testSymbolicName() {
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>symbolicName(Predicates.equalTo("foo")).apply(item));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>symbolicName(Predicates.equalTo("wrongname")).apply(item));
+ }
+
+ @Test
+ public void testIsBestVersion() {
+ CatalogItem<Entity, EntitySpec<?>> itemV1 = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+ CatalogItem<Entity, EntitySpec<?>> itemV2 = createItem(CatalogItemBuilder.newEntity("foo", "2.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+ CatalogItem<Entity, EntitySpec<?>> itemV3Disabled = createItem(CatalogItemBuilder.newEntity("foo", "3.0")
+ .disabled(true)
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>isBestVersion(mgmt).apply(itemV2));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>isBestVersion(mgmt).apply(itemV1));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>isBestVersion(mgmt).apply(itemV3Disabled));
+ }
+
+ @Test
+ public void testEntitledToSee() {
+ // TODO No entitlements configured, so everything allowed - therefore test not thorough enough!
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .plan("services:\n- type: brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>entitledToSee(mgmt).apply(item));
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testJavaType() {
+ CatalogItem<Entity, EntitySpec<?>> item = createItem(CatalogItemBuilder.newEntity("foo", "1.0")
+ .javaType("brooklyn.entity.basic.BasicEntity")
+ .build());
+
+ assertTrue(CatalogPredicates.<Entity,EntitySpec<?>>javaType(Predicates.equalTo("brooklyn.entity.basic.BasicEntity")).apply(item));
+ assertFalse(CatalogPredicates.<Entity,EntitySpec<?>>javaType(Predicates.equalTo("wrongtype")).apply(item));
+ }
+
+ @SuppressWarnings("deprecation")
+ protected <T, SpecT> CatalogItem<T, SpecT> createItem(CatalogItem<T, SpecT> item) {
+ catalog.addItem(item);
+ return item;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected <T, SpecT> CatalogItem<T, SpecT> deprecateItem(CatalogItem<T, SpecT> orig) {
+ CatalogItem<T, SpecT> item = (CatalogItem<T, SpecT>) catalog.getCatalogItem(orig.getSymbolicName(), orig.getVersion());
+ item.setDeprecated(true);
+ catalog.persist(item);
+ return item;
+ }
+}