Search in sources :

Example 16 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream 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 17 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream 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)

Example 18 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class MakeCakeCommandHandler method makeCake.

@Handles("example.command.make-cake")
public void makeCake(final JsonEnvelope command) throws EventStreamException {
    LOGGER.info("=============> Inside make-cake Command Handler");
    final UUID recipeId = getUUID(command.payloadAsJsonObject(), FIELD_RECIPE_ID).get();
    final UUID cakeId = getUUID(command.payloadAsJsonObject(), FIELD_CAKE_ID).get();
    final EventStream eventStream = eventSource.getStreamById(recipeId);
    final Recipe recipe = aggregateService.get(eventStream, Recipe.class);
    eventStream.append(recipe.makeCake(cakeId).map(toEnvelopeWithMetadataFrom(command)), Tolerance.CONSECUTIVE);
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) UUID(java.util.UUID) JsonObjects.getUUID(uk.gov.justice.services.messaging.JsonObjects.getUUID) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 19 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class RecipeCommandHandler method uploadPhotograph.

@Handles("example.command.upload-photograph")
public void uploadPhotograph(final JsonEnvelope command) throws EventStreamException {
    LOGGER.trace("=============> Inside upload-photograph Command Handler. RecipeId: " + command.payloadAsJsonObject().getString(FIELD_RECIPE_ID));
    final UUID recipeId = getUUID(command.payloadAsJsonObject(), FIELD_RECIPE_ID).get();
    final UUID photoId = getUUID(command.payloadAsJsonObject(), FIELD_PHOTO_ID).get();
    final EventStream eventStream = eventSource.getStreamById(recipeId);
    final Recipe recipe = aggregateService.get(eventStream, Recipe.class);
    eventStream.append(recipe.addPhotograph(photoId).map(toEnvelopeWithMetadataFrom(command)), Tolerance.NON_CONSECUTIVE);
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) UUID(java.util.UUID) JsonObjects.getUUID(uk.gov.justice.services.messaging.JsonObjects.getUUID) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 20 with EventStream

use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.

the class RecipeCommandHandler method renameRecipe.

@Handles("example.command.rename-recipe")
public void renameRecipe(final Envelope<RenameRecipe> command) throws EventStreamException {
    LOGGER.trace("=============> Inside rename-recipe Command Handler");
    final UUID recipeId = UUID.fromString(command.payload().getRecipeId());
    final String name = command.payload().getName();
    final EventStream eventStream = eventSource.getStreamById(recipeId);
    final Recipe recipe = aggregateService.get(eventStream, Recipe.class);
    eventStream.append(recipe.renameRecipe(name).map(toEnvelopeWithMetadataFrom(command)), Tolerance.NON_CONSECUTIVE);
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonObjects.getString(uk.gov.justice.services.messaging.JsonObjects.getString) UUID(java.util.UUID) JsonObjects.getUUID(uk.gov.justice.services.messaging.JsonObjects.getUUID) Handles(uk.gov.justice.services.core.annotation.Handles)

Aggregations

EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)28 Test (org.junit.Test)19 UUID (java.util.UUID)18 UUID.randomUUID (java.util.UUID.randomUUID)13 EnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.EnvelopeEventStream)10 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)9 TestAggregate (uk.gov.justice.domain.aggregate.TestAggregate)7 ZonedDateTime (java.time.ZonedDateTime)5 Stream (java.util.stream.Stream)5 Handles (uk.gov.justice.services.core.annotation.Handles)5 SnapshotAwareEnvelopeEventStream (uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream)5 Recipe (uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)5 JsonObjects.getUUID (uk.gov.justice.services.messaging.JsonObjects.getUUID)5 JsonObject (javax.json.JsonObject)4 AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)3 EventA (uk.gov.justice.services.core.aggregate.event.EventA)3 EventFoundEvent (uk.gov.justice.services.core.extension.EventFoundEvent)3 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)3 DefaultObjectInputStreamStrategy (uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy)2 JsonObjects.getString (uk.gov.justice.services.messaging.JsonObjects.getString)2