org.neo4j.test.assertion.Assert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-utils Show documentation
Show all versions of test-utils Show documentation
A set of utilities used by tests.
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.test.assertion;
import static java.time.Duration.ZERO;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.neo4j.test.conditions.Conditions.condition;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.assertj.core.api.Condition;
import org.awaitility.core.ConditionFactory;
import org.junit.jupiter.api.function.Executable;
import org.neo4j.function.Suppliers;
import org.neo4j.function.ThrowingAction;
public final class Assert {
private Assert() {}
public static void awaitUntilAsserted(ThrowingAction condition) {
awaitUntilAsserted(null, condition);
}
public static void awaitUntilAsserted(String alias, ThrowingAction condition) {
await(alias)
.atMost(1, MINUTES)
.pollDelay(ZERO)
.pollInterval(50, MILLISECONDS)
.pollInSameThread()
.untilAsserted(condition::apply);
}
public static void assertEventually(
Callable actual, Predicate super T> predicate, long timeout, TimeUnit timeUnit) {
assertEventually(EMPTY, actual, condition(predicate), timeout, timeUnit);
}
public static void assertEventually(
Callable actual, Condition super T> condition, long timeout, TimeUnit timeUnit) {
assertEventually(EMPTY, actual, condition, timeout, timeUnit);
}
public static void assertEventually(
String message, Callable actual, Condition super T> condition, long timeout, TimeUnit timeUnit) {
awaitCondition(message, timeout, timeUnit)
.untilAsserted(() -> assertThat(actual.call()).satisfies(condition));
}
public static void assertEventually(
String message, Callable actual, Predicate super T> predicate, long timeout, TimeUnit timeUnit) {
awaitCondition(message, timeout, timeUnit)
.untilAsserted(() -> assertThat(actual.call()).satisfies(condition(predicate)));
}
public static void assertEventually(
Supplier messageSupplier,
Callable actual,
Condition super T> condition,
long timeout,
TimeUnit timeUnit) {
assertEventually(ignore -> messageSupplier.get(), actual, condition, timeout, timeUnit);
}
public static void assertEventually(
Function messageGenerator,
Callable actual,
Condition super T> condition,
long timeout,
TimeUnit timeUnit) {
awaitCondition("await condition", timeout, timeUnit).untilAsserted(() -> {
var value = actual.call();
assertThat(value).as(messageGenerator.apply(value)).satisfies(condition);
});
}
public static void assertEventuallyThrows(
String message, Class expectedType, Executable actual, long timeout, TimeUnit timeUnit) {
assertEventuallyThrows(Suppliers.singleton(message), expectedType, actual, timeout, timeUnit);
}
public static void assertEventuallyThrows(
Supplier messageGenerator,
Class expectedType,
Executable actual,
long timeout,
TimeUnit timeUnit) {
awaitCondition("should throw", timeout, timeUnit)
.untilAsserted(() -> assertThrows(expectedType, actual, messageGenerator));
}
public static void assertEventuallyDoesNotThrow(
String message, Executable action, long timeout, TimeUnit timeUnit, long pollDelay, TimeUnit pollUnit) {
awaitCondition("should not throw", timeout, timeUnit, pollDelay, pollUnit)
.untilAsserted(() -> assertDoesNotThrow(action, message));
}
public static void assertEventuallyDoesNotThrow(
String message, Executable action, long timeout, TimeUnit timeUnit) {
awaitCondition("should not throw", timeout, timeUnit).untilAsserted(() -> assertDoesNotThrow(action, message));
}
private static ConditionFactory awaitCondition(String alias, long timeout, TimeUnit timeUnit) {
return awaitCondition(alias, timeout, timeUnit, 10, MILLISECONDS).pollInSameThread();
}
private static ConditionFactory awaitCondition(
String alias, long timeout, TimeUnit timeUnit, long pollDelay, TimeUnit pollUnit) {
return await(alias)
.atMost(timeout, timeUnit)
.pollDelay(pollDelay, pollUnit)
.pollInSameThread();
}
}