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
commit deefd6cf7cc3ca0227eebe89c1b2aecbfdc74c7f Author: Alex Heneveld <a...@cloudsoft.io> AuthorDate: Fri Feb 2 14:47:42 2024 +0000 support boolean values and suppliers being set for dsl predicate / conditions --- .../util/core/predicates/DslPredicates.java | 10 ++++++ .../util/core/predicates/DslPredicateTest.java | 41 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) 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 76efad2c9b..d185c7a3ff 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 @@ -93,6 +93,7 @@ public class DslPredicates { // could use json shorthand instead, but this is simpler TypeCoercions.registerAdapter(String.class, DslPredicate.class, DslPredicates::implicitlyEqualTo); + TypeCoercions.registerAdapter(Boolean.class, DslPredicate.class, DslPredicates::always); // TypeCoercions.registerAdapter(DeferredSupplier.class, DslPredicate.class, DslPredicates::implicitlyEqualTo); // TypeCoercions.registerAdapter(WorkflowExpressionResolution.WrappedUnresolvedExpression.class, DslPredicate.class, DslPredicates::implicitlyEqualTo); @@ -503,6 +504,11 @@ public class DslPredicates { return nestedPredicateCheck((DslPredicate) test, result); } + if (test instanceof Boolean) { + // if a boolean is supplied as an implicit, return it; it was probably a condition + return (Boolean) test; + } + if ((!(test instanceof BrooklynObject) && value instanceof BrooklynObject) || (!(test instanceof Iterable) && value instanceof Iterable)) { throw new IllegalStateException("Implicit value used for equality check comparing "+test+" with "+value+", which is probably not what was meant. Use explicit 'equals: ...' syntax for this case."); @@ -1057,6 +1063,10 @@ public class DslPredicates { return result; } + public static DslPredicate always(boolean x) { + return x ? alwaysTrue() : alwaysFalse(); + } + public static DslPredicate equalTo(Object x) { DslEntityPredicateDefault result = new DslEntityPredicateDefault(); result.equals = WrappedValue.of(x); diff --git a/core/src/test/java/org/apache/brooklyn/util/core/predicates/DslPredicateTest.java b/core/src/test/java/org/apache/brooklyn/util/core/predicates/DslPredicateTest.java index 016059ebbd..a043064db3 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/predicates/DslPredicateTest.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/predicates/DslPredicateTest.java @@ -26,12 +26,15 @@ import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.collections.MutableSet; import org.apache.brooklyn.util.core.flags.TypeCoercions; import org.apache.brooklyn.util.core.predicates.DslPredicates.DslPredicate; +import org.apache.brooklyn.util.core.task.DeferredSupplier; +import org.apache.brooklyn.util.javalang.AtomicReferences; import org.apache.brooklyn.util.time.Duration; import org.apache.brooklyn.util.time.Time; import org.testng.annotations.Test; import java.util.Arrays; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Predicate; @@ -609,4 +612,42 @@ public class DslPredicateTest extends BrooklynMgmtUnitTestSupport { Asserts.assertFailsWith(() -> { p2.test(MutableList.of("x", "zz")); }, e -> Asserts.expectedFailureContainsIgnoreCase(e, "assert", "value", "zz")); } + @Test + public void testCoercionAndSuppliers() { + DslPredicates.DslPredicate p; + + p = TypeCoercions.coerce("yessir", DslPredicates.DslPredicate.class); + Asserts.assertTrue(p.test("yessir")); + Asserts.assertFalse(p.test("no sir")); + + p = TypeCoercions.coerce( (DeferredSupplier) ()->"yessir", DslPredicates.DslPredicate.class); + Asserts.assertTrue(p.test("yessir")); + Asserts.assertFalse(p.test("no sir")); + + AtomicReference fickle = new AtomicReference("yessir"); + p = TypeCoercions.coerce( (DeferredSupplier) ()->fickle.get(), DslPredicates.DslPredicate.class); + Asserts.assertTrue(p.test("yessir")); + Asserts.assertFalse(p.test("no sir")); + fickle.set("no sir"); + Asserts.assertFalse(p.test("yessir")); + Asserts.assertTrue(p.test("no sir")); + + // now boolean, especially atomic boolean + Asserts.assertTrue(TypeCoercions.coerce(true, DslPredicates.DslPredicate.class).test("anything")); + Asserts.assertFalse(TypeCoercions.coerce(false, DslPredicates.DslPredicate.class).test("anything")); + + fickle.set(true); + Asserts.assertTrue(p.test(true)); + Asserts.assertTrue(p.test("anything")); + Asserts.assertTrue(p.test(false)); + + fickle.set(false); + Asserts.assertFalse(p.test(true)); + Asserts.assertFalse(p.test("anything")); + Asserts.assertFalse(p.test(false)); + + p = TypeCoercions.coerce( (DeferredSupplier) ()->false, DslPredicates.DslPredicate.class); + Asserts.assertFalse(p.test("anything")); + } + }