Repository: brooklyn-server
Updated Branches:
  refs/heads/master a941963c9 -> 586795b32


Added identity method to DSL


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2c80d164
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2c80d164
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2c80d164

Branch: refs/heads/master
Commit: 2c80d164eefe605c07f14b51d763e310a6314b3f
Parents: a941963
Author: Andrew Donald Kennedy <andrew.kenn...@cloudsoftcorp.com>
Authored: Wed Jun 8 18:18:47 2016 +0100
Committer: Andrew Donald Kennedy <andrew.kenn...@cloudsoftcorp.com>
Committed: Sat Jun 25 23:36:56 2016 +0100

----------------------------------------------------------------------
 .../spi/dsl/methods/BrooklynDslCommon.java      |  4 +
 .../brooklyn/spi/dsl/methods/DslComponent.java  | 41 +++++++++-
 .../camp/brooklyn/IdentityYamlTest.java         | 82 ++++++++++++++++++++
 .../test/resources/test-entity-identity.yaml    | 30 +++++++
 4 files changed, 154 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c80d164/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..07c127e 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
@@ -112,6 +112,10 @@ public class BrooklynDslCommon {
         return new DslComponent(Scope.THIS, "").attributeWhenReady(sensorName);
     }
 
+    public static BrooklynDslDeferredSupplier<?> identity() {
+        return new DslComponent(Scope.THIS, "").identity();
+    }
+
     /** Returns a {@link Sensor}, looking up the sensor on the context if 
available and using that,
      * or else defining an untyped (Object) sensor */
     public static BrooklynDslDeferredSupplier<Sensor<?>> sensor(String 
sensorName) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c80d164/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..c43f3bb 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
@@ -46,6 +46,7 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.Callables;
 
 public class DslComponent extends BrooklynDslDeferredSupplier<Entity> {
 
@@ -184,19 +185,53 @@ public class DslComponent extends 
BrooklynDslDeferredSupplier<Entity> {
     }
 
     // DSL words which return things
-    
+
+    public BrooklynDslDeferredSupplier<?> identity() {
+        return new Identity(this);
+    }
+    protected static class Identity extends 
BrooklynDslDeferredSupplier<Object> {
+        private static final long serialVersionUID = -1L;
+        private final DslComponent component;
+        public Identity(DslComponent component) {
+            this.component = Preconditions.checkNotNull(component);
+        }
+
+        @Override
+        public Task<Object> newTask() {
+            Entity targetEntity = component.get();
+            return Tasks.create("identity", 
Callables.<Object>returning(targetEntity.getId()));
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hashCode(component);
+        }
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (obj == null || getClass() != obj.getClass()) return false;
+            Identity that = Identity.class.cast(obj);
+            return Objects.equal(this.component, that.component);
+        }
+        @Override
+        public String toString() {
+            return (component.scope==Scope.THIS ? "" : 
component.toString()+".") + "identity()";
+        }
+    }
+
     public BrooklynDslDeferredSupplier<?> attributeWhenReady(final String 
sensorName) {
         return new AttributeWhenReady(this, sensorName);
     }
-    // class simply makes the memento XML files nicer
     protected static class AttributeWhenReady extends 
BrooklynDslDeferredSupplier<Object> {
         private static final long serialVersionUID = 1740899524088902383L;
         private final DslComponent component;
         private final String sensorName;
+
         public AttributeWhenReady(DslComponent component, String sensorName) {
             this.component = Preconditions.checkNotNull(component);
             this.sensorName = sensorName;
         }
+
         @SuppressWarnings("unchecked")
         @Override
         public Task<Object> newTask() {
@@ -207,11 +242,11 @@ public class DslComponent extends 
BrooklynDslDeferredSupplier<Entity> {
             }
             return (Task<Object>) 
DependentConfiguration.attributeWhenReady(targetEntity, 
(AttributeSensor<?>)targetSensor);
         }
+
         @Override
         public int hashCode() {
             return Objects.hashCode(component, sensorName);
         }
-
         @Override
         public boolean equals(Object obj) {
             if (this == obj) return true;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c80d164/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/IdentityYamlTest.java
----------------------------------------------------------------------
diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/IdentityYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/IdentityYamlTest.java
new file mode 100644
index 0000000..c059e68
--- /dev/null
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/IdentityYamlTest.java
@@ -0,0 +1,82 @@
+/*
+uniqueSshConnection * 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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.Iterables;
+
+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.test.entity.TestEntity;
+import org.apache.brooklyn.util.text.StringPredicates;
+
+@Test
+public class IdentityYamlTest extends AbstractYamlTest {
+    private static final Logger log = 
LoggerFactory.getLogger(IdentityYamlTest.class);
+
+    private static final ConfigKey<String> TEST_ENTITY_ONE_ID = 
ConfigKeys.newStringConfigKey("testentityone.id");
+    private static final ConfigKey<String> TEST_ENTITY_TWO_ID = 
ConfigKeys.newStringConfigKey("testentitytwo.id");
+
+    protected Iterable<? extends Entity> 
setupAndCheckTestEntityInBasicYamlWith() throws Exception {
+        Entity app = 
createAndStartApplication(loadYaml("test-entity-identity.yaml"));
+        waitForApplicationTasks(app);
+
+        Assert.assertEquals(app.getDisplayName(), "test-entity-identity");
+
+        log.info("App started:");
+        Entities.dumpInfo(app);
+
+        Assert.assertEquals(Iterables.size(app.getChildren()), 2, "Expected 
app to have child entity");
+        Iterable<? extends Entity> testEntities = 
Iterables.filter(app.getChildren(), TestEntity.class);
+        Assert.assertEquals(Iterables.size(testEntities), 2, "Expected app to 
have two test entities");
+
+        return testEntities;
+    }
+
+    @Test
+    public void testYamlParsing() throws Exception {
+        setupAndCheckTestEntityInBasicYamlWith();
+    }
+
+    @Test
+    public void testBrooklynIdentityFunction() throws Exception {
+        Iterable<? extends Entity> testEntities = 
setupAndCheckTestEntityInBasicYamlWith();
+        Entity testEntityOne = Iterables.find(testEntities, 
EntityPredicates.displayNameSatisfies(StringPredicates.containsLiteral("One")));
+        Entity testEntityTwo = Iterables.find(testEntities, 
EntityPredicates.displayNameSatisfies(StringPredicates.containsLiteral("Two")));
+
+        Assert.assertNotNull(testEntityOne, "Test entity one should be 
present");
+        Assert.assertNotNull(testEntityTwo, "Test entity two should be 
present");
+
+        Assert.assertEquals(testEntityOne.config().get(TEST_ENTITY_ONE_ID), 
testEntityOne.getId(), "Entity one IDs should match");
+        Assert.assertEquals(testEntityOne.config().get(TEST_ENTITY_TWO_ID), 
testEntityTwo.getId(), "Entity two IDs should match");
+    }
+
+    @Override
+    protected Logger getLogger() {
+        return log;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c80d164/camp/camp-brooklyn/src/test/resources/test-entity-identity.yaml
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/resources/test-entity-identity.yaml 
b/camp/camp-brooklyn/src/test/resources/test-entity-identity.yaml
new file mode 100644
index 0000000..d238ec8
--- /dev/null
+++ b/camp/camp-brooklyn/src/test/resources/test-entity-identity.yaml
@@ -0,0 +1,30 @@
+# 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.
+#
+name: test-entity-identity
+description: TestEntity with references to other entity ids
+origin: https://github.com/apache/brooklyn
+services:
+  - type: org.apache.brooklyn.core.test.entity.TestEntity
+    id: testentityone
+    name: "Test Entity One"
+    brooklyn.config:
+      testentityone.id: $brooklyn:identity()
+      testentitytwo.id: $brooklyn:entity("testentitytwo").identity()
+  - type: org.apache.brooklyn.core.test.entity.TestEntity
+    id: testentitytwo
+    name: "test Entity Two"

Reply via email to