Search in sources :

Example 11 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class EventsService method events.

public List<EventEntry> events(final UUID streamId, final Position position, final Direction direction, final long pageSize) {
    final EventStream eventStream = eventSource.getStreamById(streamId);
    if (position.isHead()) {
        final long versionHead = eventStream.size() - pageSize + 1;
        final Stream<JsonEnvelope> events = eventStream.readFrom(versionHead).limit(pageSize);
        return reverse(eventEntries(events));
    }
    if (position.isFirst()) {
        final int versionFirst = 1;
        final Stream<JsonEnvelope> events = eventStream.readFrom(versionFirst).limit(pageSize);
        return reverse(eventEntries(events));
    }
    if (FORWARD.equals(direction)) {
        final long version = position.getPosition();
        final Stream<JsonEnvelope> events = eventStream.readFrom(version).limit(pageSize);
        return reverse(eventEntries(events));
    }
    if (BACKWARD.equals(direction)) {
        final long version = position.getPosition() - pageSize + 1;
        final Stream<JsonEnvelope> events = eventStream.readFrom(version).limit(pageSize);
        return reverse(eventEntries(events));
    }
    return emptyList();
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Example 12 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method shouldNotCreateNewSnapshotOnAggregateChangeWhenWeJustOneExistingSnapshots.

@Test
public void shouldNotCreateNewSnapshotOnAggregateChangeWhenWeJustOneExistingSnapshots() throws Exception {
    final Class aggregateClass = TestAggregate.class;
    final UUID streamId = randomUUID();
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
    final Optional<AggregateSnapshot> snapshot = snapshotRepository.getLatestSnapshot(streamId, aggregateClass);
    assertThat(snapshot, not(nullValue()));
    assertThat(snapshot.isPresent(), equalTo(true));
    final EventStream updatedStream = eventSource.getStreamById(streamId);
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD - 2);
    final Optional<AggregateSnapshot<TestAggregate>> snapshotChanged = snapshotRepository.getLatestSnapshot(streamId, aggregateClass);
    assertThat(snapshotChanged, not(nullValue()));
    assertThat(snapshotChanged.isPresent(), equalTo(true));
    assertThat(snapshotChanged.get().getType(), equalTo(aggregateClass.getName()));
    assertThat(snapshotChanged.get().getStreamId(), equalTo(streamId));
    assertThat(snapshotChanged.get().getVersionId(), equalTo(25L));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(48));
    final TestAggregate aggregateFromSnapshot = snapshotChanged.get().getAggregate(new DefaultObjectInputStreamStrategy());
    assertThat(aggregateFromSnapshot.numberOfAppliedEvents(), is(25));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) SnapshotAwareEnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID.randomUUID(java.util.UUID.randomUUID) DefaultObjectInputStreamStrategy(uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Example 13 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method createEventStreamAndApply.

private <T extends Aggregate> void createEventStreamAndApply(final UUID streamId, final long count, final String eventName, final Class<T> aggregateClass) throws EventStreamException {
    final EventStream eventStream = eventSource.getStreamById(streamId);
    final T aggregate = aggregateService.get(eventStream, aggregateClass);
    final List<JsonEnvelope> envelopes = new LinkedList<>();
    for (int i = 1; i <= count; i++) {
        final JsonEnvelope envelope = envelope().with(metadataWithRandomUUID(eventName).createdAt(clock.now()).withStreamId(streamId)).withPayloadOf("value", "name").build();
        aggregate.apply(new EventA(String.valueOf(i)));
        envelopes.add(envelope);
    }
    eventStream.append(envelopes.stream());
}
Also used : SnapshotAwareEnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) EventA(uk.gov.justice.domain.event.EventA) LinkedList(java.util.LinkedList)

Example 14 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method appendEventsViaAggregate.

private void appendEventsViaAggregate(final UUID streamId, final long eventCount) throws Exception {
    final EventStream eventStream = eventSource.getStreamById(streamId);
    TestAggregate aggregateRebuilt = aggregateService.get(eventStream, TestAggregate.class);
    eventStream.append(createEventAndApply(streamId, eventCount, aggregateRebuilt));
}
Also used : SnapshotAwareEnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate)

Example 15 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class DefaultAggregateServiceIT method shouldCreateAggregateFromEmptyStream.

@Test
public void shouldCreateAggregateFromEmptyStream() {
    final EventStream eventStream = eventSource.getStreamById(STREAM_ID);
    final TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
    assertThat(aggregate, notNullValue());
    assertThat(aggregate.recordedEvents(), empty());
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, STREAM_ID), is(0));
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) EnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) Test(org.junit.Test)

Aggregations

EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)28 Test (org.junit.Test)19 UUID (java.util.UUID)18 UUID.randomUUID (java.util.UUID.randomUUID)13 EnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream)10 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)9 TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)7 ZonedDateTime (java.time.ZonedDateTime)5 Stream (java.util.stream.Stream)5 Handles (uk.gov.justice.services.core.annotation.Handles)5 SnapshotAwareEnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream)5 Recipe (uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)5 JsonObjects.getUUID (uk.gov.justice.services.messaging.JsonObjects.getUUID)5 JsonObject (javax.json.JsonObject)4 AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)3 EventA (uk.gov.justice.services.core.aggregate.event.EventA)3 EventFoundEvent (uk.gov.justice.services.core.extension.EventFoundEvent)3 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)3 DefaultObjectInputStreamStrategy (uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy)2 JsonObjects.getString (uk.gov.justice.services.messaging.JsonObjects.getString)2