This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 4440df9e15 tests and fix for guava predicates not being coerced
4440df9e15 is described below

commit 4440df9e1548d06fb2651ec48b817117f0f3cfc8
Author: Alex Heneveld <[email protected]>
AuthorDate: Mon Dec 19 19:32:20 2022 +0000

    tests and fix for guava predicates not being coerced
---
 .../brooklyn/spi/dsl/DslPredicateYamlTest.java     | 38 ++++++++++++++++++++++
 .../brooklyn/util/core/flags/TypeCoercions.java    |  3 ++
 .../util/core/predicates/DslPredicates.java        |  5 ++-
 3 files changed, 45 insertions(+), 1 deletion(-)

diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslPredicateYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslPredicateYamlTest.java
index 864d2d3726..ed9de8bb97 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslPredicateYamlTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslPredicateYamlTest.java
@@ -97,6 +97,44 @@ public class DslPredicateYamlTest extends AbstractYamlTest {
         Asserts.assertTrue( predicate.apply(app) );
     }
 
+    @Test
+    public void testDslPredicateConfigAsGuavaPredicate() throws Exception {
+        DslPredicates.init();
+        Entity app = createAndStartApplication(
+                "services:",
+                "- type: " + BasicApplication.class.getName(),
+                "  brooklyn.config:",
+                "    "+TestEntity.CONF_STRING.getName()+": x",
+                "    expected: x",
+                "    test.confPredicate:",
+                "          $brooklyn:object:\n" +
+                "              type: com.google.common.base.Predicates\n" +
+                "              factoryMethod.name: not\n" +
+                "              factoryMethod.args:\n" +
+                "                - $brooklyn:object:\n" +
+                "                    type: 
com.google.common.base.Predicates\n" +
+                "                    factoryMethod.name: equalTo\n" +
+                "                    factoryMethod.args:\n" +
+                "                      - $brooklyn:object:\n" +
+                "                          type: 
org.apache.brooklyn.util.collections.MutableMap\n" +
+                "                          factoryMethod.name: of");
+        DslPredicates.DslPredicate predicate = 
app.config().get(TestEntity.CONF_PREDICATE);
+        Asserts.assertFalse( predicate.apply(MutableMap.of()) );
+        Asserts.assertTrue( predicate.apply(MutableMap.of("a","b")) );
+
+        app = createAndStartApplication(
+                "services:",
+                "- type: " + BasicApplication.class.getName(),
+                "  brooklyn.config:",
+                "    "+TestEntity.CONF_STRING.getName()+": x",
+                "    expected: x",
+                "    test.confPredicate:",
+                "      when: truthy");
+        predicate = app.config().get(TestEntity.CONF_PREDICATE);
+        Asserts.assertFalse( predicate.apply(MutableMap.of()) );
+        Asserts.assertTrue( predicate.apply(MutableMap.of("a","b")) );
+    }
+
     @Test
     public void testDslTargetLocationRetargets() throws Exception {
         Entity app = createAndStartApplication(
diff --git 
a/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java 
b/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java
index c6a90c824f..7dfbc11dd0 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java
@@ -35,6 +35,7 @@ import org.apache.brooklyn.core.resolve.jackson.WrappedValue;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.util.JavaGroovyEquivalents;
 import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.predicates.DslPredicates;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
@@ -137,6 +138,8 @@ public class TypeCoercions {
                 return Sensors.newSensor(Object.class, input);
             }
         });
+
+        DslPredicates.init();
     }
     
     /**
diff --git 
a/core/src/main/java/org/apache/brooklyn/util/core/predicates/DslPredicates.java
 
b/core/src/main/java/org/apache/brooklyn/util/core/predicates/DslPredicates.java
index 3730ed0d1a..f117f47140 100644
--- 
a/core/src/main/java/org/apache/brooklyn/util/core/predicates/DslPredicates.java
+++ 
b/core/src/main/java/org/apache/brooklyn/util/core/predicates/DslPredicates.java
@@ -25,7 +25,6 @@ import com.fasterxml.jackson.databind.*;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.util.TokenBuffer;
 import com.google.common.annotations.Beta;
-import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.reflect.TypeToken;
@@ -73,6 +72,7 @@ import java.util.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.BiFunction;
 import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.regex.Pattern;
 
 public class DslPredicates {
@@ -85,6 +85,9 @@ public class DslPredicates {
 
         TypeCoercions.registerAdapter(java.util.function.Predicate.class, 
DslEntityPredicate.class, DslEntityPredicateAdapter::new);
         TypeCoercions.registerAdapter(java.util.function.Predicate.class, 
DslPredicate.class, DslPredicateAdapter::new);
+        // subsumed in above
+//        
TypeCoercions.registerAdapter(com.google.common.base.Predicate.class, 
DslEntityPredicate.class, DslEntityPredicateAdapter::new);
+//        
TypeCoercions.registerAdapter(com.google.common.base.Predicate.class, 
DslPredicate.class, DslPredicateAdapter::new);
 
         // TODO could use json shorthand instead?
         TypeCoercions.registerAdapter(String.class, DslPredicate.class, 
DslPredicates::implicitlyEqualTo);

Reply via email to