Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/1066#discussion_r46173382
--- Diff:
usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
---
@@ -39,122 +42,216 @@
* @author m4rkmckenna on 11/11/2015.
*/
public class TestFrameworkAssertions {
- private static final Logger LOG =
LoggerFactory.getLogger(TestFrameworkAssertions.class);
+
+ public static final String IS_NULL = "isNull";
+ public static final String NOT_NULL = "notNull";
+ public static final String IS_EQUAL_TO = "isEqualTo";
+ public static final String EQUAL_TO = "equalTo";
+ public static final String EQUALS = "equals";
+ public static final String MATCHES = "matches";
+ public static final String CONTAINS = "contains";
+ public static final String IS_EMPTY = "isEmpty";
+ public static final String NOT_EMPTY = "notEmpty";
+ public static final String HAS_TRUTH_VALUE = "hasTruthValue";
+ public static final String UNKNOWN_CONDITION = "unknown condition";
+
private TestFrameworkAssertions() {
}
+
/**
- * Evaluates all assertions against dataSupplier
+ * Get assertions tolerantly from a configuration key.
+ * This supports either a simple map of assertions, such as
*
- * @param dataSupplier
- * @param flags
- * @param assertions
- */
- public static void checkAssertions(final Supplier<String>
dataSupplier, final Map flags, final List<Map<String, Object>> assertions) {
- //Iterate through assert array
- for (final Map<String, Object> assertionsMap : assertions) {
- checkAssertions(dataSupplier, flags, assertionsMap);
- }
+ <pre>
+ assertOut:
+ contains: 2 users
+ matches: .*[\d]* days.*
+ </pre>
+ * or a list of such maps, (which allows you to repeat keys):
+ <pre>
+ assertOut:
+ - contains: 2 users
+ - contains: 2 days
+ </pre>
+ or
+ private static List<Map<String,Object>>
getAssertions(ConfigKey<Object> key) {
}
+ */
+ public static List<Map<String, Object>> getAssertions(Entity entity,
ConfigKey<Object> key) {
+ Object config = entity.getConfig(key);
+ Maybe<Map<String, Object>> maybeMap =
TypeCoercions.tryCoerce(config, new TypeToken<Map<String, Object>>() {});
+ if (maybeMap.isPresent()) {
+ return Collections.singletonList(maybeMap.get());
+ }
- /**
- * Evaluates all assertions against dataSupplier
- *
- * @param dataSupplier
- * @param flags
- * @param assertionsMap
- */
- public static void checkAssertions(final Supplier<String>
dataSupplier, final Map flags, final Map<String, Object> assertionsMap) {
- for (final Map.Entry<String, Object> assertion :
assertionsMap.entrySet()) {
- final Maybe<Predicate<String>> optionalPredicate =
getPredicate(assertion.getKey(), assertion.getValue());
- Asserts.succeedsEventually(flags, new
PredicateChecker(dataSupplier, optionalPredicate.get()));
+ Maybe<List<Map<String, Object>>> maybeList =
TypeCoercions.tryCoerce(config,
+ new TypeToken<List<Map<String, Object>>>() {});
+ if (maybeList.isPresent()) {
+ return maybeList.get();
}
+
+ throw new FatalConfigurationRuntimeException(key.getDescription()
+ " is not a map or list of maps");
}
- /**
- * Returns the predicate associated with the predicateKey if one exists
- *
- * @param predicateKey
- * @param predicateTarget
- * @return {@link Maybe} of {@Link Predicate}
- */
- public static Maybe<Predicate<String>> getPredicate(final String
predicateKey, final Object predicateTarget) {
- if (StringUtils.equalsIgnoreCase("isNull", predicateKey)) {
- return Maybe.of(Predicates.<String>isNull());
- } else if (StringUtils.equalsIgnoreCase("notNull", predicateKey)) {
- return Maybe.of(Predicates.<String>notNull());
- } else if (StringUtils.equalsIgnoreCase("isEqualTo", predicateKey)
- || StringUtils.equalsIgnoreCase("equalTo", predicateKey)
- || StringUtils.equalsIgnoreCase("equals", predicateKey)) {
- return
Maybe.of(Predicates.equalTo(TypeCoercions.coerce(predicateTarget.toString(),
String.class)));
- } else if (StringUtils.equalsIgnoreCase("matches", predicateKey)) {
- return
Maybe.of(buildMatchesPredicate(TypeCoercions.coerce(predicateTarget,
String.class)));
- } else if (StringUtils.equalsIgnoreCase("contains", predicateKey))
{
- return
Maybe.of(buildContainsPredicate(TypeCoercions.coerce(predicateTarget,
String.class)));
+
+ public static <T> void checkAssertions(Map<String,?> flags,
+ Map<String, Object> assertions,
+ String target,
+ final Supplier<T>
actualSupplier) {
+
+ AssertionSupport support = new AssertionSupport();
+ checkAssertions(support, flags, assertions, target,
actualSupplier);
+ support.validate();
+ }
+
+
+ public static <T> void checkAssertions(Map<String,?> flags,
+ List<Map<String, Object>>
assertions,
+ String target,
+ final Supplier<T>
actualSupplier) {
+
+ AssertionSupport support = new AssertionSupport();
+ for (Map<String, Object> assertionMap : assertions) {
+ checkAssertions(support, flags, assertionMap, target,
actualSupplier);
}
- return Maybe.absent(String.format("No predicate found with
signature [%s]", predicateKey));
+ support.validate();
}
- /**
- * Builds a predicate that checks if a string contains the supplied
value
- *
- * @param predicateTarget
- * @return {@link Predicate}
- */
- private static Predicate<String> buildContainsPredicate(final String
predicateTarget) {
- return new Predicate<String>() {
+ public static <T> void checkAssertions(final AssertionSupport support,
+ Map<String,?> flags,
+ final List<Map<String, Object>>
assertions,
+ final String target,
+ final Supplier<T>
actualSupplier) {
- @Override
- public boolean apply(@Nullable final String input) {
- return StringUtils.contains(input, predicateTarget);
- }
+ for (Map<String, Object> assertionMap : assertions) {
+ checkAssertions(support, flags, assertionMap, target,
actualSupplier);
+ }
+ }
- @Override
- public String toString() {
- return
String.format("TestFrameworkAssertions.contains(%s)", predicateTarget);
- }
- };
+ public static <T> void checkAssertions(final AssertionSupport support,
+ Map<String,?> flags,
+ final Map<String, Object>
assertions,
+ final String target,
+ final Supplier<T>
actualSupplier) {
+
+ if (null == assertions) {
+ return;
+ }
+ try {
+ Asserts.succeedsEventually(flags, new Runnable() {
+ @Override
+ public void run() {
+ T actual = actualSupplier.get();
+ checkActualAgainstAssertions(support, assertions,
target, actual);
+ }
+ });
+ } catch (Throwable t) {
--- End diff --
I'm very wary of swallowing the wrong kind of exception - e.g. if it was an
`InterruptedException`, then important the thread is left in an interrupted
state (see http://www.ibm.com/developerworks/library/j-jtp05236/).
Perhaps do `support.fail(t); Exceptions.propagateIfFatal(t);`?
(but I haven't checked what support.fail does).
---
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.
---