Class StubAggregateLifecycle

Direct Known Subclasses:
StubAggregateLifecycleExtension, StubAggregateLifecycleRule

public class StubAggregateLifecycle extends AggregateLifecycle
Stub implementation of an AggregateLifecycle that registers all applied events for verification later. This lifecycle instance can be activated (see activate()) and deactivated (see close()) at will. Events applied while it is active are stored and can be retrieved using getAppliedEvents() or getAppliedEventPayloads().

When using with JUnit, consider using the StubAggregateLifecycleRule with @Rule instead, as it is easier and safer to use.

  • Constructor Details

    • StubAggregateLifecycle

      public StubAggregateLifecycle()
  • Method Details

    • activate

      public void activate()
      Activates this lifecycle instance. Any invocations to static AggregateLifecycle methods will use this instance until close() is called.
    • close

      public void close()
      Closes this lifecycle instance, restoring to the situation prior to this lifecycle being started. If any lifecycle instance was active before this one started, it will be reactivated.
    • getIsLive

      protected boolean getIsLive()
      Description copied from class: AggregateLifecycle
      Indicates whether this Aggregate instance is 'live'. This means events currently applied represent events that are currently happening, as opposed to events representing historic decisions.
      Specified by:
      getIsLive in class AggregateLifecycle
      Returns:
      true if the aggregate is 'live', false if the aggregate is initializing state based on historic events
    • doCreateNew

      protected <T> Aggregate<T> doCreateNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception
      Description copied from class: AggregateLifecycle
      Creates a new aggregate instance. In order for new aggregate to be created, a Repository should be available to the current aggregate. Repository of an aggregate to be created is exposed to the current aggregate via RepositoryProvider.
      Specified by:
      doCreateNew in class AggregateLifecycle
      Type Parameters:
      T - type of new aggregate to be created
      Parameters:
      aggregateType - type of new aggregate to be created
      factoryMethod - factory method which creates new aggregate
      Returns:
      a new aggregate instance
      Throws:
      Exception - thrown if something goes wrong during instantiation of new aggregate
    • type

      protected String type()
      Description copied from class: AggregateLifecycle
      Retrieve a String denoting the type of this Aggregate.
      Specified by:
      type in class AggregateLifecycle
      Returns:
      a String denoting the type of this Aggregate
    • identifier

      protected Object identifier()
      Description copied from class: AggregateLifecycle
      Retrieve a Object denoting the identifier of this Aggregate.
      Specified by:
      identifier in class AggregateLifecycle
      Returns:
      a Object denoting the identifier of this Aggregate
    • version

      protected Long version()
      Description copied from class: AggregateLifecycle
      Gets the version of the aggregate.
      Specified by:
      version in class AggregateLifecycle
      Returns:
      the current version of the aggregate
    • doMarkDeleted

      protected void doMarkDeleted()
      Description copied from class: AggregateLifecycle
      Marks this aggregate as deleted. Implementations may react differently to aggregates marked for deletion. Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event information.
      Specified by:
      doMarkDeleted in class AggregateLifecycle
    • doApply

      protected <T> ApplyMore doApply(T payload, MetaData metaData)
      Description copied from class: AggregateLifecycle
      Apply a DomainEventMessage with given payload and metadata (metadata from interceptors will be combined with the provided metadata). The event should be applied to the aggregate immediately and scheduled for publication to other event handlers.

      The event should be applied on all entities part of this aggregate. If the event is applied from an event handler of the aggregate and additional events need to be applied that depends on state changes brought about by the first event the returned ApplyMore instance should allow for additional events to be applied after this event.

      Specified by:
      doApply in class AggregateLifecycle
      Parameters:
      payload - the payload of the event to apply
      metaData - any meta-data that must be registered with the Event
      Returns:
      a gizmo to apply additional events after the given event has been processed by the entire aggregate
      See Also:
    • getAppliedEvents

      public List<EventMessage<?>> getAppliedEvents()
      Returns the list of applied Events for this lifecycle instance.

      Note that this list is not reset when activating or deactivating the lifecycle.

      Returns:
      the list of messages applied while this lifecycle instance was active
    • getAppliedEventPayloads

      public List<Object> getAppliedEventPayloads()
      Returns the payloads of the Events applied while this lifecycle instance was active. This is usually the parameter passed to the AggregateLifecycle.apply(Object) method.
      Returns:
      the payloads of the applied events.
    • isMarkedDeleted

      public boolean isMarkedDeleted()
      Indicates whether an Aggregate has invoked "markDeleted" while this lifecycle was active.
      Returns:
      true if AggregateLifecycle.markDeleted() was invoked, otherwise false