blob: dfc7708779014e5e044625307f66cc883115d136 [file] [log] [blame]
package brooklyn.test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import brooklyn.config.ConfigKey;
import brooklyn.entity.Entity;
import brooklyn.entity.Group;
import brooklyn.event.AttributeSensor;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
public class EntityTestUtils {
// TODO Delete methods from TestUtils, to just have them here (or switch so TestUtils delegates here,
// and deprecate methods in TestUtils until deleted).
public static <T> void assertAttributeEquals(Entity entity, AttributeSensor<T> attribute, T expected) {
assertEquals(entity.getAttribute(attribute), expected);
}
public static <T> void assertConfigEquals(Entity entity, ConfigKey<T> configKey, T expected) {
assertEquals(entity.getConfig(configKey), expected);
}
public static <T> void assertAttributeEqualsEventually(final Entity entity, final AttributeSensor<T> attribute, final T expected) {
assertAttributeEqualsEventually(Maps.newLinkedHashMap(), entity, attribute, expected);
}
public static <T> void assertAttributeEqualsEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) {
// Not using assertAttributeEventually(predicate) so get nicer error message
Asserts.succeedsEventually((Map)flags, new Runnable() {
@Override public void run() {
assertAttributeEquals(entity, attribute, expected);
}});
}
public static <T> T assertAttributeEventuallyNonNull(final Entity entity, final AttributeSensor<T> attribute) {
return assertAttributeEventuallyNonNull(Maps.newLinkedHashMap(), entity, attribute);
}
public static <T> T assertAttributeEventuallyNonNull(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute) {
return assertAttributeEventually(flags, entity, attribute, Predicates.notNull());
}
public static <T> T assertAttributeEventually(final Entity entity, final AttributeSensor<T> attribute, Predicate<? super T> predicate) {
return assertAttributeEventually(ImmutableMap.of(), entity, attribute, predicate);
}
public static <T> T assertAttributeEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate) {
final AtomicReference<T> result = new AtomicReference<T>();
Asserts.succeedsEventually((Map)flags, new Runnable() {
@Override public void run() {
T val = entity.getAttribute(attribute);
assertTrue(predicate.apply(val), "val="+val);
result.set(val);
}});
return result.get();
}
public static <T extends Entity> void assertPredicateEventuallyTrue(final T entity, final Predicate<? super T> predicate) {
assertPredicateEventuallyTrue(Maps.newLinkedHashMap(), entity, predicate);
}
public static <T extends Entity> void assertPredicateEventuallyTrue(Map<?,?> flags, final T entity, final Predicate<? super T> predicate) {
Asserts.succeedsEventually((Map)flags, new Runnable() {
@Override public void run() {
assertTrue(predicate.apply(entity));
}});
}
public static <T> void assertAttributeEqualsContinually(final Entity entity, final AttributeSensor<T> attribute, final T expected) {
assertAttributeEqualsContinually(Maps.newLinkedHashMap(), entity, attribute, expected);
}
public static <T> void assertAttributeEqualsContinually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) {
Asserts.succeedsContinually(flags, new Runnable() {
@Override public void run() {
assertAttributeEquals(entity, attribute, expected);
}});
}
public static void assertGroupSizeEqualsEventually(final Group group, int expected) {
assertGroupSizeEqualsEventually(ImmutableMap.of(), group, expected);
}
public static void assertGroupSizeEqualsEventually(Map<?,?> flags, final Group group, final int expected) {
Asserts.succeedsEventually((Map)flags, new Runnable() {
@Override public void run() {
Collection<Entity> members = group.getMembers();
assertEquals(members.size(), expected, "members="+members);
}});
}
}