Search in sources :

Example 1 with EventC

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

the class SnapshotAwareAggregateServiceTest method shouldReplayDeltaOfEventsOnAggregate.

@Test
public void shouldReplayDeltaOfEventsOnAggregate() throws AggregateChangeDetectedException {
    defaultAggregateService.logger = logger;
    defaultAggregateService.jsonObjectToObjectConverter = jsonObjectToObjectConverter;
    final UUID streamId = UUID.randomUUID();
    final long currentStreamVersion = 5L;
    final TestAggregate aggregate = new TestAggregate();
    final long snapshotVersion = 2L;
    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);
    when(snapshotService.getLatestVersionedAggregate(streamId, TestAggregate.class)).thenReturn(Optional.of(new VersionedAggregate<>(snapshotVersion, aggregate)));
    when(eventStream.readFrom(snapshotVersion + 1)).thenReturn(of(jsonEventA, jsonEventB, jsonEventC));
    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);
    aggregateService.get(eventStream, TestAggregate.class);
    assertThat(aggregate.recordedEvents(), hasItems(eventA, eventB, eventC));
}
Also used : 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) VersionedAggregate(uk.gov.justice.domain.snapshot.VersionedAggregate) Test(org.junit.Test)

Example 2 with EventC

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

Aggregations

UUID (java.util.UUID)2 UUID.randomUUID (java.util.UUID.randomUUID)2 Test (org.junit.Test)2 TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)2 EventA (uk.gov.justice.domain.event.EventA)2 EventB (uk.gov.justice.domain.event.EventB)2 EventC (uk.gov.justice.domain.event.EventC)2 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)2 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)2 VersionedAggregate (uk.gov.justice.domain.snapshot.VersionedAggregate)1 AggregateChangeDetectedException (uk.gov.justice.services.core.aggregate.exception.AggregateChangeDetectedException)1