Search in sources :

Example 16 with JsonEnvelope

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

the class JsonEnvelopeBuilderTest method shouldBuildJsonEnvelopeContainingPayloadFromJsonEnvelope.

@Test
public void shouldBuildJsonEnvelopeContainingPayloadFromJsonEnvelope() throws Exception {
    final UUID id = randomUUID();
    final String name = "name";
    final MetadataBuilder metadata = metadataBuilder().withId(id).withName(name);
    final JsonObjectBuilder payload = createObjectBuilder().add("test", "value");
    final JsonEnvelope envelope = JsonEnvelopeBuilder.envelope().withPayloadFrom(JsonEnvelope.envelopeFrom(metadata, payload)).build();
    assertThat(envelope.metadata(), nullValue());
    assertThat(envelope, jsonEnvelope().withPayloadOf(payloadIsJson(withJsonPath("$.test", equalTo("value")))));
}
Also used : MetadataBuilder(uk.gov.justice.services.messaging.MetadataBuilder) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) JsonObjectBuilder(javax.json.JsonObjectBuilder) Test(org.junit.Test)

Example 17 with JsonEnvelope

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

the class EnveloperFactoryTest method shouldCreateUsableEnveloper.

@Test
public void shouldCreateUsableEnveloper() throws Exception {
    final JsonObject inputPayload = Json.createObjectBuilder().add("value", "init").build();
    final JsonObject outputPayload = Json.createObjectBuilder().add("name", "output").build();
    final JsonEnvelope envelope = EnvelopeFactory.createEnvelope("init.name", inputPayload);
    final JsonEnvelope result = EnveloperFactory.createEnveloper().withMetadataFrom(envelope, "expected.name").apply(outputPayload);
    assertThat(result, jsonEnvelope(metadata().withName("expected.name"), payloadIsJson(allOf(withJsonPath("$.name", equalTo("output")), withoutJsonPath("$.value")))));
}
Also used : JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 18 with JsonEnvelope

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

the class EnveloperFactoryTest method shouldCreateEnveloperWithRegisteredEventClasses.

@Test
public void shouldCreateEnveloperWithRegisteredEventClasses() throws Exception {
    final UUID id1 = UUID.randomUUID();
    final String name = "name";
    final String id2 = "id2";
    final String value = "value";
    final JsonObject inputPayload = Json.createObjectBuilder().add(value, "init").build();
    final JsonEnvelope envelope = EnvelopeFactory.createEnvelope("init.name", inputPayload);
    final EnveloperFactoryTest.TestEvent1 testEvent_1 = new EnveloperFactoryTest.TestEvent1(id1, name);
    final EnveloperFactoryTest.TestEvent2 testEvent_2 = new EnveloperFactoryTest.TestEvent2(id2, value);
    final Stream<Object> events = Stream.of(testEvent_1, testEvent_2);
    final Enveloper enveloper = EnveloperFactory.createEnveloperWithEvents(TestEvent1.class, TestEvent2.class);
    final List<JsonEnvelope> resultEvents = events.map(enveloper.withMetadataFrom(envelope)).collect(toList());
    assertThat(resultEvents, listContaining(jsonEnvelope(metadata().withName("test.event.1"), payloadIsJson(allOf(withJsonPath("$.id", equalTo(id1.toString())), withJsonPath("$.name", equalTo(name))))), jsonEnvelope(metadata().withName("test.event.2"), payloadIsJson(allOf(withJsonPath("$.id", equalTo(id2)), withJsonPath("$.value", equalTo(value)))))));
}
Also used : JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Enveloper(uk.gov.justice.services.core.enveloper.Enveloper) JsonObject(javax.json.JsonObject) UUID(java.util.UUID) Test(org.junit.Test)

Example 19 with JsonEnvelope

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

the class EventConverterTest method shouldCreateEventLog.

@Test
public void shouldCreateEventLog() throws Exception {
    JsonEnvelope envelope = envelopeFrom(metadataBuilder().withId(ID).withName(NAME).withStreamId(STREAM_ID).withVersion(SEQUENCE_ID).createdAt(clock.now()), createObjectBuilder().add(PAYLOAD_FIELD_NAME, PAYLOAD_FIELD_VALUE));
    Event event = eventConverter.eventOf(envelope);
    assertThat(event.getId(), equalTo(ID));
    assertThat(event.getName(), equalTo(NAME));
    assertThat(event.getStreamId(), equalTo(STREAM_ID));
    assertThat(event.getSequenceId(), equalTo(SEQUENCE_ID));
    assertThat(event.getCreatedAt(), is(clock.now()));
    JSONAssert.assertEquals(METADATA_JSON, event.getMetadata(), false);
    JSONAssert.assertEquals(envelope.payloadAsJsonObject().toString(), event.getPayload(), false);
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 20 with JsonEnvelope

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

the class EventConverterTest method shouldCreateEnvelope.

@Test
public void shouldCreateEnvelope() throws Exception {
    JsonEnvelope actualEnvelope = eventConverter.envelopeOf(new Event(ID, STREAM_ID, SEQUENCE_ID, NAME, METADATA_JSON, PAYLOAD_JSON, new UtcClock().now()));
    assertThat(actualEnvelope.metadata().id(), equalTo(ID));
    assertThat(actualEnvelope.metadata().name(), equalTo(NAME));
    String actualPayload = actualEnvelope.payloadAsJsonObject().toString();
    JSONAssert.assertEquals(PAYLOAD_JSON, actualPayload, false);
}
Also used : UtcClock(uk.gov.justice.services.common.util.UtcClock) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

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