Search in sources :

Example 6 with EventA

use of uk.gov.justice.domain.event.EventA in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceTest method shouldRebuildAggregateOnModelChange.

@Test
public void shouldRebuildAggregateOnModelChange() throws AggregateChangeDetectedException {
    defaultAggregateService.logger = logger;
    defaultAggregateService.jsonObjectToObjectConverter = jsonObjectToObjectConverter;
    final UUID streamId = UUID.randomUUID();
    final long currentStreamVersion = 3L;
    final JsonEnvelope jsonEventA = envelope().with(metadataWithRandomUUID("eventA")).withPayloadOf("value1", "name1").build();
    final JsonEnvelope jsonEventB = envelope().with(metadataWithRandomUUID("eventB")).withPayloadOf("value2", "name1").build();
    final JsonEnvelope jsonEventC = envelope().with(metadataWithRandomUUID("eventC")).withPayloadOf("value3", "name1").build();
    registerEvent(EventA.class, "eventA");
    registerEvent(EventB.class, "eventB");
    registerEvent(EventC.class, "eventC");
    final EventA eventA = new EventA("A1");
    final EventB eventB = new EventB("B1");
    final EventC eventC = new EventC("C1");
    when(eventStream.getId()).thenReturn(streamId);
    when(eventStream.getCurrentVersion()).thenReturn(currentStreamVersion);
    doThrow(new AggregateChangeDetectedException("Aggregate Change Detected")).when(snapshotService).getLatestVersionedAggregate(streamId, TestAggregate.class);
    when(jsonObjectToObjectConverter.convert(jsonEventA.payloadAsJsonObject(), EventA.class)).thenReturn(eventA);
    when(jsonObjectToObjectConverter.convert(jsonEventB.payloadAsJsonObject(), EventB.class)).thenReturn(eventB);
    when(jsonObjectToObjectConverter.convert(jsonEventC.payloadAsJsonObject(), EventC.class)).thenReturn(eventC);
    when(eventStream.read()).thenReturn(of(jsonEventA, jsonEventB, jsonEventC));
    final TestAggregate aggregate1 = aggregateService.get(eventStream, TestAggregate.class);
    verify(snapshotService).removeAllSnapshots(streamId, TestAggregate.class);
    assertThat(aggregate1.recordedEvents(), hasItems(eventA, eventB, eventC));
}
Also used : AggregateChangeDetectedException(uk.gov.justice.services.core.aggregate.exception.AggregateChangeDetectedException) EventB(uk.gov.justice.domain.event.EventB) EventC(uk.gov.justice.domain.event.EventC) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) EventA(uk.gov.justice.domain.event.EventA) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 7 with EventA

use of uk.gov.justice.domain.event.EventA 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 8 with EventA

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

Aggregations

EventA (uk.gov.justice.domain.event.EventA)8 Test (org.junit.Test)7 TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)7 VersionedAggregate (uk.gov.justice.domain.snapshot.VersionedAggregate)6 JsonObject (javax.json.JsonObject)5 EventB (uk.gov.justice.domain.event.EventB)4 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)3 UUID (java.util.UUID)2 UUID.randomUUID (java.util.UUID.randomUUID)2 EventC (uk.gov.justice.domain.event.EventC)2 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)2 LinkedList (java.util.LinkedList)1 AggregateChangeDetectedException (uk.gov.justice.services.core.aggregate.exception.AggregateChangeDetectedException)1 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)1 SnapshotAwareEnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream)1