Search in sources :

Example 1 with CustomClassLoaderObjectInputStreamStrategy

use of uk.gov.justice.domain.aggregate.classloader.CustomClassLoaderObjectInputStreamStrategy 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));
}
Also used : DynamicAggregateTestClassGenerator(uk.gov.justice.domain.aggregate.classloader.DynamicAggregateTestClassGenerator) CustomClassLoaderObjectInputStreamStrategy(uk.gov.justice.domain.aggregate.classloader.CustomClassLoaderObjectInputStreamStrategy) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID.randomUUID(java.util.UUID.randomUUID) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Aggregations

UUID (java.util.UUID)1 UUID.randomUUID (java.util.UUID.randomUUID)1 Test (org.junit.Test)1 CustomClassLoaderObjectInputStreamStrategy (uk.gov.justice.domain.aggregate.classloader.CustomClassLoaderObjectInputStreamStrategy)1 DynamicAggregateTestClassGenerator (uk.gov.justice.domain.aggregate.classloader.DynamicAggregateTestClassGenerator)1 AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)1 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)1