Search in sources :

Example 1 with DefaultObjectInputStreamStrategy

use of uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy 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));
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) 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 2 with DefaultObjectInputStreamStrategy

use of uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy 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));
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) 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 3 with DefaultObjectInputStreamStrategy

use of uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy 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 4 with DefaultObjectInputStreamStrategy

use of uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy in project microservice_framework by CJSCommonPlatform.

the class CakeShopIT method tweakRecipeSnapshotName.

private void tweakRecipeSnapshotName(final String recipeId, final String newRecipeName) throws AggregateChangeDetectedException {
    final AggregateSnapshot<Recipe> recipeAggregateSnapshot = recipeAggregateSnapshotOf(recipeId).get();
    final Recipe recipe = recipeAggregateSnapshot.getAggregate(new DefaultObjectInputStreamStrategy());
    setField(recipe, "name", newRecipeName);
    SNAPSHOT_REPOSITORY.removeAllSnapshots(recipeAggregateSnapshot.getStreamId(), Recipe.class);
    SNAPSHOT_REPOSITORY.storeSnapshot(new AggregateSnapshot(recipeAggregateSnapshot.getStreamId(), recipeAggregateSnapshot.getVersionId(), recipe));
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) DefaultObjectInputStreamStrategy(uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot)

Example 5 with DefaultObjectInputStreamStrategy

use of uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method shouldNotStoreANewSnapshotOnTopOfExistingSnapshotsWhenThresholdNotMet.

@Test
public void shouldNotStoreANewSnapshotOnTopOfExistingSnapshotsWhenThresholdNotMet() throws Exception {
    final UUID streamId = randomUUID();
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
    final EventStream stream = eventSource.getStreamById(streamId);
    final TestAggregate aggregate = aggregateService.get(stream, TestAggregate.class);
    stream.append(createEventAndApply(streamId, SNAPSHOT_THRESHOLD - 2, aggregate));
    final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
    assertThat(snapshot, notNullValue());
    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(48));
    TestAggregate aggregateFromSnapshot = snapshot.get().getAggregate(new DefaultObjectInputStreamStrategy());
    assertThat(aggregateFromSnapshot.numberOfAppliedEvents(), is(25));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
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) 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)

Aggregations

AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)6 DefaultObjectInputStreamStrategy (uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy)6 UUID (java.util.UUID)5 UUID.randomUUID (java.util.UUID.randomUUID)5 Test (org.junit.Test)5 TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)5 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)5 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)2 SnapshotAwareEnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream)2 Recipe (uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)1