use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.
the class DefaultAggregateServiceTest method shouldCreateAggregateFromStreamOfThreeWithAFilteredOutSystemEvent.
@Test
public void shouldCreateAggregateFromStreamOfThreeWithAFilteredOutSystemEvent() {
JsonObject eventPayloadA = mock(JsonObject.class);
JsonObject eventPayloadB = mock(JsonObject.class);
EventA eventA = mock(EventA.class);
EventB eventB = mock(EventB.class);
when(jsonObjectToObjectConverter.convert(eventPayloadA, EventA.class)).thenReturn(eventA);
when(jsonObjectToObjectConverter.convert(eventPayloadB, EventB.class)).thenReturn(eventB);
when(eventStream.read()).thenReturn(Stream.of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA), envelopeFrom(metadataWithRandomUUID("eventB"), eventPayloadB), envelopeFrom(metadataWithRandomUUID("system.events.eventC"), eventPayloadB)));
when(eventStream.getId()).thenReturn(STREAM_ID);
aggregateService.register(new EventFoundEvent(EventA.class, "eventA"));
aggregateService.register(new EventFoundEvent(EventB.class, "eventB"));
TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
assertThat(aggregate, notNullValue());
assertThat(aggregate.recordedEvents(), hasSize(2));
assertThat(aggregate.recordedEvents().get(0), equalTo(eventA));
assertThat(aggregate.recordedEvents().get(1), equalTo(eventB));
verify(logger).info("Registering event {}, {} with DefaultAggregateService", "eventA", EventA.class);
verify(logger).info("Registering event {}, {} with DefaultAggregateService", "eventB", EventB.class);
verify(logger).trace("Recreating aggregate for instance {} of aggregate type {}", STREAM_ID, TestAggregate.class);
}
use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.
the class DefaultAggregateServiceTest method shouldCreateAggregateFromEmptyStream.
@Test
public void shouldCreateAggregateFromEmptyStream() {
when(eventStream.read()).thenReturn(Stream.empty());
when(eventStream.getId()).thenReturn(STREAM_ID);
TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
assertThat(aggregate, notNullValue());
assertThat(aggregate.recordedEvents(), empty());
verify(logger).trace("Recreating aggregate for instance {} of aggregate type {}", STREAM_ID, TestAggregate.class);
}
use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.
the class DefaultAggregateServiceTest method shouldCreateAggregateFromSingletonStream.
@Test
public void shouldCreateAggregateFromSingletonStream() {
JsonObject eventPayloadA = mock(JsonObject.class);
EventA eventA = mock(EventA.class);
when(jsonObjectToObjectConverter.convert(eventPayloadA, EventA.class)).thenReturn(eventA);
when(eventStream.read()).thenReturn(Stream.of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA)));
when(eventStream.getId()).thenReturn(STREAM_ID);
registerEvent(EventA.class, "eventA");
TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
assertThat(aggregate, notNullValue());
assertThat(aggregate.recordedEvents(), hasSize(1));
assertThat(aggregate.recordedEvents().get(0), equalTo(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);
}
use of uk.gov.justice.domain.aggregate.TestAggregate in project microservice_framework by CJSCommonPlatform.
the class SnapshotAwareAggregateServiceTest method shouldCreateAggregateFromStreamWithTwoEvents.
@Test
public void shouldCreateAggregateFromStreamWithTwoEvents() throws AggregateChangeDetectedException {
final EventA eventA = new EventA();
final EventB eventB = mock(EventB.class);
final JsonObject eventPayloadA = mock(JsonObject.class);
final JsonObject eventPayloadB = mock(JsonObject.class);
final Optional<VersionedAggregate<TestAggregate>> versionedAggregate = Optional.empty();
when(jsonObjectToObjectConverter.convert(eventPayloadA, EventA.class)).thenReturn(eventA);
when(jsonObjectToObjectConverter.convert(eventPayloadB, EventB.class)).thenReturn(eventB);
when(eventStream.getId()).thenReturn(STREAM_ID);
when(eventStream.read()).thenReturn(of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA), envelopeFrom(metadataWithRandomUUID("eventB"), eventPayloadB)));
when(snapshotService.getLatestVersionedAggregate(STREAM_ID, TestAggregate.class)).thenReturn(versionedAggregate);
registerEvent(EventA.class, "eventA");
registerEvent(EventB.class, "eventB");
final TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
assertThat(aggregate, notNullValue());
assertThat(aggregate.recordedEvents(), hasSize(2));
assertThat(aggregate.recordedEvents().get(0), equalTo(eventA));
assertThat(aggregate.recordedEvents().get(1), equalTo(eventB));
verify(logger).info("Registering event {}, {} with DefaultAggregateService", "eventA", EventA.class);
verify(logger).info("Registering event {}, {} with DefaultAggregateService", "eventB", EventB.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);
}
use of uk.gov.justice.domain.aggregate.TestAggregate 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));
}
Aggregations