Search in sources :

Example 16 with TestAggregate

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

the class SnapshotAwareAggregateServiceTest method shouldCreateAggregateFromStreamOfThreeWithAFilteredOutSystemEvent.

@Test
public void shouldCreateAggregateFromStreamOfThreeWithAFilteredOutSystemEvent() throws AggregateChangeDetectedException {
    final Optional<VersionedAggregate<TestAggregate>> versionedAggregate = Optional.empty();
    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.getId()).thenReturn(STREAM_ID);
    when(eventStream.read()).thenReturn(Stream.of(envelopeFrom(metadataWithRandomUUID("eventA"), eventPayloadA), envelopeFrom(metadataWithRandomUUID("eventB"), eventPayloadB), envelopeFrom(metadataWithRandomUUID("system.events.eventC"), eventPayloadB)));
    when(snapshotService.getLatestVersionedAggregate(STREAM_ID, TestAggregate.class)).thenReturn(versionedAggregate);
    when(eventStream.getId()).thenReturn(STREAM_ID);
    registerEvent(EventA.class, "eventA");
    registerEvent(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);
}
Also used : EventB(uk.gov.justice.domain.event.EventB) 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 17 with TestAggregate

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

the class SnapshotAwareAggregateServiceTest method shouldThrowExceptionForUnregisteredEvent.

@Test(expected = IllegalStateException.class)
public void shouldThrowExceptionForUnregisteredEvent() throws AggregateChangeDetectedException {
    defaultAggregateService.logger = logger;
    defaultAggregateService.jsonObjectToObjectConverter = jsonObjectToObjectConverter;
    final JsonObject eventPayloadA = mock(JsonObject.class);
    final EventA eventA = mock(EventA.class);
    when(eventStream.getId()).thenReturn(STREAM_ID);
    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)));
    aggregateService.get(eventStream, 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 18 with TestAggregate

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

the class DefaultAggregateServiceTest method shouldCreateAggregateFromStreamOfTwo.

@Test
public void shouldCreateAggregateFromStreamOfTwo() {
    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)));
    when(eventStream.getId()).thenReturn(STREAM_ID);
    registerEvent(EventA.class, "eventA");
    registerEvent(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);
}
Also used : EventB(uk.gov.justice.services.core.aggregate.event.EventB) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonObject(javax.json.JsonObject) EventA(uk.gov.justice.services.core.aggregate.event.EventA) Test(org.junit.Test)

Example 19 with TestAggregate

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

the class SnapshotAwareAggregateServiceTest method shouldCreateAggregateFromEmptyStream.

@Test
public void shouldCreateAggregateFromEmptyStream() throws AggregateChangeDetectedException {
    when(eventStream.getId()).thenReturn(STREAM_ID);
    when(snapshotService.getLatestVersionedAggregate(STREAM_ID, TestAggregate.class)).thenReturn(Optional.of(new VersionedAggregate<>(INITIAL_AGGREGATE_VERSION, new TestAggregate())));
    when(eventStream.readFrom(NEXT_AGGREGATE_VERSION)).thenReturn(Stream.empty());
    final TestAggregate aggregate = aggregateService.get(eventStream, TestAggregate.class);
    assertThat(aggregate, notNullValue());
    assertThat(aggregate.recordedEvents(), empty());
    verify(logger).trace("SnapshotAwareAggregateService Recreating aggregate for instance {} of aggregate type {}", STREAM_ID, TestAggregate.class);
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) VersionedAggregate(uk.gov.justice.domain.snapshot.VersionedAggregate) Test(org.junit.Test)

Example 20 with TestAggregate

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

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