use of uk.gov.justice.domain.snapshot.AggregateSnapshot in project microservice_framework by CJSCommonPlatform.
the class SnapshotAwareAggregateServiceIT method shouldNotStoreABrandNewSnapshotWhenEventCountInTheStreamReachesThresholdNotMet.
@Test
public void shouldNotStoreABrandNewSnapshotWhenEventCountInTheStreamReachesThresholdNotMet() throws Exception {
final UUID streamId = randomUUID();
appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD - 2);
final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
assertThat(snapshot, not(nullValue()));
assertThat(snapshot.isPresent(), equalTo(false));
assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(23));
assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
use of uk.gov.justice.domain.snapshot.AggregateSnapshot in project microservice_framework by CJSCommonPlatform.
the class SnapshotAwareAggregateServiceIT method shouldStoreABrandNewSnapshotWhenEventCountInTheStreamReachesThreshold.
@Test
public void shouldStoreABrandNewSnapshotWhenEventCountInTheStreamReachesThreshold() throws Exception {
final UUID streamId = randomUUID();
appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
final TestAggregate aggregateFromSnapshot = snapshot.get().getAggregate(new DefaultObjectInputStreamStrategy());
assertThat(snapshot, not(nullValue()));
assertThat(snapshot.isPresent(), equalTo(true));
assertThat(snapshot.get().getType(), equalTo(TYPE));
assertThat(snapshot.get().getStreamId(), equalTo(streamId));
assertThat(snapshot.get().getVersionId(), equalTo(25L));
assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(25));
assertThat(aggregateFromSnapshot.numberOfAppliedEvents(), is(25));
assertThat(aggregateFromSnapshot.recordedEvents().size(), is(25));
assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
use of uk.gov.justice.domain.snapshot.AggregateSnapshot in project microservice_framework by CJSCommonPlatform.
the class SnapshotAwareAggregateServiceIT method shouldCreateNewSnapshotOnAggregateChangeWhenWeHaveMultipleExistingSnapshots.
@Test
public void shouldCreateNewSnapshotOnAggregateChangeWhenWeHaveMultipleExistingSnapshots() throws Exception {
final Class aggregateClass = TestAggregate.class;
final UUID streamId = randomUUID();
final long initialNumberOfSnapshots = 4;
for (int i = 0; i < initialNumberOfSnapshots; i++) {
appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
}
final Optional<AggregateSnapshot> snapshot = snapshotRepository.getLatestSnapshot(streamId, aggregateClass);
assertThat(snapshot, not(nullValue()));
assertThat(snapshot.isPresent(), equalTo(true));
appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD - 2);
final Optional<AggregateSnapshot> newSnapshot = snapshotRepository.getLatestSnapshot(streamId, aggregateClass);
assertThat(newSnapshot, not(nullValue()));
assertThat(newSnapshot.isPresent(), equalTo(true));
assertThat(newSnapshot.get().getType(), equalTo(aggregateClass.getName()));
assertThat(newSnapshot.get().getStreamId(), equalTo(streamId));
assertThat(newSnapshot.get().getVersionId(), equalTo(initialNumberOfSnapshots * SNAPSHOT_THRESHOLD));
assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(123));
TestAggregate aggregateFromSnapshot2 = (TestAggregate) newSnapshot.get().getAggregate(new DefaultObjectInputStreamStrategy());
assertThat(aggregateFromSnapshot2.numberOfAppliedEvents(), is(100));
assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
use of uk.gov.justice.domain.snapshot.AggregateSnapshot in project microservice_framework by CJSCommonPlatform.
the class SnapshotAwareAggregateServiceIT method shouldRebuildSnapshotOnAggregateModelChange.
@Test
public void shouldRebuildSnapshotOnAggregateModelChange() throws Exception {
final UUID streamId = randomUUID();
final DynamicAggregateTestClassGenerator classGenerator = new DynamicAggregateTestClassGenerator();
final Class oldAggregateClass = classGenerator.generatedTestAggregateClassOf(1L, TEST_AGGREGATE_PACKAGE, TEST_AGGREGATE_CLASS_NAME);
final long initialNumberOfSnapshots = 4;
for (int i = 1; i <= initialNumberOfSnapshots; i++) {
createEventStreamAndApply(streamId, SNAPSHOT_THRESHOLD, "context.eventA", oldAggregateClass);
}
final Optional<AggregateSnapshot> snapshot = snapshotRepository.getLatestSnapshot(streamId, oldAggregateClass);
assertThat(snapshot, not(nullValue()));
assertThat(snapshot.isPresent(), equalTo(true));
assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(100));
final Class newAggregateClass = classGenerator.generatedTestAggregateClassOf(2L, TEST_AGGREGATE_PACKAGE, TEST_AGGREGATE_CLASS_NAME);
snapshotService.setStreamStrategy(new CustomClassLoaderObjectInputStreamStrategy(classLoaderWithGeneratedAggregateLoaded()));
createEventStreamAndApply(streamId, SNAPSHOT_THRESHOLD - 2, "context.eventA", newAggregateClass);
final Optional<AggregateSnapshot> newSnapshot = snapshotRepository.getLatestSnapshot(streamId, newAggregateClass);
assertThat(newSnapshot, not(nullValue()));
assertThat(newSnapshot.isPresent(), equalTo(true));
assertThat(newSnapshot.get().getType(), equalTo(newAggregateClass.getName()));
assertThat(newSnapshot.get().getStreamId(), equalTo(streamId));
assertThat(newSnapshot.get().getVersionId(), equalTo(123L));
assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(123));
assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
use of uk.gov.justice.domain.snapshot.AggregateSnapshot 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));
}
Aggregations