use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.
the class EventStreamMockHelperTest method shouldVerifyAppendCallAndReturnStreamOfJsonEnvelopes.
@Test
public void shouldVerifyAppendCallAndReturnStreamOfJsonEnvelopes() throws Exception {
final EventStream eventStream = mock(EventStream.class);
final JsonEnvelope jsonEnvelope_1 = mock(JsonEnvelope.class);
final JsonEnvelope jsonEnvelope_2 = mock(JsonEnvelope.class);
eventStream.append(Stream.of(jsonEnvelope_1, jsonEnvelope_2));
final Stream<JsonEnvelope> jsonEnvelopeStream = EventStreamMockHelper.verifyAppendAndGetArgumentFrom(eventStream);
final List<JsonEnvelope> jsonEnvelopes = jsonEnvelopeStream.collect(toList());
assertThat(jsonEnvelopes, contains(jsonEnvelope_1, jsonEnvelope_2));
}
use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.
the class EventsServiceTest method shouldReturnFirstEvents.
@Test
public void shouldReturnFirstEvents() throws Exception {
final UUID streamId = 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(streamId, "Test Name1").withVersion(1L).withStreamId(streamId).createdAt(event1CreatedAt)).build();
final JsonEnvelope event2 = envelope().withPayloadOf("value2", "field2").with(metadataOf(streamId, "Test Name2").withVersion(2L).withStreamId(streamId).createdAt(event2CreatedAt)).build();
final EventStream eventStream = mock(EventStream.class);
when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
when(eventStream.size()).thenReturn(2L);
when(eventStream.readFrom(first().getPosition())).thenReturn(Stream.of(event1, event2));
final List<EventEntry> eventEntries = service.events(streamId, first(), FORWARD, pageSize);
assertThat(eventEntries, hasSize(2));
assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(0).getName(), is("Test Name2"));
assertThat(eventEntries.get(0).getPosition(), is(2L));
assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event2CreatedAt)));
assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
assertThat(eventEntries.get(0).getPayload(), is(payload2));
assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(1).getName(), is("Test Name1"));
assertThat(eventEntries.get(1).getPosition(), is(1L));
assertThat(eventEntries.get(1).getPayload(), is(payload1));
assertThat(eventEntries.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 EventsServiceTest method shouldReturnNextEvents.
@Test
public void shouldReturnNextEvents() throws Exception {
final UUID streamId = randomUUID();
final UUID firstEventId = randomUUID();
final UUID secondEventId = randomUUID();
final ZonedDateTime event3CreatedAt = now();
final ZonedDateTime event4CreatedAt = now();
final long pageSize = 2L;
final JsonObject payload4 = createObjectBuilder().add("field4", "value4").build();
final JsonObject payload3 = createObjectBuilder().add("field3", "value3").build();
final JsonEnvelope event4 = envelope().withPayloadOf("value4", "field4").with(metadataOf(secondEventId, "Test Name4").withVersion(4L).withStreamId(streamId).createdAt(event4CreatedAt)).build();
final JsonEnvelope event3 = envelope().withPayloadOf("value3", "field3").with(metadataOf(firstEventId, "Test Name3").withVersion(3L).withStreamId(streamId).createdAt(event3CreatedAt)).build();
final EventStream eventStream = mock(EventStream.class);
final long positionId = 3L;
when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
when(eventStream.readFrom(positionId)).thenReturn(Stream.of(event3, event4));
final List<EventEntry> eventEntries = service.events(streamId, position(positionId), FORWARD, pageSize);
assertThat(eventEntries, hasSize(2));
assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(0).getName(), is("Test Name4"));
assertThat(eventEntries.get(0).getPosition(), is(4L));
assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event4CreatedAt)));
assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
assertThat(eventEntries.get(0).getPayload(), is(payload4));
assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(1).getName(), is("Test Name3"));
assertThat(eventEntries.get(1).getPosition(), is(3L));
assertThat(eventEntries.get(1).getPayload(), is(payload3));
assertThat(eventEntries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event3CreatedAt)));
}
use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.
the class EventsServiceTest method shouldReturnPreviousEvents.
@Test
public void shouldReturnPreviousEvents() throws Exception {
final UUID streamId = randomUUID();
final UUID firstEventId = randomUUID();
final UUID secondEventId = randomUUID();
final ZonedDateTime event2CreatedAt = now();
final ZonedDateTime event3CreatedAt = now();
final ZonedDateTime event4CreatedAt = now();
final long pageSize = 2L;
final JsonObject payload3 = createObjectBuilder().add("field3", "value3").build();
final JsonObject payload2 = createObjectBuilder().add("field2", "value2").build();
final JsonEnvelope event2 = envelope().withPayloadOf("value2", "field2").with(metadataOf(secondEventId, "Test Name2").withVersion(2L).withStreamId(streamId).createdAt(event2CreatedAt)).build();
final JsonEnvelope event3 = envelope().withPayloadOf("value3", "field3").with(metadataOf(firstEventId, "Test Name3").withVersion(3L).withStreamId(streamId).createdAt(event3CreatedAt)).build();
final JsonEnvelope event4 = envelope().withPayloadOf("value4", "field4").with(metadataOf(secondEventId, "Test Name4").withVersion(4L).withStreamId(streamId).createdAt(event4CreatedAt)).build();
final EventStream eventStream = mock(EventStream.class);
when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
when(eventStream.readFrom(2L)).thenReturn(Stream.of(event2, event3, event4));
final List<EventEntry> eventEntries = service.events(streamId, position(3L), BACKWARD, pageSize);
assertThat(eventEntries, hasSize(2));
assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(0).getName(), is("Test Name3"));
assertThat(eventEntries.get(0).getPosition(), is(3L));
assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event3CreatedAt)));
assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
assertThat(eventEntries.get(0).getPayload(), is(payload3));
assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
assertThat(eventEntries.get(1).getName(), is("Test Name2"));
assertThat(eventEntries.get(1).getPosition(), is(2L));
assertThat(eventEntries.get(1).getPayload(), is(payload2));
assertThat(eventEntries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event2CreatedAt)));
}
use of uk.gov.justice.services.eventsourcing.source.core.EventStream in project microservice_framework by CJSCommonPlatform.
the class EventsServiceTest method shouldReturnEventExist.
@Test
public void shouldReturnEventExist() {
final UUID streamId = randomUUID();
final long position = 1L;
final ZonedDateTime createdAt = now();
final JsonEnvelope event = envelope().withPayloadOf("value1", "field1").with(metadataOf(streamId, "Test Name1").withVersion(position).withStreamId(streamId).createdAt(createdAt)).build();
final EventStream eventStream = mock(EventStream.class);
when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
when(eventStream.readFrom(1L)).thenReturn(Stream.of(event));
assertTrue(service.eventExists(streamId, position));
verify(eventSource).getStreamById(streamId);
verify(eventStream).readFrom(position);
}
Aggregations