Search in sources :

Example 21 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceTest method shouldCreateAggregateFromStreamWithOneEvent.

@Test
public void shouldCreateAggregateFromStreamWithOneEvent() throws AggregateChangeDetectedException {
    Optional<VersionedAggregate<TestAggregate>> versionedAggregate = Optional.empty();
    final EventA eventA = new EventA();
    final JsonObject eventPayloadA = mock(JsonObject.class);
    when(jsonObjectToObjectConverter.convert(eventPayloadA, EventA.class)).thenReturn(eventA);
    when(eventStream.getId()).thenReturn(STREAM_ID);
    when(eventStream.read()).thenReturn(of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA)));
    when(snapshotService.getLatestVersionedAggregate(STREAM_ID, TestAggregate.class)).thenReturn(versionedAggregate);
    registerEvent(EventA.class, "eventA");
    registerEvent(EventB.class, "eventB");
    final TestAggregate aggregateActual = aggregateService.get(eventStream, TestAggregate.class);
    assertThat(aggregateActual, notNullValue());
    assertThat(aggregateActual.recordedEvents(), hasSize(1));
    assertThat(aggregateActual.recordedEvents(), hasItems(eventA));
    verify(logger).info("Registering event {}, {} with DefaultAggregateService", "eventA", EventA.class);
    verify(logger).trace("Recreating aggregate for instance {} of aggregate type {}", STREAM_ID, TestAggregate.class);
    verify(logger).trace("SnapshotAwareAggregateService Recreating aggregate for instance {} of aggregate type {}", STREAM_ID, TestAggregate.class);
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonObject(javax.json.JsonObject) EventA(uk.gov.justice.domain.event.EventA) VersionedAggregate(uk.gov.justice.domain.snapshot.VersionedAggregate) Test(org.junit.Test)

Example 22 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceTest method shouldThrowExceptionForNonInstantiatableEvent.

@Test(expected = RuntimeException.class)
public void shouldThrowExceptionForNonInstantiatableEvent() throws AggregateChangeDetectedException {
    defaultAggregateService.logger = logger;
    defaultAggregateService.jsonObjectToObjectConverter = jsonObjectToObjectConverter;
    final JsonObject eventPayloadA = mock(JsonObject.class);
    final EventA eventA = mock(EventA.class);
    when(snapshotService.getLatestVersionedAggregate(STREAM_ID, TestAggregate.class)).thenReturn(Optional.of(new VersionedAggregate<>(INITIAL_AGGREGATE_VERSION, new TestAggregate())));
    when(jsonObjectToObjectConverter.convert(eventPayloadA, EventA.class)).thenReturn(eventA);
    when(eventStream.readFrom(NEXT_AGGREGATE_VERSION)).thenReturn(of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA)));
    registerEvent(EventA.class, "eventA");
    aggregateService.get(eventStream, PrivateAggregate.class);
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonObject(javax.json.JsonObject) EventA(uk.gov.justice.domain.event.EventA) VersionedAggregate(uk.gov.justice.domain.snapshot.VersionedAggregate) Test(org.junit.Test)

Example 23 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method shouldNotStoreABrandNewSnapshotWhenStrategyDoesNotMandateSavingSnapshot.

@Test
public void shouldNotStoreABrandNewSnapshotWhenStrategyDoesNotMandateSavingSnapshot() throws Exception {
    final UUID streamId = randomUUID();
    final EventStream stream = eventSource.getStreamById(streamId);
    final TestAggregate aggregate = aggregateService.get(stream, TestAggregate.class);
    stream.append(createEventAndApply(streamId, 24, aggregate));
    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(24));
    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) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Example 24 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate 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)

Example 25 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceIT method shouldStoreANewSnapshotOnTopOfExistingSnapshot.

@Test
public void shouldStoreANewSnapshotOnTopOfExistingSnapshot() throws Exception {
    final UUID streamId = randomUUID();
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
    final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
    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(50L));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(50));
    TestAggregate aggregateFromSnapshot = snapshot.get().getAggregate(new DefaultObjectInputStreamStrategy());
    assertThat(aggregateFromSnapshot.numberOfAppliedEvents(), is(50));
    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)

Aggregations

TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)28 Test (org.junit.Test)27 UUID (java.util.UUID)8 UUID.randomUUID (java.util.UUID.randomUUID)8 JsonObject (javax.json.JsonObject)8 AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)8 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)8 EventA (uk.gov.justice.domain.event.EventA)7 VersionedAggregate (uk.gov.justice.domain.snapshot.VersionedAggregate)7 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)7 DefaultObjectInputStreamStrategy (uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy)5 EventA (uk.gov.justice.services.core.aggregate.event.EventA)5 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)5 EventB (uk.gov.justice.domain.event.EventB)4 SnapshotAwareEnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream)4 EventB (uk.gov.justice.services.core.aggregate.event.EventB)3 EventFoundEvent (uk.gov.justice.services.core.extension.EventFoundEvent)3 EnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream)3 NoSerializableTestAggregate (uk.gov.justice.domain.aggregate.NoSerializableTestAggregate)2 EventC (uk.gov.justice.domain.event.EventC)2