This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 16c28fc0a11ec0b5bc56588f16604c3f35b4f56a Author: Alex Heneveld <alex.henev...@cloudsoftcorp.com> AuthorDate: Fri Aug 6 12:22:19 2021 +0100 fix scope root evaluation, passes previous test changes semantics however for when used on the root node; test is more flexible in this case --- .../brooklyn/camp/brooklyn/EntitiesYamlTest.java | 26 +++++++++++++++++----- .../camp/brooklyn/ReferencingYamlTestEntity.java | 6 +++++ .../org/apache/brooklyn/core/entity/Entities.java | 11 +++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java index 44cdfe0..4ee8a4c 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java @@ -557,7 +557,8 @@ public class EntitiesYamlTest extends AbstractYamlTest { " test.reference.scope_root: $brooklyn:scopeRoot()", " brooklyn.children:", " - type: ref_child", - " name: RP-grandchild=RC"); + " name: RP-grandchild=RC", + " test.reference.scope_root2: $brooklyn:scopeRoot()"); Entity app = createAndStartApplication( "brooklyn.config:", @@ -576,7 +577,8 @@ public class EntitiesYamlTest extends AbstractYamlTest { " test.reference.scope_root: $brooklyn:scopeRoot()", " brooklyn.children:", " - type: ref_parent", - " name: APP-greatgrandchild=RP"); + " name: APP-greatgrandchild=RP", + " test.reference.scope_root2: $brooklyn:scopeRoot()"); assertScopes(app, "APP", app, app); Entity e1 = nextChild(app); @@ -584,20 +586,21 @@ public class EntitiesYamlTest extends AbstractYamlTest { Entity e2 = nextChild(e1); assertScopes(e2, "APP-grandchild", app, app); Entity e3 = nextChild(e2); - assertScopes(e3, "APP-greatgrandchild=RP", app, e3); + assertScopes(e3, "APP-greatgrandchild=RP", app, e2, app); Entity e4 = nextChild(e3); assertScopes(e4, "RP-child", app, e3); Entity e5 = nextChild(e4); - assertScopes(e5, "RP-grandchild=RC", app, e5); + assertScopes(e5, "RP-grandchild=RC", app, e5, e3); Entity e6 = nextChild(e5); assertScopes(e6, "RC-child", app, e5); } + @Test public void testScopeReferences() throws Exception { doTestScopeReferences(ReferencingYamlTestEntity.class.getName()); } - @Test(groups="WIP") + @Test public void testScopeReferencesComplex() throws Exception { addCatalogItems( "brooklyn.catalog:", @@ -614,9 +617,20 @@ public class EntitiesYamlTest extends AbstractYamlTest { return Iterables.getOnlyElement(entity.getChildren()); } private static void assertScopes(Entity entity, String name, Entity root, Entity scopeRoot) { + assertScopes(entity, name, root, scopeRoot, null); + } + private static void assertScopes(Entity entity, String name, Entity root, Entity scopeRoot, Entity scopeRoot2) { if (name!=null) assertEquals(entity.getDisplayName(), name); assertEquals(entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_ROOT), root); - assertEquals(entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_SCOPE_ROOT), scopeRoot); + + Entity actualScopeRoot = entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_SCOPE_ROOT); + if (!actualScopeRoot.equals(scopeRoot) && !actualScopeRoot.equals(scopeRoot2)) { + Assert.fail("Wrong scope root; should be either "+scopeRoot+" or "+scopeRoot2+"; but is actually "+actualScopeRoot); + } + // TODO would be nice if we can capture which blueprint scopeRoot is used in - but this requires introspecting the DSL + // currently it will always equal scopeRoot2; if we could convert it to "self()" when the definition is loaded, that would solve it + + assertEquals(entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_SCOPE_ROOT2), scopeRoot2); } private void checkReferences(final Entity entity, Map<ConfigKey<Entity>, Entity> keyToEntity) throws Exception { diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java index 7a8ac59..bc13ecf 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java @@ -20,7 +20,9 @@ package org.apache.brooklyn.camp.brooklyn; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.ImplementedBy; +import org.apache.brooklyn.config.ConfigInheritance; import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.BasicConfigInheritance; import org.apache.brooklyn.core.config.BasicConfigKey; import com.google.common.reflect.TypeToken; @@ -35,6 +37,10 @@ public interface ReferencingYamlTestEntity extends Entity { public static final ConfigKey<Entity> TEST_REFERENCE_SCOPE_ROOT = BasicConfigKey.builder(new TypeToken<Entity>(){}) .name("test.reference.scope_root") .build(); + public static final ConfigKey<Entity> TEST_REFERENCE_SCOPE_ROOT2 = BasicConfigKey.builder(new TypeToken<Entity>(){}) + .name("test.reference.scope_root2") + .runtimeInheritance(BasicConfigInheritance.NEVER_INHERITED) + .build(); @SuppressWarnings("serial") public static final ConfigKey<Entity> TEST_REFERENCE_APP = BasicConfigKey.builder(new TypeToken<Entity>(){}) .name("test.reference.app") diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java index f794793..cd28df8 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java @@ -18,6 +18,7 @@ */ package org.apache.brooklyn.core.entity; +import org.apache.brooklyn.core.mgmt.BrooklynTags; import static org.apache.brooklyn.util.guava.Functionals.isSatisfied; import java.io.Closeable; @@ -1149,6 +1150,16 @@ public class Entities { public static Entity catalogItemScopeRoot(Entity entity) { Entity root = entity; + + Integer depth = BrooklynTags.getDepthInAncestorTag(root.tags().getTags()); + if (depth!=null && depth>0) { + while (depth>0) { + root = root.getParent(); + depth--; + } + return root; + } + while (root.getParent() != null && root != root.getParent() && Objects.equal(root.getParent().getCatalogItemId(), root.getCatalogItemId())) {