Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 13887f699 -> 404b7e02b


Adds yaml-based tests for regexReplacement


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

Branch: refs/heads/master
Commit: a96cb55cd4dd44b4b8bfb60f39794cc2beb7c9e1
Parents: 0779626
Author: Martin Harris <[email protected]>
Authored: Mon Sep 21 14:03:16 2015 +0100
Committer: Martin Harris <[email protected]>
Committed: Thu Oct 15 15:40:41 2015 +0100

----------------------------------------------------------------------
 .../enricher/stock/AbstractTransformer.java     |  6 +-
 .../core/entity/DependentConfigurationTest.java |  3 -
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 70 ++++++++++++++++++++
 3 files changed, 74 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a96cb55c/core/src/main/java/org/apache/brooklyn/enricher/stock/AbstractTransformer.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/enricher/stock/AbstractTransformer.java
 
b/core/src/main/java/org/apache/brooklyn/enricher/stock/AbstractTransformer.java
index 89ebb1a..8a9cf53 100644
--- 
a/core/src/main/java/org/apache/brooklyn/enricher/stock/AbstractTransformer.java
+++ 
b/core/src/main/java/org/apache/brooklyn/enricher/stock/AbstractTransformer.java
@@ -57,14 +57,16 @@ public abstract class AbstractTransformer<T,U> extends 
AbstractEnricher implemen
     public void setEntity(EntityLocal entity) {
         super.setEntity(entity);
 
-        Function<SensorEvent<T>, U> transformation = getTransformation();
         this.producer = getConfig(PRODUCER) == null ? entity: 
getConfig(PRODUCER);
         this.sourceSensor = (Sensor<T>) getRequiredConfig(SOURCE_SENSOR);
         Sensor<?> targetSensorSpecified = getConfig(TARGET_SENSOR);
         this.targetSensor = targetSensorSpecified!=null ? (Sensor<U>) 
targetSensorSpecified : (Sensor<U>) this.sourceSensor;
         if (producer.equals(entity) && targetSensorSpecified==null) {
+            // We cannot call getTransformation() here to log the 
tranformation, as it will attempt
+            // to resolve the transformation, which will cause the entity 
initialization thread to
+            // block
             LOG.error("Refusing to add an enricher which reads and publishes 
on the same sensor: "+
-                producer+"."+sourceSensor+" (computing "+transformation+")");
+                producer+"."+sourceSensor+" (computing transformation)");
             // we don't throw because this error may manifest itself after a 
lengthy deployment, 
             // and failing it at that point simply because of an enricher is 
not very pleasant
             // (at least not until we have good re-run support across the 
board)

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a96cb55c/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
 
b/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
index 17cc6fd..6d73c6d 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/entity/DependentConfigurationTest.java
@@ -32,9 +32,6 @@ import java.util.concurrent.atomic.AtomicReference;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
-import org.apache.brooklyn.api.sensor.Sensor;
-import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.core.sensor.DependentConfiguration;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a96cb55c/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git 
a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
 
b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 42bf9f1..1408d13 100644
--- 
a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ 
b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -25,6 +25,7 @@ import java.util.concurrent.Callable;
 import org.apache.brooklyn.api.entity.Application;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
@@ -35,6 +36,7 @@ import 
org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.test.EntityTestUtils;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.slf4j.Logger;
@@ -241,4 +243,72 @@ public class DslAndRebindYamlTest extends AbstractYamlTest 
{
             "    test.confName: $brooklyn:formatString(\"hello %s\", 
\"world\")");
     }
 
+
+    /*
+           - type: org.apache.brooklyn.enricher.stock.Transformer
+          brooklyn.config:
+            enricher.sourceSensor: 
$brooklyn:sensor("mongodb.server.replicaSet.primary.endpoint")
+            enricher.targetSensor: $brooklyn:sensor("justtheport")
+            enricher.transformation: 
$brooklyn:function.regexReplacement("^.*:", "")
+        - type: org.apache.brooklyn.enricher.stock.Transformer
+          brooklyn.config:
+            enricher.sourceSensor: 
$brooklyn:sensor("mongodb.server.replicaSet.primary.endpoint")
+            enricher.targetSensor: $brooklyn:sensor("directport")
+            enricher.targetValue: 
$brooklyn:regexReplacement($brooklyn:attributeWhenReady("mongodb.server.replicaSet.primary.endpoint"),
 "^.*:", "foo")
+     */
+
+    @Test
+    public void testRegexReplacementWithStrings() throws Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith(
+                "  brooklyn.config:",
+                "    test.regex.config: 
$brooklyn:regexReplacement(\"somefooname\", \"foo\", \"bar\")"
+        );
+        Assert.assertEquals("somebarname", 
testEntity.getConfig(ConfigKeys.newStringConfigKey("test.regex.config")));
+    }
+
+    @Test
+    public void testRegexReplacementWithAttributeWhenReady() throws Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith(
+                "  brooklyn.config:",
+                "    test.regex.config: 
$brooklyn:regexReplacement($brooklyn:attributeWhenReady(\"test.regex.source\"), 
$brooklyn:attributeWhenReady(\"test.regex.pattern\"), 
$brooklyn:attributeWhenReady(\"test.regex.replacement\"))"
+        );
+        testEntity.sensors().set(Sensors.newStringSensor("test.regex.source"), 
"somefooname");
+        
testEntity.sensors().set(Sensors.newStringSensor("test.regex.pattern"), "foo");
+        
testEntity.sensors().set(Sensors.newStringSensor("test.regex.replacement"), 
"bar");
+
+        Assert.assertEquals("somebarname", 
testEntity.getConfig(ConfigKeys.newStringConfigKey("test.regex.config")));
+    }
+
+    @Test
+    public void testRegexReplacementFunctionWithStrings() throws Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith(
+                "  brooklyn.enrichers:",
+                "  - type: org.apache.brooklyn.enricher.stock.Transformer",
+                "    brooklyn.config:",
+                "      enricher.sourceSensor: $brooklyn:sensor(\"test.name\")",
+                "      enricher.targetSensor: 
$brooklyn:sensor(\"test.name.transformed\")",
+                "      enricher.transformation: 
$brooklyn:function.regexReplacement(\"foo\", \"bar\")"
+        );
+        testEntity.sensors().set(TestEntity.NAME, "somefooname");
+        AttributeSensor<String> transformedSensor = 
Sensors.newStringSensor("test.name.transformed");
+        EntityTestUtils.assertAttributeEqualsEventually(testEntity, 
transformedSensor, "somebarname");
+    }
+
+    @Test
+    public void testRegexReplacementFunctionWithAttributeWhenReady() throws 
Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith(
+                "  brooklyn.enrichers:",
+                "  - type: org.apache.brooklyn.enricher.stock.Transformer",
+                "    brooklyn.config:",
+                "      enricher.sourceSensor: $brooklyn:sensor(\"test.name\")",
+                "      enricher.targetSensor: 
$brooklyn:sensor(\"test.name.transformed\")",
+                "      enricher.transformation: 
$brooklyn:function.regexReplacement($brooklyn:attributeWhenReady(\"test.pattern\"),
 $brooklyn:attributeWhenReady(\"test.replacement\"))"
+        );
+        testEntity.sensors().set(Sensors.newStringSensor("test.pattern"), 
"foo");
+        testEntity.sensors().set(Sensors.newStringSensor("test.replacement"), 
"bar");
+        testEntity.sensors().set(TestEntity.NAME, "somefooname");
+        AttributeSensor<String> transformedSensor = 
Sensors.newStringSensor("test.name.transformed");
+        EntityTestUtils.assertAttributeEqualsEventually(testEntity, 
transformedSensor, "somebarname");
+    }
+
 }

Reply via email to