Repository: brooklyn-server Updated Branches: refs/heads/master e79e0bb2e -> ac1b90802
Move DEFAULT_DISPLAY_NAME from app to entity Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/9b951e0e Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/9b951e0e Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/9b951e0e Branch: refs/heads/master Commit: 9b951e0eae8b3db1d2bcd331aab8ef223589b2ce Parents: d2fd128 Author: Aled Sage <[email protected]> Authored: Tue Jun 14 18:16:42 2016 +0100 Committer: Aled Sage <[email protected]> Committed: Wed Jun 15 10:17:45 2016 +0100 ---------------------------------------------------------------------- .../camp/brooklyn/EntityNameYamlTest.java | 90 +++++++++++++++++++ .../core/entity/AbstractApplication.java | 27 +++--- .../brooklyn/core/entity/AbstractEntity.java | 9 ++ .../entity/AbstractApplicationLegacyTest.java | 20 ----- .../brooklyn/core/entity/EntityNameTest.java | 92 ++++++++++++++++++++ 5 files changed, 201 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9b951e0e/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityNameYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityNameYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityNameYamlTest.java new file mode 100644 index 0000000..d0c8f4d --- /dev/null +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EntityNameYamlTest.java @@ -0,0 +1,90 @@ +/* + * 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.core.entity.Entities; +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.base.Joiner; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; + +@Test +public class EntityNameYamlTest extends AbstractYamlTest { + private static final Logger log = LoggerFactory.getLogger(EntityNameYamlTest.class); + + @Test + public void testExplicitDisplayName() throws Exception { + String yaml = Joiner.on("\n").join( + "services:", + "- type: org.apache.brooklyn.core.test.entity.TestEntity", + " name: myDisplayName"); + deployAndAssertDisplayName(yaml, "myDisplayName"); + } + + @Test + public void testExplicitDefaultDisplayName() throws Exception { + String yaml = Joiner.on("\n").join( + "services:", + "- type: org.apache.brooklyn.core.test.entity.TestEntity", + " brooklyn.config:", + " defaultDisplayName: myDefaultName"); + deployAndAssertDisplayName(yaml, "myDefaultName"); + } + + @Test + public void testExplicitDefaultDisplayNameReferrencingConfig() throws Exception { + String yaml = Joiner.on("\n").join( + "services:", + "- type: org.apache.brooklyn.core.test.entity.TestEntity", + " brooklyn.config:", + " myconf: myval", + " defaultDisplayName:", + " $brooklyn:formatString:", + " - \"PREFIX%sSUFFIX\"", + " - $brooklyn:config(\"myconf\")"); + deployAndAssertDisplayName(yaml, "PREFIXmyvalSUFFIX"); + } + + @Test + public void testBrooklynConfig() throws Exception { + String yaml = Joiner.on("\n").join( + "services:", + "- type: org.apache.brooklyn.core.test.entity.TestEntity", + " name: myDisplayName"); + deployAndAssertDisplayName(yaml, "myDisplayName"); + } + + protected void deployAndAssertDisplayName(String yaml, String expectedName) throws Exception { + Entity app = createAndStartApplication(yaml); + Entity entity = Iterables.getOnlyElement(Entities.descendants(app, Predicates.instanceOf(TestEntity.class))); + assertEquals(entity.getDisplayName(), expectedName); + } + + @Override + protected Logger getLogger() { + return log; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9b951e0e/core/src/main/java/org/apache/brooklyn/core/entity/AbstractApplication.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractApplication.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractApplication.java index db2cd9f..70e07e4 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractApplication.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractApplication.java @@ -21,16 +21,9 @@ package org.apache.brooklyn.core.entity; import java.util.Collection; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.ImmutableSet; - import org.apache.brooklyn.api.entity.Application; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.ServiceProblemsLogic; @@ -38,8 +31,11 @@ import org.apache.brooklyn.core.entity.trait.StartableMethods; import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.exceptions.RuntimeInterruptedException; -import org.apache.brooklyn.util.text.Strings; import org.apache.brooklyn.util.time.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableSet; /** * Users can extend this to define the entities in their application, and the relationships between @@ -47,18 +43,18 @@ import org.apache.brooklyn.util.time.Time; * their entities. */ public abstract class AbstractApplication extends AbstractEntity implements StartableApplication { - - private static final Logger log = LoggerFactory.getLogger(AbstractApplication.class); - - /** - * The default name to use for this app, if not explicitly overridden by the top-level app. + + /* + * Note that DEFAULT_DISPLAY_NAME is particularly important for apps. + * It gives the default name to use for this app, if not explicitly overridden by the top-level app. * Necessary to avoid the app being wrapped in another layer of "BasicApplication" on deployment. * Previously, the catalog item gave an explicit name (rathe rthan this defaultDisplayName), which * meant that if the user chose a different name then AMP would automatically wrap this app so * that both names would be presented. */ - public static final ConfigKey<String> DEFAULT_DISPLAY_NAME = ConfigKeys.newStringConfigKey("defaultDisplayName"); + private static final Logger log = LoggerFactory.getLogger(AbstractApplication.class); + private volatile Application application; public AbstractApplication() { @@ -66,9 +62,6 @@ public abstract class AbstractApplication extends AbstractEntity implements Star public void init() { super.init(); - if (Strings.isNonBlank(getConfig(DEFAULT_DISPLAY_NAME))) { - setDefaultDisplayName(getConfig(DEFAULT_DISPLAY_NAME)); - } initApp(); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9b951e0e/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java index 6440583..e38343e 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java @@ -59,6 +59,7 @@ import org.apache.brooklyn.core.BrooklynFeatureEnablement; import org.apache.brooklyn.core.BrooklynLogging; import org.apache.brooklyn.core.catalog.internal.CatalogUtils; import org.apache.brooklyn.core.config.ConfigConstraints; +import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.config.render.RendererHints; import org.apache.brooklyn.core.enricher.AbstractEnricher; import org.apache.brooklyn.core.entity.internal.EntityConfigMap; @@ -149,6 +150,11 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E static { BrooklynInitialization.initAll(); } + /** + * The default name to use for this entity, if not explicitly overridden. + */ + public static final ConfigKey<String> DEFAULT_DISPLAY_NAME = ConfigKeys.newStringConfigKey("defaultDisplayName"); + public static final BasicNotificationSensor<Location> LOCATION_ADDED = new BasicNotificationSensor<Location>( Location.class, "entity.location.added", "Location dynamically added to entity"); public static final BasicNotificationSensor<Location> LOCATION_REMOVED = new BasicNotificationSensor<Location>( @@ -1611,6 +1617,9 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E public void init() { super.init(); initEnrichers(); + if (Strings.isNonBlank(getConfig(DEFAULT_DISPLAY_NAME))) { + setDefaultDisplayName(getConfig(DEFAULT_DISPLAY_NAME)); + } } /** http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9b951e0e/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java index 647e102..cabff36 100644 --- a/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java @@ -26,7 +26,6 @@ import java.util.List; import org.apache.brooklyn.api.entity.EntitySpec; import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; import org.apache.brooklyn.core.location.SimulatedLocation; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.core.test.entity.TestApplication; @@ -137,23 +136,4 @@ public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport { app.stop(); assertEquals(child.getCallHistory(), ImmutableList.of()); } - - @Test - public void testAppUsesDefaultDisplayName() { - EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) - .configure(AbstractApplication.DEFAULT_DISPLAY_NAME, "myDefaultName"); - TestApplication app2 = ApplicationBuilder.newManagedApp(appSpec, mgmt); - - assertEquals(app2.getDisplayName(), "myDefaultName"); - } - - @Test - public void testAppUsesDisplayNameOverDefaultName() { - EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) - .displayName("myName") - .configure(AbstractApplication.DEFAULT_DISPLAY_NAME, "myDefaultName"); - TestApplication app2 = ApplicationBuilder.newManagedApp(appSpec, mgmt); - - assertEquals(app2.getDisplayName(), "myName"); - } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9b951e0e/core/src/test/java/org/apache/brooklyn/core/entity/EntityNameTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityNameTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityNameTest.java new file mode 100644 index 0000000..444370d --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityNameTest.java @@ -0,0 +1,92 @@ +/* + * 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.entity; + +import static org.testng.Assert.assertEquals; + +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.util.core.task.DeferredSupplier; +import org.testng.annotations.Test; + +public class EntityNameTest extends BrooklynAppUnitTestSupport { + + @Test + public void testDisplayNameWhenNothingSupplied() { + TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class)); + assertEquals(entity.getDisplayName(), "TestEntity:"+entity.getId().substring(0, 4)); + } + + @Test + public void testExplicitDisplayName() { + TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) + .displayName("myDisplayName")); + assertEquals(entity.getDisplayName(), "myDisplayName"); + } + + @Test + public void testExplicitDefaultDisplayName() { + TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) + .configure(AbstractEntity.DEFAULT_DISPLAY_NAME, "myDefaultName")); + assertEquals(entity.getDisplayName(), "myDefaultName"); + } + + @Test + public void testExplicitDefaultDisplayNameOverriddenByRealName() { + TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) + .configure(AbstractEntity.DEFAULT_DISPLAY_NAME, "myDefaultName") + .displayName("myDisplayName")); + assertEquals(entity.getDisplayName(), "myDisplayName"); + } + + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void testDefaultDisplayNameUsesDeferredSupplier() { + TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) + .configure((ConfigKey)AbstractEntity.DEFAULT_DISPLAY_NAME, new DeferredSupplier<String>() { + @Override public String get() { + return "myDefaultName"; + }})); + assertEquals(entity.getDisplayName(), "myDefaultName"); + } + + + @Test + public void testAppUsesDefaultDisplayName() { + EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) + .configure(AbstractApplication.DEFAULT_DISPLAY_NAME, "myDefaultName"); + TestApplication app2 = ApplicationBuilder.newManagedApp(appSpec, mgmt); + + assertEquals(app2.getDisplayName(), "myDefaultName"); + } + + @Test + public void testAppUsesDisplayNameOverDefaultName() { + EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) + .displayName("myName") + .configure(AbstractApplication.DEFAULT_DISPLAY_NAME, "myDefaultName"); + TestApplication app2 = ApplicationBuilder.newManagedApp(appSpec, mgmt); + + assertEquals(app2.getDisplayName(), "myName"); + } +}
