Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

JsonEnvelope Building for Version 2.3.0 and above

Shaun Francis edited this page Aug 11, 2017 · 1 revision

JsonEnvelope Building for Version 2.3.0 and above

JsonEnvelope building has changed from version 2.3.0 of the framework.

Usage when developing in the Microservice Framework

The following dependencies are required to allow construction.

The framework-api-core for the production code

<dependency>
    <groupId>uk.gov.justice.services</groupId>
    <artifactId>framework-api-core</artifactId>
</dependency>

and the messaging-core for unit testing

<dependency>
    <groupId>uk.gov.justice.services</groupId>
    <artifactId>messaging-core</artifactId>
    <scope>test</scope>
 </dependency>

To construct a JsonEnvelope use:

import static java.util.UUID.randomUUID;
import static javax.json.Json.createObjectBuilder;
import static uk.gov.justice.services.messaging.JsonEnvelope.envelopeFrom;
import static uk.gov.justice.services.messaging.JsonEnvelope.metadataBuilder;
...

    final UUID correlationId = randomUUID();
    final UUID sessionId = randomUUID();
    final UUID userId = randomUUID();
    final UUID streamId = randomUUID();
    final UUID id = randomUUID();
    final String commandName = "notification-added";

    final JsonEnvelope envelope = envelopeFrom(
            metadataBuilder()
                    .withId(id)
                    .withName(commandName)
                    .withClientCorrelationId(correlationId.toString())
                    .withSessionId(sessionId.toString())
                    .withUserId(userId.toString())
                    .withStreamId(streamId),
    createObjectBuilder()
            .add("exampleField", "example value"));

To construct a JsonEnvelope using the metadata from another JsonEnvelope:

    final JsonEnvelope otherEnvelope = envelopeFrom(
            envelope.metadata(), 
            createObjectBuilder().add("exampleField", "example value").build());

To construct with a JsonValue.NULL payload:

    final JsonEnvelope envelope = envelopeFrom(
            metadataBuilder()
                    .withId(randomUUID())
                    .withName("notification-added"),
            JsonValue.NULL);

Usage in tests can be simplified by using JsonEnvelopeBuilder

The following dependency is required to allow construction.

<dependency>
    <groupId>uk.gov.justice.services</groupId>
    <artifactId>test-utils-core</artifactId>
    <scope>test</scope>
</dependency>

To construct a JsonEnvelope use:

import static java.util.UUID.randomUUID;
import static javax.json.Json.createObjectBuilder;
import static uk.gov.justice.services.test.utils.core.messaging.JsonEnvelopeBuilder.envelope;
import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID;
...

    final UUID correlationId = randomUUID();
    final UUID sessionId = randomUUID();
    final UUID userId = randomUUID();
    final UUID streamId = randomUUID();
    final String commandName = "notification-added";

    final JsonEnvelope command = envelope()
            .with(metadataWithRandomUUID(commandName)
                    .withClientCorrelationId(correlationId.toString())
                    .withSessionId(sessionId.toString())
                    .withUserId(userId.toString())
                    .withStreamId(streamId))
            .withPayloadOf("example value", "exampleField")
            .build();