Search in sources :

Example 6 with TestAggregate

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

the class DefaultSnapshotServiceTest method shouldCreateSnapshotIfStrategyMandatesCreation.

@Test
public void shouldCreateSnapshotIfStrategyMandatesCreation() throws AggregateChangeDetectedException {
    final TestAggregate aggregate = new TestAggregate();
    final Optional<AggregateSnapshot<TestAggregate>> aggregateSnapshot = Optional.empty();
    final Long currentSnapshotVersion = 0l;
    final Long currentAggregateVersionId = 26l;
    when(snapshotRepository.getLatestSnapshot(STREAM_ID, TestAggregate.class)).thenReturn(aggregateSnapshot);
    when(snapshotRepository.getLatestSnapshotVersion(STREAM_ID, TestAggregate.class)).thenReturn(currentSnapshotVersion);
    when(snapshotStrategy.shouldCreateSnapshot(currentAggregateVersionId, currentSnapshotVersion)).thenReturn(true);
    snapshotService.attemptAggregateStore(STREAM_ID, currentAggregateVersionId, aggregate);
    verify(snapshotRepository, times(1)).storeSnapshot(snapshotArgumentCaptor.capture());
    assertThat(snapshotArgumentCaptor.getValue(), notNullValue());
    assertThat(snapshotArgumentCaptor.getValue().getVersionId(), is(currentAggregateVersionId));
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) NoSerializableTestAggregate(uk.gov.justice.domain.aggregate.NoSerializableTestAggregate) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Example 7 with TestAggregate

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

the class DefaultSnapshotServiceTest method shouldNotCreateAggregateIfStrategyDoesNotMandatesCreation.

@Test
public void shouldNotCreateAggregateIfStrategyDoesNotMandatesCreation() throws AggregateChangeDetectedException {
    final Long currentSnapshotVersion = 0l;
    final Long currentAggregateVersionId = 26l;
    final TestAggregate aggregate = new TestAggregate();
    when(snapshotRepository.getLatestSnapshotVersion(STREAM_ID, TestAggregate.class)).thenReturn(currentSnapshotVersion);
    when(snapshotStrategy.shouldCreateSnapshot(currentAggregateVersionId, currentSnapshotVersion)).thenReturn(false);
    snapshotService.attemptAggregateStore(STREAM_ID, currentAggregateVersionId, aggregate);
    verify(snapshotRepository, never()).storeSnapshot(any(AggregateSnapshot.class));
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) NoSerializableTestAggregate(uk.gov.justice.domain.aggregate.NoSerializableTestAggregate) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Example 8 with TestAggregate

use of uk.gov.justice.domain.aggregate.TestAggregate 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));
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) EnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) Test(org.junit.Test)

Example 9 with TestAggregate

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

the class DefaultAggregateServiceIT method shouldCreateAggregateFromSingletonStream.

@Test
public void shouldCreateAggregateFromSingletonStream() throws EventStreamException {
    final EventStream eventStream = eventSource.getStreamById(STREAM_ID);
    aggregateService.register(new EventFoundEvent(EventA.class, "context.eventA"));
    aggregateService.get(eventStream, TestAggregate.class);
    eventStream.append(Stream.of(envelopeFrom("context.eventA")));
    final TestAggregate aggregate = aggregateService.get(eventSource.getStreamById(STREAM_ID), TestAggregate.class);
    assertThat(aggregate, notNullValue());
    assertThat(aggregate.recordedEvents(), hasSize(1));
    assertThat(aggregate.recordedEvents().get(0).getClass(), equalTo(EventA.class));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, STREAM_ID), is(1));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, STREAM_ID), is(1));
}
Also used : EventFoundEvent(uk.gov.justice.services.core.extension.EventFoundEvent) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) EnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) EventA(uk.gov.justice.services.core.aggregate.event.EventA) Test(org.junit.Test)

Example 10 with TestAggregate

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

the class DefaultAggregateServiceIT method shouldCreateAggregateFromStreamOfTwo.

@Test
public void shouldCreateAggregateFromStreamOfTwo() throws EventStreamException {
    final EventStream eventStream = eventSource.getStreamById(STREAM_ID);
    aggregateService.register(new EventFoundEvent(EventA.class, "context.eventA"));
    aggregateService.register(new EventFoundEvent(EventB.class, "context.eventB"));
    aggregateService.get(eventStream, TestAggregate.class);
    eventStream.append(Stream.of(envelopeFrom("context.eventA"), envelopeFrom("context.eventB")));
    final TestAggregate aggregate = aggregateService.get(eventSource.getStreamById(STREAM_ID), TestAggregate.class);
    assertThat(aggregate, notNullValue());
    assertThat(aggregate.recordedEvents(), hasSize(2));
    assertThat(aggregate.recordedEvents().get(0).getClass(), equalTo(EventA.class));
    assertThat(aggregate.recordedEvents().get(1).getClass(), equalTo(EventB.class));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, STREAM_ID), is(2));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, STREAM_ID), is(1));
}
Also used : EventFoundEvent(uk.gov.justice.services.core.extension.EventFoundEvent) EventB(uk.gov.justice.services.core.aggregate.event.EventB) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) EnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) EventA(uk.gov.justice.services.core.aggregate.event.EventA) 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