Search in sources :

Example 1 with Event

use of uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event in project microservice_framework by CJSCommonPlatform.

the class EventsPageIT method storeEvents.

private void storeEvents(final int eventNumber) throws InvalidPositionException {
    for (int i = 1; i <= eventNumber; i++) {
        final UUID id = randomUUID();
        final Metadata metadata = JsonEnvelope.metadataBuilder().withId(id).withStreamId(STREAM_ID).withName("name").createdAt(now()).withVersion(i).build();
        final Event event = new Event(randomUUID(), STREAM_ID, valueOf(i), "Test Name" + i, metadata.asJsonObject().toString(), createObjectBuilder().add("field" + i, "value" + i).build().toString(), new UtcClock().now());
        eventsRepository.insert(event);
    }
}
Also used : UtcClock(uk.gov.justice.services.common.util.UtcClock) Metadata(uk.gov.justice.services.messaging.Metadata) Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID)

Example 2 with Event

use of uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event in project microservice_framework by CJSCommonPlatform.

the class DefaultEventRepository method storeEvent.

@Override
@Transactional(dontRollbackOn = OptimisticLockingRetryException.class)
public void storeEvent(final JsonEnvelope envelope) throws StoreEventRequestFailedException {
    try {
        final Event event = eventConverter.eventOf(envelope);
        logger.trace("Storing event {} into stream {} at position {}", event.getName(), event.getStreamId(), event.getSequenceId());
        eventJdbcRepository.insert(event);
    } catch (InvalidPositionException ex) {
        throw new StoreEventRequestFailedException(String.format("Could not store event for position %d of stream %s", envelope.metadata().position().orElse(null), envelope.metadata().streamId().orElse(null)), ex);
    }
}
Also used : StoreEventRequestFailedException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.StoreEventRequestFailedException) Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) InvalidPositionException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException) Transactional(javax.transaction.Transactional)

Example 3 with Event

use of uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event in project microservice_framework by CJSCommonPlatform.

the class DefaultEventRepositoryTest method shouldStoreEventEnvelope.

@Test
public void shouldStoreEventEnvelope() throws Exception {
    final String name = "name123";
    final Event event = new Event(null, STREAM_ID, POSITION, name, null, null, now());
    when(eventConverter.eventOf(envelope)).thenReturn(event);
    defaultEventRepository.storeEvent(envelope);
    verify(eventJdbcRepository).insert(event);
    verify(logger).trace("Storing event {} into stream {} at position {}", name, STREAM_ID, POSITION);
}
Also used : Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) Test(org.junit.Test)

Example 4 with Event

use of uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event in project microservice_framework by CJSCommonPlatform.

the class CakeShopIT method shouldSetDateCreatedTimestampInEventStore.

@Test
public void shouldSetDateCreatedTimestampInEventStore() {
    final UUID orderId = randomUUID();
    final Response commandResponse = sendTo(ORDERS_RESOURCE_URI + orderId.toString()).request().post(entity(jsonObject().add("recipeId", randomUUID().toString()).add("deliveryDate", "2016-01-21T23:42:03.522+07:00").build().toString(), ORDER_CAKE_MEDIA_TYPE));
    assertThat(commandResponse.getStatus(), is(ACCEPTED));
    await().until(() -> queryForOrder(orderId.toString()).httpCode() == OK);
    final Stream<Event> events = EVENT_LOG_REPOSITORY.findByStreamIdOrderByPositionAsc(orderId);
    final Event event = events.findFirst().get();
    assertThat(event.getCreatedAt(), is(notNullValue()));
    assertThat(event.getCreatedAt(), is(within(10L, SECONDS, new UtcClock().now())));
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(javax.ws.rs.core.Response) ApiResponse(uk.gov.justice.services.example.cakeshop.it.util.ApiResponse) UtcClock(uk.gov.justice.services.common.util.UtcClock) Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 5 with Event

use of uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event in project microservice_framework by CJSCommonPlatform.

the class CakeShopIT method shouldRegisterRecipeAddedEvent.

@Test
public void shouldRegisterRecipeAddedEvent() {
    final String recipeId = "163af847-effb-46a9-96bc-32a0f7526f99";
    sendTo(RECIPES_RESOURCE_URI + recipeId).request().post(entity(jsonObject().add("name", "Vanilla cake").add("glutenFree", false).add("ingredients", createArrayBuilder().add(createObjectBuilder().add("name", "vanilla").add("quantity", 2)).build()).build().toString(), ADD_RECIPE_MEDIA_TYPE));
    await().until(() -> eventsWithPayloadContaining(recipeId).size() == 1);
    final Event event = eventsWithPayloadContaining(recipeId).get(0);
    assertThat(event.getName(), is("example.recipe-added"));
    with(event.getMetadata()).assertEquals("stream.id", recipeId).assertEquals("stream.version", 1);
    final String eventPayload = event.getPayload();
    with(eventPayload).assertThat("$.recipeId", equalTo(recipeId)).assertThat("$.name", equalTo("Vanilla cake")).assertThat("$.glutenFree", equalTo(false)).assertThat("$.ingredients[0].name", equalTo("vanilla")).assertThat("$.ingredients[0].quantity", equalTo(2));
}
Also used : Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) Test(org.junit.Test)

Aggregations

Event (uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event)10 Test (org.junit.Test)8 UUID (java.util.UUID)5 UUID.randomUUID (java.util.UUID.randomUUID)5 Stream (java.util.stream.Stream)3 EventStream (uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStream)3 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)3 HttpResponse (org.apache.http.HttpResponse)2 UtcClock (uk.gov.justice.services.common.util.UtcClock)2 File (java.io.File)1 Transactional (javax.transaction.Transactional)1 Response (javax.ws.rs.core.Response)1 HttpEntity (org.apache.http.HttpEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 InvalidPositionException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException)1 StoreEventRequestFailedException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.StoreEventRequestFailedException)1 ApiResponse (uk.gov.justice.services.example.cakeshop.it.util.ApiResponse)1 Metadata (uk.gov.justice.services.messaging.Metadata)1 StreamCloseSpy (uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy)1