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