Class UniAssertSubscriber<T>

java.lang.Object
io.smallrye.mutiny.helpers.test.UniAssertSubscriber<T>
Type Parameters:
T - the type of the items
All Implemented Interfaces:
ContextSupport, UniSubscriber<T>

public class UniAssertSubscriber<T> extends Object implements UniSubscriber<T>
A Uni UniSubscriber for testing purposes that comes with useful assertion helpers.
  • Constructor Details

    • UniAssertSubscriber

      public UniAssertSubscriber(Context context, boolean cancelled)
      Create a new UniAssertSubscriber.
      Parameters:
      context - the subscription context, cannot be null
      cancelled - true when the subscription shall be cancelled upfront, false otherwise
    • UniAssertSubscriber

      public UniAssertSubscriber(boolean cancelled)
      Create a new UniAssertSubscriber with an upfront cancellation configuration and an empty Context.
    • UniAssertSubscriber

      public UniAssertSubscriber()
      Create a new UniAssertSubscriber with no upfront cancellation and an empty Context.
  • Method Details

    • create

      public static <T> UniAssertSubscriber<T> create()
      Create a new UniAssertSubscriber with no upfront cancellation and an empty Context.
      Type Parameters:
      T - the type of the item
      Returns:
      a new subscriber
    • create

      public static <T> UniAssertSubscriber<T> create(Context context)
      Create a new UniAssertSubscriber with no upfront cancellation and a Context.
      Type Parameters:
      T - the type of the item
      Parameters:
      context - the context, cannot be null
      Returns:
      a new subscriber
    • context

      public Context context()
      Description copied from interface: ContextSupport
      Provide a context.

      Since calls to this method shall only be triggered when a Mutiny pipeline uses a withContext operator, there is no need in general for caching the context value in a field of the implementing class. Exceptions include operators that have cross-subscriber semantics such as memoizers or broadcasters.

      This method is expected to be called once per withContext operator.

      Specified by:
      context in interface ContextSupport
      Returns:
      the context, must not be null.
    • onSubscribe

      public void onSubscribe(UniSubscription subscription)
      Description copied from interface: UniSubscriber
      Event handler called once the subscribed Uni has taken into account the subscription. The Uni have triggered the computation of the item.

      IMPORTANT: UniSubscriber.onItem(Object) and UniSubscriber.onFailure(Throwable) would not be called before the invocation of this method.

      Specified by:
      onSubscribe in interface UniSubscriber<T>
      Parameters:
      subscription - the subscription allowing to cancel the computation.
    • onItem

      public void onItem(T item)
      Description copied from interface: UniSubscriber
      Event handler called once the item has been computed by the subscribed Uni.

      IMPORTANT: this method will be only called once per subscription. If UniSubscriber.onFailure(Throwable) is called, this method won't be called.

      Specified by:
      onItem in interface UniSubscriber<T>
      Parameters:
      item - the item, may be null.
    • onFailure

      public void onFailure(Throwable failure)
      Description copied from interface: UniSubscriber
      Called if the computation of the item by the subscriber Uni failed.

      IMPORTANT: this method will be only called once per subscription. If UniSubscriber.onItem(Object) is called, this method won't be called.

      Specified by:
      onFailure in interface UniSubscriber<T>
      Parameters:
      failure - the failure, cannot be null.
    • awaitItem

      public UniAssertSubscriber<T> awaitItem()
      Awaits for an item event. It waits at most AssertSubscriber.DEFAULT_TIMEOUT.

      If the timeout expired, or if a failure event is received instead of the expected completion, the check fails.

      Returns:
      this UniAssertSubscriber
    • awaitItem

      public UniAssertSubscriber<T> awaitItem(Duration duration)
      Awaits for a item event at most duration.

      If the timeout expired, or if a failure event is received instead of the expected completion, the check fails.

      Parameters:
      duration - the duration, must not be null
      Returns:
      this UniAssertSubscriber
    • awaitFailure

      public UniAssertSubscriber<T> awaitFailure()
      Awaits for a failure event. It waits at most AssertSubscriber.DEFAULT_TIMEOUT.

      If the timeout expired, or if an item event is received instead of the expected failure, the check fails.

      Returns:
      this UniAssertSubscriber
    • awaitFailure

      public UniAssertSubscriber<T> awaitFailure(Consumer<Throwable> assertion)
      Awaits for a failure event and validate it. It waits at most AssertSubscriber.DEFAULT_TIMEOUT.

      If the timeout expired, or if an item event is received instead of the expected failure, the check fails. The received failure is validated using the assertion consumer. The code of the consumer is expected to throw an AssertionError to indicate that the failure didn't pass the validation. The consumer is not called if no failures are received.

      Parameters:
      assertion - a check validating the received failure (if any). Must not be null
      Returns:
      this UniAssertSubscriber
    • awaitFailure

      public UniAssertSubscriber<T> awaitFailure(Duration duration)
      Awaits for a failure event. It waits at most duration.

      If the timeout expired, or if an item event is received instead of the expected failure, the check fails.

      Parameters:
      duration - the max duration to wait, must not be null
      Returns:
      this UniAssertSubscriber
    • awaitFailure

      public UniAssertSubscriber<T> awaitFailure(Consumer<Throwable> assertion, Duration duration)
      Awaits for a failure event and validate it. It waits at most duration.

      If the timeout expired, or if an item event is received instead of the expected failure, the check fails. The received failure is validated using the assertion consumer. The code of the consumer is expected to throw an AssertionError to indicate that the failure didn't pass the validation. The consumer is not called if no failures are received.

      Parameters:
      assertion - a check validating the received failure (if any). Must not be null
      duration - the max duration to wait, must not be null
      Returns:
      this UniAssertSubscriber
    • awaitSubscription

      public UniAssertSubscriber<T> awaitSubscription()
      Awaits for a subscription event (the subscriber receives a UniSubscription from the upstream. It waits at most AssertSubscriber.DEFAULT_TIMEOUT.

      If the timeout expired, the check fails.

      Returns:
      this UniAssertSubscriber
    • awaitSubscription

      public UniAssertSubscriber<T> awaitSubscription(Duration duration)
      Awaits for a subscription event (the subscriber receives a UniSubscription from the upstream. It waits at most duration.

      If the timeout expired, the check fails.

      Parameters:
      duration - the UniAssertSubscriber, must not be null
      Returns:
      this AssertSubscriber
    • assertCompleted

      public UniAssertSubscriber<T> assertCompleted()
      Assert that the Uni has completed.
      Returns:
      this UniAssertSubscriber
    • assertFailed

      public UniAssertSubscriber<T> assertFailed()
      Assert that the Uni has failed.
      Returns:
      this UniAssertSubscriber
    • getItem

      public T getItem()
      Get the Uni item, if any.
      Returns:
      the item or null
    • getFailure

      public Throwable getFailure()
      Get the Uni failure, if any.
      Returns:
      the failure or null
    • assertItem

      public UniAssertSubscriber<T> assertItem(T expected)
      Assert that the Uni has received an item.
      Parameters:
      expected - the expected item
      Returns:
      this UniAssertSubscriber
    • assertFailedWith

      public UniAssertSubscriber<T> assertFailedWith(Class<? extends Throwable> expectedTypeOfFailure, String expectedMessage)
      Assert that the Uni has failed.
      Parameters:
      expectedTypeOfFailure - the expected failure type
      expectedMessage - a message that is expected to be contained in the failure message
      Returns:
      this UniAssertSubscriber
    • assertFailedWith

      public UniAssertSubscriber<T> assertFailedWith(Class<? extends Throwable> expectedTypeOfFailure)
      Assert that the Uni has failed.
      Parameters:
      expectedTypeOfFailure - the expected failure type
      Returns:
      this UniAssertSubscriber
    • getOnItemThreadName

      public String getOnItemThreadName()
      Get the name of the thread that called onItem(Object), if any.
      Returns:
      the thread name
    • getOnFailureThreadName

      public String getOnFailureThreadName()
      Get the name of the thread that called onFailure(Throwable), if any.
      Returns:
      the thread name
    • getOnSubscribeThreadName

      public String getOnSubscribeThreadName()
      Get the name of the thread that called onSubscribe(UniSubscription), if any.
      Returns:
      the thread name
    • cancel

      public void cancel()
      Cancel the subscription.
    • assertTerminated

      public UniAssertSubscriber<T> assertTerminated()
      Assert that the Uni has terminated.
      Returns:
      this UniAssertSubscriber
    • assertNotTerminated

      public UniAssertSubscriber<T> assertNotTerminated()
      Assert that the Uni has not terminated.
      Returns:
      this UniAssertSubscriber
    • assertSubscribed

      public UniAssertSubscriber<T> assertSubscribed()
      Assert that the Uni has been subscribed.
      Returns:
      this UniAssertSubscriber
    • assertNotSubscribed

      public UniAssertSubscriber<T> assertNotSubscribed()
      Assert that the Uni has not been subscribed.
      Returns:
      this UniAssertSubscriber
    • getSignals

      public List<UniSignal> getSignals()
      Get the UniSignal audit trail for this subscriber.
      Returns:
      the signals in receive order
    • assertSignalsReceivedInOrder

      public UniAssertSubscriber<T> assertSignalsReceivedInOrder()
      Assert that signals have been received in correct order.

      An example of an legal sequence would be receiving onSubscribe -> onItem. An example of an illegal sequence would be receiving onItem -> onSubscribe.

      Returns:
      this UniAssertSubscriber