Repository: brooklyn-server Updated Branches: refs/heads/master a58ba05c7 -> 79092da6a
Yaml entity refs: support $brooklyn:self() - Adds proper test coverage - Fixes sibling/descendant/ancestor so will never return self Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/0fb22c5b Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/0fb22c5b Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/0fb22c5b Branch: refs/heads/master Commit: 0fb22c5be684225e9175788c9dcb08574cde990d Parents: a941963 Author: Aled Sage <[email protected]> Authored: Thu Jun 23 15:49:12 2016 +0100 Committer: Aled Sage <[email protected]> Committed: Mon Jun 27 17:59:36 2016 +0100 ---------------------------------------------------------------------- .../spi/dsl/methods/BrooklynDslCommon.java | 3 + .../brooklyn/spi/dsl/methods/DslComponent.java | 33 +++- .../camp/brooklyn/EntityRefsYamlTest.java | 186 +++++++++++++++++++ .../resources/test-referencing-entities.yaml | 2 +- 4 files changed, 213 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/0fb22c5b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java index 48ea03f..4af7f2c 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java @@ -67,6 +67,9 @@ public class BrooklynDslCommon { // Access specific entities + public static DslComponent self() { + return new DslComponent(Scope.THIS, null); + } public static DslComponent entity(String id) { return new DslComponent(Scope.GLOBAL, id); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/0fb22c5b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java index 9dd3a04..e0641ca 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java @@ -28,6 +28,7 @@ import org.apache.brooklyn.api.sensor.AttributeSensor; import org.apache.brooklyn.api.sensor.Sensor; import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants; import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier; +import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent.Scope; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.entity.Entities; import org.apache.brooklyn.core.entity.EntityInternal; @@ -44,6 +45,8 @@ import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes; import com.google.common.base.Objects; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -100,30 +103,36 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { @Override public Entity call() throws Exception { Iterable<Entity> entitiesToSearch = null; + EntityInternal entity = getEntity(); + Predicate<Entity> notSelfPredicate = Predicates.not(Predicates.<Entity>equalTo(entity)); + switch (scope) { case THIS: - return getEntity(); + return entity; case PARENT: - return getEntity().getParent(); + return entity.getParent(); case GLOBAL: - entitiesToSearch = ((EntityManagerInternal)getEntity().getManagementContext().getEntityManager()) + entitiesToSearch = ((EntityManagerInternal)entity.getManagementContext().getEntityManager()) .getAllEntitiesInApplication( entity().getApplication() ); break; case ROOT: - return getEntity().getApplication(); + return entity.getApplication(); case SCOPE_ROOT: - return Entities.catalogItemScopeRoot(getEntity()); + return Entities.catalogItemScopeRoot(entity); case DESCENDANT: - entitiesToSearch = Entities.descendants(getEntity()); + entitiesToSearch = Entities.descendants(entity); + entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate); break; case ANCESTOR: - entitiesToSearch = Entities.ancestors(getEntity()); + entitiesToSearch = Entities.ancestors(entity); + entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate); break; case SIBLING: - entitiesToSearch = getEntity().getParent().getChildren(); + entitiesToSearch = entity.getParent().getChildren(); + entitiesToSearch = Iterables.filter(entitiesToSearch, notSelfPredicate); break; case CHILD: - entitiesToSearch = getEntity().getChildren(); + entitiesToSearch = entity.getChildren(); break; default: throw new IllegalStateException("Unexpected scope "+scope); @@ -136,7 +145,7 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { // TODO may want to block and repeat on new entities joining? throw new NoSuchElementException("No entity matching id " + componentId+ - (scope==Scope.GLOBAL ? "" : ", in scope "+scope+" wrt "+getEntity()+ + (scope==Scope.GLOBAL ? "" : ", in scope "+scope+" wrt "+entity+ (scopeComponent!=null ? " ("+scopeComponent+" from "+entity()+")" : ""))); } } @@ -172,6 +181,10 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { return new DslComponent(this, Scope.GLOBAL, scopeOrId); } + public DslComponent self() { + return new DslComponent(this, Scope.THIS, null); + } + public DslComponent parent() { return new DslComponent(this, Scope.PARENT, ""); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/0fb22c5b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityRefsYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityRefsYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityRefsYamlTest.java new file mode 100644 index 0000000..2542834 --- /dev/null +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityRefsYamlTest.java @@ -0,0 +1,186 @@ +/* + * 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.camp.brooklyn; + +import static org.testng.Assert.assertEquals; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.EntityPredicates; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +import com.google.common.collect.Iterables; + +@Test +public class EntityRefsYamlTest extends AbstractYamlTest { + private static final Logger log = LoggerFactory.getLogger(EntityRefsYamlTest.class); + + @Test + public void testRefToSelf() throws Exception { + Entity app = createAndStartApplication( + "services:", + "- type: " + TestEntity.class.getName(), + " test.confObject: $brooklyn:self()", + " test.confName: $brooklyn:self().attributeWhenReady(\"mysensor\")"); + Entity entity = Iterables.getOnlyElement(app.getChildren()); + + assertEquals(entity.getConfig(TestEntity.CONF_OBJECT), entity); + + entity.sensors().set(Sensors.newStringSensor("mysensor"), "myval"); + assertEquals(entity.getConfig(TestEntity.CONF_NAME), "myval"); + } + + @Test + public void testRefToParent() throws Exception { + Entity app = createAndStartApplication( + "services:", + "- type: " + TestEntity.class.getName(), + " name: entity1", + " brooklyn.children:", + " - type: " + TestEntity.class.getName(), + " brooklyn.config:", + " test.confObject: $brooklyn:parent()"); + Entity parent = Iterables.getOnlyElement(app.getChildren()); + Entity child = Iterables.getOnlyElement(parent.getChildren()); + + assertEquals(child.getConfig(TestEntity.CONF_OBJECT), parent); + } + + @Test + public void testRefToRoot() throws Exception { + Entity app = createAndStartApplication( + "services:", + "- type: " + TestEntity.class.getName(), + " brooklyn.config:", + " test.confObject: $brooklyn:root()"); + Entity entity = Iterables.getOnlyElement(app.getChildren()); + + assertEquals(entity.getConfig(TestEntity.CONF_OBJECT), app); + } + + @Test + public void testRefToScopeRoot() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: myCatalogItem", + " item:", + " type: "+ TestEntity.class.getName(), + " brooklyn.children:", + " - type: " + TestEntity.class.getName(), + " brooklyn.config:", + " test.confObject: $brooklyn:scopeRoot()"); + + Entity app = createAndStartApplication( + "services:", + "- type: myCatalogItem"); + Entity entity = Iterables.getOnlyElement(app.getChildren()); + Entity child = Iterables.getOnlyElement(entity.getChildren()); + + assertEquals(child.getConfig(TestEntity.CONF_OBJECT), entity); + } + + @Test + public void testRefToEntityById() throws Exception { + // Tests for sibling, descendant and ancestor + Entity app = createAndStartApplication( + "services:", + "- type: " + TestEntity.class.getName(), + " brooklyn.config:", + " conf.entity: $brooklyn:entity(\"childid\")", + " conf.component: $brooklyn:component(\"childid\")", + " conf.component.global: $brooklyn:component(\"global\", \"childid\")", + " brooklyn.children:", + " - type: " + TestEntity.class.getName(), + " id: childid"); + Entity entity = Iterables.getOnlyElement(app.getChildren()); + Entity child = Iterables.getOnlyElement(entity.getChildren()); + + assertEquals(entity.getConfig(newConfigKey("conf.entity")), child); + assertEquals(entity.getConfig(newConfigKey("conf.component")), child); + assertEquals(entity.getConfig(newConfigKey("conf.component.global")), child); + } + + @Test + public void testRefToRelative() throws Exception { + // Tests for sibling, descendant and ancestor + String duplicatedId = "myDuplicatedId"; + + Entity app = createAndStartApplication( + "services:", + "- type: " + TestEntity.class.getName(), + " id: "+duplicatedId, + " name: entity1", + " brooklyn.config:", + " conf1.sibling: $brooklyn:sibling(\""+duplicatedId+"\")", + " conf1.descendant: $brooklyn:descendant(\""+duplicatedId+"\")", + " conf1.sibling2: $brooklyn:component(\"sibling\", \""+duplicatedId+"\")", + " conf1.descendant2: $brooklyn:component(\"descendant\", \""+duplicatedId+"\")", + " brooklyn.children:", + " - type: " + TestEntity.class.getName(), + " id: "+duplicatedId, + " name: entity1.1", + "- type: " + TestEntity.class.getName(), + " id: "+duplicatedId, + " name: entity2", + " brooklyn.children:", + " - type: " + TestEntity.class.getName(), + " id: "+duplicatedId, + " name: entity2.1", + " brooklyn.config:", + " conf2.1.sibling: $brooklyn:sibling(\""+duplicatedId+"\")", + " conf2.1.ancestor: $brooklyn:ancestor(\""+duplicatedId+"\")", + " conf2.1.sibling2: $brooklyn:component(\"sibling\", \""+duplicatedId+"\")", + " conf2.1.ancestor2: $brooklyn:component(\"ancestor\", \""+duplicatedId+"\")", + " - type: " + TestEntity.class.getName(), + " id: "+duplicatedId, + " name: entity2.2"); + + Entities.dumpInfo(app); + + Entity entity1 = Iterables.find(Entities.descendants(app), EntityPredicates.displayNameEqualTo("entity1")); + Entity entity1_1 = Iterables.find(Entities.descendants(app), EntityPredicates.displayNameEqualTo("entity1.1")); + Entity entity2 = Iterables.find(Entities.descendants(app), EntityPredicates.displayNameEqualTo("entity2")); + Entity entity2_1 = Iterables.find(Entities.descendants(app), EntityPredicates.displayNameEqualTo("entity2.1")); + Entity entity2_2 = Iterables.find(Entities.descendants(app), EntityPredicates.displayNameEqualTo("entity2.2")); + + assertEquals(entity1.getConfig(newConfigKey("conf1.sibling")), entity2); + assertEquals(entity1.getConfig(newConfigKey("conf1.sibling2")), entity2); + assertEquals(entity1.getConfig(newConfigKey("conf1.descendant")), entity1_1); + assertEquals(entity1.getConfig(newConfigKey("conf1.descendant2")), entity1_1); + assertEquals(entity2_1.getConfig(newConfigKey("conf2.1.sibling")), entity2_2); + assertEquals(entity2_1.getConfig(newConfigKey("conf2.1.sibling2")), entity2_2); + assertEquals(entity2_1.getConfig(newConfigKey("conf2.1.ancestor")), entity2); + assertEquals(entity2_1.getConfig(newConfigKey("conf2.1.ancestor2")), entity2); + } + + protected ConfigKey<Object> newConfigKey(String name) { + return ConfigKeys.newConfigKey(Object.class, name); + } + + @Override + protected Logger getLogger() { + return log; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/0fb22c5b/camp/camp-brooklyn/src/test/resources/test-referencing-entities.yaml ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/resources/test-referencing-entities.yaml b/camp/camp-brooklyn/src/test/resources/test-referencing-entities.yaml index f723a40..e33b857 100644 --- a/camp/camp-brooklyn/src/test/resources/test-referencing-entities.yaml +++ b/camp/camp-brooklyn/src/test/resources/test-referencing-entities.yaml @@ -114,7 +114,7 @@ services: brooklyn.config: test.reference.root: $brooklyn:root() test.reference.scope_root: $brooklyn:scopeRoot() - test.reference.app: $brooklyn:parent().parent().descendant("app1") + test.reference.app: $brooklyn:parent().parent().self() test.reference.entity1: $brooklyn:component("e1") test.reference.entity2: $brooklyn:component("e2") test.reference.child1: $brooklyn:component("c1")
