// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package software.aws.toolkits.jetbrains.utils import org.assertj.core.api.AbstractAssert import org.assertj.core.api.AbstractThrowableAssert import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.CompletableFutureAssert import java.time.Duration import java.util.concurrent.CompletionStage import java.util.concurrent.TimeUnit import java.util.function.Consumer private val TIMEOUT = Duration.ofSeconds(1) fun CompletableFutureAssert.wait(): CompletableFutureAssert { try { matches { it.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS) != null } } catch (e: Exception) { // suppress } return this } fun CompletableFutureAssert.hasValue(value: T) { wait().isCompletedWithValue(value) } val CompletionStage.value get() = toCompletableFuture().get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS) val CompletableFutureAssert.hasException get() = this.wait().isCompletedExceptionally fun , ACTUAL : Throwable> AbstractThrowableAssert.hasCauseWithMessage( message: String ): AbstractThrowableAssert { satisfies { parentThrowable -> assertThat(parentThrowable.cause).isNotNull.hasMessage(message) } return this } inline fun AbstractAssert<*, *>.isInstanceOf() = isInstanceOf(T::class.java) inline fun AbstractAssert<*, *>.isInstanceOfSatisfying(checker: Consumer) = isInstanceOfSatisfying(T::class.java, checker)