Change DSL method to entityId from identity
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/d91d420f Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/d91d420f Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/d91d420f Branch: refs/heads/master Commit: d91d420f3b1c8ed1608878c34db0b220ec185ebf Parents: 2c80d16 Author: Andrew Donald Kennedy <[email protected]> Authored: Sat Jun 25 23:36:27 2016 +0100 Committer: Andrew Donald Kennedy <[email protected]> Committed: Sat Jun 25 23:37:09 2016 +0100 ---------------------------------------------------------------------- .../spi/dsl/methods/BrooklynDslCommon.java | 4 +- .../brooklyn/spi/dsl/methods/DslComponent.java | 74 ++++++++++---------- .../camp/brooklyn/IdentityYamlTest.java | 42 +++++------ .../test/resources/test-entity-identity.yaml | 30 -------- 4 files changed, 61 insertions(+), 89 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d91d420f/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 07c127e..9f767e3 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,8 +112,8 @@ public class BrooklynDslCommon { return new DslComponent(Scope.THIS, "").attributeWhenReady(sensorName); } - public static BrooklynDslDeferredSupplier<?> identity() { - return new DslComponent(Scope.THIS, "").identity(); + public static BrooklynDslDeferredSupplier<?> entityId() { + return new DslComponent(Scope.THIS, "").entityId(); } /** Returns a {@link Sensor}, looking up the sensor on the context if available and using that, http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d91d420f/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 c43f3bb..19ea766 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 @@ -41,6 +41,8 @@ import org.apache.brooklyn.util.core.task.Tasks; import org.apache.brooklyn.util.guava.Maybe; import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes; +import com.google.common.base.CaseFormat; +import com.google.common.base.Converter; import com.google.common.base.Objects; import com.google.common.base.Optional; import com.google.common.base.Preconditions; @@ -186,13 +188,13 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { // DSL words which return things - public BrooklynDslDeferredSupplier<?> identity() { - return new Identity(this); + public BrooklynDslDeferredSupplier<?> entityId() { + return new EntityId(this); } - protected static class Identity extends BrooklynDslDeferredSupplier<Object> { - private static final long serialVersionUID = -1L; + protected static class EntityId extends BrooklynDslDeferredSupplier<Object> { private final DslComponent component; - public Identity(DslComponent component) { + + public EntityId(DslComponent component) { this.component = Preconditions.checkNotNull(component); } @@ -210,12 +212,12 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; - Identity that = Identity.class.cast(obj); + EntityId that = EntityId.class.cast(obj); return Objects.equal(this.component, that.component); } @Override public String toString() { - return (component.scope==Scope.THIS ? "" : component.toString()+".") + "identity()"; + return (component.scope==Scope.THIS ? "" : component.toString()+".") + "entityId()"; } } @@ -358,40 +360,40 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> { } public static enum Scope { - GLOBAL ("global"), - CHILD ("child"), - PARENT ("parent"), - SIBLING ("sibling"), - DESCENDANT ("descendant"), - ANCESTOR("ancestor"), - ROOT("root"), - SCOPE_ROOT("scopeRoot"), - THIS ("this"); - - public static final Set<Scope> VALUES = ImmutableSet.of(GLOBAL, CHILD, PARENT, SIBLING, DESCENDANT, ANCESTOR, ROOT, SCOPE_ROOT, THIS); - - private final String name; - - private Scope(String name) { - this.name = name; - } - + GLOBAL, + CHILD, + PARENT, + SIBLING, + DESCENDANT, + ANCESTOR, + ROOT, + SCOPE_ROOT, + THIS; + + private static Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE); + public static Scope fromString(String name) { - return tryFromString(name).get(); + Maybe<Scope> parsed = tryFromString(name); + return parsed.get(); } - + public static Maybe<Scope> tryFromString(String name) { - for (Scope scope : VALUES) - if (scope.name.toLowerCase().equals(name.toLowerCase())) - return Maybe.of(scope); - return Maybe.absent(new IllegalArgumentException(name + " is not a valid scope")); + try { + Scope scope = valueOf(converter.convert(name)); + return Maybe.of(scope); + } catch (Exception cause) { + return Maybe.absent(cause); + } } - + public static boolean isValid(String name) { - for (Scope scope : VALUES) - if (scope.name.toLowerCase().equals(name.toLowerCase())) - return true; - return false; + Maybe<Scope> check = tryFromString(name); + return check.isPresentAndNonNull(); + } + + @Override + public String toString() { + return converter.reverse().convert(name()); } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d91d420f/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 index c059e68..480931d 100644 --- 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 @@ -28,10 +28,8 @@ 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 { @@ -41,37 +39,39 @@ public class IdentityYamlTest extends AbstractYamlTest { 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")); + Entity app = createAndStartApplication( + "services:", + " - type: " + TestEntity.class.getName(), + " id: testentityone", + " name: \"Test Entity One\"", + " brooklyn.config:", + " testentityone.id: $brooklyn:entityId()", + " testentitytwo.id: $brooklyn:entity(\"testentitytwo\").entityId()", + " - type: " + TestEntity.class.getName(), + " id: testentitytwo", + " name: \"Test Entity Two\""); 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; + return Iterables.filter(app.getChildren(), TestEntity.class); } @Test public void testYamlParsing() throws Exception { - setupAndCheckTestEntityInBasicYamlWith(); + Iterable<? extends Entity> testEntities = setupAndCheckTestEntityInBasicYamlWith(); + + Assert.assertEquals(Iterables.size(testEntities), 2, "Should be two entities"); } @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"))); + Entity entityOne = Iterables.find(testEntities, EntityPredicates.displayNameEqualTo("Test Entity One")); + Entity entityTwo = Iterables.find(testEntities, EntityPredicates.displayNameEqualTo("Test Entity Two")); - Assert.assertNotNull(testEntityOne, "Test entity one should be present"); - Assert.assertNotNull(testEntityTwo, "Test entity two should be present"); + Assert.assertNotNull(entityOne, "Test entity one should be present"); + Assert.assertNotNull(entityTwo, "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"); + Assert.assertEquals(entityOne.config().get(TEST_ENTITY_ONE_ID), entityOne.getId(), "Entity one IDs should match"); + Assert.assertEquals(entityOne.config().get(TEST_ENTITY_TWO_ID), entityTwo.getId(), "Entity two IDs should match"); } @Override http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d91d420f/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 deleted file mode 100644 index d238ec8..0000000 --- a/camp/camp-brooklyn/src/test/resources/test-entity-identity.yaml +++ /dev/null @@ -1,30 +0,0 @@ -# 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"
