Unwrap camp plan with parent overriding child displayName Don't prevent unwrapping in case displayName is different, override the child's name instead
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/94263c60 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/94263c60 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/94263c60 Branch: refs/heads/master Commit: 94263c60ca54f37dab8855b48cc38f47bd11d23e Parents: ae30539 Author: Svetoslav Neykov <[email protected]> Authored: Fri Nov 6 18:48:05 2015 +0200 Committer: Svetoslav Neykov <[email protected]> Committed: Mon Nov 9 12:49:38 2015 +0200 ---------------------------------------------------------------------- .../core/mgmt/EntityManagementUtils.java | 31 +--- .../brooklyn/camp/brooklyn/AppYamlTest.java | 9 +- .../camp/brooklyn/ApplicationsYamlTest.java | 163 ++++++++++++++++--- 3 files changed, 149 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94263c60/core/src/main/java/org/apache/brooklyn/core/mgmt/EntityManagementUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/EntityManagementUtils.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/EntityManagementUtils.java index 9c4ebc1..59bcc87 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/EntityManagementUtils.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/EntityManagementUtils.java @@ -246,7 +246,7 @@ public class EntityManagementUtils { * See {@link #WRAPPER_APP_MARKER}. */ @Beta //where should this live long-term? public static void mergeWrapperParentSpecToChildEntity(EntitySpec<? extends Application> wrapperParent, EntitySpec<?> wrappedChild) { - if (Strings.isEmpty(wrappedChild.getDisplayName())) + if (Strings.isNonEmpty(wrapperParent.getDisplayName())) wrappedChild.displayName(wrapperParent.getDisplayName()); if (!wrapperParent.getLocations().isEmpty()) wrappedChild.locations(wrapperParent.getLocations()); @@ -270,7 +270,7 @@ public class EntityManagementUtils { * for use when adding from a plan specifying an application which was wrapped because it had to be. * @see #WRAPPER_APP_MARKER */ public static boolean canPromoteWrappedApplication(EntitySpec<? extends Application> app) { - if (app.getChildren().size()!=1) + if (!hasSingleChild(app)) return false; EntitySpec<?> childSpec = Iterables.getOnlyElement(app.getChildren()); @@ -283,9 +283,8 @@ public class EntityManagementUtils { /** returns true if the spec is for an empty-ish wrapper app, * for use when adding from a plan specifying multiple entities but nothing significant at the application level. * @see #WRAPPER_APP_MARKER */ - public static boolean canPromoteChildrenInWrappedApplication(EntitySpec<?> spec) { - return canPromoteBasedOnName(spec) && - isWrapperApp(spec) && + public static boolean canPromoteChildrenInWrappedApplication(EntitySpec<? extends Application> spec) { + return isWrapperApp(spec) && hasSingleChild(spec) && //equivalent to no keys starting with "brooklyn." spec.getEnrichers().isEmpty() && spec.getInitializers().isEmpty() && @@ -296,26 +295,8 @@ public class EntityManagementUtils { return Boolean.TRUE.equals(spec.getConfig().get(EntityManagementUtils.WRAPPER_APP_MARKER)); } - private static boolean canPromoteBasedOnName(EntitySpec<?> spec) { - if (!Strings.isEmpty(spec.getDisplayName())) { - if (spec.getChildren().size()==1) { - String childName = Iterables.getOnlyElement(spec.getChildren()).getDisplayName(); - if (Strings.isEmpty(childName) || childName.equals(spec.getDisplayName())) { - // if child has no name, or it's the same, could still promote - return true; - } else { - return false; - } - } else { - // if name set at root and promoting children would be ambiguous, do not promote - return false; - } - } else if (spec.getChildren().size()>1) { - // don't allow multiple children if a name is specified as a root - return false; - } else { - return true; - } + private static boolean hasSingleChild(EntitySpec<?> spec) { + return spec.getChildren().size() == 1; } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94263c60/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AppYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AppYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AppYamlTest.java index c4e1d7d..c462889 100644 --- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AppYamlTest.java +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AppYamlTest.java @@ -19,10 +19,13 @@ package org.apache.brooklyn.camp.brooklyn; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import java.io.StringReader; +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.core.mgmt.EntityManagementUtils; import org.apache.brooklyn.core.test.entity.TestApplication; import org.apache.brooklyn.core.test.entity.TestEntity; import org.apache.brooklyn.entity.stock.BasicApplication; @@ -66,10 +69,10 @@ public class AppYamlTest extends AbstractYamlTest { "- serviceType: org.apache.brooklyn.core.test.entity.TestApplication", " name: myEntityName"); - BasicApplication app = (BasicApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - TestApplication entity = (TestApplication) Iterables.getOnlyElement(app.getChildren()); + Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); + assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); assertEquals(app.getDisplayName(), "myTopLevelName"); - assertEquals(entity.getDisplayName(), "myEntityName"); + assertEquals(app.getChildren().size(), 0); } @Test http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94263c60/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ApplicationsYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ApplicationsYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ApplicationsYamlTest.java index 01d90f5..b58e5ca 100644 --- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ApplicationsYamlTest.java +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ApplicationsYamlTest.java @@ -22,8 +22,14 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; +import org.apache.brooklyn.api.entity.Application; import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.camp.brooklyn.TestSensorAndEffectorInitializer.TestConfigurableInitializer; import org.apache.brooklyn.core.mgmt.EntityManagementUtils; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.core.test.policy.TestEnricher; +import org.apache.brooklyn.core.test.policy.TestPolicy; import org.apache.brooklyn.entity.stock.BasicApplication; import org.apache.brooklyn.entity.stock.BasicEntity; import org.slf4j.Logger; @@ -36,24 +42,31 @@ import com.google.common.collect.Iterables; public class ApplicationsYamlTest extends AbstractYamlTest { private static final Logger log = LoggerFactory.getLogger(ApplicationsYamlTest.class); + @Override + protected LocalManagementContext newTestManagementContext() { + // Don't need osgi + return LocalManagementContextForTests.newInstance(); + } + @Test public void testWrapsEntity() throws Exception { Entity app = createAndStartApplication( "services:", "- type: " + BasicEntity.class.getName()); - assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); - assertTrue(app instanceof BasicApplication); - assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicEntity); + assertWrapped(app, BasicEntity.class); } @Test - public void testDoesNotWrapApp() throws Exception { + public void testWrapsMultipleApps() throws Exception { Entity app = createAndStartApplication( "services:", + "- type: " + BasicApplication.class.getName(), "- type: " + BasicApplication.class.getName()); - assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); + assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); assertTrue(app instanceof BasicApplication); - assertTrue(app.getChildren().isEmpty()); + assertEquals(app.getChildren().size(), 2); + } + } @Test @@ -62,10 +75,15 @@ public class ApplicationsYamlTest extends AbstractYamlTest { "wrappedApp: true", "services:", "- type: " + BasicApplication.class.getName()); - assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); - assertTrue(app instanceof BasicApplication); - assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicApplication); - assertTrue(Iterables.getOnlyElement(app.getChildren()).getChildren().isEmpty()); + assertWrapped(app, BasicApplication.class); + } + + @Test + public void testDoesNotWrapApp() throws Exception { + Entity app = createAndStartApplication( + "services:", + "- type: " + BasicApplication.class.getName()); + assertDoesNotWrap(app, BasicApplication.class, null); } @Test @@ -74,41 +92,134 @@ public class ApplicationsYamlTest extends AbstractYamlTest { "wrappedApp: false", "services:", "- type: " + BasicApplication.class.getName()); - assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); - assertTrue(app instanceof BasicApplication); - assertTrue(app.getChildren().isEmpty()); + assertDoesNotWrap(app, BasicApplication.class, null); } @Test - public void testWrapsEntityIfDifferentTopLevelName() throws Exception { + public void testDoesNotWrapEntityIfDifferentTopLevelName() throws Exception { Entity app = createAndStartApplication( "name: topLevel", "services:", "- type: " + BasicApplication.class.getName(), " name: bottomLevel"); - assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); - assertTrue(app instanceof BasicApplication); - assertEquals(app.getDisplayName(), "topLevel"); - assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicApplication); - assertTrue(Iterables.getOnlyElement(app.getChildren()).getChildren().isEmpty()); - assertEquals(Iterables.getOnlyElement(app.getChildren()).getDisplayName(), "bottomLevel"); + assertDoesNotWrap(app, BasicApplication.class, "topLevel"); } - + @Test public void testDoesNotWrapsEntityIfNoNameOnService() throws Exception { Entity app = createAndStartApplication( "name: topLevel", "services:", "- type: " + BasicApplication.class.getName()); - assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); - assertTrue(app instanceof BasicApplication); - assertTrue(app.getChildren().isEmpty()); - assertEquals(app.getDisplayName(), "topLevel"); + assertDoesNotWrap(app, BasicApplication.class, "topLevel"); } - + + @Test + public void testDoesNotWrapCatalogItemWithDisplayName() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: simple", + " version: " + TEST_VERSION, + " displayName: catalogLevel", + " item:", + " services:", + " - type: " + BasicApplication.class.getName()); + Entity app = createAndStartApplication( + "name: topLevel", + "services:", + "- type: simple:" + TEST_VERSION); + assertDoesNotWrap(app, BasicApplication.class, "topLevel"); + } + + @Test + public void testDoesNotWrapCatalogItemWithServiceName() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: simple", + " version: " + TEST_VERSION, + " displayName: catalogLevel", + " item:", + " services:", + " - type: " + BasicApplication.class.getName(), + " defaultDisplayName: defaultServiceName", + " displayName: explicitServiceName"); + Entity app = createAndStartApplication( + "name: topLevel", + "services:", + "- type: simple:" + TEST_VERSION); + assertDoesNotWrap(app, BasicApplication.class, "topLevel"); + } + + @Test + public void testDoesNotWrapCatalogItemAndOverridesName() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: simple", + " version: " + TEST_VERSION, + " displayName: catalogLevel", + " item:", + " services:", + " - type: " + BasicApplication.class.getName()); + Entity app = createAndStartApplication( + "services:", + "- type: simple:" + TEST_VERSION, + " name: serviceLevel"); + assertDoesNotWrap(app, BasicApplication.class, "serviceLevel"); + } + + @Test + public void testDoesNotWrapCatalogItemAndUsesCatalogName() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: simple", + " version: " + TEST_VERSION, + " displayName: catalogLevel", + " item:", + " services:", + " - type: " + BasicApplication.class.getName()); + Entity app = createAndStartApplication( + "services:", + "- type: simple:" + TEST_VERSION); + assertDoesNotWrap(app, BasicApplication.class, "catalogLevel"); + } + + @Test + public void testDoesNotWrapCatalogItemAndUsesCatalogServiceName() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " id: simple", + " version: " + TEST_VERSION, + " displayName: catalogLevel", + " item:", + " services:", + " - type: " + BasicApplication.class.getName(), + " name: catalogServiceLevel"); + Entity app = createAndStartApplication( + "services:", + "- type: simple:" + TEST_VERSION); + assertDoesNotWrap(app, BasicApplication.class, "catalogServiceLevel"); + } + @Override protected Logger getLogger() { return log; } + private void assertWrapped(Entity app, Class<? extends Entity> wrappedEntityType) { + assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); + assertTrue(app instanceof BasicApplication); + Entity child = Iterables.getOnlyElement(app.getChildren()); + assertTrue(wrappedEntityType.isInstance(child)); + assertTrue(child.getChildren().isEmpty()); + } + + private void assertDoesNotWrap(Entity app, Class<? extends Application> entityType, String displayName) { + assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); + assertTrue(entityType.isInstance(app)); + if (displayName != null) { + assertEquals(app.getDisplayName(), displayName); + } + assertEquals(app.getChildren().size(), 0); + } + }
