brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [2/5] incubator-brooklyn git commit: Adds AbstractApplication.DEFAULT_DISPLAY_NAME config
Date Mon, 29 Jun 2015 10:13:36 GMT
Adds AbstractApplication.DEFAULT_DISPLAY_NAME config

- Allows a default display name to be specified for an app, which
  can then be overridden within the yaml at the very top level 
  (without causing the app to be wrapped in another app!). If not
  overridden, this default will be used.
- Adds AppYamlTest, to test different scenarios of the app either
  being wrapped or not wrapped.
- Also tidies up AbstractApplicationLegacyTest.

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/3cccf00f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/3cccf00f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/3cccf00f

Branch: refs/heads/master
Commit: 3cccf00f75def8bbc2e00c3ec16d7a7d5cb539db
Parents: a390594
Author: Aled Sage <aled.sage@gmail.com>
Authored: Sat Jun 27 11:53:04 2015 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Sat Jun 27 11:53:04 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/AbstractApplication.java       |  14 +++
 .../basic/AbstractApplicationLegacyTest.java    |  75 ++++++++----
 .../io/brooklyn/camp/brooklyn/AppYamlTest.java  | 119 +++++++++++++++++++
 3 files changed, 186 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3cccf00f/core/src/main/java/brooklyn/entity/basic/AbstractApplication.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/AbstractApplication.java b/core/src/main/java/brooklyn/entity/basic/AbstractApplication.java
index ce038e2..f796645 100644
--- a/core/src/main/java/brooklyn/entity/basic/AbstractApplication.java
+++ b/core/src/main/java/brooklyn/entity/basic/AbstractApplication.java
@@ -25,6 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import brooklyn.config.BrooklynProperties;
+import brooklyn.config.ConfigKey;
 import brooklyn.entity.Application;
 import brooklyn.entity.Entity;
 import brooklyn.entity.basic.ServiceStateLogic.ServiceProblemsLogic;
@@ -35,6 +36,7 @@ import brooklyn.management.internal.ManagementContextInternal;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.exceptions.RuntimeInterruptedException;
 import brooklyn.util.flags.SetFromFlag;
+import brooklyn.util.text.Strings;
 import brooklyn.util.time.Time;
 
 /**
@@ -49,6 +51,15 @@ public abstract class AbstractApplication extends AbstractEntity implements
Star
     @Deprecated
     public 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.
+     * 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");
+
     @SetFromFlag("mgmt")
     private volatile ManagementContext mgmt;
     
@@ -63,6 +74,9 @@ 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/incubator-brooklyn/blob/3cccf00f/core/src/test/java/brooklyn/entity/basic/AbstractApplicationLegacyTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/AbstractApplicationLegacyTest.java b/core/src/test/java/brooklyn/entity/basic/AbstractApplicationLegacyTest.java
index d07b3eb..cc0e7d7 100644
--- a/core/src/test/java/brooklyn/entity/basic/AbstractApplicationLegacyTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/AbstractApplicationLegacyTest.java
@@ -19,21 +19,24 @@
 package brooklyn.entity.basic;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 
 import java.util.List;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
 import brooklyn.entity.BrooklynAppUnitTestSupport;
 import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.location.LocationSpec;
 import brooklyn.location.basic.SimulatedLocation;
 import brooklyn.test.entity.TestApplication;
 import brooklyn.test.entity.TestEntity;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-
 /**
  * Tests the deprecated use of AbstractAppliation, where its constructor is called directly.
  * 
@@ -41,60 +44,69 @@ import com.google.common.collect.ImmutableMap;
  */
 public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport {
 
+    private SimulatedLocation loc;
     private List<SimulatedLocation> locs;
     
     @BeforeMethod(alwaysRun=true)
     @Override
     public void setUp() throws Exception {
         super.setUp();
-        locs = ImmutableList.of(new SimulatedLocation());
+        loc = mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
+        locs = ImmutableList.of(loc);
     }
     
     // App and its children will be implicitly managed on first effector call on app
     @Test
-    public void testStartAndStopCallsChildren() throws Exception {
+    public void testStartAndStopUnmanagedAppAutomanagesTheAppAndChildren() throws Exception
{
         // deliberately unmanaged
         TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class));
         TestEntity child = app2.addChild(EntitySpec.create(TestEntity.class));
+        assertFalse(Entities.isManaged(app2));
+        assertFalse(Entities.isManaged(child));
         
         app2.invoke(AbstractApplication.START, ImmutableMap.of("locations", locs)).get();
-        assertEquals(child.getCount(), 1);
-        assertEquals(app.getManagementContext().getEntityManager().getEntity(app.getId()),
app);
-        assertEquals(app.getManagementContext().getEntityManager().getEntity(child.getId()),
child);
+        assertTrue(Entities.isManaged(app2));
+        assertTrue(Entities.isManaged(child));
+        assertEquals(child.getCallHistory(), ImmutableList.of("start"));
+        assertEquals(mgmt.getEntityManager().getEntity(app2.getId()), app2);
+        assertEquals(mgmt.getEntityManager().getEntity(child.getId()), child);
         
         app2.stop();
-        assertEquals(child.getCount(), 0);
+        assertEquals(child.getCallHistory(), ImmutableList.of("start", "stop"));
+        assertFalse(Entities.isManaged(child));
+        assertFalse(Entities.isManaged(app2));
     }
     
     @Test
     public void testStartAndStopWhenManagedCallsChildren() {
         TestEntity child = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-
-        assertEquals(app.getManagementContext().getEntityManager().getEntity(app.getId()),
app);
-        assertEquals(app.getManagementContext().getEntityManager().getEntity(child.getId()),
child);
+        assertTrue(Entities.isManaged(app));
+        assertTrue(Entities.isManaged(child));
 
         app.start(locs);
-        assertEquals(child.getCount(), 1);
+        assertEquals(child.getCallHistory(), ImmutableList.of("start"));
         
         app.stop();
-        assertEquals(child.getCount(), 0);
+        assertEquals(child.getCallHistory(), ImmutableList.of("start", "stop"));
+        assertFalse(Entities.isManaged(child));
+        assertFalse(Entities.isManaged(app));
     }
     
     @Test
-    public void testStartDoesNotStartPremanagedChildren() {
+    public void testStartOnManagedAppDoesNotStartPremanagedChildren() {
         TestEntity child = app.addChild(EntitySpec.create(TestEntity.class));
         
         app.start(locs);
-        assertEquals(child.getCount(), 0);
+        assertEquals(child.getCallHistory(), ImmutableList.of());
     }
     
     @Test
-    public void testStartDoesNotStartUnmanagedChildren() {
+    public void testStartOnManagedAppDoesNotStartUnmanagedChildren() {
         TestEntity child = app.createAndManageChild(EntitySpec.create(TestEntity.class));
         Entities.unmanage(child);
         
         app.start(locs);
-        assertEquals(child.getCount(), 0);
+        assertEquals(child.getCallHistory(), ImmutableList.of());
     }
     
     @Test
@@ -102,21 +114,40 @@ public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport
{
         TestEntity child = app.createAndManageChild(EntitySpec.create(TestEntity.class));
         
         app.start(locs);
-        assertEquals(child.getCount(), 1);
+        assertEquals(child.getCallHistory(), ImmutableList.of("start"));
         
         Entities.unmanage(child);
         
         app.stop();
-        assertEquals(child.getCount(), 1);
+        assertEquals(child.getCallHistory(), ImmutableList.of("start"));
     }
     
     @Test
-    public void testStopDoesNotStopPremanagedChildren() {
+    public void testStopOnManagedAppDoesNotStopPremanagedChildren() {
         app.start(locs);
         
         TestEntity child = app.addChild(EntitySpec.create(TestEntity.class));
         
         app.stop();
-        assertEquals(child.getCount(), 0);
+        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/incubator-brooklyn/blob/3cccf00f/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java
new file mode 100644
index 0000000..26784ff
--- /dev/null
+++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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 io.brooklyn.camp.brooklyn;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.StringReader;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+import com.google.api.client.repackaged.com.google.common.base.Joiner;
+import com.google.common.collect.Iterables;
+
+import brooklyn.entity.basic.BasicApplication;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
+
+@Test
+public class AppYamlTest extends AbstractYamlTest {
+    private static final Logger log = LoggerFactory.getLogger(AppYamlTest.class);
+
+    @Test
+    public void testAutoWrapsEntityInApp() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestEntity");
+        
+        BasicApplication app = (BasicApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        @SuppressWarnings("unused")
+        TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren());
+    }
+    
+    @Test
+    public void testDoesNotAutoWrapApp() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestApplication");
+        
+        TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        assertTrue(app.getChildren().isEmpty());
+    }
+    
+    @Test
+    public void testWrapsAppIfNameAtTopLevelAndOnApp() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "name: myTopLevelName",
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestApplication",
+                "  name: myEntityName");
+        
+        BasicApplication app = (BasicApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        TestApplication entity = (TestApplication) Iterables.getOnlyElement(app.getChildren());
+        assertEquals(app.getDisplayName(), "myTopLevelName");
+        assertEquals(entity.getDisplayName(), "myEntityName");
+    }
+    
+    @Test
+    public void testDoesNotWrapAppIfNoConflictingNameOnApp() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "name: myTopLevelName",
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestApplication");
+        
+        TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        assertTrue(app.getChildren().isEmpty());
+        assertEquals(app.getDisplayName(), "myTopLevelName");
+    }
+    
+    @Test
+    public void testDoesNotWrapAppWithDefaultDisplayName() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "name: myTopLevelName",
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestApplication",
+                "  brooklyn.config:",
+                "    defaultDisplayName: myDefaultEntityName");
+        
+        TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        assertTrue(app.getChildren().isEmpty());
+        assertEquals(app.getDisplayName(), "myTopLevelName");
+    }
+    
+    @Test
+    public void testUsesDefaultDisplayNameIfNoOther() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- serviceType: brooklyn.test.entity.TestApplication",
+                "  brooklyn.config:",
+                "    defaultDisplayName: myDefaultEntityName");
+        
+        TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml));
+        assertTrue(app.getChildren().isEmpty());
+        assertEquals(app.getDisplayName(), "myDefaultEntityName");
+    }
+    
+    @Override
+    protected Logger getLogger() {
+        return log;
+    }
+}


Mime
View raw message