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();
}
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));
}
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());
}
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));
}
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));
}
Aggregations