Search in sources :

Example 1 with EventStream

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));
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 2 with EventStream

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)));
}
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 3 with EventStream

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)));
}
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 4 with EventStream

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)));
}
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 5 with EventStream

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);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) 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