Search in sources :

Example 96 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareEnvelopeEventStreamTest method shouldAttemptSnapshotCreationOnAppendingEvents.

@Test
public void shouldAttemptSnapshotCreationOnAppendingEvents() throws Exception {
    final TestAggregate aggregate = new TestAggregate();
    eventStream.registerAggregates(TestAggregate.class, aggregate);
    final long streamVersionAfterAppending = 14L;
    final Stream<JsonEnvelope> streamOfEvents = Stream.of(envelope().build());
    when(eventStreamManager.append(STREAM_ID, streamOfEvents)).thenReturn(streamVersionAfterAppending);
    eventStream.append(streamOfEvents);
    verify(snapshotService).attemptAggregateStore(STREAM_ID, streamVersionAfterAppending, aggregate);
}
Also used : TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 97 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareEnvelopeEventStreamTest method shouldReturnStreamFromPosition.

@Test
public void shouldReturnStreamFromPosition() throws Exception {
    final long position = 10L;
    final JsonEnvelope event = envelope().with(metadataWithDefaults().withVersion(1L)).build();
    when(eventStreamManager.readFrom(STREAM_ID, position)).thenReturn(Stream.of(event));
    Stream<JsonEnvelope> stream = eventStream.readFrom(position);
    List<JsonEnvelope> events = stream.collect(toList());
    assertThat(events, hasSize(1));
    assertThat(events.get(0), is(event));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 98 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareEnvelopeEventStreamTest method shouldReturnStreamOfEnvelopes.

@Test
public void shouldReturnStreamOfEnvelopes() throws Exception {
    final JsonEnvelope event = envelope().with(metadataWithDefaults().withVersion(1L)).build();
    when(eventStreamManager.read(STREAM_ID)).thenReturn(Stream.of(event));
    Stream<JsonEnvelope> stream = eventStream.read();
    List<JsonEnvelope> events = stream.collect(toList());
    assertThat(events, hasSize(1));
    assertThat(events.get(0), is(event));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 99 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class SnapshotAwareAggregateServiceTest method shouldReplayDeltaOfEventsOnAggregate.

@Test
public void shouldReplayDeltaOfEventsOnAggregate() throws AggregateChangeDetectedException {
    defaultAggregateService.logger = logger;
    defaultAggregateService.jsonObjectToObjectConverter = jsonObjectToObjectConverter;
    final UUID streamId = UUID.randomUUID();
    final long currentStreamVersion = 5L;
    final TestAggregate aggregate = new TestAggregate();
    final long snapshotVersion = 2L;
    final JsonEnvelope jsonEventA = envelope().with(metadataWithRandomUUID("eventA")).withPayloadOf("value1", "name1").build();
    final JsonEnvelope jsonEventB = envelope().with(metadataWithRandomUUID("eventB")).withPayloadOf("value2", "name1").build();
    final JsonEnvelope jsonEventC = envelope().with(metadataWithRandomUUID("eventC")).withPayloadOf("value3", "name1").build();
    registerEvent(EventA.class, "eventA");
    registerEvent(EventB.class, "eventB");
    registerEvent(EventC.class, "eventC");
    final EventA eventA = new EventA("A1");
    final EventB eventB = new EventB("B1");
    final EventC eventC = new EventC("C1");
    when(eventStream.getId()).thenReturn(streamId);
    when(eventStream.getCurrentVersion()).thenReturn(currentStreamVersion);
    when(snapshotService.getLatestVersionedAggregate(streamId, TestAggregate.class)).thenReturn(Optional.of(new VersionedAggregate<>(snapshotVersion, aggregate)));
    when(eventStream.readFrom(snapshotVersion + 1)).thenReturn(of(jsonEventA, jsonEventB, jsonEventC));
    when(jsonObjectToObjectConverter.convert(jsonEventA.payloadAsJsonObject(), EventA.class)).thenReturn(eventA);
    when(jsonObjectToObjectConverter.convert(jsonEventB.payloadAsJsonObject(), EventB.class)).thenReturn(eventB);
    when(jsonObjectToObjectConverter.convert(jsonEventC.payloadAsJsonObject(), EventC.class)).thenReturn(eventC);
    aggregateService.get(eventStream, TestAggregate.class);
    assertThat(aggregate.recordedEvents(), hasItems(eventA, eventB, eventC));
}
Also used : EventB(uk.gov.justice.domain.event.EventB) EventC(uk.gov.justice.domain.event.EventC) TestAggregate(uk.gov.justice.domain.aggregate.TestAggregate) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) EventA(uk.gov.justice.domain.event.EventA) MetadataBuilderFactory.metadataWithRandomUUID(uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) VersionedAggregate(uk.gov.justice.domain.snapshot.VersionedAggregate) Test(org.junit.Test)

Example 100 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class LocalAuditInterceptor method process.

@Override
public InterceptorContext process(final InterceptorContext interceptorContext, final InterceptorChain interceptorChain) {
    final String component = interceptorContext.getComponentName();
    recordAudit(interceptorContext.inputEnvelope(), component);
    final InterceptorContext outputContext = interceptorChain.processNext(interceptorContext);
    final Optional<JsonEnvelope> jsonEnvelope = outputContext.outputEnvelope();
    jsonEnvelope.ifPresent(envelope -> recordAudit(envelope, component));
    return outputContext;
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Aggregations

JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)276 Test (org.junit.Test)249 UUID (java.util.UUID)69 UUID.randomUUID (java.util.UUID.randomUUID)64 JsonObject (javax.json.JsonObject)30 JsonValue (javax.json.JsonValue)26 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)24 Metadata (uk.gov.justice.services.messaging.Metadata)18 Method (java.lang.reflect.Method)17 Function (java.util.function.Function)14 StreamBufferEvent (uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent)10 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)9 JsonObjects.getJsonObject (uk.gov.justice.services.messaging.JsonObjects.getJsonObject)9 EndpointDefinition (uk.gov.justice.services.clients.core.EndpointDefinition)8 MediaType (uk.gov.justice.services.core.mapping.MediaType)8 StreamStatus (uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus)7 ZonedDateTime (java.time.ZonedDateTime)6 Collection (java.util.Collection)6 Optional (java.util.Optional)6 HttpHeaders (javax.ws.rs.core.HttpHeaders)6