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)));
}
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)));
}
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();
}
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));
}
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));
}
Aggregations