Search in sources :

Example 21 with EventStream

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

the class RecipeCommandHandler method addRecipe.

@Handles("example.command.add-recipe")
public void addRecipe(final JsonEnvelope command) throws EventStreamException {
    LOGGER.trace("=============> Inside add-recipe Command Handler. RecipeId: " + command.payloadAsJsonObject().getString(FIELD_RECIPE_ID));
    final UUID recipeId = getUUID(command.payloadAsJsonObject(), FIELD_RECIPE_ID).get();
    final String name = getString(command.payloadAsJsonObject(), FIELD_NAME).get();
    final Boolean glutenFree = getBoolean(command.payloadAsJsonObject(), FIELD_GLUTEN_FREE).get();
    final List<Ingredient> ingredients = ingredientsFrom(command.payloadAsJsonObject());
    final EventStream eventStream = eventSource.getStreamById(recipeId);
    final Recipe recipe = aggregateService.get(eventStream, Recipe.class);
    eventStream.append(recipe.addRecipe(recipeId, name, glutenFree, ingredients).map(toEnvelopeWithMetadataFrom(command)));
}
Also used : Ingredient(uk.gov.justice.services.example.cakeshop.domain.Ingredient) 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) JsonObjects.getBoolean(uk.gov.justice.services.messaging.JsonObjects.getBoolean) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 22 with EventStream

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

the class EventsServiceTest method shouldReturnHeadEvents.

@Test
public void shouldReturnHeadEvents() throws Exception {
    final UUID streamId = randomUUID();
    final UUID firstEventId = randomUUID();
    final UUID secondEventId = randomUUID();
    final ZonedDateTime event1CreatedAt = now();
    final ZonedDateTime event2CreatedAt = now();
    final long pageSize = 2L;
    final JsonObject payload1 = createObjectBuilder().add("field1", "value1").build();
    final JsonObject payload2 = createObjectBuilder().add("field2", "value2").build();
    final JsonEnvelope event1 = envelope().withPayloadOf("value1", "field1").with(metadataOf(firstEventId, "Test Name1").withVersion(1L).withStreamId(streamId).createdAt(event1CreatedAt)).build();
    final JsonEnvelope event2 = envelope().withPayloadOf("value2", "field2").with(metadataOf(secondEventId, "Test Name2").withVersion(2L).withStreamId(streamId).createdAt(event2CreatedAt)).build();
    final EventStream eventStream = mock(EventStream.class);
    when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
    when(eventStream.getPosition()).thenReturn(2L);
    when(eventStream.readFrom(eventStream.size() - 1)).thenReturn(Stream.of(event1, event2));
    final List<EventEntry> entries = service.events(streamId, head(), BACKWARD, pageSize);
    assertThat(entries, hasSize(2));
    assertThat(entries.get(0).getStreamId(), is(streamId.toString()));
    assertThat(entries.get(0).getName(), is("Test Name2"));
    assertThat(entries.get(0).getPosition(), is(2L));
    assertThat(entries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event2CreatedAt)));
    assertThat(entries.get(0).getPayload(), is(notNullValue()));
    assertThat(entries.get(0).getPayload(), is(payload2));
    assertThat(entries.get(1).getStreamId(), is(streamId.toString()));
    assertThat(entries.get(1).getName(), is("Test Name1"));
    assertThat(entries.get(1).getPosition(), is(1L));
    assertThat(entries.get(1).getPayload(), is(payload1));
    assertThat(entries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event1CreatedAt)));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 23 with EventStream

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

the class EventsService method eventExists.

public boolean eventExists(final UUID streamId, final long version) {
    final EventStream eventStream = eventSource.getStreamById(streamId);
    final Stream<JsonEnvelope> event = eventStream.readFrom(version);
    return event.findAny().isPresent();
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Example 24 with EventStream

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

the class SnapshotAwareAggregateServiceIT method shouldNotStoreABrandNewSnapshotWhenStrategyDoesNotMandateSavingSnapshot.

@Test
public void shouldNotStoreABrandNewSnapshotWhenStrategyDoesNotMandateSavingSnapshot() throws Exception {
    final UUID streamId = randomUUID();
    final EventStream stream = eventSource.getStreamById(streamId);
    final TestAggregate aggregate = aggregateService.get(stream, TestAggregate.class);
    stream.append(createEventAndApply(streamId, 24, aggregate));
    final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
    assertThat(snapshot, not(nullValue()));
    assertThat(snapshot.isPresent(), equalTo(false));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(24));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
Also used : SnapshotAwareEnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID.randomUUID(java.util.UUID.randomUUID) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

Example 25 with EventStream

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

the class SnapshotAwareAggregateServiceIT method shouldNotStoreANewSnapshotOnTopOfExistingSnapshotsWhenThresholdNotMet.

@Test
public void shouldNotStoreANewSnapshotOnTopOfExistingSnapshotsWhenThresholdNotMet() throws Exception {
    final UUID streamId = randomUUID();
    appendEventsViaAggregate(streamId, SNAPSHOT_THRESHOLD);
    final EventStream stream = eventSource.getStreamById(streamId);
    final TestAggregate aggregate = aggregateService.get(stream, TestAggregate.class);
    stream.append(createEventAndApply(streamId, SNAPSHOT_THRESHOLD - 2, aggregate));
    final Optional<AggregateSnapshot<TestAggregate>> snapshot = snapshotRepository.getLatestSnapshot(streamId, TestAggregate.class);
    assertThat(snapshot, notNullValue());
    assertThat(snapshot.isPresent(), equalTo(true));
    assertThat(snapshot.get().getType(), equalTo(TYPE));
    assertThat(snapshot.get().getStreamId(), equalTo(streamId));
    assertThat(snapshot.get().getVersionId(), equalTo(25L));
    assertThat(rowCount(SQL_EVENT_LOG_COUNT_BY_STREAM_ID, streamId), is(48));
    TestAggregate aggregateFromSnapshot = snapshot.get().getAggregate(new DefaultObjectInputStreamStrategy());
    assertThat(aggregateFromSnapshot.numberOfAppliedEvents(), is(25));
    assertThat(rowCount(SQL_EVENT_STREAM_COUNT_BY_STREAM_ID, streamId), is(1));
}
Also used : SnapshotAwareEnvelopeEventStream(uk.gov.justice.services.eventsourcing.source.core.SnapshotAwareEnvelopeEventStream) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) UUID(java.util.UUID) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID.randomUUID(java.util.UUID.randomUUID) DefaultObjectInputStreamStrategy(uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy) AggregateSnapshot(uk.gov.justice.domain.snapshot.AggregateSnapshot) Test(org.junit.Test)

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