Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/482#discussion_r92156784
--- Diff:
camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java
---
@@ -0,0 +1,655 @@
+/*
+ * Copyright 2016 The Apache Software Foundation.
+ *
+ * Licensed 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.spi.dsl;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest;
+import
org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.DslTestCallable;
+import
org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.DslTestSupplierWrapper;
+import
org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.TestDslSupplier;
+import
org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.TestDslSupplierValue;
+import
org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.custom.UserSuppliedPackageType;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.entity.EntityInternal;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.entity.stock.BasicApplication;
+import org.apache.brooklyn.entity.stock.BasicEntity;
+import org.apache.brooklyn.test.Asserts;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+
+// Doesn't test executing the DSL from different contexts (i.e. fetching
the config from children inheriting it)
+public class DslYamlTest extends AbstractYamlTest {
+ private static final ConfigKey<Object> DEST =
ConfigKeys.newConfigKey(Object.class, "dest");
+ private static final ConfigKey<Object> DEST2 =
ConfigKeys.newConfigKey(Object.class, "dest2");
+ private static final ConfigKey<Object> DEST3 =
ConfigKeys.newConfigKey(Object.class, "dest3");
+
+ // See also test-referencing-entities.yaml
+
+ // No tests for entitySpec, object, formatString, external - relying
on extensive tests elsewhere
+
+ @Test
+ public void testDslSelf() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:self()");
+ assertEquals(getConfigEventually(app, DEST), app);
+ }
+
+ @Test
+ public void testDslEntity() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:entity(\"child\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: child");
+ assertEquals(getConfigEventually(app, DEST),
Iterables.getOnlyElement(app.getChildren()));
+ }
+
+ @Test
+ public void testDslParent() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:parent()");
+ final Entity child = Iterables.getOnlyElement(app.getChildren());
+ assertEquals(getConfigEventually(child, DEST), app);
+ }
+
+ @Test
+ public void testDslChild() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:child(\"child\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: child",
+ " - type: " + BasicEntity.class.getName(),
+ " id: another-child");
+ assertEquals(getConfigEventually(app, DEST),
app.getChildren().iterator().next());
+ }
+
+ @Test
+ public void testDslSibling() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: child",
+ " brooklyn.config:",
+ " dest: $brooklyn:sibling(\"another-child\")",
+ " - type: " + BasicEntity.class.getName(),
+ " id: another-child");
+ final Entity child1 = Iterables.get(app.getChildren(), 0);
+ final Entity child2 = Iterables.get(app.getChildren(), 1);
+ assertEquals(getConfigEventually(child1, DEST), child2);
+ }
+
+ @Test
+ public void testDslDescendant() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " id: self",
+ " brooklyn.config:",
+ " dest: $brooklyn:descendant(\"child\")",
+ " dest2: $brooklyn:descendant(\"grand-child\")",
+ " dest3: $brooklyn:descendant(\"self\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: child",
+ " - type: " + BasicEntity.class.getName(),
+ " id: another-child",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: grand-child");
+ final Entity child1 = Iterables.get(app.getChildren(), 0);
+ final Entity child2 = Iterables.get(app.getChildren(), 1);
+ final Entity grandChild =
Iterables.getOnlyElement(child2.getChildren());
+ assertEquals(getConfigEventually(app, DEST), child1);
+ assertEquals(getConfigEventually(app, DEST2), grandChild);
+ try {
+ assertEquals(getConfigEventually(app, DEST3), app);
+ Asserts.shouldHaveFailedPreviously("Self not in descendant
scope");
+ } catch (Exception e) {
+ Asserts.expectedFailureContains(e, "No entity matching id
self");
+ }
+ }
+
+ @Test
+ public void testDslAncestor() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " id: app",
+ " brooklyn.config:",
+ " dest: $brooklyn:ancestor(\"app\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:ancestor(\"app\")",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:ancestor(\"app\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:ancestor(\"app\")");
+ final Entity child1 = Iterables.get(app.getChildren(), 0);
+ final Entity child2 = Iterables.get(app.getChildren(), 1);
+ final Entity grandChild =
Iterables.getOnlyElement(child2.getChildren());
+ assertEquals(getConfigEventually(child1, DEST), app);
+ assertEquals(getConfigEventually(child2, DEST), app);
+ assertEquals(getConfigEventually(grandChild, DEST), app);
+ try {
+ assertEquals(getConfigEventually(app, DEST), app);
+ Asserts.shouldHaveFailedPreviously("App not in ancestor
scope");
+ } catch (Exception e) {
+ Asserts.expectedFailureContains(e, "No entity matching id
app");
+ }
+ }
+
+ @Test
+ public void testDslRoot() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " id: app",
+ " brooklyn.config:",
+ " dest: $brooklyn:root()",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:root()",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:root()",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:root()");
+ final Entity child1 = Iterables.get(app.getChildren(), 0);
+ final Entity child2 = Iterables.get(app.getChildren(), 1);
+ final Entity grandChild =
Iterables.getOnlyElement(child2.getChildren());
+ assertEquals(getConfigEventually(child1, DEST), app);
+ assertEquals(getConfigEventually(child2, DEST), app);
+ assertEquals(getConfigEventually(grandChild, DEST), app);
+ assertEquals(getConfigEventually(app, DEST), app);
+ }
+
+ @Test
+ public void testDslScopeRoot() throws Exception {
+ addCatalogItems(
+ "brooklyn.catalog:",
+ " version: " + TEST_VERSION,
+ " items:",
+ " - id: simple-item",
+ " itemType: entity",
+ " item:",
+ " type: "+ BasicEntity.class.getName(),
+ " - id: wrapping-plain",
+ " itemType: entity",
+ " item:",
+ " type: "+ BasicEntity.class.getName(),
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " brooklyn.config:",
+ " dest: $brooklyn:scopeRoot()",
+ " - id: wrapping-simple",
+ " itemType: entity",
+ " item:",
+ " type: "+ BasicEntity.class.getName(),
+ " brooklyn.children:",
+ " - type: simple-item",
+ " brooklyn.config:",
+ " dest: $brooklyn:scopeRoot()");
+
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.children:",
+ " - type: wrapping-plain",
+ " - type: wrapping-simple");
+ Entity child1 = Iterables.get(app.getChildren(), 0);
+ Entity child2 = Iterables.get(app.getChildren(), 1);
+ assertScopeRoot(child1, false);
+ // TODO Not the result I'd expect - in both cases the entity
argument should the the scopeRoot element, not its child
+ assertScopeRoot(child2, true);
+ }
+
+ private void assertScopeRoot(Entity entity, boolean isScopeBugged)
throws Exception {
+ Entity child = Iterables.getOnlyElement(entity.getChildren());
+ if (!isScopeBugged) {
+ assertEquals(getConfigEventually(child, DEST), entity);
+ } else {
+ assertEquals(getConfigEventually(child, DEST), child);
+ }
+ }
+
+ @Test
+ public void testDslConfig() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " source: myvalue",
+ " dest: $brooklyn:config(\"source\")");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test
+ public void testDslConfigOnEntity() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " dest:
$brooklyn:entity(\"sourceEntity\").config(\"source\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: sourceEntity",
+ " brooklyn.config:",
+ " source: myvalue");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test(groups="WIP") // config accepts strings only, no suppliers
+ public void testDslConfigWithDeferredArg() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " source: myvalue",
+ " configName: source",
+ " dest: $brooklyn:config(config(\"configName\"))");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test(groups="WIP") // config accepts strings only, no suppliers
+ public void testDslConfigOnEntityWithDeferredArg() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " entityName: sourceEntity",
+ " configName: source",
+ " dest:
$brooklyn:entity(config(\"entityName\")).config(config(\"configName\"))",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: sourceEntity",
+ " brooklyn.config:",
+ " source: myvalue");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test
+ public void testDslAttributeWhenReady() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.initializers:",
+ " - type: org.apache.brooklyn.core.sensor.StaticSensor",
+ " brooklyn.config:",
+ " name: source",
+ " static.value: myvalue",
+ " brooklyn.config:",
+ " dest: $brooklyn:attributeWhenReady(\"source\")");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test
+ public void testDslAttributeWhenReadyOnEntity() throws Exception {
+ final Entity app = createAndStartApplication(
+ "services:",
+ "- type: " + BasicApplication.class.getName(),
+ " brooklyn.config:",
+ " dest:
$brooklyn:entity(\"sourceEntity\").attributeWhenReady(\"source\")",
+ " brooklyn.children:",
+ " - type: " + BasicEntity.class.getName(),
+ " id: sourceEntity",
+ " brooklyn.initializers:",
+ " - type: org.apache.brooklyn.core.sensor.StaticSensor",
+ " brooklyn.config:",
+ " name: source",
+ " static.value: myvalue");
+ assertEquals(getConfigEventually(app, DEST), "myvalue");
+ }
+
+ @Test(groups="WIP") // config accepts strings only, no suppliers
--- End diff --
Cut-n-paste - think this comment should be `attributeWhenReady accepts
strings only, no suppliers`, also on next method
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---