Class AssertSubscriber<T>

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

public class AssertSubscriber<T> extends Object implements MultiSubscriber<T>, ContextSupport
A Multi Flow.Subscriber for testing purposes that comes with useful assertion helpers.
  • Field Details

    • DEFAULT_TIMEOUT

      public static Duration DEFAULT_TIMEOUT
      The default timeout used by await method.

      This static field is mutable, the authors assume that you know what you are doing if you ever feel like changing its value.

    • DEFAULT_MUTINY_AWAIT_TIMEOUT

      public static final String DEFAULT_MUTINY_AWAIT_TIMEOUT
      Name of the environment variable for setting the default await methods timeout (in seconds).
      See Also:
  • Constructor Details

    • AssertSubscriber

      public AssertSubscriber(Context context, long requested, boolean cancelled)
      Creates a new AssertSubscriber.
      Parameters:
      context - the context
      requested - the number of initially requested items
      cancelled - true if the subscription is immediately cancelled, false otherwise
    • AssertSubscriber

      public AssertSubscriber(long requested, boolean cancelled)
      Creates a new AssertSubscriber.
      Parameters:
      requested - the number of initially requested items
      cancelled - true if the subscription is immediately cancelled, false otherwise
    • AssertSubscriber

      public AssertSubscriber()
      Creates a new AssertSubscriber with 0 requested items and no upfront cancellation.
    • AssertSubscriber

      public AssertSubscriber(long requested)
      Creates a new AssertSubscriber with no upfront cancellation.
      Parameters:
      requested - the number of initially requested items
  • Method Details

    • create

      public static <T> AssertSubscriber<T> create()
      Creates a new AssertSubscriber with 0 requested items and no upfront cancellation.
      Type Parameters:
      T - the items type
      Returns:
      a new subscriber
    • create

      public static <T> AssertSubscriber<T> create(long requested)
      Creates a new AssertSubscriber with no upfront cancellation.
      Type Parameters:
      T - the items type
      Parameters:
      requested - the number of initially requested items
      Returns:
      a new subscriber
    • create

      public static <T> AssertSubscriber<T> create(Context context)
      Creates a new AssertSubscriber with 0 requested items and no upfront cancellation.
      Type Parameters:
      T - the items type
      Parameters:
      context - the context
      Returns:
      a new subscriber
    • create

      public static <T> AssertSubscriber<T> create(Context context, long requested)
      Creates a new AssertSubscriber with no upfront cancellation.
      Type Parameters:
      T - the items type
      Parameters:
      context - the context
      requested - the number of initially requested items
      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.
    • assertCompleted

      public AssertSubscriber<T> assertCompleted()
      Assert that the multi has completed.
      Returns:
      this AssertSubscriber
    • assertFailedWith

      public AssertSubscriber<T> assertFailedWith(Class<? extends Throwable> expectedTypeOfFailure, String expectedFailureMessage)
      Assert that the multi has failed.
      Parameters:
      expectedTypeOfFailure - the expected failure type
      expectedFailureMessage - a message to be contained in the failure message, or null when any message is fine
      Returns:
      this AssertSubscriber
    • assertFailedWith

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

      public AssertSubscriber<T> assertHasNotReceivedAnyItem()
      Assert that no item has been received yet.
      Returns:
      this AssertSubscriber
    • assertSubscribed

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

      public AssertSubscriber<T> assertNotSubscribed()
      Assert that the multi has not been subscribed.
      Returns:
      this AssertSubscriber
    • assertTerminated

      public AssertSubscriber<T> assertTerminated()
      Assert that the multi has been terminated, i.e. received a failure or a completion event.
      Returns:
      this AssertSubscriber
    • assertNotTerminated

      public AssertSubscriber<T> assertNotTerminated()
      Assert that the multi has not been terminated, i.e. did not received a failure or a completion event.
      Returns:
      this AssertSubscriber
    • assertItems

      @SafeVarargs public final AssertSubscriber<T> assertItems(T... expected)
      Assert that a sequence of items has been received (in whole and in exact order).
      Parameters:
      expected - a sequence of items
      Returns:
      this AssertSubscriber
    • getLastItem

      public T getLastItem()
      Returns:
      get the last received item, potentially null if no items have been received.
    • assertLastItem

      public AssertSubscriber<T> assertLastItem(T expected)
      Asserts that the last received item is equal to expected. The assertion fails if no items have been received.
      Parameters:
      expected - the expected item, must not be null
      Returns:
      this AssertSubscriber
    • awaitNextItem

      public AssertSubscriber<T> awaitNextItem()
      Awaits for the next item. If no item have been received before the default timeout, an AssertionError is thrown.

      Note that it requests one item from the upstream.

      Returns:
      this AssertSubscriber
      See Also:
    • awaitNextItem

      public AssertSubscriber<T> awaitNextItem(Duration duration)
      Awaits for the next item. If no item have been received before the given timeout, an AssertionError is thrown.

      Note that it requests one item from the upstream.

      Parameters:
      duration - the timeout, must not be null
      Returns:
      this AssertSubscriber
      See Also:
    • awaitNextItems

      public AssertSubscriber<T> awaitNextItems(int number)
      Awaits for the next number items. If not enough items have been received before the default timeout, an AssertionError is thrown.

      This method requests number items the upstream.

      Parameters:
      number - the number of items to expect, must be neither 0 nor negative.
      Returns:
      this AssertSubscriber
      See Also:
    • awaitNextItems

      public AssertSubscriber<T> awaitNextItems(int number, int request)
      Awaits for the next number items. If not enough items have been received before the default timeout, an AssertionError is thrown.
      Parameters:
      number - the number of items to expect, must neither be 0 nor negative.
      request - if not 0, the number of items to request upstream.
      Returns:
      this AssertSubscriber
    • awaitNextItems

      public AssertSubscriber<T> awaitNextItems(int number, Duration duration)
      Awaits for the next number items. If not enough items have been received before the given timeout, an AssertionError is thrown.

      This method requests number items upstream.

      Parameters:
      number - the number of items to expect, must be neither 0 nor negative.
      duration - the timeout, must not be null
      Returns:
      this AssertSubscriber
    • awaitNextItems

      public AssertSubscriber<T> awaitNextItems(int number, int request, Duration duration)
      Awaits for the next number items. If not enough items have been received before the given timeout, an AssertionError is thrown.
      Parameters:
      number - the number of items to expect, must be neither 0 nor negative.
      request - if not 0, the number of items to request upstream.
      duration - the timeout, must not be null
      Returns:
      this AssertSubscriber
    • awaitItems

      public AssertSubscriber<T> awaitItems(int number)
      Awaits for the subscriber to receive number items in total (including the ones received after calling this method). If not enough items have been received before the default timeout, an AssertionError is thrown.

      Unlike awaitNextItems(int, int), this method does not request items from the upstream.

      Parameters:
      number - the number of items to expect, must be neither 0 nor negative.
      Returns:
      this AssertSubscriber
    • awaitItems

      public AssertSubscriber<T> awaitItems(int number, Duration duration)
      Awaits for the subscriber to receive number items in total (including the ones received after calling this method). If not enough items have been received before the given timeout, an AssertionError is thrown.

      Unlike awaitNextItems(int, int), this method does not requests items from the upstream.

      Parameters:
      number - the number of items to expect, must be neither 0 nor negative.
      duration - the timeout, must not be null
      Returns:
      this AssertSubscriber
    • awaitCompletion

      public AssertSubscriber<T> awaitCompletion()
      Awaits for a completion event. It waits at most DEFAULT_TIMEOUT.

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

      Returns:
      this AssertSubscriber
    • awaitCompletion

      public AssertSubscriber<T> awaitCompletion(Duration duration)
      Awaits for a completion 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 AssertSubscriber
    • awaitFailure

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

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

      Returns:
      this AssertSubscriber
    • awaitFailure

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

      If the timeout expired, or if a completion 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 AssertSubscriber
    • awaitFailure

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

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

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

      public AssertSubscriber<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 a completion 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 AssertSubscriber
    • awaitSubscription

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

      If the timeout expired, the check fails.

      Returns:
      this AssertSubscriber
    • awaitSubscription

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

      If the timeout expired, the check fails.

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

      public AssertSubscriber<T> cancel()
      Cancel the subscription.
      Returns:
      this AssertSubscriber
    • request

      public AssertSubscriber<T> request(long req)
      Request items.
      Parameters:
      req - the number of items to request.
      Returns:
      this AssertSubscriber
    • onSubscribe

      public void onSubscribe(Flow.Subscription s)
      Specified by:
      onSubscribe in interface Flow.Subscriber<T>
    • onItem

      public void onItem(T t)
      Description copied from interface: MultiSubscriber
      Method called when the upstream emits an item event, in response to to requests to Flow.Subscription.request(long).
      Specified by:
      onItem in interface MultiSubscriber<T>
      Parameters:
      t - the item, must not be null.
    • onFailure

      public void onFailure(Throwable t)
      Description copied from interface: MultiSubscriber
      Method called when the upstream emits a failure terminal event.

      No further events will be sent even if Flow.Subscription.request(long) is invoked again.

      Specified by:
      onFailure in interface MultiSubscriber<T>
      Parameters:
      t - the failure, must not be null.
    • onCompletion

      public void onCompletion()
      Description copied from interface: MultiSubscriber
      Method called when the upstream emits a completion terminal event.

      No further events will be sent even if Flow.Subscription.request(long) is invoked again.

      Specified by:
      onCompletion in interface MultiSubscriber<T>
    • getItems

      public List<T> getItems()
      The list of items that have been received.
      Returns:
      the list
    • getFailure

      public Throwable getFailure()
      The reported failure, if any.
      Returns:
      the failure or null
    • run

      public AssertSubscriber<T> run(Runnable action)
      Run an action.
      Parameters:
      action - the action
      Returns:
      this AssertSubscriber
    • isCancelled

      public boolean isCancelled()
      Check whether the subscription has been cancelled or not.
      Returns:
      a boolean
    • hasCompleted

      public boolean hasCompleted()
      Check whether the multi has completed.
      Returns:
      a boolean